Commit 3754983f authored by SOLIMAN Sylvain's avatar SOLIMAN Sylvain

starting to check conservations for real, and more tests

parent ab379d62
...@@ -25,12 +25,12 @@ add_conservation(Conservation) :- ...@@ -25,12 +25,12 @@ add_conservation(Conservation) :-
caption might not be correct.' caption might not be correct.'
), ),
doc( doc(
'When added, the conservation law will be checked against the rules (i.e. 'When added, the conservation law will be checked against the reactions
purely from stoichiometry), if that fails against the kinetics. Since (i.e. purely from stoichiometry), if that fails against the kinetics.
these checks are not complete, even a failure will be accepted with a Since these checks are not complete, even a failure will be accepted with
warning.' a warning.'
), ),
solution_to_list(Conservation, C), solution_to_conservation(Conservation, C),
add_item(conservation, C). add_item(conservation, C).
...@@ -38,7 +38,7 @@ delete_conservation(Conservation) :- ...@@ -38,7 +38,7 @@ delete_conservation(Conservation) :-
biocham_command, biocham_command,
type(Conservation, solution), type(Conservation, solution),
doc('removes the given mass conservation law.'), doc('removes the given mass conservation law.'),
solution_to_list(Conservation, C), solution_to_conservation(Conservation, C),
find_item([model: current_model, id: Id, type: conservation, item: C]), find_item([model: current_model, id: Id, type: conservation, item: C]),
delete_item(Id). delete_item(Id).
...@@ -46,10 +46,9 @@ delete_conservation(Conservation) :- ...@@ -46,10 +46,9 @@ delete_conservation(Conservation) :-
delete_conservations :- delete_conservations :-
biocham_command, biocham_command,
doc('removes all mass conservation laws.'), doc('removes all mass conservation laws.'),
\+ ( forall(
item([model: current_model, kind: conservation, id: Id]), item([model: current_model, kind: conservation, id: Id]),
delete_item(Id), delete_item(Id)
fail
). ).
...@@ -62,10 +61,13 @@ list_conservations :- ...@@ -62,10 +61,13 @@ list_conservations :-
check_conservations :- check_conservations :-
biocham_command, biocham_command,
doc( doc(
'checks all conservation laws against rules, and if necessary kinetics 'checks all conservation laws against reactions, and if necessary kinetics
(see also \\command{add_conservation/1}).' (see also \\command{add_conservation/1}).'
), ),
true. forall(
item([model: current_model, kind: conservation, item: C]),
check_conservation(C)
).
search_conservations :- search_conservations :-
...@@ -85,4 +87,63 @@ search_conservations(Integer) :- ...@@ -85,4 +87,63 @@ search_conservations(Integer) :-
true. true.
%%% End of public API
solution_to_conservation(Solution, Conservation) :-
devdoc('transforms a solution to a factorized list of coeff*object'),
reaction_editor:solution_to_list(Solution, List),
reaction_editor:simplify_solution(List, Conservation).
check_conservation(C) :-
(
check_conserv_reactions(C)
->
message('~w checked from reactions', [C])
;
check_conserv_num(C)
->
message('~w checked from kinetics', [C])
;
message('~w trusted but not checked', [C]),
throw(error(unchecked_conservation_law(C)))
).
check_conserv_reactions(C) :-
forall(
(
item([model: current_model, kind: reaction, item: Item]),
reaction(Item, _Kinetics, Reactants, Products)
),
(
scalar_conservation(Reactants, C, Moiety),
scalar_conservation(Products, C, Moiety)
)
).
check_conserv_num(_) :-
fail.
scalar_conservation([], _, 0).
scalar_conservation([K*M | L], C, Total) :-
(
member(I*M, C)
->
true
;
I = 0
),
scalar_conservation(L, C, T1),
Total is I*K + T1.
message(M, O) :-
format(M, O),
nl.
% vi: set ts=2 sts=2 sw=2 % vi: set ts=2 sts=2 sw=2
...@@ -21,7 +21,7 @@ test( ...@@ -21,7 +21,7 @@ test(
test( test(
'delete_conservation deletes the correct conservation', 'delete_conservation deletes the correct conservation',
[ [
setup((models:clear_model, add_some_conservations)), setup(set_some_conservations),
cleanup(models:clear_model), cleanup(models:clear_model),
true(Conservations == [[1*'a-a', 2*'a'], [1*'c-c', 2*'c']]) true(Conservations == [[1*'a-a', 2*'a'], [1*'c-c', 2*'c']])
] ]
...@@ -33,7 +33,7 @@ test( ...@@ -33,7 +33,7 @@ test(
test( test(
'delete_conservations deletes all conservations', 'delete_conservations deletes all conservations',
[ [
setup((models:clear_model, add_some_conservations)), setup(set_some_conservations),
cleanup(models:clear_model), cleanup(models:clear_model),
true(Conservations == []) true(Conservations == [])
] ]
...@@ -42,18 +42,30 @@ test( ...@@ -42,18 +42,30 @@ test(
all_items([model: current_model, kind: conservation], Conservations). all_items([model: current_model, kind: conservation], Conservations).
test(
'list_conservations lists all conservations',
[
setup(set_some_conservations),
cleanup(models:clear_model),
true(Conservations ==
'[0] [1*a-a,2*a]\n[1] [1*b-b,2*b]\n[2] [1*c-c,2*c]\n')
]
) :-
with_output_to(atom(Conservations), command(list_conservations)).
test( test(
'check_conservations is ok with trivial P-invariants', 'check_conservations is ok with trivial P-invariants',
[ [
setup(models:clear_model), setup(models:clear_model),
cleanup(models:clear_model), cleanup(models:clear_model)
blocked(not_implemented)
] ]
) :- ) :-
% not written 'a => b' to allow flycheck by separate compilation… % not written 'a => b' to allow flycheck by separate compilation…
command(add_reaction('=>'(a, b))), command(add_reaction('=>'(a, b))),
command(add_reaction('=>'(b, a))),
command(add_conservation(a + b)), command(add_conservation(a + b)),
command(check_conservations). with_output_to(atom(_), command(check_conservations)).
test( test(
...@@ -61,17 +73,18 @@ test( ...@@ -61,17 +73,18 @@ test(
[ [
setup(models:clear_model), setup(models:clear_model),
cleanup(models:clear_model), cleanup(models:clear_model),
error(unproven_conservation_law([1*'a', 2*'b'])), throws(error(unchecked_conservation_law([1*'a', 2*'b'])))
fixme(not_implemented)
] ]
) :- ) :-
% not written 'a => b' to allow flycheck by separate compilation… % not written 'a => b' to allow flycheck by separate compilation…
command(add_reaction('=>'(2*a, b))),
command(add_reaction('=>'(a, b))), command(add_reaction('=>'(a, b))),
command(add_conservation(a + 2*b)), command(add_conservation(a + 2*b)),
command(check_conservations). with_output_to(atom(_), command(check_conservations)).
add_some_conservations :- set_some_conservations :-
models:clear_model,
command(add_conservation(a-a + 2*a)), command(add_conservation(a-a + 2*a)),
command(add_conservation(b-b + 2*b)), command(add_conservation(b-b + 2*b)),
command(add_conservation(c-c + 2*c)). command(add_conservation(c-c + 2*c)).
......
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