From efef3c1929d82442985bc8ba46d11baafd446c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20K=C3=B6nig?= Date: Wed, 4 Dec 2024 15:49:05 +0100 Subject: [PATCH] working on julia export --- src/sbmlutils/converters/odefac.py | 18 ++++++++ .../resources/converters/odefac_template.jl | 41 +++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 src/sbmlutils/resources/converters/odefac_template.jl diff --git a/src/sbmlutils/converters/odefac.py b/src/sbmlutils/converters/odefac.py index 1c204fcd..af266bea 100644 --- a/src/sbmlutils/converters/odefac.py +++ b/src/sbmlutils/converters/odefac.py @@ -467,6 +467,24 @@ def to_R(self, r_file: Optional[Path] = None) -> str: return content + def to_julia(self, jl_file: Optional[Path] = None) -> str: + """Write ODEs to julia. + + Generated files can be used as an input for DifferentialEquations.jl + https://docs.sciml.ai/DiffEqDocs/stable/ + + """ + content = self._render_template( + template_file="odefac_template.jl", + index_offset=1, + replace_symbols=True, + ) + if jl_file: + with open(jl_file, "w") as f: + f.write(content) + + return content + def to_markdown(self, md_file: Optional[Path] = None) -> str: """Write ODEs to markdown.""" content = self._render_template( diff --git a/src/sbmlutils/resources/converters/odefac_template.jl b/src/sbmlutils/resources/converters/odefac_template.jl new file mode 100644 index 00000000..00d51d83 --- /dev/null +++ b/src/sbmlutils/resources/converters/odefac_template.jl @@ -0,0 +1,41 @@ +# ------------------------------------------------------------------------------------- +# Autogenerated ODE definition from SBML file with sbmlutils +# (https://github.com/matthiaskoenig/sbmlutils). +# +# model: {{ model.getId() }} +# ------------------------------------------------------------------------------------- + +module {{ model.getId() }} + export p, x0, f_dxdt, xids, pids, yids + + # ids + xids = [{% for id in xids %}"{{ id }}"{% if not loop.last %}, {% endif %}{% endfor %}] + pids = [{% for id in pids %}"{{ id }}"{% if not loop.last %}, {% endif %}{% endfor %}] + + # initial conditions + x0 = [ + {% for id in xids %} + {{ x0[id] }}{% if not loop.last %},{% endif %} # x0[{{ loop.index }}] {{ id }} + {% endfor %} + ] + + # parameters + p = [ + {% for id in pids %} + {{ p[id] }}{% if not loop.last %},{% endif %} # p[{{ loop.index }}] {{ id }} + {% endfor %} + ] + + # odes + function f_dxdt(dx, x, p, t) + {% for id in yids %} + {{ id }} = {{ y[id] }} # y[{{ loop.index }}] {{ id }} + {% endfor %} + + {% for id in xids %} + dx[{{loop.index}}] = {{ dx[id] }} # dx[{{ loop.index }}] {{ id }} + {% endfor %} + + end + +end