|
1 | | -# The AbstractSystem Interface |
2 | | - |
3 | | -## Overview |
4 | | - |
5 | | -The `AbstractSystem` interface is the core of the system level of ModelingToolkit.jl. |
6 | | -It establishes a common set of functionality that is used between systems |
7 | | -from ODEs and chemical reactions, allowing users to have a common framework for |
8 | | -model manipulation and compilation. |
9 | | - |
10 | | -## Composition and Accessor Functions |
11 | | - |
12 | | -Each `AbstractSystem` has lists of variables in context, such as distinguishing |
13 | | -parameters vs states. In addition, an `AbstractSystem` also can hold other |
14 | | -`AbstractSystem` types. Direct accessing of the values, such as `sys.states`, |
15 | | -gives the immediate list, while the accessor functions `states(sys)` gives the |
16 | | -total set, which includes that of all systems held inside. |
17 | | - |
18 | | -The values which are common to all `AbstractSystem`s are: |
19 | | - |
20 | | -- `equations(sys)`: All equations that define the system and its subsystems. |
21 | | -- `states(sys)`: All the states in the system and its subsystems. |
22 | | -- `parameters(sys)`: All parameters of the system and its subsystems. |
23 | | -- `nameof(sys)`: The name of the current-level system. |
24 | | -- `get_eqs(sys)`: Equations that define the current-level system. |
25 | | -- `get_states(sys)`: States that are in the current-level system. |
26 | | -- `get_ps(sys)`: Parameters that are in the current-level system. |
27 | | -- `get_systems(sys)`: Subsystems of the current-level system. |
28 | | - |
29 | | -Optionally, a system could have: |
30 | | - |
31 | | -- `observed(sys)`: All observed equations of the system and its subsystems. |
32 | | -- `get_observed(sys)`: Observed equations of the current-level system. |
33 | | -- `get_default_u0(sys)`: A `Dict` that maps states into their default initial |
34 | | - condition. |
35 | | -- `get_default_p(sys)`: A `Dict` that maps parameters into their default value. |
36 | | -- `independent_variable(sys)`: The independent variable of a system. |
37 | | -- `get_noiseeqs(sys)`: Noise equations of the current-level system. |
38 | | - |
39 | | -Note that there's `get_iv(sys)`, but it is not advised to use, since it errors |
40 | | -when the system has no field `iv`. `independent_variable(sys)` returns `nothing` |
41 | | -for `NonlinearSystem`s. |
42 | | - |
43 | | -A system could also have caches: |
44 | | - |
45 | | -- `get_jac(sys)`: The Jacobian of a system. |
46 | | -- `get_tgrad(sys)`: The gradient with respect to time of a system. |
47 | | - |
48 | | -## Transformations |
49 | | - |
50 | | -Transformations are functions which send a valid `AbstractSystem` definition to |
51 | | -another `AbstractSystem`. These are passes, like optimizations (e.g., Block-Lower |
52 | | -Triangle transformations), or changes to the representation, which allow for |
53 | | -alternative numerical methods to be utilized on the model (e.g., DAE index reduction). |
54 | | - |
55 | | -## Function Calculation and Generation |
56 | | - |
57 | | -The calculation and generation functions allow for calculating additional |
58 | | -quantities to enhance the numerical methods applied to the resulting system. |
59 | | -The calculations, like `calculate_jacobian`, generate ModelingToolkit IR for |
60 | | -the Jacobian of the system, while the generations, like `generate_jacobian`, |
61 | | -generate compiled output for the numerical solvers by applying `build_function` |
62 | | -to the generated code. Additionally, many systems have function-type outputs, |
63 | | -which cobble together the generation functionality for a system, for example, |
64 | | -`ODEFunction` can be used to generate a DifferentialEquations-based `ODEFunction` |
65 | | -with compiled version of the ODE itself, the Jacobian, the mass matrix, etc. |
66 | | - |
67 | | -Below are the possible calculation and generation functions: |
68 | | - |
69 | | -```@docs |
70 | | -calculate_tgrad |
71 | | -calculate_gradient |
72 | | -calculate_jacobian |
73 | | -calculate_factorized_W |
74 | | -calculate_hessian |
75 | | -generate_tgrad |
76 | | -generate_gradient |
77 | | -generate_jacobian |
78 | | -generate_factorized_W |
79 | | -generate_hessian |
80 | | -``` |
81 | | - |
82 | | -Additionally, `jacobian_sparsity(sys)` and `hessian_sparsity(sys)` |
83 | | -exist on the appropriate systems for fast generation of the sparsity |
84 | | -patterns via an abstract interpretation without requiring differentiation. |
85 | | - |
86 | | -## Problem Constructors |
87 | | - |
88 | | -At the end, the system types have `DEProblem` constructors, like `ODEProblem`, |
89 | | -which allow for directly generating the problem types required for numerical |
90 | | -methods. The first argument is always the `AbstractSystem`, and the proceeding |
91 | | -arguments match the argument order of their original constructors. Whenever an |
92 | | -array would normally be provided, such as `u0` the initial condition of an |
93 | | -`ODEProblem`, it is instead replaced with a variable map, i.e., an array of |
94 | | -pairs `var=>value`, which allows the user to designate the values without having |
95 | | -to know the order that ModelingToolkit is internally using. |
| 1 | +# The AbstractSystem Interface |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The `AbstractSystem` interface is the core of the system level of ModelingToolkit.jl. |
| 6 | +It establishes a common set of functionality that is used between systems |
| 7 | +from ODEs and chemical reactions, allowing users to have a common framework for |
| 8 | +model manipulation and compilation. |
| 9 | + |
| 10 | +## Constructors and Naming |
| 11 | + |
| 12 | +The `AbstractSystem` interface has a consistent method for constructing systems. |
| 13 | +Generally it follows the order of: |
| 14 | + |
| 15 | +1. Equations |
| 16 | +2. Independent Variables |
| 17 | +3. Dependent Variables (or States) |
| 18 | +4. Parameters |
| 19 | + |
| 20 | +All other pieces are handled via keyword arguments. `AbstractSystem`s share the |
| 21 | +same keyword arguments, which are: |
| 22 | + |
| 23 | +- `system`: This is used for specifying subsystems for hierarchical modeling with |
| 24 | + reusable components. For more information, see the [components page](@ref components) |
| 25 | +- Defaults: Keyword arguments like `default_u0` are used for specifying default |
| 26 | + values which are used. If a value is not given at the `SciMLProblem` construction |
| 27 | + time, its numerical value will be the default. |
| 28 | + |
| 29 | +## Composition and Accessor Functions |
| 30 | + |
| 31 | +Each `AbstractSystem` has lists of variables in context, such as distinguishing |
| 32 | +parameters vs states. In addition, an `AbstractSystem` also can hold other |
| 33 | +`AbstractSystem` types. Direct accessing of the values, such as `sys.states`, |
| 34 | +gives the immediate list, while the accessor functions `states(sys)` gives the |
| 35 | +total set, which includes that of all systems held inside. |
| 36 | + |
| 37 | +The values which are common to all `AbstractSystem`s are: |
| 38 | + |
| 39 | +- `equations(sys)`: All equations that define the system and its subsystems. |
| 40 | +- `states(sys)`: All the states in the system and its subsystems. |
| 41 | +- `parameters(sys)`: All parameters of the system and its subsystems. |
| 42 | +- `nameof(sys)`: The name of the current-level system. |
| 43 | +- `get_eqs(sys)`: Equations that define the current-level system. |
| 44 | +- `get_states(sys)`: States that are in the current-level system. |
| 45 | +- `get_ps(sys)`: Parameters that are in the current-level system. |
| 46 | +- `get_systems(sys)`: Subsystems of the current-level system. |
| 47 | + |
| 48 | +Optionally, a system could have: |
| 49 | + |
| 50 | +- `observed(sys)`: All observed equations of the system and its subsystems. |
| 51 | +- `get_observed(sys)`: Observed equations of the current-level system. |
| 52 | +- `get_default_u0(sys)`: A `Dict` that maps states into their default initial |
| 53 | + condition. |
| 54 | +- `get_default_p(sys)`: A `Dict` that maps parameters into their default value. |
| 55 | +- `independent_variable(sys)`: The independent variable of a system. |
| 56 | +- `get_noiseeqs(sys)`: Noise equations of the current-level system. |
| 57 | + |
| 58 | +Note that there's `get_iv(sys)`, but it is not advised to use, since it errors |
| 59 | +when the system has no field `iv`. `independent_variable(sys)` returns `nothing` |
| 60 | +for `NonlinearSystem`s. |
| 61 | + |
| 62 | +A system could also have caches: |
| 63 | + |
| 64 | +- `get_jac(sys)`: The Jacobian of a system. |
| 65 | +- `get_tgrad(sys)`: The gradient with respect to time of a system. |
| 66 | + |
| 67 | +## Transformations |
| 68 | + |
| 69 | +Transformations are functions which send a valid `AbstractSystem` definition to |
| 70 | +another `AbstractSystem`. These are passes, like optimizations (e.g., Block-Lower |
| 71 | +Triangle transformations), or changes to the representation, which allow for |
| 72 | +alternative numerical methods to be utilized on the model (e.g., DAE index reduction). |
| 73 | + |
| 74 | +## Function Calculation and Generation |
| 75 | + |
| 76 | +The calculation and generation functions allow for calculating additional |
| 77 | +quantities to enhance the numerical methods applied to the resulting system. |
| 78 | +The calculations, like `calculate_jacobian`, generate ModelingToolkit IR for |
| 79 | +the Jacobian of the system, while the generations, like `generate_jacobian`, |
| 80 | +generate compiled output for the numerical solvers by applying `build_function` |
| 81 | +to the generated code. Additionally, many systems have function-type outputs, |
| 82 | +which cobble together the generation functionality for a system, for example, |
| 83 | +`ODEFunction` can be used to generate a DifferentialEquations-based `ODEFunction` |
| 84 | +with compiled version of the ODE itself, the Jacobian, the mass matrix, etc. |
| 85 | + |
| 86 | +Below are the possible calculation and generation functions: |
| 87 | + |
| 88 | +```@docs |
| 89 | +calculate_tgrad |
| 90 | +calculate_gradient |
| 91 | +calculate_jacobian |
| 92 | +calculate_factorized_W |
| 93 | +calculate_hessian |
| 94 | +generate_tgrad |
| 95 | +generate_gradient |
| 96 | +generate_jacobian |
| 97 | +generate_factorized_W |
| 98 | +generate_hessian |
| 99 | +``` |
| 100 | + |
| 101 | +Additionally, `jacobian_sparsity(sys)` and `hessian_sparsity(sys)` |
| 102 | +exist on the appropriate systems for fast generation of the sparsity |
| 103 | +patterns via an abstract interpretation without requiring differentiation. |
| 104 | + |
| 105 | +## Problem Constructors |
| 106 | + |
| 107 | +At the end, the system types have `DEProblem` constructors, like `ODEProblem`, |
| 108 | +which allow for directly generating the problem types required for numerical |
| 109 | +methods. The first argument is always the `AbstractSystem`, and the proceeding |
| 110 | +arguments match the argument order of their original constructors. Whenever an |
| 111 | +array would normally be provided, such as `u0` the initial condition of an |
| 112 | +`ODEProblem`, it is instead replaced with a variable map, i.e., an array of |
| 113 | +pairs `var=>value`, which allows the user to designate the values without having |
| 114 | +to know the order that ModelingToolkit is internally using. |
0 commit comments