Commit 4de85abb authored by HEMERY Mathieu's avatar HEMERY Mathieu
Browse files

Mainly reflow + separation of eval_coeff with rw_coeff and eval_state

parent 9cdf8bdc
......@@ -9,6 +9,9 @@
normalize_number/2
]).
% Insert here for separate compilation and linting
:- use_module(doc).
:- use_module(util).
:- dynamic(canonical/3).
......
......@@ -4,10 +4,10 @@
]
).
% Only for separate compilation/linting
% Only for separate compilation and linting
:- use_module(doc).
:- use_module(arithmetic_rules). % simplify,
:- use_module(util). % substitute,
:- use_module(util).
:- use_module(arithmetic_rules).
%! rosenbrock_init
%
......@@ -127,8 +127,8 @@ ode_solver([
% Sublist is the N first element of List
take(Size, List, Sublist) :-
length(Sublist, Size),
prefix(Sublist, List).
prefix(Sublist, List),
length(Sublist, Size).
%! initialize_functions_list
......@@ -234,53 +234,121 @@ update_functions_state(CurrentState,Time) :-
NewLastRow =.. [row,Time|NewCurrentState],
append(Table,[NewLastRow],NewTable),
nb_setval(numerical_table,NewTable).
%! eval_matrix(+Symbolic_Matrix, +Current_State, +Parameters, -Evaluated_Matrix)
%! eval_matrix(+Matrix, +States, +Parameters, -Evaluated_Matrix)
%
% Evaluates matrix with informations on current state of the simulation and parameters
% Current_State is the list of the species concentration at current time
% insert all the numerical value in a symbolic matrix
% States and Parameters are simple list of value
eval_matrix([],_,_,[]).
eval_matrix([Row|T],CurrentState,Parameters,[RowEvaluated|ET]) :-
eval_row(Row,CurrentState,Parameters,RowEvaluated),
eval_matrix(T,CurrentState,Parameters,ET).
eval_row(Row,CurrentState,Parameters,RowEvaluated),
eval_matrix(T,CurrentState,Parameters,ET).
eval_row([],_,_,[]).
eval_row([Element|T],CurrentState,Parameters,[ElementEvaluated|ET]) :-
eval_coeff(Element,CurrentState,Parameters,_,ElementEvaluated),!,
eval_row([Element|T],CurrentState,Parameters,[Evaluation|ET]) :-
eval_coeff(Element, CurrentState, Parameters, _, Evaluation),
eval_row(T,CurrentState,Parameters,ET).
eval_coeff(Expr, CurrentState, Parameters, Time, Evaluation) :-
length(CurrentState, NSpecies),
generate_variable_index(NSpecies, IndexSpecies),
length(Parameters, Nparam),
generate_param_index(Nparam, IndexParameters),
nb_getval(hdid, StepSize),
append([IndexSpecies, IndexParameters, [t, random, infinity, step_size]], Index),
append([CurrentState, Parameters, [Time, random_float, inf, StepSize]], Values),
substitute(Index, Values, Expr, Expr_trans),
Evaluation is Expr_trans.
generate_variable_index(N, List) :-
Nm is N-1,
numlist(0,Nm,List_tempo),
put_bracket(List_tempo, List).
%! eval_coeff(+Expr, +States, +Parameters, +Time, -Evaluation)
%
% Evaluate an expression by rewritting the occurence of the concentration and parameters,
% simplyfying the resulting expression and calling is.
% The rewritting is delegated to rw_coeff
eval_coeff(Expr, States, Parameters, Time, Evaluation) :-
rw_coeff(Expr, States, Parameters, Time, Rw_expr),!,
simplify(Rw_expr, Simple_expr), % may be skipped, to check with better tests on rail
Evaluation is Simple_expr.
rw_coeff(Element,_,_,_,Element) :-
number(Element),!.
rw_coeff(Element1 ^ Element2,CurrentState,Parameters,Time,ElementEvaluated1 ^ ElementEvaluated2) :-
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(Element1 + Element2,CurrentState,Parameters,Time,ElementEvaluated1 + ElementEvaluated2) :-
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(Element1 - Element2,CurrentState,Parameters,Time,ElementEvaluated1 - ElementEvaluated2) :-
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(Element1 * Element2,CurrentState,Parameters,Time,ElementEvaluated1 * ElementEvaluated2) :-
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(Element1 / Element2,CurrentState,Parameters,Time,ElementEvaluated1 / ElementEvaluated2) :-
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(-Element,CurrentState,Parameters,Time,-ElementEvaluated) :-
rw_coeff(Element,CurrentState,Parameters,Time,ElementEvaluated).
rw_coeff([IndexVariable],CurrentState,_,_,ElementEvaluated) :-
nth0(IndexVariable,CurrentState,ElementEvaluated).
rw_coeff(p(ParameterIndex),CurrentState,Parameters,Time,ElementEvaluated) :-
nth0(ParameterIndex,Parameters,Element2),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated).
rw_coeff(floor(Element),CurrentState,Parameters,Time,Result) :-
!,
rw_coeff(Element,CurrentState,Parameters,Time,ElementEval),
Result is floor(ElementEval).
rw_coeff(random,_,_,_,Result) :-
!,Result is random_float.
rw_coeff(infinity,_,_,_,Result) :-
!,Result is inf.
put_bracket([], []) :- !.
put_bracket([Head|Tail], [[Head]|BTail]) :-
put_bracket(Tail, BTail).
rw_coeff(t,_,_,Time,Time) :-
!.
generate_param_index(N, List) :-
Nm is N-1,
numlist(0,Nm,List_tempo),
put_parenthesis(List_tempo, List).
rw_coeff(step_size,_,_,_,Result) :-
!,
nb_getval(hdid,Result).
put_parenthesis([], []) :- !.
put_parenthesis([Head|Tail], [p(Head)|PTail]) :-
put_parenthesis(Tail, PTail).
rw_coeff(min(Element1, Element2),CurrentState,Parameters,Time,min(ElementEvaluated1,ElementEvaluated2)) :-
!,
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(max(Element1, Element2),CurrentState,Parameters,Time,max(ElementEvaluated1,ElementEvaluated2)) :-
!,
rw_coeff(Element1,CurrentState,Parameters,Time,ElementEvaluated1),
rw_coeff(Element2,CurrentState,Parameters,Time,ElementEvaluated2).
rw_coeff(log(Element),CurrentState,Parameters,Time,log(ElementEvaluated)) :-
!,
rw_coeff(Element,CurrentState,Parameters,Time,ElementEvaluated).
rw_coeff(exp(Element),CurrentState,Parameters,Time,exp(ElementEvaluated)) :-
!,
rw_coeff(Element,CurrentState,Parameters,Time,ElementEvaluated).
rw_coeff(cos(Element),CurrentState,Parameters,Time,cos(ElementEvaluated)) :-
!,
rw_coeff(Element,CurrentState,Parameters,Time,ElementEvaluated).
rw_coeff(sin(Element),CurrentState,Parameters,Time,sin(ElementEvaluated)) :-
!,
rw_coeff(Element,CurrentState,Parameters,Time,ElementEvaluated).
%! eval_state(+State, +Parameters, -Evaluated_state)
%
% evaluate the parameter in a state
eval_state(State, Parameters, Evaluated) :-
eval_row(State, [], Parameters, Evaluated).
/*
eval_state([],_,[]).
eval_state([p(ParameterIndex)|Elements],Parameters,[NewElement|NewElements]) :-
......@@ -289,6 +357,7 @@ eval_state([p(ParameterIndex)|Elements],Parameters,[NewElement|NewElements]) :-
eval_state([Element|Elements],Parameters,[Element|NewElements]) :-
eval_state(Elements,Parameters,NewElements).
*/
/* This a tool for the rosenbrock method */
......@@ -782,7 +851,11 @@ handle_rosenbrock_events(State,Time) :-
)
).*/
/* Check if the condition to fire an event is true (can elso be used if conditional changes in events) */
%! test_condition(+State, +Time, +Condition, +Result)
%
% Check if the condition to fire an event is true
% Can also be used if conditional changes in events
test_condition(State,Time,Condition,Result) :-
Condition =.. [Op, A, B],
......@@ -832,7 +905,7 @@ test_condition(State,Time,not Condition,Result) :-
test_condition(State,Time,Condition,Result) :-
nb_getval(parameters_list,Parameters),
eval_coeff(Condition,State,Parameters,Time,Result).
rw_coeff(Condition,State,Parameters,Time,Result).
test_condition(_,_,true,true).
......
......@@ -3,6 +3,7 @@
:- begin_tests(rosenbrock).
test(eva_coeff_1) :-
nb_setval(hdid,0.01),
rosenbrock:eval_coeff(1*[0],[1.5,2.5,3.5],[-1,2], 1, R1), R1 =:= 1.5,
rosenbrock:eval_coeff(2*[0] + p(1),[1.5,2.5,3.5],[-1,2], 1, R2), R2 =:= 5.0,
rosenbrock:eval_coeff(t,[1.5,2.5,3.5],[-1,2], 1.23, R3), R3 =:= 1.23,
......@@ -12,9 +13,10 @@ test(eva_coeff_1) :-
rosenbrock:eval_coeff([1]^p(1),[1.5,2.5,3.5],[-1,2], 1, R7), R7 =:= 6.25.
test(eva_coeff_2) :-
nb_setval(hdid,0.01),
rosenbrock:eval_coeff(floor(1*[0]),[1.5,2.5,3.5],[-1,2], 1, R1), R1 =:= 1,
rosenbrock:eval_coeff(min([0], p(1)),[1.5,2.5,3.5],[-1,2], 1, R2), R2 =:= 1.5,
rosenbrock:eval_coeff(infinity,[1.5,2.5,3.5],[-1,2], 1, R3), R3 =:= inf,
rosenbrock:eval_coeff(random,[1.5,2.5,3.5],[-1,2], 1, R4).
rosenbrock:eval_coeff(random,[1.5,2.5,3.5],[-1,2], 1, _R4).
:- end_tests(rosenbrock).
......@@ -46,6 +46,7 @@
]).
:- use_module(library(process)).
:- use_module(doc).
name_variables(L) :-
doc('
......
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