A “Hello, World!” program is a simple computer program. It is the common starting point for learning a new langauge or environment. The program’s purpose is typically to output the text “Hello, World!” to the console. The program serves to introduce syntax and basic structures that the learner will need going forward.
#include <iostream>int main(){// This is a commentstd::cout <<"Hello, World!"<<std::endl;return0;}
# This is a commentprintln("Hello, World!")
program hello! This is a commentprint*, 'Hello, World!'end program hello
However, our goal is to build models and run simulations. Therefore, our “Hello, World!” program serves that purpose. The program will solve the following differential equation.
\[
\dot{x} = -ax
\]
1.1 Loading Packages
First, load all modules needed for the example for ModelingToolkit.jl.
Collection of reusable component models commonly used in modeling various systems
3
State-of-the-art collection of differential equations solvers
4
Creating visualizations from simulation outputs
1.2 Defining a Model
Next, the independent and dependent variables are defined.
Here, t is the independent variable representing time and x is a variable which depends on time, t.
@variables t x(t)
\[ \begin{equation}
\left[
\begin{array}{c}
t \\
x\left( t \right) \\
\end{array}
\right]
\end{equation}
\]
An operator must also be defined which performs differentiation with respect to time, t.
D =Differential(t)
(::Differential) (generic function with 3 methods)
Model parameters are defined in a similar fashion. These are inputs not outputs.
@parameters a
\[ \begin{equation}
\left[
\begin{array}{c}
a \\
\end{array}
\right]
\end{equation}
\]
Create a system of ordinary differential equations, of type ODESystem, which consists of a single equation.
@named fol =ODESystem([D(x) ~-a * x])
\[ \begin{align}
\frac{\mathrm{d} x\left( t \right)}{\mathrm{d}t} =& - a x\left( t \right)
\end{align}
\]
1.3 Setting up a Simulation
Now that a system or model exists, we must describe a simulation that we want to perform on this model. We call this a problem, and assign it to type ODEProblem.
There are several inputs we can use to create the problem we wish to study - to start, only consider the ordered arguments shown below:
model which has type ODESystem
initial conditions which is represented by a Pair1
Finally, we want to create a graph using the data from our solution. This is very easy since there exists a plotting recipe specifically for ODESolution objects (which you can see from above is exactly the type produces by the solve function).
plot(sol)
That was easy! There is only one output plotted because there was only one dependent variable in our model. When working with larger models, specify which variables should be plotted by providing a list to the idxs keyword argument (along with any other attributes you want to modify).
We just loaded packages, defined a model, ran a simulation and visualized results. Although this example model is simple, many workflows will require these same steps.
In Julia, a Pair is a collection of two elements. A Pair has fields first and second for accessing each element. To construct a Pair, the sytax is [first => second] (notice the surrounding square brackets).↩︎
In Julia, a Tuple is a collection of any number of elements. Accessing elements of a Tuple is only possible using their index as a Tuple does not have field names. To construct a Tuple, the syntax is (1, 'b', pi)(notice the surrounding parentheses).↩︎