-
Notifications
You must be signed in to change notification settings - Fork 162
Expand file tree
/
Copy patharkode_erkstep.c
More file actions
2094 lines (1800 loc) · 70.4 KB
/
arkode_erkstep.c
File metadata and controls
2094 lines (1800 loc) · 70.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
/*---------------------------------------------------------------
* Programmer(s): Daniel R. Reynolds @ UMBC
*---------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2025-2026, Lawrence Livermore National Security,
* University of Maryland Baltimore County, and the SUNDIALS contributors.
* Copyright (c) 2013-2025, Lawrence Livermore National Security
* and Southern Methodist University.
* Copyright (c) 2002-2013, Lawrence Livermore National Security.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
*---------------------------------------------------------------
* This is the implementation file for ARKODE's ERK time stepper
* module.
*--------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <nvector/nvector_manyvector.h>
#include <sundials/sundials_adjointcheckpointscheme.h>
#include <sundials/sundials_adjointstepper.h>
#include <sundials/sundials_context.h>
#include <sundials/sundials_math.h>
#include "arkode/arkode.h"
#include "arkode/arkode_butcher.h"
#include "arkode_erkstep_impl.h"
#include "arkode_impl.h"
#include "arkode_interp_impl.h"
#include "sundials/sundials_errors.h"
#include "sundials/sundials_types.h"
#include "sundials_adjointstepper_impl.h"
/*===============================================================
Exported functions
===============================================================*/
void* ERKStepCreate(ARKRhsFn f, sunrealtype t0, N_Vector y0, SUNContext sunctx)
{
ARKodeMem ark_mem;
ARKodeERKStepMem step_mem;
int retval;
/* Check that f is supplied */
if (f == NULL)
{
arkProcessError(NULL, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
MSG_ARK_NULL_F);
return (NULL);
}
/* Check for legal input parameters */
if (y0 == NULL)
{
arkProcessError(NULL, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
MSG_ARK_NULL_Y0);
return (NULL);
}
if (!sunctx)
{
arkProcessError(NULL, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
MSG_ARK_NULL_SUNCTX);
return (NULL);
}
/* Create ark_mem structure and set default values */
ark_mem = arkCreate(sunctx);
if (ark_mem == NULL)
{
arkProcessError(NULL, ARK_MEM_NULL, __LINE__, __func__, __FILE__,
MSG_ARK_NO_MEM);
return (NULL);
}
/* Allocate ARKodeERKStepMem structure, and initialize to zero */
step_mem = NULL;
step_mem = (ARKodeERKStepMem)malloc(sizeof(struct ARKodeERKStepMemRec));
if (step_mem == NULL)
{
arkProcessError(ark_mem, ARK_MEM_FAIL, __LINE__, __func__, __FILE__,
MSG_ARK_ARKMEM_FAIL);
ARKodeFree((void**)&ark_mem);
return (NULL);
}
memset(step_mem, 0, sizeof(struct ARKodeERKStepMemRec));
/* Attach step_mem structure and function pointers to ark_mem */
ark_mem->step_init = erkStep_Init;
ark_mem->step_fullrhs = erkStep_FullRHS;
ark_mem->step = erkStep_TakeStep;
ark_mem->step_printallstats = erkStep_PrintAllStats;
ark_mem->step_writeparameters = erkStep_WriteParameters;
ark_mem->step_setusecompensatedsums = NULL;
ark_mem->step_resize = erkStep_Resize;
ark_mem->step_free = erkStep_Free;
ark_mem->step_printmem = erkStep_PrintMem;
ark_mem->step_setoptions = erkStep_SetOptions;
ark_mem->step_setdefaults = erkStep_SetDefaults;
ark_mem->step_setrelaxfn = erkStep_SetRelaxFn;
ark_mem->step_setorder = erkStep_SetOrder;
ark_mem->step_getnumrhsevals = erkStep_GetNumRhsEvals;
ark_mem->step_getestlocalerrors = erkStep_GetEstLocalErrors;
ark_mem->step_setforcing = erkStep_SetInnerForcing;
ark_mem->step_supports_adaptive = SUNTRUE;
ark_mem->step_supports_relaxation = SUNTRUE;
ark_mem->step_mem = (void*)step_mem;
/* Set default values for optional inputs */
retval = erkStep_SetDefaults((void*)ark_mem);
if (retval != ARK_SUCCESS)
{
arkProcessError(ark_mem, retval, __LINE__, __func__, __FILE__,
"Error setting default solver options");
ARKodeFree((void**)&ark_mem);
return (NULL);
}
/* Allocate the general ERK stepper vectors using y0 as a template */
/* NOTE: F, cvals and Xvecs will be allocated later on
(based on the number of ERK stages) */
/* Copy the input parameters into ARKODE state */
step_mem->f = f;
/* Update the ARKODE workspace requirements -- UPDATE */
ark_mem->liw += 41; /* fcn/data ptr, int, long int, sunindextype, sunbooleantype */
ark_mem->lrw += 10;
/* Initialize all the counters */
step_mem->nfe = 0;
/* Initialize fused op work space */
step_mem->cvals = NULL;
step_mem->Xvecs = NULL;
step_mem->nfusedopvecs = 0;
/* Initialize external polynomial forcing data */
step_mem->forcing = NULL;
step_mem->nforcing = 0;
/* Initialize main ARKODE infrastructure */
retval = arkInit(ark_mem, t0, y0, FIRST_INIT);
if (retval != ARK_SUCCESS)
{
arkProcessError(ark_mem, retval, __LINE__, __func__, __FILE__,
"Unable to initialize main ARKODE infrastructure");
ARKodeFree((void**)&ark_mem);
return (NULL);
}
return ((void*)ark_mem);
}
/*---------------------------------------------------------------
ERKStepReInit:
This routine re-initializes the ERKStep module to solve a new
problem of the same size as was previously solved. This routine
should also be called when the problem dynamics or desired solvers
have changed dramatically, so that the problem integration should
resume as if started from scratch.
Note all internal counters are set to 0 on re-initialization.
---------------------------------------------------------------*/
int ERKStepReInit(void* arkode_mem, ARKRhsFn f, sunrealtype t0, N_Vector y0)
{
ARKodeMem ark_mem;
ARKodeERKStepMem step_mem;
int retval;
/* access ARKodeERKStepMem structure */
retval = erkStep_AccessARKODEStepMem(arkode_mem, __func__, &ark_mem, &step_mem);
if (retval != ARK_SUCCESS) { return (retval); }
/* Check if ark_mem was allocated */
if (ark_mem->MallocDone == SUNFALSE)
{
arkProcessError(ark_mem, ARK_NO_MALLOC, __LINE__, __func__, __FILE__,
MSG_ARK_NO_MALLOC);
return (ARK_NO_MALLOC);
}
/* Check that f is supplied */
if (f == NULL)
{
arkProcessError(ark_mem, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
MSG_ARK_NULL_F);
return (ARK_ILL_INPUT);
}
/* Check for legal input parameters */
if (y0 == NULL)
{
arkProcessError(ark_mem, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
MSG_ARK_NULL_Y0);
return (ARK_ILL_INPUT);
}
/* Copy the input parameters into ARKODE state */
step_mem->f = f;
/* Initialize main ARKODE infrastructure */
retval = arkInit(arkode_mem, t0, y0, FIRST_INIT);
if (retval != ARK_SUCCESS)
{
arkProcessError(ark_mem, retval, __LINE__, __func__, __FILE__,
"Unable to initialize main ARKODE infrastructure");
return (retval);
}
/* Initialize all the counters */
step_mem->nfe = 0;
return (ARK_SUCCESS);
}
/*===============================================================
Interface routines supplied to ARKODE
===============================================================*/
/*---------------------------------------------------------------
erkStep_Resize:
This routine resizes the memory within the ERKStep module.
---------------------------------------------------------------*/
int erkStep_Resize(ARKodeMem ark_mem, N_Vector y0,
SUNDIALS_MAYBE_UNUSED sunrealtype hscale,
SUNDIALS_MAYBE_UNUSED sunrealtype t0, ARKVecResizeFn resize,
void* resize_data)
{
ARKodeERKStepMem step_mem;
sunindextype lrw1, liw1, lrw_diff, liw_diff;
int i, retval;
/* access ARKodeERKStepMem structure */
retval = erkStep_AccessStepMem(ark_mem, __func__, &step_mem);
if (retval != ARK_SUCCESS) { return (retval); }
/* Determine change in vector sizes */
lrw1 = liw1 = 0;
if (y0->ops->nvspace != NULL) { N_VSpace(y0, &lrw1, &liw1); }
lrw_diff = lrw1 - ark_mem->lrw1;
liw_diff = liw1 - ark_mem->liw1;
ark_mem->lrw1 = lrw1;
ark_mem->liw1 = liw1;
/* Resize the RHS vectors */
for (i = 0; i < step_mem->stages; i++)
{
if (!arkResizeVec(ark_mem, resize, resize_data, lrw_diff, liw_diff, y0,
&step_mem->F[i]))
{
arkProcessError(ark_mem, ARK_MEM_FAIL, __LINE__, __func__, __FILE__,
"Unable to resize vector");
return (ARK_MEM_FAIL);
}
}
return (ARK_SUCCESS);
}
/*---------------------------------------------------------------
erkStep_Free frees all ERKStep memory.
---------------------------------------------------------------*/
void erkStep_Free(ARKodeMem ark_mem)
{
int j;
sunindextype Bliw, Blrw;
ARKodeERKStepMem step_mem;
/* nothing to do if ark_mem is already NULL */
if (ark_mem == NULL) { return; }
/* conditional frees on non-NULL ERKStep module */
if (ark_mem->step_mem != NULL)
{
step_mem = (ARKodeERKStepMem)ark_mem->step_mem;
/* free the Butcher table */
if (step_mem->B != NULL)
{
ARKodeButcherTable_Space(step_mem->B, &Bliw, &Blrw);
ARKodeButcherTable_Free(step_mem->B);
step_mem->B = NULL;
ark_mem->liw -= Bliw;
ark_mem->lrw -= Blrw;
}
/* free the RHS vectors */
if (step_mem->F != NULL)
{
for (j = 0; j < step_mem->stages; j++)
{
arkFreeVec(ark_mem, &step_mem->F[j]);
}
free(step_mem->F);
step_mem->F = NULL;
ark_mem->liw -= step_mem->stages;
}
/* free the reusable arrays for fused vector interface */
if (step_mem->cvals != NULL)
{
free(step_mem->cvals);
step_mem->cvals = NULL;
ark_mem->lrw -= step_mem->nfusedopvecs;
}
if (step_mem->Xvecs != NULL)
{
free(step_mem->Xvecs);
step_mem->Xvecs = NULL;
ark_mem->liw -= step_mem->nfusedopvecs;
}
step_mem->nfusedopvecs = 0;
/* free work arrays for MRI forcing */
if (step_mem->stage_times)
{
free(step_mem->stage_times);
step_mem->stage_times = NULL;
ark_mem->lrw -= step_mem->stages;
}
if (step_mem->stage_coefs)
{
free(step_mem->stage_coefs);
step_mem->stage_coefs = NULL;
ark_mem->lrw -= step_mem->stages;
}
/* free the time stepper module itself */
free(ark_mem->step_mem);
ark_mem->step_mem = NULL;
}
}
/*---------------------------------------------------------------
erkStep_PrintMem:
This routine outputs the memory from the ERKStep structure to
a specified file pointer (useful when debugging).
---------------------------------------------------------------*/
void erkStep_PrintMem(ARKodeMem ark_mem, FILE* outfile)
{
ARKodeERKStepMem step_mem;
int retval;
#ifdef SUNDIALS_DEBUG_PRINTVEC
int i;
#endif
/* access ARKodeERKStepMem structure */
retval = erkStep_AccessStepMem(ark_mem, __func__, &step_mem);
if (retval != ARK_SUCCESS) { return; }
/* output integer quantities */
fprintf(outfile, "ERKStep: q = %i\n", step_mem->q);
fprintf(outfile, "ERKStep: p = %i\n", step_mem->p);
fprintf(outfile, "ERKStep: stages = %i\n", step_mem->stages);
/* output long integer quantities */
fprintf(outfile, "ERKStep: nfe = %li\n", step_mem->nfe);
/* output sunrealtype quantities */
fprintf(outfile, "ERKStep: Butcher table:\n");
ARKodeButcherTable_Write(step_mem->B, outfile);
#ifdef SUNDIALS_DEBUG_PRINTVEC
/* output vector quantities */
for (i = 0; i < step_mem->stages; i++)
{
fprintf(outfile, "ERKStep: F[%i]:\n", i);
N_VPrintFile(step_mem->F[i], outfile);
}
#endif
}
/*---------------------------------------------------------------
erkStep_Init:
This routine is called just prior to performing internal time
steps (after all user "set" routines have been called) from
within arkInitialSetup.
With initialization types FIRST_INIT this routine:
- sets/checks the ARK Butcher tables to be used
- allocates any memory that depends on the number of ARK
stages, method order, or solver options
- sets the call_fullrhs flag
With other initialization types, this routine does nothing.
---------------------------------------------------------------*/
int erkStep_Init(ARKodeMem ark_mem, SUNDIALS_MAYBE_UNUSED sunrealtype tout,
int init_type)
{
ARKodeERKStepMem step_mem;
sunbooleantype reset_efun;
int retval, j;
/* access ARKodeERKStepMem structure */
retval = erkStep_AccessStepMem(ark_mem, __func__, &step_mem);
if (retval != ARK_SUCCESS) { return (retval); }
/* immediately return if resize or reset */
if (init_type == RESIZE_INIT || init_type == RESET_INIT)
{
return (ARK_SUCCESS);
}
/* enforce use of arkEwtSmallReal if using a fixed step size,
an internal error weight function, and not performing accumulated
temporal error estimation */
reset_efun = SUNTRUE;
if (!ark_mem->fixedstep) { reset_efun = SUNFALSE; }
if (ark_mem->user_efun) { reset_efun = SUNFALSE; }
if (ark_mem->AccumErrorType != ARK_ACCUMERROR_NONE) { reset_efun = SUNFALSE; }
if (reset_efun)
{
ark_mem->user_efun = SUNFALSE;
ark_mem->efun = arkEwtSetSmallReal;
ark_mem->e_data = ark_mem;
}
/* Create Butcher table (if not already set) */
retval = erkStep_SetButcherTable(ark_mem);
if (retval != ARK_SUCCESS)
{
arkProcessError(ark_mem, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
"Could not create Butcher table");
return (ARK_ILL_INPUT);
}
/* Check that Butcher table are OK */
retval = erkStep_CheckButcherTable(ark_mem);
if (retval != ARK_SUCCESS)
{
arkProcessError(ark_mem, ARK_ILL_INPUT, __LINE__, __func__, __FILE__,
"Error in Butcher table");
return (ARK_ILL_INPUT);
}
/* Retrieve/store method and embedding orders now that table is finalized */
step_mem->q = ark_mem->hadapt_mem->q = step_mem->B->q;
step_mem->p = ark_mem->hadapt_mem->p = step_mem->B->p;
/* Ensure that if adaptivity or error accumulation is enabled, then
method includes embedding coefficients */
if ((!ark_mem->fixedstep || (ark_mem->AccumErrorType != ARK_ACCUMERROR_NONE)) &&
(step_mem->p == 0))
{
arkProcessError(ark_mem, ARK_ILL_INPUT, __LINE__, __func__,
__FILE__, "Temporal error estimation cannot be performed without embedding coefficients");
return (ARK_ILL_INPUT);
}
/* Allocate ARK RHS vector memory, update storage requirements */
/* Allocate F[0] ... F[stages-1] if needed */
if (step_mem->F == NULL)
{
step_mem->F = (N_Vector*)calloc(step_mem->stages, sizeof(N_Vector));
}
for (j = 0; j < step_mem->stages; j++)
{
if (!arkAllocVec(ark_mem, ark_mem->ewt, &(step_mem->F[j])))
{
return (ARK_MEM_FAIL);
}
}
ark_mem->liw += step_mem->stages; /* pointers */
/* Allocate reusable arrays for fused vector interface */
step_mem->nfusedopvecs = 2 * step_mem->stages + 2 + step_mem->nforcing;
if (step_mem->cvals == NULL)
{
step_mem->cvals = (sunrealtype*)calloc(step_mem->nfusedopvecs,
sizeof(sunrealtype));
if (step_mem->cvals == NULL) { return (ARK_MEM_FAIL); }
ark_mem->lrw += step_mem->nfusedopvecs;
}
if (step_mem->Xvecs == NULL)
{
step_mem->Xvecs = (N_Vector*)calloc(step_mem->nfusedopvecs, sizeof(N_Vector));
if (step_mem->Xvecs == NULL) { return (ARK_MEM_FAIL); }
ark_mem->liw += step_mem->nfusedopvecs; /* pointers */
}
/* Allocate workspace for MRI forcing -- need to allocate here as the
number of stages may not bet set before this point and we assume
SetInnerForcing has been called before the first step i.e., methods
start with a fast integration */
if (step_mem->nforcing > 0)
{
if (!(step_mem->stage_times))
{
step_mem->stage_times = (sunrealtype*)calloc(step_mem->stages,
sizeof(sunrealtype));
ark_mem->lrw += step_mem->stages;
}
if (!(step_mem->stage_coefs))
{
step_mem->stage_coefs = (sunrealtype*)calloc(step_mem->stages,
sizeof(sunrealtype));
ark_mem->lrw += step_mem->stages;
}
}
/* Override the interpolant degree (if needed), used in arkInitialSetup */
if (step_mem->q > 1 && ark_mem->interp_degree > (step_mem->q - 1))
{
/* Limit max degree to at most one less than the method global order */
ark_mem->interp_degree = step_mem->q - 1;
}
else if (step_mem->q == 1 && ark_mem->interp_degree > 1)
{
/* Allow for linear interpolant with first order methods to ensure
solution values are returned at the time interval end points */
ark_mem->interp_degree = 1;
}
/* set appropriate TakeStep routine based on problem configuration */
if (ark_mem->do_adjoint) { ark_mem->step = erkStep_TakeStep_Adjoint; }
else { ark_mem->step = erkStep_TakeStep; }
/* Signal to shared arkode module that full RHS evaluations are required */
ark_mem->call_fullrhs = SUNTRUE;
return (ARK_SUCCESS);
}
/*------------------------------------------------------------------------------
erkStep_FullRHS:
This is just a wrapper to call the user-supplied RHS function, f(t,y).
This will be called in one of three 'modes':
ARK_FULLRHS_START -> called in the following circumstances:
(a) at the beginning of a simulation i.e., at
(tn, yn) = (t0, y0) or (tR, yR),
(b) when transitioning between time steps t_{n-1}
\to t_{n} to fill f_{n-1} within the Hermite
interpolation module, or
(c) by ERKStep at the start of the first internal step.
In each case, we may check the fn_is_current flag to
know whether the values stored in F[0] are up-to-date,
allowing us to copy those values instead of recomputing.
If these values are not current, then the RHS should be
stored in F[0] for reuse later, before copying the values
into the output vector.
ARK_FULLRHS_END -> called in the following circumstances:
(a) when temporal root-finding is enabled, this will be
called in-between steps t_{n-1} \to t_{n} to fill f_{n},
(b) when high-order dense output is requested from the
Hermite interpolation module in-between steps t_{n-1}
\to t_{n} to fill f_{n}, or
(c) by ERKStep when starting a time step t_{n} \to t_{n+1}
and when using an FSAL method.
Again, we may check the fn_is_current flag to know whether
ARKODE believes that the values stored in F[0] are
up-to-date, and may just be copied. If the values stored
in F[0] are not current, then the only instance where
recomputation is not needed is (c), since the values in
F[stages - 1] may be copied into F[0]. In all other cases,
the RHS should be recomputed and stored in F[0] for reuse
later, before copying the values into the output vector.
ARK_FULLRHS_OTHER -> called in the following circumstances:
(a) when estimating the initial time step size,
(b) for high-order dense output with the Hermite
interpolation module, or
(c) by an "outer" stepper when ERKStep is used as an
inner solver).
All of these instances will occur in-between ERKStep time
steps, but the (t,y) input does not correspond to an
"official" time step, thus the RHS should always be
evaluated, with the values *not* stored in F[0].
----------------------------------------------------------------------------*/
int erkStep_FullRHS(ARKodeMem ark_mem, sunrealtype t, N_Vector y, N_Vector f,
int mode)
{
int nvec, retval;
ARKodeERKStepMem step_mem;
sunbooleantype recomputeRHS;
sunrealtype* cvals;
N_Vector* Xvecs;
sunrealtype stage_coefs = ONE;
/* access ARKodeERKStepMem structure */
retval = erkStep_AccessStepMem(ark_mem, __func__, &step_mem);
if (retval != ARK_SUCCESS) { return (retval); }
/* local shortcuts for use with fused vector operations */
cvals = step_mem->cvals;
Xvecs = step_mem->Xvecs;
/* perform RHS functions contingent on 'mode' argument */
switch (mode)
{
case ARK_FULLRHS_START:
/* compute the RHS if needed */
if (!(ark_mem->fn_is_current))
{
/* call the user-supplied pre-RHS function (if supplied) */
if (ark_mem->PreRhsFn)
{
retval = ark_mem->PreRhsFn(t, y, ark_mem->user_data);
if (retval != 0) { return (ARK_PRERHSFN_FAIL); }
}
/* call f */
retval = step_mem->f(t, y, step_mem->F[0], ark_mem->user_data);
step_mem->nfe++;
if (retval != 0)
{
arkProcessError(ark_mem, ARK_RHSFUNC_FAIL, __LINE__, __func__, __FILE__,
MSG_ARK_RHSFUNC_FAILED, t);
return (ARK_RHSFUNC_FAIL);
}
}
/* copy RHS into output */
N_VScale(ONE, step_mem->F[0], f);
/* apply external polynomial forcing */
if (step_mem->nforcing > 0)
{
cvals[0] = ONE;
Xvecs[0] = f;
nvec = 1;
erkStep_ApplyForcing(step_mem, &t, &stage_coefs, 1, &nvec);
N_VLinearCombination(nvec, cvals, Xvecs, f);
}
break;
case ARK_FULLRHS_END:
/* determine if RHS function needs to be recomputed */
if (!(ark_mem->fn_is_current))
{
recomputeRHS = !ARKodeButcherTable_IsStifflyAccurate(step_mem->B);
/* First Same As Last methods are not FSAL when relaxation is enabled */
if (ark_mem->relax_enabled) { recomputeRHS = SUNTRUE; }
/* base RHS call on recomputeRHS argument */
if (recomputeRHS)
{
/* call the user-supplied pre-RHS function (if supplied) */
if (ark_mem->PreRhsFn)
{
retval = ark_mem->PreRhsFn(t, y, ark_mem->user_data);
if (retval != 0) { return (ARK_PRERHSFN_FAIL); }
}
/* call f */
retval = step_mem->f(t, y, step_mem->F[0], ark_mem->user_data);
step_mem->nfe++;
if (retval != 0)
{
arkProcessError(ark_mem, ARK_RHSFUNC_FAIL, __LINE__, __func__,
__FILE__, MSG_ARK_RHSFUNC_FAILED, t);
return (ARK_RHSFUNC_FAIL);
}
}
else { N_VScale(ONE, step_mem->F[step_mem->stages - 1], step_mem->F[0]); }
/* copy RHS vector into output */
N_VScale(ONE, step_mem->F[0], f);
/* apply external polynomial forcing */
if (step_mem->nforcing > 0)
{
cvals[0] = ONE;
Xvecs[0] = f;
nvec = 1;
erkStep_ApplyForcing(step_mem, &t, &stage_coefs, 1, &nvec);
N_VLinearCombination(nvec, cvals, Xvecs, f);
}
}
break;
case ARK_FULLRHS_OTHER:
/* call the user-supplied pre-RHS function (if supplied) */
if (ark_mem->PreRhsFn)
{
retval = ark_mem->PreRhsFn(t, y, ark_mem->user_data);
if (retval != 0) { return (ARK_PRERHSFN_FAIL); }
}
/* call f */
retval = step_mem->f(t, y, f, ark_mem->user_data);
step_mem->nfe++;
if (retval != 0)
{
arkProcessError(ark_mem, ARK_RHSFUNC_FAIL, __LINE__, __func__, __FILE__,
MSG_ARK_RHSFUNC_FAILED, t);
return (ARK_RHSFUNC_FAIL);
}
/* apply external polynomial forcing */
if (step_mem->nforcing > 0)
{
cvals[0] = ONE;
Xvecs[0] = f;
nvec = 1;
erkStep_ApplyForcing(step_mem, &t, &stage_coefs, 1, &nvec);
N_VLinearCombination(nvec, cvals, Xvecs, f);
}
break;
default:
/* return with RHS failure if unknown mode is passed */
arkProcessError(ark_mem, ARK_RHSFUNC_FAIL, __LINE__, __func__, __FILE__,
"Unknown full RHS mode");
return (ARK_RHSFUNC_FAIL);
}
return (ARK_SUCCESS);
}
/*---------------------------------------------------------------
erkStep_TakeStep:
This routine serves the primary purpose of the ERKStep module:
it performs a single ERK step (with embedding, if possible).
The output variable dsmPtr should contain estimate of the
weighted local error if an embedding is present; otherwise it
should be 0.
The input/output variable nflagPtr is used to gauge convergence
of any algebraic solvers within the step. As this routine
involves no algebraic solve, it is set to 0 (success).
The return value from this routine is:
0 => step completed successfully
>0 => step encountered recoverable failure;
reduce step and retry (if possible)
<0 => step encountered unrecoverable failure
---------------------------------------------------------------*/
int erkStep_TakeStep(ARKodeMem ark_mem, sunrealtype* dsmPtr, int* nflagPtr)
{
int retval, is, js, nvec, mode;
sunrealtype* cvals;
N_Vector* Xvecs;
ARKodeERKStepMem step_mem;
/* initialize algebraic solver convergence flag to success */
*nflagPtr = ARK_SUCCESS;
/* access ARKodeERKStepMem structure */
retval = erkStep_AccessStepMem(ark_mem, __func__, &step_mem);
if (retval != ARK_SUCCESS) { return (retval); }
/* determine if method has fsal property */
sunbooleantype fsal = ARKodeButcherTable_IsStifflyAccurate(step_mem->B);
/* local shortcuts for fused vector operations */
cvals = step_mem->cvals;
Xvecs = step_mem->Xvecs;
SUNLogInfo(ARK_LOGGER, "begin-stages-list", "stage = 0, tcur = " SUN_FORMAT_G,
ark_mem->tcur);
SUNLogExtraDebugVec(ARK_LOGGER, "stage", ark_mem->yn, "z_0(:) =");
/* Call the full RHS if needed. If this is the first step then we may need to
evaluate or copy the RHS values from an earlier evaluation (e.g., to
compute h0). For subsequent steps treat this RHS evaluation as an
evaluation at the end of the just completed step to potentially reuse
(FSAL methods) RHS evaluations from the end of the last step. */
if (!(ark_mem->fn_is_current))
{
mode = (ark_mem->initsetup) ? ARK_FULLRHS_START : ARK_FULLRHS_END;
retval = ark_mem->step_fullrhs(ark_mem, ark_mem->tn, ark_mem->yn,
ark_mem->fn, mode);
if (retval)
{
SUNLogInfo(ARK_LOGGER, "end-stages-list",
"status = failed rhs eval, retval = %i", retval);
return ARK_RHSFUNC_FAIL;
}
ark_mem->fn_is_current = SUNTRUE;
}
SUNLogExtraDebugVec(ARK_LOGGER, "stage RHS", step_mem->F[0], "F_0(:) =");
if (ark_mem->checkpoint_scheme)
{
sunbooleantype do_save;
SUNErrCode errcode =
SUNAdjointCheckpointScheme_NeedsSaving(ark_mem->checkpoint_scheme,
ark_mem->checkpoint_step_idx, 0,
ark_mem->tcur, &do_save);
if (errcode)
{
arkProcessError(ark_mem, ARK_ADJ_CHECKPOINT_FAIL, __LINE__, __func__,
__FILE__,
"SUNAdjointCheckpointScheme_NeedsSaving returned %d",
errcode);
return ARK_ADJ_CHECKPOINT_FAIL;
}
if (do_save)
{
errcode =
SUNAdjointCheckpointScheme_InsertVector(ark_mem->checkpoint_scheme,
ark_mem->checkpoint_step_idx, 0,
ark_mem->tcur, ark_mem->yn);
if (errcode)
{
arkProcessError(ark_mem, ARK_ADJ_CHECKPOINT_FAIL, __LINE__, __func__,
__FILE__,
"SUNAdjointCheckpointScheme_InsertVector returned %d",
errcode);
return ARK_ADJ_CHECKPOINT_FAIL;
}
}
}
SUNLogInfo(ARK_LOGGER, "end-stages-list", "status = success");
/* Loop over internal stages to the step; since the method is explicit
the first stage RHS is just the full RHS from the start of the step */
for (is = 1; is < step_mem->stages; is++)
{
/* Set current stage time(s) */
ark_mem->tcur = ark_mem->tn + step_mem->B->c[is] * ark_mem->h;
SUNLogInfo(ARK_LOGGER, "begin-stages-list",
"stage = %i, tcur = " SUN_FORMAT_G, is, ark_mem->tcur);
/* Set ycur to current stage solution */
nvec = 0;
for (js = 0; js < is; js++)
{
cvals[nvec] = ark_mem->h * step_mem->B->A[is][js];
Xvecs[nvec] = step_mem->F[js];
nvec += 1;
}
cvals[nvec] = ONE;
Xvecs[nvec] = ark_mem->yn;
nvec += 1;
/* apply external polynomial forcing */
if (step_mem->nforcing > 0)
{
for (js = 0; js < is; js++)
{
step_mem->stage_times[js] = ark_mem->tn + step_mem->B->c[js] * ark_mem->h;
step_mem->stage_coefs[js] = ark_mem->h * step_mem->B->A[is][js];
}
erkStep_ApplyForcing(step_mem, step_mem->stage_times,
step_mem->stage_coefs, is, &nvec);
}
/* call fused vector operation to do the work */
retval = N_VLinearCombination(nvec, cvals, Xvecs, ark_mem->ycur);
if (retval != 0)
{
SUNLogInfo(ARK_LOGGER, "end-stages-list",
"status = failed vector op, retval = %i", retval);
return (ARK_VECTOROP_ERR);
}
/* apply user-supplied stage postprocessing function (if supplied) unless
this is the last stage of a FSAL method, then apply the user-supplied
step postprocessing function (if supplied) */
if (is == step_mem->stages - 1 && fsal && ark_mem->PostProcessStepFn)
{
retval = ark_mem->PostProcessStepFn(ark_mem->tcur, ark_mem->ycur,
ark_mem->user_data);
if (retval != 0)
{
SUNLogInfo(ARK_LOGGER, "end-stages-list",
"status = failed postprocess step, retval = %i", retval);
return (ARK_POSTPROCESS_STEP_FAIL);
}
}
else if (ark_mem->PostProcessStageFn)
{
retval = ark_mem->PostProcessStageFn(ark_mem->tcur, ark_mem->ycur,
ark_mem->user_data);
if (retval != 0)
{
SUNLogInfo(ARK_LOGGER, "end-stages-list",
"status = failed postprocess stage, retval = %i", retval);
return (ARK_POSTPROCESS_STAGE_FAIL);
}
}
/* call the user-supplied pre-RHS function (if supplied) */
if (ark_mem->PreRhsFn)
{
retval = ark_mem->PreRhsFn(ark_mem->tcur, ark_mem->ycur,
ark_mem->user_data);
if (retval != 0)
{
SUNLogInfo(ARK_LOGGER, "end-stages-list",
"status = failed preprocess RHS, retval = %i", retval);
return (ARK_PRERHSFN_FAIL);
}
}
/* compute updated RHS */
retval = step_mem->f(ark_mem->tcur, ark_mem->ycur, step_mem->F[is],
ark_mem->user_data);
step_mem->nfe++;
SUNLogExtraDebugVec(ARK_LOGGER, "stage RHS", step_mem->F[is],
"F_%i(:) =", is);
SUNLogInfoIf(retval != 0, ARK_LOGGER, "end-stages-list",
"status = failed rhs eval, retval = %i", retval);
if (retval < 0) { return (ARK_RHSFUNC_FAIL); }
if (retval > 0) { return (ARK_UNREC_RHSFUNC_ERR); }
/* checkpoint stage for adjoint (if necessary) */
if (ark_mem->checkpoint_scheme)
{
sunbooleantype do_save;
SUNErrCode errcode =
SUNAdjointCheckpointScheme_NeedsSaving(ark_mem->checkpoint_scheme,
ark_mem->checkpoint_step_idx, is,
ark_mem->tcur, &do_save);
if (errcode)
{
arkProcessError(ark_mem, ARK_ADJ_CHECKPOINT_FAIL, __LINE__, __func__,
__FILE__,
"SUNAdjointCheckpointScheme_NeedsSaving returned %d",
errcode);
return ARK_ADJ_CHECKPOINT_FAIL;
}
if (do_save)
{
errcode =
SUNAdjointCheckpointScheme_InsertVector(ark_mem->checkpoint_scheme,
ark_mem->checkpoint_step_idx,
is, ark_mem->tcur,
ark_mem->ycur);
if (errcode)
{
arkProcessError(ark_mem, ARK_ADJ_CHECKPOINT_FAIL, __LINE__, __func__,
__FILE__,
"SUNAdjointCheckpointScheme_InsertVector returned %d",
errcode);
return ARK_ADJ_CHECKPOINT_FAIL;
}
}
}
SUNLogInfo(ARK_LOGGER, "end-stages-list", "status = success");
} /* loop over stages */
SUNLogInfo(ARK_LOGGER, "begin-compute-solution", "");
/* compute time-evolved solution (in ark_ycur), error estimate (in dsm) */
ark_mem->tcur = ark_mem->tn + ark_mem->h;
retval = erkStep_ComputeSolutions(ark_mem, dsmPtr);
if (retval < 0)
{
SUNLogInfo(ARK_LOGGER, "end-compute-solution",
"status = failed compute solution, retval = %i", retval);
return (retval);
}
SUNLogExtraDebugVec(ARK_LOGGER, "updated solution", ark_mem->ycur, "ycur(:) =");
SUNLogInfo(ARK_LOGGER, "end-compute-solution", "status = success");
if (ark_mem->checkpoint_scheme)
{
sunbooleantype do_save;
SUNErrCode errcode =
SUNAdjointCheckpointScheme_NeedsSaving(ark_mem->checkpoint_scheme,
ark_mem->checkpoint_step_idx,
step_mem->B->stages,
ark_mem->tn + ark_mem->h, &do_save);
if (errcode)