simulation.py
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 7 19:06:37 2021
@author: Rocky
"""
import numpy as np
import system_model as lac
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():
# Defining Input functions
lorenz_att = lac.Model()
rhs_xx_pp_symb = lorenz_att.get_rhs_symbolic()
print("Computational Equations:\n")
for i, eq in enumerate(rhs_xx_pp_symb):
print(f"dot_x{i+1} =", eq)
latt_rhs = lorenz_att.get_rhs_func()
# Initial State values
xx0 = [0.1, 0.1, 0.1]
t_end = 30
tt = np.linspace(0, t_end, 10000) # vector of times for simulation
sol = solve_ivp(latt_rhs, (0, t_end), xx0, t_eval=tt)
save_plot(sol)
return sol
def save_plot(simulation_data):
fig = plt.figure(figsize=(10, 4.5))
ax = plt.axes(projection="3d")
ax.plot(simulation_data.y[0], simulation_data.y[1], simulation_data.y[2])
ax.set(xlabel="$x_1$", ylabel="$x_2$", zlabel="$x_3$")
plt.title("Full Simulation")
plt.tight_layout()
save_plot_in_dir()
def evaluate_simulation(simulation_data):
"""
:param simulation_data: simulation_data of system_model
:return:
"""
expected_final_state = [3.30299301, 4.43659481, 17.91529214]
rc = ResultContainer(score=1.0)
simulated_final_state = simulation_data.y[:, -1]
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
r, b, sigma = self.pp_symb
# create symbolic rhs function vector
dx1_dt = -sigma * x + sigma * y
dx2_dt = -x * z + r * x - y
dx3_dt = x * y - b * z
self.dxx_dt_symb = sp.Matrix([dx1_dt, dx2_dt, dx3_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
# tailing "_nv" stands for "numerical value"
model_name = "Lorenz_Attractor"
# CREATE SYMBOLIC PARAMETERS
pp_symb = [r, b, sigma] = sp.symbols("r, b, sigma", real=True)
# SYMBOLIC PARAMETER FUNCTIONS
r_sf = 28
b_sf = 8 / 3
sigma_sf = 10
# List of symbolic parameter functions
pp_sf = [r_sf, b_sf, sigma_sf]
# OPTIONAL
# range of parameters
r_range = (24.74, 99)
b_range = None
sigma_range = None
# OPTIONAL
# list of ranges
pp_range_list = [r_range, b_range, sigma_range]
# 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
# DON'T CHANGE FOLLOWING ENTRIES: "Symbol", "Value"
tabular_header = ["Parameter Name", "Symbol", "Value"]
# Define column text alignments
col_alignment = ["left", "center", "left"]
# Define Entries of all columns before the Symbol-Column
# --- Entries need to be latex code
col_1 = ["Raileight coeff", "Parameter", "Prandtl Number"]
# contains all lists of the columns before the "Symbol" Column
# --- Empty list, if there are no columns before the "Symbol" Column
start_columns_list = [col_1]
# contains all lists of columns after the FIX ENTRIES
# --- Empty list, if there are no columns after the "Value" column
end_columns_list = []