Management of Equations¶
When you read solution files with an object of class Reader, then you get a Base containing the variables stored in the files. You may want to compute some other variables depending on these latter variables.
You can set equations in Antares. From the user point of view, an equation is a statement with the equals sign “=” that connects a variable name on the left-hand side and an expression on the right-hand side. If you go deeper in the library implementation, then the equation is strictly speaking a function. The latter concept is much more powerful than the first one in terms of complexity you can address.
But, in this tutorial, only the user-defined equation are introduced. You can also refer to http://cerfacs.fr/antares/src/tutorial/equations.html
import numpy as np
import antares
nnx, nny = (3, 2)
x_lst = np.linspace(0., 2., nnx)
y_lst = np.linspace(0., 1., nny)
xco, yco = np.meshgrid(x_lst, y_lst, indexing='ij')
base = antares.Base()
zone1 = base['0'] = antares.Zone()
zone1.shared['x'] = xco
zone1.shared['y'] = yco
instant = zone1['0'] = antares.Instant()
instant[('rho', 'node')] = np.ones(xco.shape)*1.2
instant['E'] = np.ones(xco.shape)*135000
base.show()
This is one way to create variables with antares. You can do it yourself with numpy arrays.
Before going further on, we have to introduce the notion of antares computer. A computer is used to compute variables thanks to python user-defined functions, and more specifically thanks to user-defined equations for this tutorial.
By default, each Instant owns a computer. The computer contains the equations and it will use the appropriate equation when asked.
So, we want to define a statement like
<variable name> = <mathematical expression>
The mathematical expression is composed of operators and variable names. The variable names must be contained in Instant objects or defined with an equation. The equations may use the following operators, symbols and predefined mathematical functions:
’**‘,’/‘,’*‘,’+‘,’-‘,’,‘,’>‘,’<‘, ’abs(’, ‘angle(’, ‘arccos(’, ‘arcsin(’, ‘unwrap(’, ‘arctan2(’, ‘arctan(’, ‘conj(’, ‘cosh(’, ‘cos(’, ‘exp(’, ‘imag(’, ‘log(’, ‘log10(’, ‘max(’, ‘min(’, ‘mean(’, ‘maximum(’, ‘minimum(’, ‘ones(’, ‘real(’, ‘sign(’, ‘sinh(’, ‘sin(’, ‘sqrt(’, ‘tanh(’, ‘tan(’, ‘zeros(’, ‘(’, ‘)’
If you want to use the equation on the whole base, which is very often the case, then set the equation with the method set_formula(). Then, you can compute the variable you just have defined with the method compute() of the Base class.
base.set_formula('rho_u = rho * 320')
base.compute('rho_u')
print(base[0][0])
print(base[0][0]['rho_u'])
When an equation is computed, the defined variable name appears in the list of variables of the Instant objects.
You can also directly use the method compute() of the Base class.
base.compute('u = rho_u / rho')
print(base[0][0])
print(base[0][0]['u'])
Variables in Instant are always taken as is. Be careful if you change the equations.
print('rho_u = ', base[0][0]['rho_u'])
print('Change formula')
base.set_formula('rho_u = rho * 310')
print(base[0][0]['rho_u'])
print('Compute formula')
base.compute('rho_u = rho * 310')
print(base[0][0]['rho_u'])
By default, intermediary variables are stored in the computer. They are not put in the Instant. Then, if you asked to compute the variable again, it will give you back the stored value.
You may want to reset the value of the variable.
base.set_formula('psta = rho*(E-Ec)*(gamma-1.)')
base.set_formula('Ec = u**2 / 3.')
gamma = 1.4
base.set_formula('gamma = %f'%gamma)
base.compute('psta')
print(base[0][0])
print(base[0][0]['psta'])
Oh I made a mistake in the kinetic energy equations.
base.set_formula('Ec = u**2 / 2.')
base.compute('psta')
print(base[0][0]['psta'])
The kinetic energy Ec is stored in the computer object and is reused (by default).
base.compute('psta', reset=False)
print(base[0][0]['psta'])
base.compute('psta', reset=True)
print(base[0][0]['psta'])
You could also not have stored the value of the variable at the first computation.
base.compute('psta', store=False)
All the previous methods are also available for zone
and instant
objects.
If you want to use the computing system in an Antares python script, you
may choose the modeling of the computer.A modeling is basically a
set of equations. The equations may be given a priori in python files as
with the modeling internal or given in a script with the function
:meth:antares.Base.set_formula
.internal is a very basic
modeling that is provided by Antares.
If you do not want to use any available modeling, then you can set your own temporary formula.
Modeling ########
base.set_computer_model('internal')
A modeling is basically a set of equations.
The equations may be given a priori in python files as with the modeling internal or given in a script with the function antares.Base.set_formula().
internal is a very basic modeling that is provided by Antares.
Let’s see what it contains
http://cerfacs.fr/antares/src/eqmanager/internal.html
base[0][0]['z'] = base[0][0]['x']
print(base[0][0])
base.compute('theta')
print(base[0][0])
Note that the variable theta is now in the Instant
Note the position of theta in the variables
theta computed from shared variables goes into the shared Instant
If you do not want to use any available modeling, then you can set your own temporary formula.
(you cannot save them yet)
By default, each Instant owns a computer.
A computer is an object used to compute variables thanks to python user-defined functions.
If you want to use the computing system in an Antares python script, you may choose the modeling of the computer.
You can give your own equations before installing Antares.
see the default file antares/eqmanager/internal/equations.py
create a directory with the name of your modeling: mkdir antares/eqmanager/mymodeling
put a file equations.py in this directory: see the structure from the modeling internal