simulation.py
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 7 19:06:37 2021
@author: Rocky
"""
import numpy as np
import system_model
from scipy.integrate import solve_ivp
from ackrep_core import ResultContainer
from ackrep_core.system_model_management import save_plot_in_dir
import matplotlib.pyplot as plt
import os
def simulate():
model = system_model.Model()
rhs_xx_pp_symb = model.get_rhs_symbolic()
print("Computational Equations:\n")
for i, eq in enumerate(rhs_xx_pp_symb):
print(f"dot_x{i+1} =", eq)
rhs = model.get_rhs_func()
# Initial State values
xx0 = [2, 3, 4]
t_end = 300
# Note: The system is simulated for 300s to generate a nice plot, but due to numerical differences
# on different hardware, the evaluation is performed at half that time.
tt = np.linspace(0, t_end, 12000 - 1)
sim = solve_ivp(rhs, (0, t_end), xx0, t_eval=tt)
save_plot(sim)
return sim
def save_plot(simulation_data):
y = simulation_data.y.tolist()
fig = plt.figure()
ax = fig.add_subplot(111, projection="3d")
ax.plot(y[0], y[1], y[2], label="Phase portrait", lw=1, c="k")
ax.set_xlabel("x", fontsize=15)
ax.set_ylabel("y", fontsize=15)
ax.set_zlabel("z", fontsize=15)
ax.legend()
ax.grid()
plt.tight_layout()
save_plot_in_dir()
def evaluate_simulation(simulation_data):
"""
:param simulation_data: simulation_data of system_model
:return:
"""
expected_final_state = [4.486449710392184, 0.9668556795992576, 2.2126416283661734]
rc = ResultContainer(score=1.0)
# Note: The system is simulated for 300s to generate a nice plot, but due to numerical differences
# on different hardware, the evaluation is performed at half that time.
simulated_final_state = simulation_data.y[:, 5999]
rc.final_state_errors = [
simulated_final_state[i] - expected_final_state[i] for i in np.arange(0, len(simulated_final_state))
]
rc.success = np.allclose(expected_final_state, simulated_final_state, rtol=0, atol=1e-2)
return rc
system_model.py
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 9 13:33:34 2021
@author: Jonathan Rockstroh
"""
import sympy as sp
import symbtools as st
import importlib
import sys, os
from ipydex import IPS, activate_ips_on_exception # for debugging only
from ackrep_core.system_model_management import GenericModel, import_parameters
# Import parameter_file
params = import_parameters()
class Model(GenericModel):
def initialize(self):
"""
this function is called by the constructor of GenericModel
:return: None
"""
# Define number of inputs -- MODEL DEPENDENT
self.u_dim = 0
# Set "sys_dim" to constant value, if system dimension is constant
# else set "sys_dim" to x_dim -- MODEL DEPENDENT
self.sys_dim = 3
# check existance of params file -> if not: System is defined to hasn't
# parameters
self.has_params = True
self.params = params
# ----------- SYMBOLIC RHS FUNCTION ---------- #
# --------------- MODEL DEPENDENT
def get_rhs_symbolic(self):
"""
:return:(matrix) symbolic rhs-functions
"""
if self.dxx_dt_symb is not None:
return self.dxx_dt_symb
x, y, z = self.xx_symb
a, b, c = self.pp_symb
# create symbolic rhs functions
dx_dt = -y - z
dy_dt = x + a * y
dz_dt = b * x - c * z + x * z
# put rhs functions into a vector
self.dxx_dt_symb = sp.Matrix([dx_dt, dy_dt, dz_dt])
return self.dxx_dt_symb
parameters.py
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 11 13:51:06 2021
@author: Jonathan Rockstroh
"""
import sys
import os
import numpy as np
import sympy as sp
import tabulate as tab
model_name = "Roessler_Atractor_1979_1"
# CREATE SYMBOLIC PARAMETERS
pp_symb = [a, b, c] = sp.symbols("a, b, c", real=True)
# SYMBOLIC PARAMETER FUNCTIONS
# spiral-type chaos
# a_sf = 0.32
# b_sf = 0.3
# c_sf = 4.5
# screw-type chaos
a_sf = 0.38
b_sf = 0.3
c_sf = 4.84
# List of symbolic parameter functions
pp_sf = [a_sf, b_sf, c_sf]
# List for Substitution
pp_subs_list = []
# OPTONAL: Dictionary which defines how certain variables shall be written
# in the tabular - key: Symbolic Variable, Value: LaTeX Representation/Code
# useful for example for complex variables: {Z: r"\underline{Z}"}
latex_names = {}
# ---------- CREATE BEGIN OF LATEX TABULAR
# Define tabular Header
tabular_header = ["Symbol", "Value"]
# Define column text alignments
col_alignment = ["center", "left"]
col_1 = []
# contains all lists of the columns before the "Symbol" Column
start_columns_list = []
# contains all lists of columns after the FIX ENTRIES
end_columns_list = []