Skip to content

Commit 2c19234

Browse files
Merge pull request #3200 from BenChung/explicit-observed-func
Add docstring and oop array construction method for build_explicit_observed_function
2 parents 24a7c2e + 14996d7 commit 2c19234

File tree

1 file changed

+45
-7
lines changed

1 file changed

+45
-7
lines changed

src/systems/diffeqs/odesystem.jl

+45-7
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,48 @@ end
409409
ODESystem(eq::Equation, args...; kwargs...) = ODESystem([eq], args...; kwargs...)
410410

411411
"""
412-
$(SIGNATURES)
413-
414-
Build the observed function assuming the observed equations are all explicit,
415-
i.e. there are no cycles.
412+
build_explicit_observed_function(sys, ts; kwargs...) -> Function(s)
413+
414+
Generates a function that computes the observed value(s) `ts` in the system `sys`, while making the assumption that there are no cycles in the equations.
415+
416+
## Arguments
417+
- `sys`: The system for which to generate the function
418+
- `ts`: The symbolic observed values whose value should be computed
419+
420+
## Keywords
421+
- `return_inplace = false`: If true and the observed value is a vector, then return both the in place and out of place methods.
422+
- `expression = false`: Generates a Julia `Expr`` computing the observed value if `expression` is true
423+
- `eval_expression = false`: If true and `expression = false`, evaluates the returned function in the module `eval_module`
424+
- `output_type = Array` the type of the array generated by a out-of-place vector-valued function
425+
- `param_only = false` if true, only allow the generated function to access system parameters
426+
- `inputs = nothing` additinoal symbolic variables that should be provided to the generated function
427+
- `checkbounds = true` checks bounds if true when destructuring parameters
428+
- `op = Operator` sets the recursion terminator for the walk done by `vars` to identify the variables that appear in `ts`. See the documentation for `vars` for more detail.
429+
- `throw = true` if true, throw an error when generating a function for `ts` that reference variables that do not exist.
430+
- `mkarray`; only used if the output is an array (that is, `!isscalar(ts)` and `ts` is not a tuple, in which case the result will always be a tuple). Called as `mkarray(ts, output_type)` where `ts` are the expressions to put in
431+
the array and `output_type` is the argument of the same name passed to build_explicit_observed_function.
432+
433+
## Returns
434+
435+
The return value will be either:
436+
* a single function `f_oop` if the input is a scalar or if the input is a Vector but `return_inplace` is false
437+
* the out of place and in-place functions `(f_ip, f_oop)` if `return_inplace` is true and the input is a `Vector`
438+
439+
The function(s) `f_oop` (and potentially `f_ip`) will be:
440+
* `RuntimeGeneratedFunction`s by default,
441+
* A Julia `Expr` if `expression` is true,
442+
* A directly evaluated Julia function in the module `eval_module` if `eval_expression` is true and `expression` is false.
443+
444+
The signatures will be of the form `g(...)` with arguments:
445+
446+
- `output` for in-place functions
447+
- `unknowns` if `param_only` is `false`
448+
- `inputs` if `inputs` is an array of symbolic inputs that should be available in `ts`
449+
- `p...` unconditionally; note that in the case of `MTKParameters` more than one parameters argument may be present, so it must be splatted
450+
- `t` if the system is time-dependent; for example `NonlinearSystem` will not have `t`
451+
452+
For example, a function `g(op, unknowns, p..., inputs, t)` will be the in-place function generated if `return_inplace` is true, `ts` is a vector,
453+
an array of inputs `inputs` is given, and `param_only` is false for a time-dependent system.
416454
"""
417455
function build_explicit_observed_function(sys, ts;
418456
inputs = nothing,
@@ -421,12 +459,12 @@ function build_explicit_observed_function(sys, ts;
421459
eval_module = @__MODULE__,
422460
output_type = Array,
423461
checkbounds = true,
424-
drop_expr = drop_expr,
425462
ps = parameters(sys),
426463
return_inplace = false,
427464
param_only = false,
428465
op = Operator,
429-
throw = true)
466+
throw = true,
467+
mkarray = MakeArray)
430468
is_tuple = ts isa Tuple
431469
if is_tuple
432470
ts = collect(ts)
@@ -582,7 +620,7 @@ function build_explicit_observed_function(sys, ts;
582620
elseif is_tuple
583621
MakeTuple(Tuple(ts))
584622
else
585-
MakeArray(ts, output_type)
623+
mkarray(ts, output_type)
586624
end
587625
oop_fn = Func(args, [],
588626
pre(Let(obsexprs,

0 commit comments

Comments
 (0)