<jupyter_start><jupyter_text>Modelica Source Code<jupyter_code>modelica_src = '''

model System
    input Real u;
    output Real x(start=1), v_x(start=1);
    Spring spring;
    Damper damper;
equation
    spring.x = x;
    damper.v = v_x;
    der(x) = v_x;
    der(v_x) = spring.f + damper.f - u;
end System;

model Spring
    Real x "displacement";
    Real f "force";
    parameter Real k = 2.0 "spring constant";
equation
    f = -k*x;
end Spring;

model Damper
    Real v "velocity";
    Real f "force";
    parameter Real c = 0.2 "damping constant";
equation
    f = -c*v;
end Damper;
'''<jupyter_output><empty_output><jupyter_text>Generation of Abstract Syntax Tree<jupyter_code>ast = pymoca.parser.parse(modelica_src)
ast.classes['System'].symbols.keys()
ast<jupyter_output><empty_output><jupyter_text>Generation of Sympy Model<jupyter_code>src_code = generator.generate(ast, 'System')
print(src_code)
exec(src_code)
model = System()
model
t = sympy.Symbol('t')
model.x.diff(t)
sol = sympy.solve(model.eqs, list(model.v) + list(model.x.diff(t)))
sol
model.x.diff(t).subs(sol)
model.linearize_symbolic()
ss = control.ss(*model.linearize())
ss
control.ss2tf(ss)
control.bode(ss[0,0], omega=pl.logspace(-1,1,100));
control.rlocus(ss[0,0], klist=pl.logspace(-2,1,1000));
pl.grid()<jupyter_output><empty_output><jupyter_text>Simulation of Sympy Model<jupyter_code>model.f
import sympy
ss_sub = {}
ss_sub.update({model.x[i]: sympy.DeferredVector('x')[i] for i in range(len(model.x))})
#ss_sub.update({model.y[i]: sympy.DeferredVector('y')[i] for i in range(len(model.y))})
ss_sub
res = model.simulate(t0=10, tf=20, dt=0.001)
pl.plot(res['t'], res['x'])
pl.grid()
pl.xlabel('t, sec')
pl.ylabel('x')
sympy.init_printing()
model.f
model.f.jacobian(model.x)<jupyter_output><empty_output><jupyter_text>Lyapunov Stability Using the energy function as a candidate function, we can show that the system has global exponential stability (GES).<jupyter_code>v_x, spring_k, t, V, x = sympy.symbols('v_x, spring.k, t, V, x')
V_eq = (v_x(t)**2 + spring_k*x(t)**2)/2
sympy.Eq(V, V_eq)
dV_dt_eq = (sympy.Matrix([V_eq]).jacobian(model.x).dot(model.f)).simplify()
sympy.Eq(V(t).diff(t), dV_dt_eq)
V_lam = sympy.lambdify(model.x, V_eq.subs(model.p0))
V_vals = [V_lam(*x) for x in res['x']]
pl.plot(res['t'], V_vals)
pl.xlabel('t')
pl.ylabel('V')
pl.grid()<jupyter_output><empty_output><jupyter_text>Parameter Sensitivity<jupyter_code>model.f.jacobian(model.p)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17380759902991108, 'fraction_numerical': 0.014147130153597413, 'mean_word_length': 5.780821917808219, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '436417', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1062, 'n_tokens_neox': 1036, 'n_words': 189, 'path': 'test/notebooks/Spring.ipynb', 'repo_name': 'PeterRabbit95/pymoca', 'size': '2474'}