Skip to content

Commit

Permalink
working on julia export
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaskoenig committed Dec 4, 2024
1 parent 126b0f6 commit efef3c1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/sbmlutils/converters/odefac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
41 changes: 41 additions & 0 deletions src/sbmlutils/resources/converters/odefac_template.jl
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit efef3c1

Please sign in to comment.