Skip to content

Commit efef3c1

Browse files
working on julia export
1 parent 126b0f6 commit efef3c1

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

src/sbmlutils/converters/odefac.py

+18
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,24 @@ def to_R(self, r_file: Optional[Path] = None) -> str:
467467

468468
return content
469469

470+
def to_julia(self, jl_file: Optional[Path] = None) -> str:
471+
"""Write ODEs to julia.
472+
473+
Generated files can be used as an input for DifferentialEquations.jl
474+
https://docs.sciml.ai/DiffEqDocs/stable/
475+
476+
"""
477+
content = self._render_template(
478+
template_file="odefac_template.jl",
479+
index_offset=1,
480+
replace_symbols=True,
481+
)
482+
if jl_file:
483+
with open(jl_file, "w") as f:
484+
f.write(content)
485+
486+
return content
487+
470488
def to_markdown(self, md_file: Optional[Path] = None) -> str:
471489
"""Write ODEs to markdown."""
472490
content = self._render_template(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# -------------------------------------------------------------------------------------
2+
# Autogenerated ODE definition from SBML file with sbmlutils
3+
# (https://github.com/matthiaskoenig/sbmlutils).
4+
#
5+
# model: {{ model.getId() }}
6+
# -------------------------------------------------------------------------------------
7+
8+
module {{ model.getId() }}
9+
export p, x0, f_dxdt, xids, pids, yids
10+
11+
# ids
12+
xids = [{% for id in xids %}"{{ id }}"{% if not loop.last %}, {% endif %}{% endfor %}]
13+
pids = [{% for id in pids %}"{{ id }}"{% if not loop.last %}, {% endif %}{% endfor %}]
14+
15+
# initial conditions
16+
x0 = [
17+
{% for id in xids %}
18+
{{ x0[id] }}{% if not loop.last %},{% endif %} # x0[{{ loop.index }}] {{ id }}
19+
{% endfor %}
20+
]
21+
22+
# parameters
23+
p = [
24+
{% for id in pids %}
25+
{{ p[id] }}{% if not loop.last %},{% endif %} # p[{{ loop.index }}] {{ id }}
26+
{% endfor %}
27+
]
28+
29+
# odes
30+
function f_dxdt(dx, x, p, t)
31+
{% for id in yids %}
32+
{{ id }} = {{ y[id] }} # y[{{ loop.index }}] {{ id }}
33+
{% endfor %}
34+
35+
{% for id in xids %}
36+
dx[{{loop.index}}] = {{ dx[id] }} # dx[{{ loop.index }}] {{ id }}
37+
{% endfor %}
38+
39+
end
40+
41+
end

0 commit comments

Comments
 (0)