Skip to content

Chemical Reactor

Let's use the system class pydykit.systems_dae.ChemicalReactor to represent a chemical reactor in terms of a differential-algebraic system and solve it's behavior in time. This system is modelled as quasilinear differential-algebraic equations.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import plotly.graph_objects as go

from pydykit.configuration import Configuration
from pydykit.examples import ExampleManager
from pydykit.managers import Manager
from pydykit.plotters import Plotter

####### Simulation
file_content = ExampleManager().get_example(name="reactor")
configuration = Configuration(**file_content)  # Validate config file content

manager = Manager()
manager.configure(configuration=configuration)

result = manager.manage()  # Run the simulation

####### Plotting
fig = go.Figure()
df = result.to_df()
plotter = Plotter(results_df=df)
plotter.plot_3d_trajectory(
    figure=fig,
    x_components=df["concentration"],
    y_components=df["temperature"],
    z_components=df["reaction_rate"],
    time=df["time"],
)

####### Show visualization
script_is_executed_on_local_machine = False  # You might want to change this

if script_is_executed_on_local_machine:
    fig.show()
else:
    print(
        fig.to_html(
            full_html=False,
            include_plotlyjs="cdn",
        )
    )

Config File

The configuration file of this simulation reads as

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
name: reactor
system:
  class_name: ChemicalReactor
  constants: [1.0, 1.0, 1.0, -100.0]
  cooling_temperature: 10.0
  reactant_concentration: 0.5
  initial_temperature: 50.0
  state:
    state: [0.0, 50.0, 0.0]
simulator:
  class_name: OneStep
  solver_name: NewtonPlainPython
  newton_epsilon: 1.e-10
  max_iterations: 40
integrator:
  class_name: MidpointDAE
time_stepper:
  class_name: FixedIncrement
  step_size: 0.01
  start: 0.0
  end: 1

For details on the usage of class_name-variable, see the example Pendulum 3D.