Skip to content

Commit

Permalink
support in simbio export
Browse files Browse the repository at this point in the history
  • Loading branch information
metelkin committed Jun 20, 2024
1 parent 8117717 commit 1ea6b8d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 49 deletions.
2 changes: 1 addition & 1 deletion export-formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ Each namespace in separate file.
|Const: `Infinity`, `NaN` |- |- |+ |+ |+ |+ |+ |+ |+ |na
|`@Scenario` support |- |- |- |- |- |- |- |+ |+ |na
|`@Record {ss: true, ...}` |- |- |+ |- |- |- |- |+ |+ |na
|`#defineFunction`, (sub) means substitution |+ (sub) |+ (sub) |+ |+ |+ |- |+ |+ |+ |na
|`#defineFunction`, (sub) means substitution |+(sub) |+(sub) |+ |+(sub) |+ |+(sub) |+ |+ |+ |na

## Math expressions conversions

Expand Down
15 changes: 8 additions & 7 deletions src/julia-export/expression.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { Expression } = require('../core/expression');

Expression.prototype.toJuliaString = function(){
Expression.prototype.toJuliaString = function(substituteByDefinitions) {
let tree = substituteByDefinitions ? this.substituteByDefinitions().exprParsed : this.exprParsed;

let juliaStringHandler = (node, options) => {
if(node.type==='ConstantNode' && Number.isInteger(node.value)){
return node.value.toExponential(); // to display 6 => 6e0; 6e23 => 6e+23
Expand Down Expand Up @@ -134,12 +136,11 @@ Expression.prototype.toJuliaString = function(){
}
};

return this.exprParsed
.toString({
parenthesis: 'keep',
implicit: 'show',
handler: juliaStringHandler
});
return tree.toString({
parenthesis: 'keep',
implicit: 'show',
handler: juliaStringHandler
});
};

module.exports = Expression;
15 changes: 8 additions & 7 deletions src/matlab-export/expression.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const { Expression } = require('../core/expression');

Expression.prototype.toMatlabString = function(){
Expression.prototype.toMatlabString = function(substituteByDefinitions = true) {
let tree = substituteByDefinitions ? this.substituteByDefinitions().exprParsed : this.exprParsed;

let matlabStringHandler = (node, options) => {
if (node.type==='FunctionNode' && node.fn.name==='pow') {
return `power(${node.args[0].toString(options)}, ${node.args[1].toString(options)})`;
Expand Down Expand Up @@ -95,12 +97,11 @@ Expression.prototype.toMatlabString = function(){
}
};

return this.exprParsed
.toString({
parenthesis: 'keep',
implicit: 'show',
handler: matlabStringHandler
});
return tree.toString({
parenthesis: 'keep',
implicit: 'show',
handler: matlabStringHandler
});
};

module.exports = Expression;
12 changes: 2 additions & 10 deletions src/simbio-export/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,8 @@ class SimbioExport extends AbstractExport {
return true;
}
// return text for all namespaces
makeText(){
let logger = this._container.logger;

// display that function definition is not supported
let userDefinedFunctions = [...this._container.functionDefStorage]
.filter(([id, functionDef]) => !functionDef.isCore)
.map(([id, functionDef]) => id);
if (userDefinedFunctions.length > 0) {
logger.warn(`User defined functions: ${userDefinedFunctions.join(', ')} are presented in platform but not supported by Simbio export.`);
}
makeText() {
//let logger = this._container.logger;

let results = this.selectedNamespaces().map(([spaceName, ns]) => {

Expand Down
26 changes: 13 additions & 13 deletions src/templates/julia-model.jl.njk
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
__platform__ = (function()
{% for fd in functionDefArray %}
function {{ fd.id }}({% for a in fd.arguments %}{{ a }}, {% endfor %}__args__...)
return {{ fd.math.toJuliaString() }}
return {{ fd.math.toJuliaString(false) }}
end
{%- endfor %}

Expand Down Expand Up @@ -85,9 +85,9 @@ function {{ id }}_init_func_(__constants__)
{%- endfor %}
{% for record in img.initRecords -%}
{% if record.isRule -%}
{{ record.id }} = {{ record.getAssignment('ode_').translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.getAssignment('ode_').translateSymbol(img.pTranslator).toJuliaString(false) }}
{%- else -%}
{{ record.id }} = {{ record.getAssignment('start_').translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.getAssignment('start_').translateSymbol(img.pTranslator).toJuliaString(false) }}
{%- endif %}
{% endfor %}
# save results
Expand Down Expand Up @@ -132,7 +132,7 @@ function {{ id }}_ode_func_(__du__, __u__, __p__, t)
{%- endfor %}
{% for record in img.odeRules %}
{%- if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString(false) }}
{% elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ record.id }}_ / {{ record.compartment }}
{% endif -%}
Expand Down Expand Up @@ -181,7 +181,7 @@ function {{ id }}_saving_generator_(__outputIds__::Vector{Symbol})
{%- endfor %}
{% for record in img.extendedRuleRecords %}
{%- if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString(false) }}
{% elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ record.id }}_ / {{ record.compartment }}
{% endif -%}
Expand Down Expand Up @@ -231,13 +231,13 @@ function {{ id }}_{{ event.switcher.id }}_condition_func_(__u__, t, __integrator
{%- endfor %}
{% for record in event.triggerRules %}
{%- if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString(false) }}
{% elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ record.id }}_ / {{ record.compartment }}
{% endif -%}
{% endfor %}

return ({{ event.switcher.trigger.translateSymbol(img.pTranslator).toJuliaString() if event.switcher.trigger is defined else 'false' }}) ? 1 : -1
return ({{ event.switcher.trigger.translateSymbol(img.pTranslator).toJuliaString(false) if event.switcher.trigger is defined else 'false' }}) ? 1 : -1
end
{%- endfor %}

Expand All @@ -257,13 +257,13 @@ function {{ id }}_{{ event.switcher.id }}_condition_func_(__u__, t, __integrator
{%- endfor %}
{% for record in event.triggerRules %}
{%- if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString(false) }}
{% elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ record.id }}_ / {{ record.compartment }}
{% endif -%}
{% endfor %}

return {{ event.switcher.trigger.translateSymbol(img.pTranslator).toJuliaString() if event.switcher.trigger is defined else '-1' }}
return {{ event.switcher.trigger.translateSymbol(img.pTranslator).toJuliaString(false) if event.switcher.trigger is defined else '-1' }}
end
{%- endfor %}

Expand All @@ -285,13 +285,13 @@ function {{ id }}_{{ event.switcher.id }}_condition_func_(__u__, t, __integrator
{%- endfor %}
{% for record in event.triggerRules %}
{%- if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString(false) }}
{% elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ record.id }}_ / {{ record.compartment }}
{% endif -%}
{% endfor %}

return {{ event.switcher.trigger.translateSymbol(img.pTranslator).toJuliaString() if event.switcher.trigger is defined else 'false' }}
return {{ event.switcher.trigger.translateSymbol(img.pTranslator).toJuliaString(false) if event.switcher.trigger is defined else 'false' }}
end
{%- endfor %}

Expand All @@ -313,7 +313,7 @@ function {{ id }}_{{ event.switcher.id }}_affect_func_(__integrator__)
{%- endfor %}
{% for record in event.affectRules %}
{%- if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString() }}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(img.pTranslator).toJuliaString(false) }}
{% elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ record.id }}_ / {{ record.compartment }}
{% endif -%}
Expand All @@ -325,7 +325,7 @@ function {{ id }}_{{ event.switcher.id }}_affect_func_(__integrator__)
{%- endfor -%}
) = (
{%- for assignment in event.affect -%}
{{ assignment.getAssignment(event.switcher.id).translateSymbol(img.pTranslator).toJuliaString() }},
{{ assignment.getAssignment(event.switcher.id).translateSymbol(img.pTranslator).toJuliaString(false) }},
{%- endfor -%}
)

