@@ -455,14 +455,30 @@ $(SIGNATURES)
455
455
Add accumulation variables for `vars`.
456
456
"""
457
457
function add_accumulations (sys:: ODESystem , vars = states (sys))
458
+ avars = [rename (v, Symbol (:accumulation_ , getname (v))) for v in vars]
459
+ add_accumulations (sys, avars .=> vars)
460
+ end
461
+
462
+ """
463
+ $(SIGNATURES)
464
+
465
+ Add accumulation variables for `vars`. `vars` is a vector of pairs in the form
466
+ of
467
+
468
+ ```julia
469
+ [cumulative_var1 => x + y, cumulative_var2 => x^2]
470
+ ```
471
+ Then, cumulative variables `cumulative_var1` and `cumulative_var2` that computes
472
+ the comulative `x + y` and `x^2` would be added to `sys`.
473
+ """
474
+ function add_accumulations (sys:: ODESystem , vars:: Vector{<:Pair} )
458
475
eqs = get_eqs (sys)
459
- accs = filter (x -> startswith ( string (x), " accumulation_ " ), states (sys) )
460
- if ! isempty (accs )
461
- error (" $accs variable names start with \" accumulation_ \" " )
476
+ avars = map (first, vars )
477
+ if (ints = intersect (avars, states (sys)); ! isempty (ints) )
478
+ error (" $ints already exist in the system! " )
462
479
end
463
- avars = [rename (v, Symbol (:accumulation_ , getname (v))) for v in vars]
464
480
D = Differential (get_iv (sys))
465
- @set! sys. eqs = [eqs; Equation[D (a) ~ v for (a, v) in zip (avars, vars)]]
481
+ @set! sys. eqs = [eqs; Equation[D (a) ~ v[ 2 ] for (a, v) in zip (avars, vars)]]
466
482
@set! sys. states = [get_states (sys); avars]
467
483
@set! sys. defaults = merge (get_defaults (sys), Dict (a => 0.0 for a in avars))
468
484
end
0 commit comments