-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathsolver_idas.cpp
More file actions
1389 lines (1222 loc) · 46.4 KB
/
solver_idas.cpp
File metadata and controls
1389 lines (1222 loc) · 46.4 KB
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
#include "amici/solver_idas.h"
#include "amici/exception.h"
#include "amici/model_dae.h"
#include "amici/sundials_linsol_wrapper.h"
#include <idas/idas.h>
#include <idas/idas_impl.h>
#include <amd.h>
#include <btf.h>
#include <colamd.h>
#include <klu.h>
namespace amici {
constexpr auto ONE = SUN_RCONST(1.0);
// Ensure AMICI options and return codes are in sync with SUNDIALS
static_assert((int)InternalSensitivityMethod::simultaneous == IDA_SIMULTANEOUS);
static_assert((int)InternalSensitivityMethod::staggered == IDA_STAGGERED);
static_assert((int)InterpolationType::hermite == IDA_HERMITE);
static_assert((int)InterpolationType::polynomial == IDA_POLYNOMIAL);
#define STATIC_ASSERT_EQUAL(amici_constant, ida_constant) \
static_assert( \
amici_constant == ida_constant, #amici_constant " != " #ida_constant \
)
STATIC_ASSERT_EQUAL(amici::AMICI_SUCCESS, IDA_SUCCESS);
STATIC_ASSERT_EQUAL(amici::AMICI_ROOT_RETURN, IDA_ROOT_RETURN);
STATIC_ASSERT_EQUAL(amici::AMICI_DATA_RETURN, IDA_TSTOP_RETURN);
STATIC_ASSERT_EQUAL(amici::AMICI_ILL_INPUT, IDA_ILL_INPUT);
STATIC_ASSERT_EQUAL(amici::AMICI_NORMAL, IDA_NORMAL);
STATIC_ASSERT_EQUAL(amici::AMICI_ONE_STEP, IDA_ONE_STEP);
STATIC_ASSERT_EQUAL(amici::AMICI_TOO_MUCH_ACC, IDA_TOO_MUCH_ACC);
STATIC_ASSERT_EQUAL(amici::AMICI_TOO_MUCH_WORK, IDA_TOO_MUCH_WORK);
STATIC_ASSERT_EQUAL(amici::AMICI_ERR_FAILURE, IDA_ERR_FAIL);
STATIC_ASSERT_EQUAL(amici::AMICI_CONV_FAILURE, IDA_CONV_FAIL);
STATIC_ASSERT_EQUAL(amici::AMICI_LSETUP_FAIL, IDA_LSETUP_FAIL);
STATIC_ASSERT_EQUAL(amici::AMICI_LINESEARCH_FAIL, IDA_LINESEARCH_FAIL);
// This does not match the CVODE code, we need separate return values
STATIC_ASSERT_EQUAL(amici::AMICI_IDAS_CONSTR_FAIL, IDA_CONSTR_FAIL);
STATIC_ASSERT_EQUAL(amici::AMICI_WARNING, IDA_WARNING);
/*
* The following static members are callback function to IDAS.
* Their signatures must not be changes.
*/
static int
fxdot(realtype t, N_Vector x, N_Vector dx, N_Vector xdot, void* user_data);
static int
fJ(realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xdot, SUNMatrix J,
void* user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3);
static int fJSparse(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xdot,
SUNMatrix J, void* user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3
);
static int
fJB(realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector xBdot, SUNMatrix JB, void* user_data, N_Vector tmp1B,
N_Vector tmp2B, N_Vector tmp3B);
static int fJSparseB(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector xBdot, SUNMatrix JB, void* user_data, N_Vector tmp1B,
N_Vector tmp2B, N_Vector tmp3B
);
static int fJBand(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xdot,
SUNMatrix J, void* user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3
);
static int fJBandB(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector xBdot, SUNMatrix JB, void* user_data, N_Vector tmp1B,
N_Vector tmp2B, N_Vector tmp3B
);
static int
fJv(realtype t, N_Vector x, N_Vector dx, N_Vector xdot, N_Vector v, N_Vector Jv,
realtype cj, void* user_data, N_Vector tmp1, N_Vector tmp2);
static int fJvB(
realtype t, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector xBdot, N_Vector vB, N_Vector JvB, realtype cj, void* user_data,
N_Vector tmpB1, N_Vector tmpB2
);
static int
froot(realtype t, N_Vector x, N_Vector dx, realtype* root, void* user_data);
static int fxBdot(
realtype t, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector xBdot, void* user_data
);
static int fqBdot(
realtype t, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector qBdot, void* user_data
);
static int fxBdot_ss(
realtype t, N_Vector xB, N_Vector dxB, N_Vector xBdot, void* user_data
);
static int fqBdot_ss(
realtype t, N_Vector xB, N_Vector dxB, N_Vector qBdot, void* user_data
);
static int fJSparseB_ss(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xBdot,
SUNMatrix JB, void* user_data, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3
);
static int fsxdot(
int Ns, realtype t, N_Vector x, N_Vector dx, N_Vector xdot, N_Vector* sx,
N_Vector* sdx, N_Vector* sxdot, void* user_data, N_Vector tmp1,
N_Vector tmp2, N_Vector tmp3
);
/* Function implementations */
void IDASolver::init(
realtype const t0, AmiVector const& x0, AmiVector const& dx0
) const {
int status;
solver_was_called_F_ = false;
t_ = t0;
x_ = x0;
dx_ = dx0;
if (get_init_done()) {
status = IDAReInit(
solver_memory_.get(), t_, x_.get_nvector(), dx_.get_nvector()
);
} else {
status = IDAInit(
solver_memory_.get(), fxdot, t_, x_.get_nvector(), dx_.get_nvector()
);
set_init_done();
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAInit");
}
void IDASolver::init_steady_state(
realtype const /*t0*/, AmiVector const& /*x0*/, AmiVector const& /*dx0*/
) const {
/* We need to set the steadystate rhs function. SUndials doesn't have this
in its public api, so we have to change it in the solver memory,
as re-calling init would unset solver settings. */
auto ida_mem = static_cast<IDAMem>(solver_memory_.get());
ida_mem->ida_res = fxBdot_ss;
}
void IDASolver::sens_init_1(
AmiVectorArray const& sx0, AmiVectorArray const& sdx0
) const {
int status = IDA_SUCCESS;
sx_ = sx0;
sdx_ = sdx0;
if (get_sensitivity_method() == SensitivityMethod::forward
&& nplist() > 0) {
if (get_sens_init_done()) {
status = IDASensReInit(
solver_memory_.get(),
static_cast<int>(get_internal_sensitivity_method()),
sx_.get_nvector_array(), sdx_.get_nvector_array()
);
} else {
status = IDASensInit(
solver_memory_.get(), nplist(),
static_cast<int>(get_internal_sensitivity_method()), fsxdot,
sx_.get_nvector_array(), sdx_.get_nvector_array()
);
set_sens_init_done();
}
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASensInit");
}
void IDASolver::b_init(
int const which, realtype const tf, AmiVector const& xB0,
AmiVector const& dxB0
) const {
int status;
xB_ = xB0;
dxB_ = dxB0;
if (get_init_done_b(which))
status = IDAReInitB(
solver_memory_.get(), which, tf, xB_.get_nvector(),
dxB_.get_nvector()
);
else {
status = IDAInitB(
solver_memory_.get(), which, fxBdot, tf, xB_.get_nvector(),
dxB_.get_nvector()
);
set_init_done_b(which);
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAInitB");
}
void IDASolver::qb_init(int const which, AmiVector const& xQB0) const {
int status;
xQB_.copy(xQB0);
if (get_quad_init_done_b(which))
status
= IDAQuadReInitB(solver_memory_.get(), which, xQB_.get_nvector());
else {
status = IDAQuadInitB(
solver_memory_.get(), which, fqBdot, xQB_.get_nvector()
);
set_quad_init_done_b(which);
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAQuadInitB");
}
void IDASolver::root_init(int const ne) const {
int status = IDARootInit(solver_memory_.get(), ne, froot);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDARootInit");
}
void IDASolver::set_dense_jac_fn() const {
int status = IDASetJacFn(solver_memory_.get(), fJ);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDADlsSetDenseJacFn");
}
void IDASolver::set_sparse_jac_fn() const {
int status = IDASetJacFn(solver_memory_.get(), fJSparse);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASlsSetSparseJacFn");
}
void IDASolver::set_band_jac_fn() const {
int status = IDASetJacFn(solver_memory_.get(), fJBand);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDADlsSetBandJacFn");
}
void IDASolver::set_jac_times_vec_fn() const {
int status = IDASetJacTimes(solver_memory_.get(), nullptr, fJv);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASpilsSetJacTimesVecFn");
}
void IDASolver::set_dense_jac_fn_b(int const which) const {
int status = IDASetJacFnB(solver_memory_.get(), which, fJB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDADlsSetDenseJacFnB");
}
void IDASolver::set_sparse_jac_fn_b(int const which) const {
int status = IDASetJacFnB(solver_memory_.get(), which, fJSparseB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASlsSetSparseJacFnB");
}
void IDASolver::set_band_jac_fn_b(int const which) const {
int status = IDASetJacFnB(solver_memory_.get(), which, fJBandB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDADlsSetBandJacFnB");
}
void IDASolver::set_jac_times_vec_fn_b(int const which) const {
int status = IDASetJacTimesB(solver_memory_.get(), which, nullptr, fJvB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASpilsSetJacTimesVecFnB");
}
void IDASolver::set_sparse_jac_fn_ss() const {
int status = IDASetJacFn(solver_memory_.get(), fJSparseB_ss);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetJacFn");
}
void IDASolver::apply_max_nonlin_iters() const {
int status
= IDASetMaxNonlinIters(solver_memory_.get(), get_max_nonlin_iters());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetMaxNonlinIters");
}
void IDASolver::apply_max_conv_fails() const {
int status = IDASetMaxConvFails(solver_memory_.get(), get_max_conv_fails());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetMaxConvFails");
}
void IDASolver::apply_constraints() const {
Solver::apply_constraints();
int status = IDASetConstraints(
solver_memory_.get(),
constraints_.size() > 0 ? constraints_.get_nvector() : nullptr
);
if (status != IDA_SUCCESS) {
throw IDAException(status, "IDASetConstraints");
}
}
void IDASolver::apply_max_step_size() const {
int status = IDASetMaxStep(solver_memory_.get(), get_max_step_size());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetMaxStep");
}
Solver* IDASolver::clone() const { return new IDASolver(*this); }
void IDASolver::allocate_solver() const {
if (!solver_memory_)
solver_memory_ = std::unique_ptr<void, std::function<void(void*)>>(
IDACreate(sunctx_), [](void* ptr) { IDAFree(&ptr); }
);
}
void IDASolver::set_ss_tolerances(
realtype const rtol, realtype const atol
) const {
int status = IDASStolerances(solver_memory_.get(), rtol, atol);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASStolerances");
}
void IDASolver::set_sens_ss_tolerances(
realtype const rtol, realtype const* atol
) const {
int status = IDASensSStolerances(
solver_memory_.get(), rtol, const_cast<realtype*>(atol)
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASensEEtolerances");
}
void IDASolver::set_sens_err_con(bool const error_corr) const {
int status = IDASetSensErrCon(solver_memory_.get(), error_corr);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetSensErrCon");
}
void IDASolver::set_quad_err_con_b(int const which, bool const flag) const {
int status = IDASetQuadErrConB(solver_memory_.get(), which, flag);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetQuadErrConB");
}
void IDASolver::set_quad_err_con(bool const flag) const {
int status = IDASetQuadErrCon(solver_memory_.get(), flag);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetQuadErrCon");
}
void IDASolver::get_root_info(int* rootsfound) const {
int status = IDAGetRootInfo(solver_memory_.get(), rootsfound);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetRootInfo");
}
void IDASolver::set_user_data() const {
int status = IDASetUserData(solver_memory_.get(), &user_data_);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetUserData");
}
void IDASolver::set_user_data_b(int const which) const {
int status = IDASetUserDataB(solver_memory_.get(), which, &user_data_);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetUserDataB");
}
void IDASolver::set_max_num_steps(long int const mxsteps) const {
int status = IDASetMaxNumSteps(solver_memory_.get(), mxsteps);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetMaxNumSteps");
}
void IDASolver::set_stab_lim_det(int const /*stldet*/) const {}
void IDASolver::
set_stab_lim_det_b(int const /*which*/, int const /*stldet*/) const {}
void IDASolver::set_id(Model const* model) const {
N_Vector id = N_VMake_Serial(
model->nx_solver, const_cast<realtype*>(model->get_id_list().data()),
sunctx_
);
int status = IDASetId(solver_memory_.get(), id);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetId");
N_VDestroy_Serial(id);
}
void IDASolver::set_suppress_alg(bool const flag) const {
int status = IDASetSuppressAlg(solver_memory_.get(), flag);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetSuppressAlg");
}
void IDASolver::reset_state(
void* ami_mem, const_N_Vector yy0, const_N_Vector yp0
) const {
auto ida_mem = static_cast<IDAMem>(ami_mem);
/* here we force the order in the next step to zero, and update the
phi arrays, this is largely copied from IDAReInit with
explanations from idas_impl.h
*/
/* Initialize the phi array */
N_VScale(ONE, const_cast<N_Vector>(yy0), ida_mem->ida_phi[0]);
N_VScale(ONE, const_cast<N_Vector>(yp0), ida_mem->ida_phi[1]);
/* Set step parameters */
/* current order */
ida_mem->ida_kk = 0;
}
void IDASolver::reinit_post_process_f(realtype const tnext) const {
reinit_post_process(solver_memory_.get(), &t_, &x_, &dx_, tnext);
}
void IDASolver::reinit_post_process_b(realtype const tnext) const {
realtype tBret;
auto ida_mem = static_cast<IDAMem>(solver_memory_.get());
auto idaadj_mem = ida_mem->ida_adj_mem;
auto idaB_mem = idaadj_mem->IDAB_mem;
// loop over all backward problems
while (idaB_mem != nullptr) {
// store current backward problem in ca_mem to make it accessible in
// adjoint rhs wrapper functions
idaadj_mem->ia_bckpbCrt = idaB_mem;
reinit_post_process(
static_cast<void*>(idaB_mem->IDA_mem), &tBret, &xB_, &dxB_, tnext
);
// idaB_mem->ida_tout = tBret;
idaB_mem = idaB_mem->ida_next;
}
force_reinit_postprocess_B_ = false;
}
void IDASolver::reinit_post_process(
void* ami_mem, realtype* t, AmiVector* yout, AmiVector* ypout, realtype tout
) const {
auto ida_mem = static_cast<IDAMem>(ami_mem);
auto nst_tmp = ida_mem->ida_nst;
ida_mem->ida_nst = 0;
auto status = IDASetStopTime(ida_mem, tout);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetStopTime");
status = IDASolve(
ami_mem, tout, t, yout->get_nvector(), ypout->get_nvector(),
IDA_ONE_STEP
);
if (status != IDA_SUCCESS)
throw IDAException(status, "reInitPostProcess");
ida_mem->ida_nst = nst_tmp + 1;
if (ida_mem->ida_adjMallocDone == SUNTRUE) {
/* add new step to history array, this is copied from CVodeF */
auto ia_mem = ida_mem->ida_adj_mem;
auto dt_mem = ia_mem->dt_mem;
if (ida_mem->ida_nst % ia_mem->ia_nsteps == 0) {
/* currently not implemented, we should never get here as we
limit IDA_mem->cv_nst < ca_mem->ca_nsteps, keeping this for
future regression */
throw IDAException(AMICI_ERROR, "reInitPostProcess");
}
/* Load next point in dt_mem */
dt_mem[ida_mem->ida_nst % ia_mem->ia_nsteps]->t = *t;
ia_mem->ia_storePnt(
ida_mem, dt_mem[ida_mem->ida_nst % ia_mem->ia_nsteps]
);
/* Set t1 field of the current ckeck point structure
for the case in which there will be no future
check points */
ia_mem->ck_mem->ck_t1 = *t;
/* tfinal is now set to *tret */
ia_mem->ia_tfinal = *t;
}
}
void IDASolver::reinit(
realtype const t0, AmiVector const& yy0, AmiVector const& yp0
) const {
auto ida_mem = static_cast<IDAMem>(solver_memory_.get());
ida_mem->ida_tn = t0;
if (solver_was_called_F_)
force_reinit_postprocess_F_ = true;
x_.copy(yy0);
dx_.copy(yp0);
reset_state(ida_mem, x_.get_nvector(), xB_.get_nvector());
}
void IDASolver::sens_reinit(
AmiVectorArray const& yyS0, AmiVectorArray const& ypS0
) const {
auto ida_mem = static_cast<IDAMem>(solver_memory_.get());
/* Initialize znS[0] in the history array */
for (int is = 0; is < nplist(); is++)
ida_mem->ida_cvals[is] = ONE;
if (solver_was_called_F_)
force_reinit_postprocess_F_ = true;
sx_.copy(yyS0);
sdx_.copy(ypS0);
auto status = N_VScaleVectorArray(
nplist(), ida_mem->ida_cvals, sx_.get_nvector_array(),
ida_mem->ida_phiS[0]
);
if (status != IDA_SUCCESS)
throw IDAException(IDA_VECTOROP_ERR, "IDASensReInit");
status = N_VScaleVectorArray(
nplist(), ida_mem->ida_cvals, sdx_.get_nvector_array(),
ida_mem->ida_phiS[1]
);
if (status != IDA_SUCCESS)
throw IDAException(IDA_VECTOROP_ERR, "IDASensReInit");
}
void IDASolver::sens_toggle_off() const {
auto status = IDASensToggleOff(solver_memory_.get());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASensToggleOff");
IDASensFree(solver_memory_.get());
/* need to deallocate sensi memory, otherwise can't reenable */
sens_initialized_ = false;
}
void IDASolver::reinit_b(
int const which, realtype const tB0, AmiVector const& yyB0,
AmiVector const& ypB0
) const {
auto ida_memB
= static_cast<IDAMem>(IDAGetAdjIDABmem(solver_memory_.get(), which));
if (solver_was_called_B_)
force_reinit_postprocess_B_ = true;
ida_memB->ida_tn = tB0;
xB_.copy(yyB0);
dxB_.copy(ypB0);
reset_state(ida_memB, xB_.get_nvector(), dxB_.get_nvector());
}
void IDASolver::reinit_quad_b(int const which, AmiVector const& yQB0) const {
auto ida_memB
= static_cast<IDAMem>(IDAGetAdjIDABmem(solver_memory_.get(), which));
if (solver_was_called_B_)
force_reinit_postprocess_B_ = true;
xQB_.copy(yQB0);
N_VScale(ONE, xQB_.get_nvector(), ida_memB->ida_phiQ[0]);
}
void IDASolver::set_sens_params(
realtype const* p, realtype const* pbar, int const* plist
) const {
int status = IDASetSensParams(
solver_memory_.get(), const_cast<realtype*>(p),
const_cast<realtype*>(pbar), const_cast<int*>(plist)
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetSensParams");
}
void IDASolver::get_dky(realtype const t, int const k) const {
int status = IDAGetDky(solver_memory_.get(), t, k, dky_.get_nvector());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetDky");
}
void IDASolver::get_sens() const {
realtype tDummy = 0;
int status
= IDAGetSens(solver_memory_.get(), &tDummy, sx_.get_nvector_array());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetSens");
}
void IDASolver::get_sens_dky(realtype const t, int const k) const {
int status
= IDAGetSensDky(solver_memory_.get(), t, k, sx_.get_nvector_array());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetSens");
}
void IDASolver::get_b(int const which) const {
realtype tDummy = 0;
int status = IDAGetB(
solver_memory_.get(), which, &tDummy, xB_.get_nvector(),
dxB_.get_nvector()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetB");
}
void IDASolver::get_dky_b(
realtype const t, int const k, int const which
) const {
int status = IDAGetDky(
IDAGetAdjIDABmem(solver_memory_.get(), which), t, k, dky_.get_nvector()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetB");
}
void IDASolver::get_quad_b(int const which) const {
realtype tDummy = 0;
int status
= IDAGetQuadB(solver_memory_.get(), which, &tDummy, xQB_.get_nvector());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetQuadB");
}
void IDASolver::get_quad(realtype& t) const {
int status = IDAGetQuad(solver_memory_.get(), &t, xQ_.get_nvector());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetQuad");
}
void IDASolver::get_quad_dky_b(
realtype const t, int const k, int const which
) const {
int status = IDAGetQuadDky(
IDAGetAdjIDABmem(solver_memory_.get(), which), t, k, xQB_.get_nvector()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetB");
}
void IDASolver::get_quad_dky(realtype const t, int const k) const {
int status = IDAGetQuadDky(solver_memory_.get(), t, k, xQ_.get_nvector());
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetQuadDky");
}
void IDASolver::adj_init() const {
int status;
if (get_adj_init_done()) {
status = IDAAdjReInit(solver_memory_.get());
} else {
status = IDAAdjInit(
solver_memory_.get(), static_cast<int>(maxsteps_),
static_cast<int>(interp_type_)
);
set_adj_init_done();
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAAdjInit");
}
void IDASolver::quad_init(AmiVector const& xQ0) const {
int status;
xQ_.copy(xQ0);
if (get_quad_init_done()) {
status = IDAQuadReInit(
solver_memory_.get(), const_cast<N_Vector>(xQ0.get_nvector())
);
} else {
status
= IDAQuadInit(solver_memory_.get(), fqBdot_ss, xQ_.get_nvector());
set_quad_init_done();
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAQuadInit");
}
void IDASolver::allocate_solver_b(int* which) const {
if (!solver_memory_B_.empty()) {
*which = 0;
return;
}
int status = IDACreateB(solver_memory_.get(), which);
if (*which + 1 > static_cast<int>(solver_memory_B_.size()))
solver_memory_B_.resize(*which + 1);
solver_memory_B_.at(*which)
= std::unique_ptr<void, std::function<void(void*)>>(
get_adj_b_mem(solver_memory_.get(), *which), [](void* /*ptr*/) {}
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDACreateB");
}
void IDASolver::set_ss_tolerances_b(
int const which, realtype const relTolB, realtype const absTolB
) const {
int status
= IDASStolerancesB(solver_memory_.get(), which, relTolB, absTolB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASStolerancesB");
}
void IDASolver::quad_ss_tolerances_b(
int const which, realtype const reltolQB, realtype const abstolQB
) const {
int status
= IDAQuadSStolerancesB(solver_memory_.get(), which, reltolQB, abstolQB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAQuadSStolerancesB");
}
void IDASolver::quad_ss_tolerances(
realtype const reltolQ, realtype const abstolQ
) const {
int status = IDAQuadSStolerances(solver_memory_.get(), reltolQ, abstolQ);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAQuadSStolerances");
}
int IDASolver::solve(realtype const tout, int const itask) const {
if (force_reinit_postprocess_F_)
reinit_post_process_f(tout);
int status = IDASolve(
solver_memory_.get(), tout, &t_, x_.get_nvector(), dx_.get_nvector(),
itask
);
solver_was_called_F_ = true;
if (status < 0) // status > 0 is okay and is used for e.g. root return
throw IntegrationFailure(status, t_);
return status;
}
int IDASolver::solve_f(
realtype const tout, int const itask, int* ncheckPtr
) const {
if (force_reinit_postprocess_F_)
reinit_post_process_f(tout);
int status = IDASolveF(
solver_memory_.get(), tout, &t_, x_.get_nvector(), xB_.get_nvector(),
itask, ncheckPtr
);
solver_was_called_F_ = true;
if (status < 0) // status > 0 is okay and is used for e.g. root return
throw IntegrationFailure(status, t_);
return status;
}
void IDASolver::solve_b(realtype const tBout, int const itaskB) const {
if (force_reinit_postprocess_B_)
reinit_post_process_b(tBout);
int status = IDASolveB(solver_memory_.get(), tBout, itaskB);
solver_was_called_B_ = true;
// This does not seem to be documented, but IDASolveB may also return
// IDA_TSTOP_RETURN
// https://github.com/LLNL/sundials/issues/580
if (status != IDA_SUCCESS && status != IDA_TSTOP_RETURN) {
gsl_Expects(status < 0);
throw IntegrationFailure(status, tBout);
}
}
void IDASolver::set_max_num_steps_b(
int const which, long int const mxstepsB
) const {
int status = IDASetMaxNumStepsB(solver_memory_.get(), which, mxstepsB);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetMaxNumStepsB");
}
void IDASolver::diag() const {
throw AmiException("Diag Solver was not implemented for DAEs");
}
void IDASolver::diag_b(int const /*which*/) const {
throw AmiException("Diag Solver was not implemented for DAEs");
}
void IDASolver::get_num_steps(void const* ami_mem, long int* numsteps) const {
int status = IDAGetNumSteps(const_cast<void*>(ami_mem), numsteps);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetNumSteps");
}
void IDASolver::get_num_rhs_evals(
void const* ami_mem, long int* numrhsevals
) const {
int status = IDAGetNumResEvals(const_cast<void*>(ami_mem), numrhsevals);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetNumResEvals");
}
void IDASolver::get_num_err_test_fails(
void const* ami_mem, long int* numerrtestfails
) const {
int status
= IDAGetNumErrTestFails(const_cast<void*>(ami_mem), numerrtestfails);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetNumErrTestFails");
}
void IDASolver::get_num_non_lin_solv_conv_fails(
void const* ami_mem, long int* numnonlinsolvconvfails
) const {
int status = IDAGetNumNonlinSolvConvFails(
const_cast<void*>(ami_mem), numnonlinsolvconvfails
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetNumNonlinSolvConvFails");
}
void IDASolver::get_last_order(void const* ami_mem, int* order) const {
int status = IDAGetLastOrder(const_cast<void*>(ami_mem), order);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetLastOrder");
}
void* IDASolver::get_adj_b_mem(void* ami_mem, int which) const {
return IDAGetAdjIDABmem(ami_mem, which);
}
void IDASolver::calc_ic(realtype tout1) const {
int status = IDACalcIC(solver_memory_.get(), IDA_YA_YDP_INIT, tout1);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDACalcIC");
status = IDAGetConsistentIC(
solver_memory_.get(), x_.get_nvector(), dx_.get_nvector()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDAGetConsistentIC");
}
void IDASolver::calc_ic_b(int const which, realtype const tout1) const {
int status = IDACalcICB(
solver_memory_.get(), which, tout1, xB_.get_nvector(),
dxB_.get_nvector()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDACalcICB");
}
void IDASolver::set_stop_time(realtype const tstop) const {
int status = IDASetStopTime(solver_memory_.get(), tstop);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetStopTime");
}
void IDASolver::turn_off_root_finding() const {
int status = IDARootInit(solver_memory_.get(), 0, nullptr);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDARootInit");
}
Model const* IDASolver::get_model() const {
if (!solver_memory_)
throw AmiException(
"Solver has not been allocated, information is not available"
);
auto const ida_mem = static_cast<IDAMem>(solver_memory_.get());
if (auto user_data = static_cast<user_data_type*>(ida_mem->ida_user_data))
return user_data->first;
return nullptr;
}
void IDASolver::set_linear_solver() const {
int status = IDASetLinearSolver(
solver_memory_.get(), linear_solver_->get(), linear_solver_->getMatrix()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "setLinearSolver");
}
void IDASolver::set_linear_solver_b(int const which) const {
int status = IDASetLinearSolverB(
solver_memory_B_[which].get(), which, linear_solver_B_->get(),
linear_solver_B_->getMatrix()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "setLinearSolverB");
}
void IDASolver::set_non_linear_solver() const {
int status = IDASetNonlinearSolver(
solver_memory_.get(), non_linear_solver_->get()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetNonlinearSolver");
}
void IDASolver::set_non_linear_solver_sens() const {
if (get_sensitivity_order() < SensitivityOrder::first)
return;
if (get_sensitivity_method() != SensitivityMethod::forward)
return;
int status = IDA_SUCCESS;
switch (ism_) {
case InternalSensitivityMethod::staggered:
status = IDASetNonlinearSolverSensStg(
solver_memory_.get(), non_linear_solver_sens_->get()
);
break;
case InternalSensitivityMethod::simultaneous:
status = IDASetNonlinearSolverSensSim(
solver_memory_.get(), non_linear_solver_sens_->get()
);
break;
case InternalSensitivityMethod::staggered1:
default:
throw AmiException(
"Unsupported internal sensitivity method selected: %d", ism_
);
}
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASolver::setNonLinearSolverSens");
}
void IDASolver::set_non_linear_solver_b(int which) const {
int status = IDASetNonlinearSolverB(
solver_memory_.get(), which, non_linear_solver_B_->get()
);
if (status != IDA_SUCCESS)
throw IDAException(status, "IDASetNonlinearSolverB");
}
/**
* @brief Jacobian of xdot with respect to states x
* @param N number of state variables
* @param t timepoint
* @param cj scaling factor, inverse of the step size
* @param x Vector with the states
* @param dx Vector with the derivative states
* @param xdot Vector with the right hand side
* @param J Matrix to which the Jacobian will be written
* @param user_data object with user input
* @param tmp1 temporary storage vector
* @param tmp2 temporary storage vector
* @param tmp3 temporary storage vector
* @return status flag indicating successful execution
**/
int fJ(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xdot,
SUNMatrix J, void* user_data, N_Vector /*tmp1*/, N_Vector /*tmp2*/,
N_Vector /*tmp3*/
) {
auto typed_udata = static_cast<IDASolver::user_data_type*>(user_data);
Expects(typed_udata);
auto model = dynamic_cast<Model_DAE*>(typed_udata->first);
Expects(model);
model->fJ(t, cj, x, dx, xdot, J);
return model->check_finite(J, ModelQuantity::J, t);
}
/**
* @brief Jacobian of xBdot with respect to adjoint state xB
* @param t timepoint
* @param cj scaling factor, inverse of the step size
* @param x Vector with the states
* @param dx Vector with the derivative states
* @param xB Vector with the adjoint states
* @param dxB Vector with the adjoint derivative states
* @param xBdot Vector with the adjoint right hand side
* @param JB Matrix to which the Jacobian will be written
* @param user_data object with user input @type Model_DAE
* @param tmp1B temporary storage vector
* @param tmp2B temporary storage vector
* @param tmp3B temporary storage vector
* @return status flag indicating successful execution
**/
int fJB(
realtype t, realtype cj, N_Vector x, N_Vector dx, N_Vector xB, N_Vector dxB,
N_Vector /*xBdot*/, SUNMatrix JB, void* user_data, N_Vector /*tmp1B*/,
N_Vector /*tmp2B*/, N_Vector /*tmp3B*/
) {
auto typed_udata = static_cast<IDASolver::user_data_type*>(user_data);
Expects(typed_udata);
auto model = dynamic_cast<Model_DAE*>(typed_udata->first);
Expects(model);
model->fJB(t, cj, x, dx, xB, dxB, JB);
return model->check_finite(JB, ModelQuantity::JB, t);
}
/**
* @brief J in sparse form (for sparse solvers from the SuiteSparse Package)
* @param t timepoint
* @param cj scalar in Jacobian (inverse stepsize)
* @param x Vector with the states
* @param dx Vector with the derivative states