Skip to content

Commit 98b818e

Browse files
author
Henning Kiel
committed
- move if condition out of for loop
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11030 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent 9e1537d commit 98b818e

File tree

3 files changed

+93
-70
lines changed

3 files changed

+93
-70
lines changed

SimulationRuntime/c/math-support/initialization.c

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,23 @@ static double leastSquareWithLambda(DATA* data, long nz, double* z, double* zNom
102102
functionAlgebraics(data);
103103
initial_residual(data, lambda, initialResiduals);
104104

105-
for(j=0; j<data->modelData.nResiduals; ++j)
105+
if (initialResidualScalingCoefficients)
106+
{
107+
/* use scaling coefficients */
108+
for(j=0; j<data->modelData.nResiduals; ++j)
109+
{
110+
scalingCoefficient = initialResidualScalingCoefficients[j];
111+
if(scalingCoefficient > 0.0)
112+
funcValue += (initialResiduals[j] / scalingCoefficient) * (initialResiduals[j] / scalingCoefficient);
113+
}
114+
}
115+
else
106116
{
107-
scalingCoefficient = initialResidualScalingCoefficients ? initialResidualScalingCoefficients[j] : 1.0;
108-
if(scalingCoefficient > 0.0)
109-
funcValue += (initialResiduals[j] / scalingCoefficient) * (initialResiduals[j] / scalingCoefficient);
117+
/* no scaling coefficients given */
118+
for(j=0; j<data->modelData.nResiduals; ++j)
119+
{
120+
funcValue += initialResiduals[j] * initialResiduals[j];
121+
}
110122
}
111123

112124
return funcValue;
@@ -138,8 +150,16 @@ static void computeInitialResidualScalingCoefficients(DATA *data, double nz, dou
138150
for(j=0; j<data->modelData.nResiduals; ++j)
139151
initialResidualScalingCoefficients[j] = 0.0;
140152

141-
for(i=0; i<nz; ++i)
142-
states[i] = z[i] * (zNominal ? zNominal[i] : 1.0);
153+
if (zNominal)
154+
{
155+
for(i=0; i<nz; ++i)
156+
states[i] = z[i] * zNominal[i];
157+
}
158+
else
159+
{
160+
for(i=0; i<nz; ++i)
161+
states[i] = z[i];
162+
}
143163

144164
/* lambda = 1.0 */
145165
leastSquareWithLambda(data, nz, states, NULL, NULL, 1.0, tmpInitialResidual1);
@@ -269,9 +289,13 @@ static void NelderMeadOptimization(long N,
269289
for(i=0; i<N; i++)
270290
{
271291
/* vertex x / var i */
272-
simplex[x*N + i] = var[i] + ((x==i) ? 1.0 : 0.0); /* canonical simplex */
292+
simplex[x*N + i] = var[i];
273293
}
274294
}
295+
for(i=0; i<N; i++)
296+
{
297+
simplex[i*N + i] += 1.0; /* canonical simplex */
298+
}
275299

276300
/*DEBUG_INFO3(LOG_INIT, "NelderMeadOptimization | increasing lambda to %g in step %d at f=%g", lambda, (int)iteration, leastSquare(data, N, simplex, scale, initialResidualScalingCoefficients, lambda, initialResiduals));*/
277301
computeInitialResidualScalingCoefficients(data, N, simplex, scale, initialResidualScalingCoefficients);
@@ -323,7 +347,7 @@ static void NelderMeadOptimization(long N,
323347
if(dump && !(iteration % dump))
324348
INFO4("NelderMeadOptimization | lambda=%g / step=%d / f=%g [%g]", lambda, (int)iteration, fvalues[xb], fvalues[xz]);
325349

326-
if(sigma < g*g && lambda < 1.0)
350+
if((sigma < (g*g)) && (lambda < 1.0))
327351
{
328352
lambda += lambda_step;
329353
if(lambda > 1.0)

SimulationRuntime/c/simulation/solver/events.c

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
* This file is part of OpenModelica.
33
*
4-
* Copyright (c) 1998-2010, Link�pings University,
5-
* Department of Computer and Information Science,
6-
* SE-58183 Link�ping, Sweden.
4+
* Copyright (c) 1998-2010, Linköpings University,
5+
* Department of Computer and Information Science,
6+
* SE-58183 Linköping, Sweden.
77
*
88
* All rights reserved.
99
*
@@ -14,7 +14,7 @@
1414
*
1515
* The OpenModelica software and the Open Source Modelica
1616
* Consortium (OSMC) Public License (OSMC-PL) are obtained
17-
* from Link�pings University, either from the above address,
17+
* from Linköpings University, either from the above address,
1818
* from the URL: http://www.ida.liu.se/projects/OpenModelica
1919
* and in the OpenModelica distribution.
2020
*
@@ -69,14 +69,14 @@ modelica_boolean sample(DATA *data, double start, double interval, int hindex)
6969
double retVal;
7070
double tmp = 1;
7171
int tmpindex = data->simulationInfo.curSampleTimeIx;
72-
72+
7373
if(tmpindex < data->simulationInfo.nSampleTimes){
7474
while((data->simulationInfo.sampleTimes[tmpindex]).activated == 1){
7575
if((data->simulationInfo.sampleTimes[tmpindex]).zc_index == hindex)
7676
tmp = 0;
77-
77+
7878
tmpindex++;
79-
79+
8080
if(tmpindex == data->simulationInfo.nSampleTimes)
8181
break;
8282
}
@@ -162,12 +162,12 @@ void initSample(DATA* data, double start, double stop)
162162
/* not used yet
163163
* long measure_start_time = clock();
164164
*/
165-
165+
166166
/* This code will generate an array of time values when sample generates events.
167167
* The only problem is our backend does not generate this array.
168168
* Sample() and sample() also need to be changed, but this should be easy to fix.
169169
*/
170-
170+
171171
int i;
172172
/* double stop = 1.0; */
173173
double d;
@@ -194,19 +194,19 @@ void initSample(DATA* data, double start, double stop)
194194

195195
for(i = 0; i < num_samples; i++){
196196
DEBUG_INFO2(LOG_EVENTS, "Generate times for sample(%f, %f)", data->simulationInfo.rawSampleExps[i].start, data->simulationInfo.rawSampleExps[i].interval);
197-
197+
198198
for(d = data->simulationInfo.rawSampleExps[i].start; ix < max_events && d <= stop; d += data->simulationInfo.rawSampleExps[i].interval){
199199
(Samples[ix]).events = d;
200200
(Samples[ix++]).zc_index = (data->simulationInfo.rawSampleExps[i]).zc_index;
201-
201+
202202
DEBUG_INFO3(LOG_EVENTS, "Generate sample(%f, %f, %d)", d, data->simulationInfo.rawSampleExps[i].interval, (data->simulationInfo.rawSampleExps[i]).zc_index);
203203
}
204204
}
205-
205+
206206
/* Sort, filter out unique values */
207207
qsort(Samples, max_events, sizeof(SAMPLE_TIME), compSample);
208208
nuniq = unique(Samples, max_events, sizeof(SAMPLE_TIME), compSampleZC);
209-
209+
210210
DEBUG_INFO1(LOG_INIT, "Number of sorted, unique sample events: %d", nuniq);
211211
for(i = 0; i < nuniq; i++)
212212
DEBUG_INFO_AL3(LOG_INIT, "%f\t HelpVar[%d]=activated(%d)", (Samples[i]).events, (Samples[i]).zc_index,(Samples[i]).activated);
@@ -227,8 +227,7 @@ void initSample(DATA* data, double start, double stop)
227227
* ! Function check if a sample expression should be activated
228228
*
229229
*/
230-
int
231-
checkForSampleEvent(DATA *data, SOLVER_INFO* solverInfo)
230+
int checkForSampleEvent(DATA *data, SOLVER_INFO* solverInfo)
232231
{
233232
double a = solverInfo->currentTime + solverInfo->currentStepSize;
234233
int b = 0;
@@ -268,8 +267,7 @@ checkForSampleEvent(DATA *data, SOLVER_INFO* solverInfo)
268267
* ! Function activated sample expression
269268
*
270269
*/
271-
int
272-
activateSampleEvents(DATA *data)
270+
int activateSampleEvents(DATA *data)
273271
{
274272
if (data->simulationInfo.curSampleTimeIx < data->simulationInfo.nSampleTimes)
275273
{
@@ -305,8 +303,7 @@ activateSampleEvents(DATA *data)
305303
* ! Function deactivate, before activated sample expression
306304
*
307305
*/
308-
void
309-
deactivateSampleEvents(DATA *data)
306+
void deactivateSampleEvents(DATA *data)
310307
{
311308
int tmpindex = data->simulationInfo.curSampleTimeIx;
312309

@@ -321,8 +318,7 @@ deactivateSampleEvents(DATA *data)
321318
* ! Function deactivate, before activated sample expression in equations
322319
*
323320
*/
324-
void
325-
deactivateSampleEventsandEquations(DATA *data)
321+
void deactivateSampleEventsandEquations(DATA *data)
326322
{
327323
while ((data->simulationInfo.sampleTimes[data->simulationInfo.curSampleTimeIx]).activated == 1)
328324
{
@@ -341,8 +337,7 @@ deactivateSampleEventsandEquations(DATA *data)
341337
If a ZeroCrossing Function cause a sign change, root finding
342338
process will start
343339
*/
344-
int
345-
CheckForNewEvent(DATA* simData, modelica_boolean* sampleactived, double* currentTime)
340+
int CheckForNewEvent(DATA* simData, modelica_boolean* sampleactived, double* currentTime)
346341
{
347342
long i = 0;
348343
LIST *eventList=NULL;
@@ -479,7 +474,7 @@ void FindRoot(DATA* simData, double *EventTime, LIST *eventList)
479474
LIST_NODE* it;
480475
fortran_integer i=0;
481476
static LIST *tmpEventList = NULL;
482-
477+
483478
double *states_right = (double*) malloc(simData->modelData.nStates * sizeof(double));
484479
double *states_left = (double*) malloc(simData->modelData.nStates * sizeof(double));
485480

@@ -513,9 +508,10 @@ void FindRoot(DATA* simData, double *EventTime, LIST *eventList)
513508
double value = fabs(simData->simulationInfo.zeroCrossings[*((long*)listFirstData(eventList))]);
514509
for(it=listFirstNode(eventList); it; it=listNextNode(it))
515510
{
516-
if(value > fabs(simData->simulationInfo.zeroCrossings[*((long*)listNodeData(it))]))
511+
double fvalue = fabs(simData->simulationInfo.zeroCrossings[*((long*)listNodeData(it))]);
512+
if(value > fvalue)
517513
{
518-
value = fabs(simData->simulationInfo.zeroCrossings[*((long*)listNodeData(it))]);
514+
value = fvalue;
519515
}
520516
}
521517
DEBUG_INFO1(LOG_ZEROCROSSINGS, "Minimum value: %f", value);
@@ -652,12 +648,11 @@ double BiSection(DATA* simData, double* a, double* b, double* states_a,
652648
Check if at least one zerocrossing has change sign
653649
is used in BiSection
654650
*/
655-
int
656-
CheckZeroCrossings(DATA *simData, LIST *tmpEventList, LIST *eventList)
651+
int CheckZeroCrossings(DATA *simData, LIST *tmpEventList, LIST *eventList)
657652
{
658653

659654
LIST_NODE *it;
660-
655+
661656
listClear(tmpEventList);
662657
for(it=listFirstNode(eventList); it; it=listNextNode(it))
663658
{
@@ -690,8 +685,7 @@ CheckZeroCrossings(DATA *simData, LIST *tmpEventList, LIST *eventList)
690685
}
691686

692687

693-
void
694-
SaveZeroCrossingsAfterEvent(DATA* simData)
688+
void SaveZeroCrossingsAfterEvent(DATA* simData)
695689
{
696690
long i = 0;
697691

@@ -704,8 +698,7 @@ SaveZeroCrossingsAfterEvent(DATA* simData)
704698
}
705699

706700

707-
void
708-
initializeZeroCrossings(DATA* simData)
701+
void initializeZeroCrossings(DATA* simData)
709702
{
710703
long i = 0;
711704
for (i = 0; i < simData->modelData.nZeroCrossings; i++)
@@ -721,8 +714,7 @@ initializeZeroCrossings(DATA* simData)
721714
}
722715
}
723716

724-
void
725-
correctDirectionZeroCrossings(DATA* simData)
717+
void correctDirectionZeroCrossings(DATA* simData)
726718
{
727719
long i = 0;
728720
for (i = 0; i < simData->modelData.nZeroCrossings; i++)

0 commit comments

Comments
 (0)