Re: [Speed-dreams-devel] Optimization of calculation of torque from rads

 Re: [Speed-dreams-devel] Optimization of calculation of torque from rads From: W.-D. Beelitz - 2010-06-21 18:45:15 ```Hello Christos, here a simple replacement for CalculateTorque in simuV3. tdble CalculateTorque2 (tEngine* engine, tdble rads) { //double StartTimeStamp = RtTimeStamp(); // To get used time tEngineCurve *curve = &(engine->curve); int i = engine->lastInterval; // Get last interval, is set to 0 at config engine tdble Tq; tdble rpm_min = curve->data[i].rads; // Current lower limit tdble rpm_max = curve->data[i+1].rads; // Current upper limit tdble alpha; // Ratio between limits if (rads > rpm_max) { if (i < engine->curve.nbPts) { rpm_min = rpm_max; engine->lastInterval = ++i; rpm_max = curve->data[i+1].rads; } } else if (rads < rpm_min) { if (i > 0) { rpm_max = rpm_min; engine->lastInterval = --i; rpm_min = curve->data[i].rads; } } alpha = (rads - rpm_min) / (rpm_max - rpm_min); Tq = (float)((1.0-alpha) * curve->data[i].Tq + alpha * curve->data[i+1].Tq); //SimTicks2 += RtDuration(StartTimeStamp); // To get used time return Tq; } The ratio of time consumed: CalculateTorque = 2.5 * CalculateTorque2. Regards Wolf-Dieter -----UrsprÃ¼ngliche Nachricht----- Von: Christos Dimitrakakis [mailto:olethros@...] Gesendet: 21 June 2010 09:31 An: speed-dreams-devel@... Betreff: Re: [Speed-dreams-devel] Optimization of calculation of torque from rads On 21/06/10 09:21, Christos Dimitrakakis wrote: > On 21/06/10 07:46, W.-D. Beelitz wrote: >> The value of rpms is changing "slowly" in most cases. PS2. So, the ideal way to implement this is as follows: 1. Assume we have r_1, ..., r_n rpm points on the spline, with values T_1, ..., T_n. 2. Let r be the last rpm value and [r_i, r_j] be the smallest interval containing r (i.e. so that j = i + 1) 3. Let r' be the current rpm value. 4. If r' > r, do binary search on the interval [r_i, r_n], else do binary search on the interval [r_1, r_j]. 5. Obtain a new interval [r'_i, r'_j]. As you can see, this reduces computation by a factor of 2. So, original complexity: O(n), binary search O(log n), and this O(log sqrt(n)). Again, I wouldn't do a linear search because once the number of points n increases, the amount of search increases with rate O(n). ---------------------------------------------------------------------------- -- ThinkGeek and WIRED's GeekDad team up for the Ultimate GeekDad Father's Day Giveaway. ONE MASSIVE PRIZE to the lucky parental unit. See the prize list and enter to win: http://p.sf.net/sfu/thinkgeek-promo _______________________________________________ Speed-dreams-devel mailing list Speed-dreams-devel@... https://lists.sourceforge.net/lists/listinfo/speed-dreams-devel ```