Expand Down
22 changes: 11 additions & 11 deletions src/templates/matlab-model.m.njk
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names

{%- for functionDefinition in functionDefArray %}
function out = {{ functionDefinition.id }}({{ functionDefinition.arguments.join(', ') }})
out = {{ functionDefinition.math.toMatlabString() }}
out = {{ functionDefinition.math.toMatlabString(false) }}
end
{%- endfor %}

Expand All @@ -55,9 +55,9 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names
{%- endfor %}
{% for record in initRecords %}
{% if record.getAssignment('start_') is defined -%}
{{ record.id }} = {{ record.getAssignment('start_').translateSymbol(pTranslator).toMatlabString() }}; % {{ record.id }}, {{ record.title }} ({{ record.units }})
{{ record.id }} = {{ record.getAssignment('start_').translateSymbol(pTranslator).toMatlabString(false) }}; % {{ record.id }}, {{ record.title }} ({{ record.units }})
{%- else -%}
{{ record.id }} = {{ record.getAssignment('ode_').translateSymbol(pTranslator).toMatlabString() }}; % {{ record.id }}, {{ record.title }} ({{ record.units }})
{{ record.id }} = {{ record.getAssignment('ode_').translateSymbol(pTranslator).toMatlabString(false) }}; % {{ record.id }}, {{ record.title }} ({{ record.units }})
{%- endif -%}
{%- endfor %}

