numerical_simulation.pl 4.06 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  numerical_simulation,
  [
Thierry Martinez's avatar
Thierry Martinez committed
4 5
    numerical_simulation/0,
    numerical_simulation/1
Thierry Martinez's avatar
Thierry Martinez committed
6 7 8 9 10 11
  ]
).


numerical_simulation :-
  biocham_command,
Thierry Martinez's avatar
Thierry Martinez committed
12
  doc('performs a numerical simulation up to 20s.'),
Thierry Martinez's avatar
Thierry Martinez committed
13
  numerical_simulation(20).
Thierry Martinez's avatar
Thierry Martinez committed
14

Thierry Martinez's avatar
Thierry Martinez committed
15

Thierry Martinez's avatar
Thierry Martinez committed
16 17 18 19
numerical_simulation(Time) :-
  biocham_command,
  type(Time, time),
  doc('performs a numerical simulation up to a given time.'),
Thierry Martinez's avatar
Thierry Martinez committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  compute_ode,
  make_ode_system,
  solve.


make_ode_system :-
  enumerate_variables,
  convert_ode.


solve :-
  gather_equations(Equations),
  gather_initial_values(InitialValues),
  gather_initial_parameter_values(InitialParameterValues),
  Options = [
    equations: Equations,
    initial_values: InitialValues,
    initial_parameter_values: InitialParameterValues,
    method: gsl_odeiv2_step_rk8pd,
    error_epsilon: 1e-6,
    initial_step_size: 1e-6,
    precision: 5,
    time_initial: 0,
    time_final: 100
  ],
  solve(Options, Table),
Thierry Martinez's avatar
Thierry Martinez committed
46
  add_trace('numerical_simulation', Table).
Thierry Martinez's avatar
Thierry Martinez committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68


gather_equations(Equations) :-
  peek_count(variable_counter, VariableCount),
  VariableMax is VariableCount - 1,
  findall(
    Equation,
    (
      between(0, VariableMax, VariableIndex),
      equation(VariableIndex, Equation)
    ),
    Equations
  ).


gather_initial_values(InitialValues) :-
  peek_count(variable_counter, VariableCount),
  VariableMax is VariableCount - 1,
  findall(
    InitialValue,
    (
      between(0, VariableMax, VariableIndex),
Thierry Martinez's avatar
Thierry Martinez committed
69
      variable(Molecule, VariableIndex),
Thierry Martinez's avatar
NuSMV  
Thierry Martinez committed
70
      get_initial_concentration(Molecule, InitialValue)
Thierry Martinez's avatar
Thierry Martinez committed
71 72 73 74 75 76 77 78 79 80 81 82
    ),
    InitialValues
  ).


gather_initial_parameter_values(InitialParameterValues) :-
  peek_count(parameter_counter, ParameterCount),
  ParameterMax is ParameterCount - 1,
  findall(
    InitialParameterValue,
    (
      between(0, ParameterMax, ParameterIndex),
Thierry Martinez's avatar
Thierry Martinez committed
83
      parameter(Parameter, ParameterIndex),
Thierry Martinez's avatar
Thierry Martinez committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
      get_parameter_value(Parameter, InitialParameterValue)
    ),
    InitialParameterValues
  ).


:- dynamic(variable/2).


enumerate_variables :-
  retractall(variable(_, _)),
  set_counter(variable_counter, 0),
  \+ (
    ode(Molecule, _),
    \+ (
      count(variable_counter, VariableIndex),
      assertz(variable(Molecule, VariableIndex))
    )
  ).


:- dynamic(equation/2).


:- dynamic(parameter/2).


convert_ode :-
  retractall(equation(_, _)),
  set_counter(parameter_counter, 0),
  \+ (
    ode(Molecule, Expression),
    \+ (
      variable(Molecule, VariableIndex),
      convert_expression(Expression, IndexedExpression),
      assertz(equation(VariableIndex, IndexedExpression))
    )
  ).


convert_expression(Value, Value) :-
  number(Value),
  !.

Thierry Martinez's avatar
Thierry Martinez committed
128
convert_expression([Molecule], [VariableIndex]) :-
Thierry Martinez's avatar
Thierry Martinez committed
129 130 131 132
  !,
  variable(Molecule, VariableIndex).

convert_expression(Parameter, p(ParameterIndex)) :-
Thierry Martinez's avatar
Thierry Martinez committed
133
  atom(Parameter),
Thierry Martinez's avatar
Thierry Martinez committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
  !,
  (
    parameter(Parameter, ParameterIndex)
  ->
    true
  ;
    count(parameter_counter, ParameterIndex),
    assertz(parameter(Parameter, ParameterIndex))
  ).

convert_expression(- A, - AIndexed) :-
  !,
  convert_expression(A, AIndexed).

convert_expression(A + B, AIndexed + BIndexed) :-
  !,
  convert_expression(A, AIndexed),
  convert_expression(B, BIndexed).

convert_expression(A - B, AIndexed - BIndexed) :-
  !,
  convert_expression(A, AIndexed),
  convert_expression(B, BIndexed).

convert_expression(A * B, AIndexed * BIndexed) :-
  !,
  convert_expression(A, AIndexed),
  convert_expression(B, BIndexed).

convert_expression(A / B, AIndexed / BIndexed) :-
  !,
  convert_expression(A, AIndexed),
  convert_expression(B, BIndexed).

convert_expression(A ^ B, AIndexed ^ BIndexed) :-
  !,
  convert_expression(A, AIndexed),
  convert_expression(B, BIndexed).

convert_expression(sqrt(A), sqrt(AIndexed)) :-
  !,
  convert_expression(A, AIndexed).

convert_expression(exp(A), exp(AIndexed)) :-
  !,
  convert_expression(A, AIndexed).

convert_expression(log(A), log(AIndexed)) :-
  !,
  convert_expression(A, AIndexed).

convert_expression(cos(A), cos(AIndexed)) :-
  !,
  convert_expression(A, AIndexed).

convert_expression(sin(A), sin(AIndexed)) :-
  !,
  convert_expression(A, AIndexed).

convert_expression(E, _Result) :-
  type_error(arithmetic_expression, E).