Commit a85dc7fa authored by Thierry Martinez's avatar Thierry Martinez

Completed ode interface

parent a4b19f02
......@@ -18,13 +18,20 @@
load_ode_system/1,
add_ode_system/1,
export_ode_system/1,
new_ode_system/1,
get_current_ode_system/1,
set_current_ode_system/1,
set_ode_system_name/2,
import_ode/2,
set_ode_initial_value/2,
set_ode_initial_value/3,
add_ode/2,
import_reactions_from_ode_system/1,
with_current_ode_system/1,
all_odes/1,
ode/2,
% export_ode/1,
load_ode/1,
add_ode/1
ode/3,
op(1000, fx, init)
]).
......@@ -34,7 +41,7 @@
new_ode_system :-
biocham_command,
doc('creates an ODE system.'),
add_item([kind: ode_system, key: new_ode_system, id: Id]),
new_ode_system(Id),
set_current_ode_system(Id).
......@@ -50,7 +57,7 @@ set_ode_system_name(Name) :-
type(Name, name),
doc('sets the name of the current ODE system.'),
get_current_ode_system(Id),
replace_item(Id, ode_system, Name, Name).
set_ode_system_name(Id, Name).
list_ode_systems :-
......@@ -82,20 +89,8 @@ ode(DX/dt = E) :-
add_ode(ODE) :-
ode(ODE),
!,
(
ODE = (d(X)/dt = _)
->
Item = ODE
;
ODE = (DX/dt = E),
atom_concat(d, X, DX)
->
Item = (d(X)/dt = E)
;
throw(error(illformed_ode(ODE)))
),
get_current_ode_system(Id),
add_item([parent: Id, kind: ode, key: X, item: Item]).
add_ode(Id, ODE).
add_ode(ODEList) :-
......@@ -113,22 +108,8 @@ add_ode(ODEList) :-
import_reactions_from_ode_system :-
biocham_command,
doc('adds the reactions that match the current ODE system.'),
\+ (
ode(X, E),
monomial(Monomial, E),
\+ (
(
Monomial = - OppositeMonomial,
coefficient_species(OppositeMonomial, Coefficient, Species)
->
add_reaction(Coefficient * Species for X => '_')
;
coefficient_species(Monomial, Coefficient, Species)
->
add_reaction(Coefficient * Species for '_' => X)
)
)
).
get_current_ode_system(Id),
import_reactions_from_ode_system(Id).
:- grammar(oderef).
......@@ -165,8 +146,10 @@ list_ode :-
doc('
\\end{example}
'),
get_current_ode_system(Id),
list_items([parent: Id, kind: ode]).
with_current_ode_system((
get_current_ode_system(Id),
list_items([parent: Id, kind: ode])
)).
ode_system :-
......@@ -175,7 +158,7 @@ ode_system :-
delete_ode_system('ode_system'),
new_ode_system,
set_ode_system_name('ode_system'),
clean_draft_ode,
check_draft_ode_cleaned,
compute_ode,
simplify_ode,
put_ode_into_system,
......@@ -186,13 +169,9 @@ import_ode(InputFile) :-
biocham_command,
type(InputFile, input_file),
doc('imports a set of ODEs.'),
new_ode_system,
set_ode_system_name(InputFile),
setup_call_cleanup(
open(InputFile, read, Stream),
read_ode(Stream),
close(Stream)
).
import_ode(InputFile, Id),
set_current_ode_system(Id).
init(InitList) :-
biocham_command,
......@@ -242,10 +221,32 @@ export_ode_system(OutputFile) :-
:- devdoc('\\section{Public API}').
new_ode_system(Id) :-
add_item([kind: ode_system, key: new_ode_system, id: Id]).
get_current_ode_system(Id) :-
get_selection(current_model, current_ode_system, [Id]).
set_current_ode_system(Id) :-
set_selection(current_model, current_ode_system, [Id]).
set_ode_system_name(Id, Name) :-
replace_item(Id, ode_system, Name, Name).
import_ode(InputFile, Id) :-
new_ode_system(Id),
set_ode_system_name(Id, InputFile),
setup_call_cleanup(
open(InputFile, read, Stream),
read_ode(Stream, Id),
close(Stream)
).
all_odes(ODEs) :-
get_current_ode_system(Id),
all_items([parent: Id, kind: ode], ODEs).
......@@ -253,6 +254,10 @@ all_odes(ODEs) :-
ode(X, E) :-
get_current_ode_system(Id),
ode(Id, X, E).
ode(Id, X, E) :-
(
var(X)
->
......@@ -262,16 +267,65 @@ ode(X, E) :-
).
:- devdoc('\\section{Private predicates}').
set_ode_initial_value(Variable, Value) :-
get_current_ode_system(Id),
set_ode_initial_value(Id, Variable, Value).
set_current_ode_system(Id) :-
set_selection(current_model, current_ode_system, [Id]).
set_ode_initial_value(Id, Variable, Value) :-
change_item([parent: Id], initial_concentration, Variable, init Variable = Value).
add_ode(Id, ODE) :-
(
ODE = (d(X)/dt = _)
->
Item = ODE
;
ODE = (DX/dt = E),
atom_concat(d, X, DX)
->
Item = (d(X)/dt = E)
;
throw(error(illformed_ode(ODE)))
),
add_item([parent: Id, kind: ode, key: X, item: Item]).
import_reactions_from_ode_system(Id) :-
\+ (
ode(Id, X, E),
monomial(Monomial, E),
\+ (
(
Monomial = - OppositeMonomial,
coefficient_species(OppositeMonomial, Coefficient, Species)
->
add_reaction(Coefficient * Species for X => '_')
;
coefficient_species(Monomial, Coefficient, Species)
->
add_reaction(Coefficient * Species for '_' => X)
)
)
).
:- devdoc('\\section{Private predicates}').
:- dynamic(draft_ode/2).
check_draft_ode_cleaned :-
(
draft_ode(_, _)
->
throw(error(check_draft_ode_cleaned))
;
true
).
clean_draft_ode :-
retractall(draft_ode(_, _)).
......@@ -328,27 +382,13 @@ put_ode_into_system :-
)
).
%export_ode(OutputFile) :-
% biocham_command,
% type(OutputFile, output_file),
% doc('
% exports the current Biocham model (reaction rules, initial state, parameters and
% macros) in an xppaut .ode file.
% '),
% automatic_suffix(OutputFile, '.ode', write, FilenameODE),
% setup_call_cleanup(
% open(FilenameODE, write, Stream),
% export_ode_stream(Stream),
% close(Stream)
% ).
delete_single_ode(Name) :-
get_current_ode_system(Id),
delete_item([parent: Id, kind: ode, key: Name]).
read_ode(Stream) :-
read_ode(Stream, Id) :-
repeat,
read_line_to_codes(Stream, Codes),
(
......@@ -369,11 +409,11 @@ read_ode(Stream) :-
(
Expression = (init X = E)
->
set_ode_initial_value(X, E)
set_ode_initial_value(Id, X, E)
;
Expression = (_DX/dt = _E)
->
add_ode(Expression)
add_ode(Id, Expression)
)
->
fail
......@@ -427,11 +467,6 @@ coefficient_species(Coefficient * Species, Coefficient, Species) :-
coefficient_species(Species, 1, Species).
set_ode_initial_value(Variable, Value) :-
get_current_ode_system(Id),
change_item([parent: Id], initial_concentration, Variable, init Variable = Value).
models:add_file_suffix('ode', add_ode_system_file).
......@@ -474,52 +509,3 @@ with_current_ode_system(Goal) :-
)
)
).
%
%
%print_ode :-
% write('Init \tODE\n'),
% \+ (
% ode(Molecule, Equation),
% get_initial_concentration(Molecule, InitialConcentration),
% \+ (
% format('~f \td[~w]/dt=~w\n', [InitialConcentration, Molecule, Equation])
% )
% ).
%
%
%load_ode(InputFile) :-
% biocham_command,
% type(InputFile, input_file),
% doc('
% creates a new Biocham model (reaction rules, initial state, parameters and
% macros) inferred from an xppaut .ode file (preferably written with
% parameters).
% '),
% external_convert_ode(InputFile, 'external_ode.bc'),
% load_biocham('external_ode.bc').
%
%
%add_ode(InputFile) :-
% biocham_command,
% type(InputFile, input_file),
% doc('
% adds reaction rules, initial state, parameters and
% macros inferred from an xppaut .ode file (preferably written with
% parameters).
% '),
% external_convert_ode(InputFile, 'external_ode.bc'),
% add_biocham('external_ode.bc').
%
%
%external_convert_ode(InputFile, OutputFile) :-
% setup_call_cleanup(
% open('external_load_ode.bc', write, Ode_bc),
% (
% portray_clause(Ode_bc, load_ode(InputFile)),
% portray_clause(Ode_bc, export_biocham(OutputFile)),
% portray_clause(Ode_bc, 'quit')
% ),
% close(Ode_bc)
% ),
% call_subprocess(path(biocham), ['external_load_ode.bc'], [stdout(pipe(_))]).
......@@ -21,33 +21,31 @@ add_function(FunctionList) :-
biocham(present(y,3)),
biocham(add_function(z=x+y)),
biocham(list_reactions),
biocham(list_ODE),
biocham(list_ode),
doc('
\\end{example}
'),
setup_call_cleanup(
open('external_ode_function.ode', write, Stream),
export_ode(FunctionList,Stream),
close(Stream)
),
add_ode('external_ode_function.ode').
new_ode_system(OdeSystem),
export_ode(FunctionList, OdeSystem),
import_reactions_from_ode_system(OdeSystem),
delete_item(OdeSystem).
export_ode(_,Stream):-
export_ode(_, OdeSystem) :-
enumerate_molecules(L), % useless if item backtracks on Object (not clear to FF)
member(Object,L),
member(Object, L),
get_initial_concentration(Object, Concentration),
format(Stream, 'init ~a = ~d\n', [Object,Concentration]),
set_ode_initial_value(OdeSystem, Object, Concentration),
fail.
export_ode(FunctionList,Stream):-
export_ode(FunctionList, OdeSystem) :-
member(Variable = _, FunctionList),
format(Stream, 'init ~a = 0\n', [Variable]),
set_ode_initial_value(OdeSystem, Variable, 0),
fail.
export_ode(FunctionList,Stream):-
export_ode(FunctionList, OdeSystem) :-
member(Variable = Value, FunctionList),
format(Stream, 'd~a/dt = ~w - ~a\n', [Variable,Value,Variable]),
add_ode(OdeSystem, d(Variable)/dt = Value - Variable),
fail.
export_ode(_,_).
export_ode(_,_).
......@@ -11,4 +11,4 @@ test('add_function'):-
command(k*[x] for x=>y),
command(add_function(z=x+y, u=x*y)).
:- end_tests(ode).
:- end_tests(odefunction).
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment