GlycoPy is an equation-oriented and object-oriented hierarchical modeling, optimization and control package.
For the installation Highly recommend you to use Poetry instead of pip or conda.
With Poetry, you can install GlycoPy by the following steps:
- first, activate the virtual environment or conda environment of your project;
- Second, in the terminal, cd to the directory of your glycopy installation package (you need to download to you computer first as it's in in the pip index yet);
- Finally, run the command: poetry install
Here is an example of a simple simulation.
import numpy as np
import matplotlib.pyplot as plt
from glycopy.base import Model
from glycopy.integrator import Integrator, Initializer
from glycopy.simulation.simulation_dyn import SimulationDyn
class SimpleDAE(Model):
def build(self):
xg = self.add_var('x', 'xg', m=2, val=[0., 1.])
self.add_var('x', 'obj', val=0.)
zg = self.add_var('z', 'zg')
pg = self.add_var('p', 'pg', val=1.0)
ug = self.add_var('u', 'ug', val=0.)
self.add_eq('ode', 'xg', [zg * xg[0] - xg[1] + ug,
xg[0]])
self.add_eq('ode', 'obj', xg[0]**2 + xg[1]**2 + ug**2)
self.add_eq('alg', 'zg', zg - (1 - xg[1]**2) * pg)
np.random.seed(123)
model = SimpleDAE('simple_dae')
dae_prob = model.setup()
T = 10
n_points = 100
time_points = np.linspace(0, T, n_points)
time_grid = np.array([0, T]) #np.linspace(0, T, int(n_points * 10))
initializer = Initializer('init', model)
integrator = Integrator('integrator', 'idas', dae_prob, initializer=initializer)
sim = SimulationDyn(model, integrator, p_var=[('simple_dae.pg', 0),])
p_var = np.random.randn(n_points-1, 1)
sim.simulation_with_events_numerical(t_event=time_points, p_var=p_var)
print('The value of the objective function is:', model.obj_obj.val)
# Plot the trajectory of the state variables
fig, ax = plt.subplots(figsize=(4, 3))
t_all = np.sort(np.array(list(set(np.concatenate((time_points.flatten(), time_grid))))))
ax.plot(t_all, model.xg_obj.val_trajectory()[:, 0], '-')
ax.plot(t_all, model.xg_obj.val_trajectory()[:, 1], '--')
ax.plot(t_all, model.zg_obj.val_trajectory(), '-.')
ax.step(time_points[:-1], p_var, where='post', color='red', label='p_var', linestyle=':')
ax.legend(['$x_{1}$', '$x_{2}$', '$z$', '$p$'])
ax.set_xlabel('t (-)')
plt.tight_layout()
plt.subplots_adjust(bottom=0.15, top=0.95)
plt.show()