Commit 3d655991 authored by Thierry Martinez's avatar Thierry Martinez
Browse files

export_ode_system

parent 551b2101
......@@ -20,6 +20,7 @@
single_model/1,
add_item/1,
replace_item/4,
change_item/4,
set_annotation/3,
get_annotation/3,
delete_annotation/2,
......@@ -787,6 +788,17 @@ replace_item(Id, Kind, Key, Item) :-
put_item(Parent, Kind, Key, Item, Id).
change_item(Options, Kind, Key, Item) :-
catch(
(
find_item([kind: Kind, key: Key, id: Id | Options]),
replace_item(Id, Kind, Key, Item)
),
error(unknown_item),
add_item([kind: Kind, key: Key, item: Item | Options])
).
create_item_id(Id) :-
count(item_id, Id).
......
......@@ -28,9 +28,10 @@ numerical_simulation(Time) :-
biocham_command,
type(Time, time),
doc('performs a numerical simulation up to a given time.'),
ode_system,
make_ode_system,
solve(Time).
with_current_ode_system((
numerical_simulation:make_ode_system,
numerical_simulation:solve(Time)
)).
make_ode_system :-
......
......@@ -14,7 +14,12 @@
ode_system/0,
import_ode/1,
import_reactions_from_ode_system/0,
(init)/1,
load_ode_system/1,
add_ode_system/1,
export_ode_system/1,
get_current_ode_system/1,
with_current_ode_system/1,
all_odes/1,
ode/2,
op(1000, fx, init)
......@@ -185,6 +190,50 @@ import_ode(InputFile) :-
close(Stream)
).
init(InitList) :-
biocham_command,
type(InitList, '*'(name = arithmetic_expression)),
doc('sets the initial value of a variable in the current set of ODEs.'),
\+ (
member(Variable = Value, InitList),
\+ (
set_ode_initial_value(Variable, Value)
)
).
load_ode_system(InputFile) :-
biocham_command,
type(InputFile, input_file),
doc('
acts as \\command{load_biocham/1} but importing a model from an ODE system.
'),
load_all('ode', InputFile).
add_ode_system(InputFile) :-
biocham_command,
type(InputFile, input_file),
doc('
acts as \\command{add_biocham/1} but importing a model from an ODE system.
'),
load_all('ode', InputFile).
export_ode_system(OutputFile) :-
biocham_command,
type(OutputFile, output_file),
doc('exports the current ODE system.'),
with_current_ode_system((
setup_call_cleanup(
open(OutputFile, write, Stream),
with_output_to(
Stream,
print_ode_system
),
close(Stream)
)
)).
:- devdoc('\\section{Public API}').
......@@ -269,7 +318,9 @@ put_ode_into_system :-
\+ (
draft_ode(X, Expression),
\+ (
add_ode(d(X)/dt = Expression)
add_ode(d(X)/dt = Expression),
get_initial_concentration(X, Concentration),
set_ode_initial_value(X, Concentration)
)
).
......@@ -280,7 +331,6 @@ delete_single_ode(Name) :-
read_ode(Stream) :-
get_current_ode_system(Id),
repeat,
read_line_to_codes(Stream, Codes),
(
......@@ -299,9 +349,9 @@ read_ode(Stream) :-
!
;
(
Expression = (init X = _E)
Expression = (init X = E)
->
add_item([parent: Id, kind: initial_concentration, key: X, item: Expression])
set_ode_initial_value(X, E)
;
Expression = (_DX/dt = _E)
->
......@@ -358,6 +408,55 @@ 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).
add_ode_system_file(InputFile) :-
import_ode(InputFile),
import_reactions_from_ode_system,
get_current_ode_system(Id),
delete_item(Id).
print_ode_system :-
get_current_ode_system(Id),
\+ (
item([parent: Id, kind: initial_concentration, item: (init X = E)]),
\+ (
format('init ~p = ~p\n', [X, E])
)
),
\+ (
item([parent: Id, kind: ode, item: (d(X)/dt = E)]),
\+ (
format('d~p/dt = ~p\n', [X, E])
)
),
format('done\n').
with_current_ode_system(Goal) :-
(
get_current_ode_system(_)
->
Goal
;
setup_call_cleanup(
ode_system,
Goal,
(
get_current_ode_system(Id),
delete_item(Id)
)
)
).
%
%
%print_ode :-
......
......@@ -40,4 +40,11 @@ test(
all_items([kind: reaction], Reactions).
test('export_ode_system', [true(Lines == ['init b = 0', 'init a = 1', 'db/dt = a', 'da/dt = -a', 'done'])]) :-
clear_model,
command(a => b),
command(present(a)),
export_ode_system('test.ode'),
list_file_lines('test.ode', Lines).
:- end_tests(ode).
......@@ -12,21 +12,12 @@
set_parameter(ParameterList) :-
biocham_command(*),
type(ParameterList, '*'(parameter = number)),
type(ParameterList, '*'(parameter = arithmetic_expression)),
doc('sets the value of parameters.'),
\+ (
member(Parameter = Value, ParameterList),
\+ (
catch(
(
find_item([kind: parameter, key: Parameter, id: Id]),
replace_item(Id, parameter, Parameter, parameter(Parameter = Value))
),
error(unknown_item),
add_item([
kind: parameter, key: Parameter, item: parameter(Parameter = Value)
])
)
change_item([], parameter, Parameter, parameter(Parameter = Value))
)
).
......
......@@ -14,7 +14,10 @@
indent/2,
view_image/1,
set_image_viewer_driver/1,
open_file/1,
list_file_lines/2,
list_stream_lines/2,
file_line_by_line/2,
stream_line_by_line/2,
term_morphism/3,
substitute/4,
call_subprocess/3
......@@ -159,6 +162,43 @@ set_image_viewer_driver(Driver) :-
nb_setval(image_viewer_driver, Driver).
list_file_lines(Filename, List) :-
findall(
Line,
file_line_by_line(Filename, Line),
List
).
list_stream_lines(Stream, List) :-
findall(
Line,
stream_line_by_line(Stream, Line),
List
).
file_line_by_line(Filename, Line) :-
setup_call_cleanup(
open(Filename, read, Stream),
stream_line_by_line(Stream, Line),
close(Stream)
).
stream_line_by_line(Stream, Line) :-
repeat,
read_line_to_codes(Stream, Codes),
(
Codes = end_of_file
->
!,
fail
;
atom_codes(Line, Codes)
).
term_morphism(F, TermIn, TermOut) :-
functor(TermIn, Functor, Arity),
functor(TermOut, Functor, Arity),
......
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