In [2]:
import sympy as sp
import numpy as np
import cloudpickle
In [37]:
q = q0, q1 = sp.symbols('q:2')
sympars = m1, g = sp.symbols('m_1, g') # Same order should be used inside MechanicalSystem Class
order = ['m1', 'g']
G = sp.Matrix([sp.cos(q0)*g, sp.sin(q1)*m1])
In [40]:
to_save = {'order': order,
'G' : sp.lambdify([q, *sympars], G)}
with open('gfile.txt', 'wb') as gfile:
test = cloudpickle.dumps(to_save)
gfile.write(test)
with open('gfile.txt', 'rb') as gfile:
model = cloudpickle.load(gfile)
In [42]:
# In main script
pars = {'m1' : 1, 'g' : 9.81} # order is arbitrary
# In systemsym
constant_names = model['order']
constant_value = [pars[name] for name in constant_names] # Corresponding numbers in same order as above
Luse = lambda q: model['G'](q, *constant_value) # in systemsym class
Luse([0,3.14/2]) # in equations of motion
Out[42]: