-
Notifications
You must be signed in to change notification settings - Fork 296
/
dassl.c
1161 lines (982 loc) · 38.2 KB
/
dassl.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
* CONDITIONS OF OSMC-PL.
*
*/
#include <string.h>
#include <setjmp.h>
#include "openmodelica.h"
#include "openmodelica_func.h"
#include "simulation_data.h"
#include "util/omc_error.h"
#include "gc/omc_gc.h"
#include "simulation/options.h"
#include "simulation/simulation_runtime.h"
#include "simulation/results/simulation_result.h"
#include "simulation/solver/solver_main.h"
#include "simulation/solver/model_help.h"
#include "simulation/solver/external_input.h"
#include "simulation/solver/epsilon.h"
#include "simulation/solver/omc_math.h"
#include "simulation/solver/jacobianSymbolical.h"
#include "simulation/solver/dassl.h"
#include "meta/meta_modelica.h"
#ifdef __cplusplus
extern "C" {
#endif
/* experimental flag for SKF TLM Master Solver Interface
* - it's used with -noEquidistantTimeGrid flag.
* - it's set to 1 if the continuous system is evaluated
* when dassl finished a step, otherwise it's 0.
*/
int RHSFinalFlag;
/* provides a dummy Jacobian to be used with DASSL */
static int dummy_Jacobian(double *t, double *y, double *yprime,double *deltaD,
double *delta, double *cj, double *h, double *wt,
double *rpar, int* ipar) {
return 0;
}
/* provides a dummy zero crossing function to be used with DASSL */
static int dummy_zeroCrossing(int *neqm, double *t, double *y, double *yp,
int *ng, double *gout, double *rpar, int* ipar) {
return 0;
}
/* provides a dumm precondition function to be used with DASSL */
static int dummy_precondition(int *neq, double *t, double *y, double *yprime,
double *savr, double *pwk, double *cj,
double *wt, double *wp, int *iwp, double *b,
double eplin, int* ires, double *rpar, int* ipar){
return 0;
}
/* Function prototypes */
static int callJacobian(double *t, double *y, double *yprime, double *deltaD,
double *pd, double *cj, double *h, double *wt,
double *rpar, int* ipar);
static int jacA_num(double *t, double *y, double *yprime, double *deltaD,
double *pd, double *cj, double *h, double *wt,
double *rpar, int* ipar);
static int jacA_numColored(double *t, double *y, double *yprime,
double *deltaD, double *pd, double *cj, double *h,
double *wt, double *rpar, int* ipar);
static int jacA_sym(double *t, double *y, double *yprime, double *deltaD,
double *pd, double *cj, double *h, double *wt,
double *rpar, int* ipar);
static int jacA_symColored(double *t, double *y, double *yprime,
double *deltaD, double *pd, double *cj, double *h,
double *wt, double *rpar, int* ipar);
static void setJacElementDasslSparse(int l, int k, int nth, double val,
void* matrixA, int rows);
void DDASKR(
int (*res) (double *t, double *y, double *yprime, double* cj, double *delta, int *ires, double *rpar, int* ipar),
int *neq,
double *t,
double *y,
double *yprime,
double *tout,
int *info,
double *rtol,
double *atol,
int *idid,
double *rwork,
int *lrw,
int *iwork,
int *liw,
double *rpar,
int *ipar,
int (*jac) (double *t, double *y, double *yprime, double *deltaD, double *delta, double *cj, double *h, double *wt, double *rpar, int* ipar),
int (*psol) (int *neq, double *t, double *y, double *yprime, double *savr, double *pwk, double *cj, double *wt, double *wp, int *iwp, double *b, double eplin, int* ires, double *rpar, int* ipar),
int (*g) (int *neqm, double *t, double *y, double *yp, int *ng, double *gout, double *rpar, int* ipar),
int *ng,
int *jroot
);
static int continue_DASSL(int* idid, double* tolarence);
/* function for calculating state values on residual form */
static int functionODE_residual(double *t, double *x, double *xprime,
double *cj, double *delta, int *ires,
double *rpar, int* ipar);
/* function for calculating zeroCrossings */
static int function_ZeroCrossingsDASSL(int *neqm, double *t, double *y,
double *yp, int *ng, double *gout,
double *rpar, int* ipar);
/*
* \brief Configure DASSL solver
*
* Allocate memory for intern data of `dasslData`.
* Configures DASSL:
* - Set relative and absolute tolerance
* - Set maximum step size, initial step size
* - Set maximum integration order
* - Set time grid
* - Set method for jacobian computation
* - Set root finding method
* - Set event handling and restart option
*
*/
int dassl_initial(DATA* data, threadData_t *threadData,
SOLVER_INFO* solverInfo, DASSL_DATA *dasslData)
{
TRACE_PUSH
/* work arrays for DASSL */
unsigned int i;
long N;
SIMULATION_DATA tmpSimData = {0};
dasslData->residualFunction = functionODE_residual;
N = data->modelData->nStates;
dasslData->N = N;
RHSFinalFlag = 0;
dasslData->liw = 40 + N;
dasslData->lrw = 60 + ((maxOrder + 4) * N) + (N * N) + (3*data->modelData->nZeroCrossings);
dasslData->rwork = (double*) calloc(dasslData->lrw, sizeof(double));
assertStreamPrint(threadData, 0 != dasslData->rwork,"out of memory");
dasslData->iwork = (int*) calloc(dasslData->liw, sizeof(int));
assertStreamPrint(threadData, 0 != dasslData->iwork,"out of memory");
dasslData->ng = (int) data->modelData->nZeroCrossings;
dasslData->jroot = (int*) calloc(data->modelData->nZeroCrossings, sizeof(int));
dasslData->rpar = (double**) malloc(3*sizeof(double*));
dasslData->ipar = (int*) malloc(sizeof(int));
dasslData->ipar[0] = ACTIVE_STREAM(LOG_JAC);
assertStreamPrint(threadData, 0 != dasslData->ipar,"out of memory");
dasslData->atol = (double*) malloc(N*sizeof(double));
dasslData->rtol = (double*) malloc(N*sizeof(double));
dasslData->info = (int*) calloc(infoLength, sizeof(int));
assertStreamPrint(threadData, 0 != dasslData->info,"out of memory");
dasslData->idid = 0;
dasslData->ysave = (double*) malloc(N*sizeof(double));
dasslData->ypsave = (double*) malloc(N*sizeof(double));
dasslData->delta_hh = (double*) malloc(N*sizeof(double));
dasslData->newdelta = (double*) malloc(N*sizeof(double));
dasslData->stateDer = (double*) calloc(N, sizeof(double));
dasslData->states = (double*) malloc(N*sizeof(double));
data->simulationInfo->currentContext = CONTEXT_ALGEBRAIC;
/* ### start configuration of dassl ### */
infoStreamPrint(LOG_SOLVER, 1, "Configuration of the dassl code:");
/* set nominal values of the states for absolute tolerances */
dasslData->info[1] = 1;
infoStreamPrint(LOG_SOLVER, 1, "The relative tolerance is %g. Following absolute tolerances are used for the states: ", data->simulationInfo->tolerance);
for(i=0; i<dasslData->N; ++i)
{
dasslData->rtol[i] = data->simulationInfo->tolerance;
dasslData->atol[i] = data->simulationInfo->tolerance * fmax(fabs(data->modelData->realVarsData[i].attribute.nominal), 1e-32);
infoStreamPrint(LOG_SOLVER_V, 0, "%d. %s -> %g", i+1, data->modelData->realVarsData[i].info.name, dasslData->atol[i]);
}
messageClose(LOG_SOLVER);
/* let dassl return at every internal step */
dasslData->info[2] = 1;
/* define maximum step size, which is dassl is allowed to go */
if (omc_flag[FLAG_MAX_STEP_SIZE])
{
double maxStepSize = atof(omc_flagValue[FLAG_MAX_STEP_SIZE]);
assertStreamPrint(threadData, maxStepSize >= DASSL_STEP_EPS, "Selected maximum step size %e is too small.", maxStepSize);
dasslData->rwork[1] = maxStepSize;
dasslData->info[6] = 1;
infoStreamPrint(LOG_SOLVER, 0, "maximum step size %g", dasslData->rwork[1]);
}
else
{
infoStreamPrint(LOG_SOLVER, 0, "maximum step size not set");
}
/* define initial step size, which is dassl is used every time it restarts */
if (omc_flag[FLAG_INITIAL_STEP_SIZE])
{
double initialStepSize = atof(omc_flagValue[FLAG_INITIAL_STEP_SIZE]);
assertStreamPrint(threadData, initialStepSize >= DASSL_STEP_EPS, "Selected initial step size %e is too small.", initialStepSize);
dasslData->rwork[2] = initialStepSize;
dasslData->info[7] = 1;
infoStreamPrint(LOG_SOLVER, 0, "initial step size %g", dasslData->rwork[2]);
}
else
{
infoStreamPrint(LOG_SOLVER, 0, "initial step size not set");
}
/* define maximum integration order of dassl */
if (omc_flag[FLAG_MAX_ORDER])
{
int maxOrder = atoi(omc_flagValue[FLAG_MAX_ORDER]);
assertStreamPrint(threadData, maxOrder >= 1 && maxOrder <= 5, "Selected maximum order %d is out of range (1-5).", maxOrder);
dasslData->iwork[2] = maxOrder;
dasslData->info[8] = 1;
}
infoStreamPrint(LOG_SOLVER, 0, "maximum integration order %d", dasslData->info[8]?dasslData->iwork[2]:maxOrder);
/* if FLAG_NOEQUIDISTANT_GRID is set, choose dassl step method */
if (omc_flag[FLAG_NOEQUIDISTANT_GRID])
{
dasslData->dasslSteps = 1; /* TRUE */
solverInfo->solverNoEquidistantGrid = 1;
}
else
{
dasslData->dasslSteps = 0; /* FALSE */
}
infoStreamPrint(LOG_SOLVER, 0, "use equidistant time grid %s", dasslData->dasslSteps?"NO":"YES");
/* check if Flags FLAG_NOEQUIDISTANT_OUT_FREQ or FLAG_NOEQUIDISTANT_OUT_TIME are set */
if (dasslData->dasslSteps){
if (omc_flag[FLAG_NOEQUIDISTANT_OUT_FREQ])
{
dasslData->dasslStepsFreq = atoi(omc_flagValue[FLAG_NOEQUIDISTANT_OUT_FREQ]);
}
else if (omc_flag[FLAG_NOEQUIDISTANT_OUT_TIME])
{
dasslData->dasslStepsTime = atof(omc_flagValue[FLAG_NOEQUIDISTANT_OUT_TIME]);
dasslData->rwork[1] = dasslData->dasslStepsTime;
dasslData->info[6] = 1;
infoStreamPrint(LOG_SOLVER, 0, "maximum step size %g", dasslData->rwork[1]);
} else {
dasslData->dasslStepsFreq = 1;
dasslData->dasslStepsTime = 0.0;
}
if (omc_flag[FLAG_NOEQUIDISTANT_OUT_FREQ] && omc_flag[FLAG_NOEQUIDISTANT_OUT_TIME]){
warningStreamPrint(LOG_STDOUT, 0, "The flags are \"noEquidistantOutputFrequency\" "
"and \"noEquidistantOutputTime\" are in opposition "
"to each other. The flag \"noEquidistantOutputFrequency\" superiors.");
}
infoStreamPrint(LOG_SOLVER, 0, "as the output frequency control is used: %d", dasslData->dasslStepsFreq);
infoStreamPrint(LOG_SOLVER, 0, "as the output frequency time step control is used: %f", dasslData->dasslStepsTime);
}
/* if FLAG_JACOBIAN is set, choose dassl jacobian calculation method */
if (omc_flag[FLAG_JACOBIAN])
{
for(i=1; i< JAC_MAX;i++)
{
if(!strcmp((const char*)omc_flagValue[FLAG_JACOBIAN], JACOBIAN_METHOD[i])){
dasslData->dasslJacobian = (int)i;
break;
}
}
if(dasslData->dasslJacobian == JAC_UNKNOWN)
{
if (ACTIVE_WARNING_STREAM(LOG_SOLVER))
{
warningStreamPrint(LOG_SOLVER, 1, "unrecognized jacobian calculation method %s, current options are:", (const char*)omc_flagValue[FLAG_JACOBIAN]);
for(i=1; i < JAC_MAX; ++i)
{
warningStreamPrint(LOG_SOLVER, 0, "%-15s [%s]", JACOBIAN_METHOD[i], JACOBIAN_METHOD_DESC[i]);
}
messageClose(LOG_SOLVER);
}
throwStreamPrint(threadData,"unrecognized jacobian calculation method %s", (const char*)omc_flagValue[FLAG_JACOBIAN]);
}
/* default case colored numerical jacobian */
}
else
{
dasslData->dasslJacobian = COLOREDNUMJAC;
}
/* selects the calculation method of the jacobian */
if(dasslData->dasslJacobian == COLOREDNUMJAC ||
dasslData->dasslJacobian == COLOREDSYMJAC ||
dasslData->dasslJacobian == SYMJAC)
{
ANALYTIC_JACOBIAN* jacobian = &(data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A]);
if (data->callback->initialAnalyticJacobianA(data, threadData, jacobian))
{
infoStreamPrint(LOG_STDOUT, 0, "Jacobian or SparsePattern is not generated or failed to initialize! Switch back to normal.");
dasslData->dasslJacobian = INTERNALNUMJAC;
} else {
ANALYTIC_JACOBIAN* jac = &data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A];
infoStreamPrint(LOG_SIMULATION, 1, "Initialized colored Jacobian:");
infoStreamPrint(LOG_SIMULATION, 0, "columns: %d rows: %d", jac->sizeCols, jac->sizeRows);
infoStreamPrint(LOG_SIMULATION, 0, "NNZ: %d colors: %d", jac->sparsePattern->numberOfNoneZeros, jac->sparsePattern->maxColors);
messageClose(LOG_SIMULATION);
}
}
/* default use a user sub-routine for JAC */
dasslData->info[4] = 1;
/* set up the appropriate function pointer */
switch (dasslData->dasslJacobian){
case COLOREDNUMJAC:
data->simulationInfo->jacobianEvals = data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A].sparsePattern->maxColors;
dasslData->jacobianFunction = jacA_numColored;
break;
case COLOREDSYMJAC:
data->simulationInfo->jacobianEvals = data->simulationInfo->analyticJacobians[data->callback->INDEX_JAC_A].sparsePattern->maxColors;
dasslData->jacobianFunction = jacA_symColored;
break;
case SYMJAC:
dasslData->jacobianFunction = jacA_sym;
break;
case NUMJAC:
dasslData->jacobianFunction = jacA_num;
break;
case INTERNALNUMJAC:
dasslData->jacobianFunction = dummy_Jacobian;
/* no user sub-routine for JAC */
dasslData->info[4] = 0;
break;
default:
throwStreamPrint(threadData,"unrecognized jacobian calculation method %s", (const char*)omc_flagValue[FLAG_JACOBIAN]);
break;
}
infoStreamPrint(LOG_SOLVER, 0, "jacobian is calculated by %s", JACOBIAN_METHOD_DESC[dasslData->dasslJacobian]);
/* if FLAG_NO_ROOTFINDING is set, choose dassl with out internal root finding */
if(omc_flag[FLAG_NO_ROOTFINDING])
{
dasslData->dasslRootFinding = 0;
dasslData->zeroCrossingFunction = dummy_zeroCrossing;
dasslData->ng = 0;
}
else
{
solverInfo->solverRootFinding = 1;
dasslData->dasslRootFinding = 1;
dasslData->zeroCrossingFunction = function_ZeroCrossingsDASSL;
}
infoStreamPrint(LOG_SOLVER, 0, "dassl uses internal root finding method %s", dasslData->dasslRootFinding?"YES":"NO");
/* if FLAG_NO_RESTART is set, choose dassl step method */
if (omc_flag[FLAG_NO_RESTART])
{
dasslData->dasslAvoidEventRestart = 1; /* TRUE */
}
else
{
dasslData->dasslAvoidEventRestart = 0; /* FALSE */
}
infoStreamPrint(LOG_SOLVER, 0, "dassl performs an restart after an event occurs %s", dasslData->dasslAvoidEventRestart?"NO":"YES");
/* ### end configuration of dassl ### */
messageClose(LOG_SOLVER);
TRACE_POP
return 0;
}
/*
* \brief Deallocates `DASSL_DATA`
*/
int dassl_deinitial(DASSL_DATA *dasslData)
{
TRACE_PUSH
unsigned int i;
/* free work arrays for DASSL */
free(dasslData->rwork);
free(dasslData->iwork);
free(dasslData->rpar);
free(dasslData->ipar);
free(dasslData->atol);
free(dasslData->rtol);
free(dasslData->info);
free(dasslData->jroot);
free(dasslData->ysave);
free(dasslData->delta_hh);
free(dasslData->newdelta);
free(dasslData->states);
free(dasslData->stateDer);
free(dasslData);
TRACE_POP
return 0;
}
/* \fn printCurrentStatesVector(int logLevel, double* y, DATA* data, double time)
*
* \param [in] [logLevel]
* \param [in] [states]
* \param [in] [data]
* \param [in] [time]
*
* This function outputs states vector.
*
*/
int printCurrentStatesVector(int logLevel, double* states, DATA* data, double time)
{
int i;
infoStreamPrint(logLevel, 1, "states at time=%g", time);
for(i=0;i<data->modelData->nStates;++i)
{
infoStreamPrint(logLevel, 0, "%d. %s = %g", i+1, data->modelData->realVarsData[i].info.name, states[i]);
}
messageClose(logLevel);
return 0;
}
/* \fn printVector(int logLevel, double* y, DATA* data, double time)
*
* \param [in] [logLevel]
* \param [in] [name]
* \param [in] [vec]
* \param [in] [size]
* \param [in] [time]
*
* This function outputs a vector of size
*
*/
int printVector(int logLevel, const char* name, double* vec, int n, double time)
{
int i;
infoStreamPrint(logLevel, 1, "%s at time=%g", name, time);
for(i=0; i<n; ++i)
{
infoStreamPrint(logLevel, 0, "%d. %g", i+1, vec[i]);
}
messageClose(logLevel);
return 0;
}
/**********************************************************************************************
* DASSL with synchronous treating of when equation
* - without integrated ZeroCrossing method.
* + ZeroCrossing are handled outside DASSL.
* + if no event occurs outside DASSL performs a warm-start
**********************************************************************************************/
int dassl_step(DATA* data, threadData_t *threadData, SOLVER_INFO* solverInfo)
{
TRACE_PUSH
double tout = 0;
int i = 0;
unsigned int ui = 0;
int retVal = 0;
int saveJumpState;
static unsigned int dasslStepsOutputCounter = 1;
DASSL_DATA *dasslData = (DASSL_DATA*) solverInfo->solverData;
SIMULATION_DATA *sData = data->localData[0];
SIMULATION_DATA *sDataOld = data->localData[1];
modelica_real* states = sData->realVars;
modelica_real* stateDer = dasslData->stateDer;
MODEL_DATA *mData = (MODEL_DATA*) data->modelData;
if (measure_time_flag) rt_tick(SIM_TIMER_SOLVER);
memcpy(stateDer, data->localData[1]->realVars + data->modelData->nStates, sizeof(double)*data->modelData->nStates);
dasslData->rpar[0] = (double*) (void*) data;
dasslData->rpar[1] = (double*) (void*) dasslData;
dasslData->rpar[2] = (double*) (void*) threadData;
saveJumpState = threadData->currentErrorStage;
threadData->currentErrorStage = ERROR_INTEGRATOR;
/* try */
#if !defined(OMC_EMCC)
MMC_TRY_INTERNAL(simulationJumpBuffer)
#endif
assertStreamPrint(threadData, 0 != dasslData->rpar, "could not passed to DDASKR");
/* If an event is triggered and processed restart dassl. */
if(!dasslData->dasslAvoidEventRestart && (solverInfo->didEventStep || 0 == dasslData->idid))
{
debugStreamPrint(LOG_EVENTS_V, 0, "Event-management forced reset of DDASKR");
/* obtain reset */
dasslData->info[0] = 0;
dasslData->idid = 0;
}
/* Calculate steps until TOUT is reached */
if (dasslData->dasslSteps)
{
/* If dasslsteps is selected, the dassl run to stopTime or next sample event */
if (data->simulationInfo->nextSampleEvent < data->simulationInfo->stopTime)
{
tout = data->simulationInfo->nextSampleEvent;
}
else
{
tout = data->simulationInfo->stopTime;
}
}
else
{
tout = solverInfo->currentTime + solverInfo->currentStepSize;
}
/* Check that tout is not less than timeValue
* else will dassl get in trouble. If that is the case we skip the current step. */
if (solverInfo->currentStepSize < DASSL_STEP_EPS)
{
infoStreamPrint(LOG_DASSL, 0, "Desired step to small try next one");
infoStreamPrint(LOG_DASSL, 0, "Interpolate linear");
/*euler step*/
for(i = 0; i < data->modelData->nStates; i++)
{
sData->realVars[i] = sDataOld->realVars[i] + stateDer[i] * solverInfo->currentStepSize;
}
sData->timeValue = solverInfo->currentTime + solverInfo->currentStepSize;
data->callback->functionODE(data, threadData);
solverInfo->currentTime = sData->timeValue;
TRACE_POP
return 0;
}
do
{
infoStreamPrint(LOG_DASSL, 1, "new step at time = %.15g", solverInfo->currentTime);
/* rhs final flag is FALSE during for dassl evaluation */
RHSFinalFlag = 0;
if (measure_time_flag) rt_accumulate(SIM_TIMER_SOLVER);
/* read input vars */
externalInputUpdate(data);
data->callback->input_function(data, threadData);
if (measure_time_flag) rt_tick(SIM_TIMER_SOLVER);
DDASKR(dasslData->residualFunction, (int*) &dasslData->N,
&solverInfo->currentTime, states, stateDer, &tout,
dasslData->info, dasslData->rtol, dasslData->atol, &dasslData->idid,
dasslData->rwork, &dasslData->lrw, dasslData->iwork, &dasslData->liw,
(double*) (void*) dasslData->rpar, dasslData->ipar, callJacobian, dummy_precondition,
dasslData->zeroCrossingFunction, (int*) &dasslData->ng, dasslData->jroot);
/* closing new step message */
messageClose(LOG_DASSL);
/* set ringbuffer time to current time */
sData->timeValue = solverInfo->currentTime;
/* rhs final flag is TRUE during for output evaluation */
RHSFinalFlag = 1;
if(dasslData->idid == -1)
{
fflush(stderr);
fflush(stdout);
warningStreamPrint(LOG_DASSL, 0, "A large amount of work has been expended.(About 500 steps). Trying to continue ...");
infoStreamPrint(LOG_DASSL, 0, "DASSL will try again...");
dasslData->info[0] = 1; /* try again */
if (solverInfo->currentTime <= data->simulationInfo->stopTime)
continue;
}
else if(dasslData->idid < 0)
{
fflush(stderr);
fflush(stdout);
retVal = continue_DASSL(&dasslData->idid, &data->simulationInfo->tolerance);
warningStreamPrint(LOG_STDOUT, 0, "can't continue. time = %f", sData->timeValue);
TRACE_POP
break;
}
else if(dasslData->idid == 5)
{
threadData->currentErrorStage = ERROR_EVENTSEARCH;
}
/* emit step, if dasslsteps is selected */
if (dasslData->dasslSteps)
{
if (omc_flag[FLAG_NOEQUIDISTANT_OUT_FREQ]){
/* output every n-th time step */
if (dasslStepsOutputCounter >= dasslData->dasslStepsFreq){
dasslStepsOutputCounter = 1; /* next line set it to one */
break;
}
dasslStepsOutputCounter++;
} else if (omc_flag[FLAG_NOEQUIDISTANT_OUT_TIME]){
/* output when time>=k*timeValue */
if (solverInfo->currentTime > dasslStepsOutputCounter * dasslData->dasslStepsTime){
dasslStepsOutputCounter++;
break;
}
} else {
break;
}
}
} while(dasslData->idid == 1);
states = dasslData->states;
#if !defined(OMC_EMCC)
MMC_CATCH_INTERNAL(simulationJumpBuffer)
#endif
threadData->currentErrorStage = saveJumpState;
/* if a state event occurs than no sample event does need to be activated */
if (data->simulationInfo->sampleActivated && solverInfo->currentTime < data->simulationInfo->nextSampleEvent)
{
data->simulationInfo->sampleActivated = 0;
}
if(ACTIVE_STREAM(LOG_DASSL))
{
infoStreamPrint(LOG_DASSL, 1, "dassl call statistics: ");
infoStreamPrint(LOG_DASSL, 0, "value of idid: %d", (int)dasslData->idid);
infoStreamPrint(LOG_DASSL, 0, "current time value: %0.4g", solverInfo->currentTime);
infoStreamPrint(LOG_DASSL, 0, "current integration time value: %0.4g", dasslData->rwork[3]);
infoStreamPrint(LOG_DASSL, 0, "step size H to be attempted on next step: %0.4g", dasslData->rwork[2]);
infoStreamPrint(LOG_DASSL, 0, "step size used on last successful step: %0.4g", dasslData->rwork[6]);
infoStreamPrint(LOG_DASSL, 0, "the order of the method used on the last step: %d", dasslData->iwork[7]);
infoStreamPrint(LOG_DASSL, 0, "the order of the method to be attempted on the next step: %d", dasslData->iwork[8]);
infoStreamPrint(LOG_DASSL, 0, "number of steps taken so far: %d", (int)dasslData->iwork[10]);
infoStreamPrint(LOG_DASSL, 0, "number of calls of functionODE() : %d", (int)dasslData->iwork[11]);
infoStreamPrint(LOG_DASSL, 0, "number of calculation of jacobian : %d", (int)dasslData->iwork[12]);
infoStreamPrint(LOG_DASSL, 0, "total number of convergence test failures: %d", (int)dasslData->iwork[13]);
infoStreamPrint(LOG_DASSL, 0, "total number of error test failures: %d", (int)dasslData->iwork[14]);
messageClose(LOG_DASSL);
}
/* save dassl stats */
for(ui = 0; ui < numStatistics; ui++)
{
assert(10 + ui < dasslData->liw);
solverInfo->solverStatsTmp[ui] = dasslData->iwork[10 + ui];
}
infoStreamPrint(LOG_DASSL, 0, "Finished DASSL step.");
if (measure_time_flag) rt_accumulate(SIM_TIMER_SOLVER);
TRACE_POP
return retVal;
}
static int continue_DASSL(int* idid, double* atol)
{
TRACE_PUSH
int retValue = -1;
switch(*idid)
{
case 1:
case 2:
case 3:
/* 1-4 means success */
break;
case -1:
warningStreamPrint(LOG_DASSL, 0, "A large amount of work has been expended.(About 500 steps). Trying to continue ...");
retValue = 1; /* adrpo: try to continue */
break;
case -2:
warningStreamPrint(LOG_STDOUT, 0, "The error tolerances are too stringent");
retValue = -2;
break;
case -3:
/* wbraun: don't throw at this point let the solver handle it */
/* throwStreamPrint("DDASKR: THE LAST STEP TERMINATED WITH A NEGATIVE IDID value"); */
retValue = -3;
break;
case -6:
warningStreamPrint(LOG_STDOUT, 0, "DDASSL had repeated error test failures on the last attempted step.");
retValue = -6;
break;
case -7:
warningStreamPrint(LOG_STDOUT, 0, "The corrector could not converge.");
retValue = -7;
break;
case -8:
warningStreamPrint(LOG_STDOUT, 0, "The matrix of partial derivatives is singular.");
retValue = -8;
break;
case -9:
warningStreamPrint(LOG_STDOUT, 0, "The corrector could not converge. There were repeated error test failures in this step.");
retValue = -9;
break;
case -10:
warningStreamPrint(LOG_STDOUT, 0, "A Modelica assert prevents the integrator to continue. For more information use -lv LOG_SOLVER");
retValue = -10;
break;
case -11:
warningStreamPrint(LOG_STDOUT, 0, "IRES equal to -2 was encountered and control is being returned to the calling program.");
retValue = -11;
break;
case -12:
warningStreamPrint(LOG_STDOUT, 0, "DDASSL failed to compute the initial YPRIME.");
retValue = -12;
break;
case -33:
warningStreamPrint(LOG_STDOUT, 0, "The code has encountered trouble from which it cannot recover.");
retValue = -33;
break;
}
TRACE_POP
return retValue;
}
int functionODE_residual(double *t, double *y, double *yd, double* cj,
double *delta, int *ires, double *rpar, int *ipar)
{
TRACE_PUSH
DATA* data = (DATA*)((double**)rpar)[0];
DASSL_DATA* dasslData = (DASSL_DATA*)((double**)rpar)[1];
threadData_t *threadData = (threadData_t*)((double**)rpar)[2];
double timeBackup;
long i;
int saveJumpState;
int success = 0;
if (measure_time_flag) rt_accumulate(SIM_TIMER_SOLVER);
if (measure_time_flag) rt_tick(SIM_TIMER_RESIDUALS);
if (data->simulationInfo->currentContext == CONTEXT_ALGEBRAIC)
{
setContext(data, t, CONTEXT_ODE);
}
printCurrentStatesVector(LOG_DASSL_STATES, y, data, *t);
printVector(LOG_DASSL_STATES, "yd", yd, data->modelData->nStates, *t);
timeBackup = data->localData[0]->timeValue;
data->localData[0]->timeValue = *t;
saveJumpState = threadData->currentErrorStage;
threadData->currentErrorStage = ERROR_INTEGRATOR;
/* try */
#if !defined(OMC_EMCC)
MMC_TRY_INTERNAL(simulationJumpBuffer)
#endif
/* read input vars */
externalInputUpdate(data);
data->callback->input_function(data, threadData);
/* eval input vars */
data->callback->functionODE(data, threadData);
/* get the difference between the temp_xd(=localData->statesDerivatives)
and xd(=statesDerivativesBackup) */
for(i=0; i < data->modelData->nStates; i++)
{
delta[i] = data->localData[0]->realVars[data->modelData->nStates + i] - yd[i];
}
printVector(LOG_DASSL_STATES, "dd", delta, data->modelData->nStates, *t);
success = 1;
#if !defined(OMC_EMCC)
MMC_CATCH_INTERNAL(simulationJumpBuffer)
#endif
if (!success) {
*ires = -1;
}
threadData->currentErrorStage = saveJumpState;
data->localData[0]->timeValue = timeBackup;
if (data->simulationInfo->currentContext == CONTEXT_ODE){
unsetContext(data);
}
if (measure_time_flag) rt_accumulate(SIM_TIMER_RESIDUALS);
if (measure_time_flag) rt_tick(SIM_TIMER_SOLVER);
TRACE_POP
return 0;
}
int function_ZeroCrossingsDASSL(int *neqm, double *t, double *y, double *yp,
int *ng, double *gout, double *rpar, int* ipar)
{
TRACE_PUSH
DATA* data = (DATA*)(void*)((double**)rpar)[0];
DASSL_DATA* dasslData = (DASSL_DATA*)(void*)((double**)rpar)[1];
threadData_t *threadData = (threadData_t*)(void*)((double**)rpar)[2];
double timeBackup;
int saveJumpState;
if (measure_time_flag) rt_accumulate(SIM_TIMER_SOLVER);
if (measure_time_flag) rt_tick(SIM_TIMER_EVENT);
if (data->simulationInfo->currentContext == CONTEXT_ALGEBRAIC)
{
setContext(data, t, CONTEXT_EVENTS);
}
saveJumpState = threadData->currentErrorStage;
threadData->currentErrorStage = ERROR_EVENTSEARCH;
timeBackup = data->localData[0]->timeValue;
data->localData[0]->timeValue = *t;
/* read input vars */
externalInputUpdate(data);
data->callback->input_function(data, threadData);
/* eval needed equations*/
data->callback->function_ZeroCrossingsEquations(data, threadData);
data->callback->function_ZeroCrossings(data, threadData, gout);
threadData->currentErrorStage = saveJumpState;
data->localData[0]->timeValue = timeBackup;
if (data->simulationInfo->currentContext == CONTEXT_EVENTS){
unsetContext(data);
}
if (measure_time_flag) rt_accumulate(SIM_TIMER_EVENT);
if (measure_time_flag) rt_tick(SIM_TIMER_SOLVER);
TRACE_POP
return 0;
}
/*
* Sets element (l,j) in matrixA to given value val.
*/
void setJacElementDasslSparse(int l, int j, int nth, double val, void* matrixA,
int rows)
{
int k = j*rows + l;
((double*) matrixA)[k]=val;
}
/* \fn jacA_symColored(double *t, double *y, double *yprime, double *deltaD, double *pd, double *cj, double *h, double *wt,
double *rpar, int* ipar)
*
*
* This function calculates symbolically the jacobian matrix and exploiting the coloring.
*/
int jacA_symColored(double *t, double *y, double *yprime, double *delta,
double *matrixA, double *cj, double *h, double *wt,
double *rpar, int *ipar)
{
TRACE_PUSH
DATA* data = (DATA*)(void*)((double**)rpar)[0];
threadData_t *threadData = (threadData_t*)(void*)((double**)rpar)[2];
DASSL_DATA* dasslData = (DASSL_DATA*)(void*)((double**)rpar)[1];
const int index = data->callback->INDEX_JAC_A;
ANALYTIC_JACOBIAN* jac = &(data->simulationInfo->analyticJacobians[index]);
unsigned int columns = jac->sizeCols;
unsigned int rows = jac->sizeRows;
unsigned int sizeTmpVars = jac->sizeTmpVars;
SPARSE_PATTERN* spp = jac->sparsePattern;
genericColoredSymbolicJacobianEvaluation(rows, columns, spp, matrixA, jac,
data, threadData, &setJacElementDasslSparse);
TRACE_POP
return 0;
}
/* \fn jacA_sym(double *t, double *y, double *yprime, double *deltaD, double *pd, double *cj, double *h, double *wt,
double *rpar, int* ipar)
*
*
* This function calculates symbolically the jacobian matrix.
*/
int jacA_sym(double *t, double *y, double *yprime, double *delta,
double *matrixA, double *cj, double *h, double *wt, double *rpar,
int *ipar)
{
TRACE_PUSH
DATA* data = (DATA*)(void*)((double**)rpar)[0];
DASSL_DATA* dasslData = (DASSL_DATA*)(void*)((double**)rpar)[1];
threadData_t *threadData = (threadData_t*)(void*)((double**)rpar)[2];
const int index = data->callback->INDEX_JAC_A;
ANALYTIC_JACOBIAN* t_jac = &(data->simulationInfo->analyticJacobians[index]);
unsigned int columns = t_jac->sizeCols;
unsigned int rows = t_jac->sizeRows;
unsigned int sizeTmpVars = t_jac->sizeTmpVars;
unsigned int i,j;
for(i=0; i < columns; i++)
{
t_jac->seedVars[i] = 1.0;
data->callback->functionJacA_column(data, threadData, t_jac, NULL);
for(j = 0; j < rows; j++)
matrixA[i*columns+j] = t_jac->resultVars[j];
t_jac->seedVars[i] = 0.0;
}
TRACE_POP
return 0;
}
/* \fn jacA_num(double *t, double *y, double *yprime, double *deltaD, double *pd, double *cj, double *h, double *wt,
double *rpar, int* ipar)
*
*
* This function calculates a jacobian matrix by
* numerical with forward finite differences.
*/
int jacA_num(double *t, double *y, double *yprime, double *delta,
double *matrixA, double *cj, double *h, double *wt, double *rpar,
int *ipar)
{
TRACE_PUSH
DATA* data = (DATA*)(void*)((double**)rpar)[0];
DASSL_DATA* dasslData = (DASSL_DATA*)(void*)((double**)rpar)[1];
threadData_t *threadData = (threadData_t*)(void*)((double**)rpar)[2];
double delta_h = numericalDifferentiationDeltaXsolver;
double delta_hh,delta_hhh, deltaInv;
double ysave;
double ypsave;
int ires;
int i,j;
/* set context for the start values extrapolation of non-linear algebraic loops */
setContext(data, t, CONTEXT_JACOBIAN);