Expand Down Expand Up @@ -119,7 +119,7 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names
{%- for record in sharedRecords %}
% {{ record.title }} ({{ record.units }}) {{ record.notes }}
{% if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString() }};
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString(false) }};
{%- elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ yTranslator[record.id] }} / {{ record.compartment }};
{%- else -%}
Expand Down Expand Up @@ -168,15 +168,15 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names
{%- for record in sharedRecords %}
% {{ record.title }} ({{ record.units }}) {{ record.notes }}
{% if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString() }};
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString(false) }};
{%- elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ yTranslator[record.id] }} / {{ record.compartment }};
{%- else -%}
{{ record.id }} = {{ yTranslator[record.id] }};
{%- endif -%}
{%- endfor %}

{% set trigger = event.switcher.trigger.translateSymbol(pTranslator).toMatlabString() if event.switcher.trigger is defined else 'false' -%}
{% set trigger = event.switcher.trigger.translateSymbol(pTranslator).toMatlabString(false) if event.switcher.trigger is defined else 'false' -%}
if ({{ trigger }}) {# && {{ pTranslator[event.switcher.id + '_'] }} #}
res = 1;
else
Expand All @@ -196,15 +196,15 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names
{%- for record in sharedRecords %}
% {{ record.title }} ({{ record.units }}) {{ record.notes }}
{% if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString() }};
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString(false) }};
{%- elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ yTranslator[record.id] }} / {{ record.compartment }};
{%- else -%}
{{ record.id }} = {{ yTranslator[record.id] }};
{%- endif -%}
{%- endfor %}

res = {{ event.switcher.trigger.translateSymbol(pTranslator).toMatlabString() if event.switcher.trigger is defined else '-1' }};
res = {{ event.switcher.trigger.translateSymbol(pTranslator).toMatlabString(false) if event.switcher.trigger is defined else '-1' }};
end
{%- endfor %}

Expand All @@ -219,7 +219,7 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names
{%- for record in sharedRecords %}
% {{ record.title }} ({{ record.units }}) {{ record.notes }}
{% if record.isRule -%}
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString() }};
{{ record.id }} = {{ record.assignments.ode_.translateSymbol(pTranslator).toMatlabString(false) }};
{%- elif record.instanceOf('Species') and not record.isAmount -%}
{{ record.id }} = {{ yTranslator[record.id] }} / {{ record.compartment }};
{%- else -%}
Expand All @@ -239,9 +239,9 @@ function [ode_func, out_func, y0_, events_conditions, events_affects] = {{ names
%%% recalculated values
{%- for assignment in event.affect %}
{% if assignment.instanceOf('Species') and not assignment.isAmount and not assignment.isRule -%}
{{ idOrSynonim(assignment) }} = ({{ assignment.getAssignment(event.switcher.id).translateSymbol(pTranslator).toMatlabString() }}) * {{ assignment.compartment }};
{{ idOrSynonim(assignment) }} = ({{ assignment.getAssignment(event.switcher.id).translateSymbol(pTranslator).toMatlabString(false) }}) * {{ assignment.compartment }};
{%- else -%}
{{ idOrSynonim(assignment) }} = {{ assignment.getAssignment(event.switcher.id).translateSymbol(pTranslator).toMatlabString() }};
{{ idOrSynonim(assignment) }} = {{ assignment.getAssignment(event.switcher.id).translateSymbol(pTranslator).toMatlabString(false) }};
{%- endif -%}
{%- endfor %}

Expand Down

0 comments on commit 1ea6b8d

Please sign in to comment.