def state_update_equation(self, state_update_equation):
if state_update_equation is None:
if self.dim_state > 0:
state_update_equation = self.state
else:
state_update_equation = self.input
assert state_update_equation.atoms(sp.Symbol) <= set(
self.constants_values.keys()) | set([dynamicsymbols._t])
self._state_update_equation = state_update_equation
if self.dim_state:
assert find_dynamicsymbols(state_update_equation) <= \
set(self.state)
self.state_update_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.state),
self._state_update_equation.subs(self.constants_values),
**self.code_generator_args
)
else:
assert find_dynamicsymbols(state_update_equation) <= \
set(self.input)
self.state_update_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.input),
self._state_update_equation.subs(self.constants_values),
**self.code_generator_args
)
python类flatten()的实例源码
def event_variable_equation(self, event_variable_equation):
assert event_variable_equation.atoms(sp.Symbol) <= set(
self.constants_values.keys()) | set([dynamicsymbols._t])
self._event_variable_equation = event_variable_equation
if self.dim_state:
assert find_dynamicsymbols(event_variable_equation) <= \
set(self.state)
self.event_variable_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.state),
self._event_variable_equation.subs(self.constants_values),
**self.code_generator_args
)
else:
assert find_dynamicsymbols(event_variable_equation) <= \
set(self.input)
self.event_variable_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.input),
self._event_variable_equation.subs(self.constants_values),
**self.code_generator_args
)
def test_solve_biquadratic():
x0, y0, x1, y1, r = symbols('x0 y0 x1 y1 r')
f_1 = (x - 1)**2 + (y - 1)**2 - r**2
f_2 = (x - 2)**2 + (y - 2)**2 - r**2
assert solve_poly_system([f_1, f_2], x, y) == \
[(S(3)/2 - sqrt(-1 + 2*r**2)/2, S(3)/2 + sqrt(-1 + 2*r**2)/2),
(S(3)/2 + sqrt(-1 + 2*r**2)/2, S(3)/2 - sqrt(-1 + 2*r**2)/2)]
f_1 = (x - 1)**2 + (y - 2)**2 - r**2
f_2 = (x - 1)**2 + (y - 1)**2 - r**2
assert solve_poly_system([f_1, f_2], x, y) == \
[(1 - sqrt(((2*r - 1)*(2*r + 1)))/2, S(3)/2),
(1 + sqrt(((2*r - 1)*(2*r + 1)))/2, S(3)/2)]
query = lambda expr: expr.is_Pow and expr.exp is S.Half
f_1 = (x - 1 )**2 + (y - 2)**2 - r**2
f_2 = (x - x1)**2 + (y - 1)**2 - r**2
result = solve_poly_system([f_1, f_2], x, y)
assert len(result) == 2 and all(len(r) == 2 for r in result)
assert all(r.count(query) == 1 for r in flatten(result))
f_1 = (x - x0)**2 + (y - y0)**2 - r**2
f_2 = (x - x1)**2 + (y - y1)**2 - r**2
result = solve_poly_system([f_1, f_2], x, y)
assert len(result) == 2 and all(len(r) == 2 for r in result)
assert all(len(r.find(query)) == 1 for r in flatten(result))
def system_from_matrix_DE(mat_DE, mat_var, mat_input=None, constants={}):
"""
Construct a symbolic DynamicalSystem using matrices. See
riccati_system example.
Parameters
----------
mat_DE : sympy Matrix
The matrix derivative expression (right hand side)
mat_var : sympy Matrix
The matrix state
mat_input : list-like of input expressions, optional
A list-like of input expressions in the matrix differential equation
constants : dict, optional
Dictionary of constants substitutions.
Returns
-------
sys : DynamicalSystem
A DynamicalSystem which can be used to numerically solve the matrix
differential equation.
"""
vec_var = list(set(sp.flatten(mat_var.tolist())))
vec_DE = sp.Matrix.zeros(len(vec_var), 1)
iterator = np.nditer(mat_DE, flags=['multi_index', 'refs_ok'])
for it in iterator:
i, j = iterator.multi_index
idx = vec_var.index(mat_var[i, j])
vec_DE[idx] = mat_DE[i, j]
sys = DynamicalSystem(vec_DE, sp.Matrix(vec_var), mat_input,
constants_values=constants)
return sys
def update_state_equation_function(self):
if not self.dim_state or self.state_equation == empty_array():
return
self.state_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.state) +
sp.flatten(self.input),
self.state_equation.subs(self.constants_values),
**self.code_generator_args
)
def update_state_jacobian_function(self):
if not self.dim_state or self.state_equation == empty_array():
return
self.state_jacobian_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.state) +
sp.flatten(self.input),
self.state_jacobian_equation.subs(self.constants_values),
**self.code_generator_args
)
def update_output_equation_function(self):
if not self.dim_output or self.output_equation == empty_array():
return
if self.dim_state:
self.output_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.state),
self.output_equation.subs(self.constants_values),
**self.code_generator_args
)
else:
self.output_equation_function = self.code_generator(
[dynamicsymbols._t] + sp.flatten(self.input),
self.output_equation.subs(self.constants_values),
**self.code_generator_args
)
def test_solve_biquadratic():
x0, y0, x1, y1, r = symbols('x0 y0 x1 y1 r')
f_1 = (x - 1)**2 + (y - 1)**2 - r**2
f_2 = (x - 2)**2 + (y - 2)**2 - r**2
assert solve_poly_system([f_1, f_2], x, y) == \
[(S(3)/2 - sqrt(-1 + 2*r**2)/2, S(3)/2 + sqrt(-1 + 2*r**2)/2),
(S(3)/2 + sqrt(-1 + 2*r**2)/2, S(3)/2 - sqrt(-1 + 2*r**2)/2)]
f_1 = (x - 1)**2 + (y - 2)**2 - r**2
f_2 = (x - 1)**2 + (y - 1)**2 - r**2
assert solve_poly_system([f_1, f_2], x, y) == \
[(1 - sqrt(((2*r - 1)*(2*r + 1)))/2, S(3)/2),
(1 + sqrt(((2*r - 1)*(2*r + 1)))/2, S(3)/2)]
query = lambda expr: expr.is_Pow and expr.exp is S.Half
f_1 = (x - 1 )**2 + (y - 2)**2 - r**2
f_2 = (x - x1)**2 + (y - 1)**2 - r**2
result = solve_poly_system([f_1, f_2], x, y)
assert len(result) == 2 and all(len(r) == 2 for r in result)
assert all(r.count(query) == 1 for r in flatten(result))
f_1 = (x - x0)**2 + (y - y0)**2 - r**2
f_2 = (x - x1)**2 + (y - y1)**2 - r**2
result = solve_poly_system([f_1, f_2], x, y)
assert len(result) == 2 and all(len(r) == 2 for r in result)
assert all(len(r.find(query)) == 1 for r in flatten(result))
def test_get_derivatives(navier_stokes_problem, grid):
""" Ensure that spatial and temporal Derivative objects are identified and returned correctly. """
expanded_equations = navier_stokes_problem.get_expanded(navier_stokes_problem.equations)
expanded_formulas = navier_stokes_problem.get_expanded(navier_stokes_problem.formulas)
spatial_derivatives, temporal_derivatives = get_derivatives(flatten(expanded_equations))
assert str(spatial_derivatives) == "[Derivative(rhou0[x0, x1, t], x0), Derivative(rhou1[x0, x1, t], x1), Derivative(rhou0[x0, x1, t]*u1[x0, x1, t], x1), Derivative(p[x0, x1, t] + rhou0[x0, x1, t]*u0[x0, x1, t], x0), Derivative(u1[x0, x1, t], x0, x1), Derivative(u0[x0, x1, t], x0, x0), Derivative(u0[x0, x1, t], x1, x1), Derivative(rhou1[x0, x1, t]*u0[x0, x1, t], x0), Derivative(p[x0, x1, t] + rhou1[x0, x1, t]*u1[x0, x1, t], x1), Derivative(u0[x0, x1, t], x0, x1), Derivative(u1[x0, x1, t], x1, x1), Derivative(u1[x0, x1, t], x0, x0), Derivative((p[x0, x1, t] + rhoE[x0, x1, t])*u0[x0, x1, t], x0), Derivative((p[x0, x1, t] + rhoE[x0, x1, t])*u1[x0, x1, t], x1), Derivative(u0[x0, x1, t], x0), Derivative(u1[x0, x1, t], x1), Derivative(u0[x0, x1, t], x1), Derivative(u1[x0, x1, t], x0), Derivative(T[x0, x1, t], x0, x0), Derivative(T[x0, x1, t], x1, x1)]"
assert str(temporal_derivatives) == "[Derivative(rho[x0, x1, t], t), Derivative(rhou0[x0, x1, t], t), Derivative(rhou1[x0, x1, t], t), Derivative(rhoE[x0, x1, t], t)]"
return
def test_maximum_derivative_order(mass, momentum, energy):
""" Check that the maximum Derivative order is second-order. """
assert maximum_derivative_order(flatten([mass.expanded, momentum.expanded, energy.expanded])) == 2
def test_expand():
""" Ensure that an equation is expanded correctly. """
equations = ["Eq(Der(rho,t), -c*Conservative(rhou_j,x_j))"]
substitutions = []
ndim = 1
constants = ["c"]
coordinate_symbol = "x"
metrics = [False, False]
formulas = ["Eq(u_i, rhou_i/rho)"]
problem = Problem(equations, substitutions, ndim, constants, coordinate_symbol, metrics, formulas)
expanded_equations = flatten(problem.get_expanded(problem.equations))
expanded_formulas = flatten(problem.get_expanded(problem.formulas))
assert len(expanded_equations) == 1
assert str(expanded_equations[0].lhs) == "Derivative(rho[x0, t], t)"
assert str(expanded_equations[0].rhs) == "-c*Derivative(rhou0[x0, t], x0)"
assert len(expanded_formulas) == 1
assert str(expanded_formulas[0].lhs) == "u0[x0, t]"
assert str(expanded_formulas[0].rhs) == "rhou0[x0, t]/rho[x0, t]"
# Test the other way of expanding equations
assert expanded_equations == flatten(problem.get_expanded(problem.expand(equations)))
return
def hasNullSpace(self):
if self.rawStoichiometryMatrix is None:
return False
for var in flatten(self.rawStoichiometryMatrix):
if var in [SympyInf, -SympyInf, SympyNan]:
return False
return True