gsl.pl 9.05 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1
:- module(
Thierry Martinez's avatar
Thierry Martinez committed
2
  gsl,
Thierry Martinez's avatar
Thierry Martinez committed
3 4 5 6
  [
    solve/2
  ]).

Thierry Martinez's avatar
SBML  
Thierry Martinez committed
7

Thierry Martinez's avatar
Thierry Martinez committed
8 9
:- use_module(library(csv)).

Thierry Martinez's avatar
SBML  
Thierry Martinez committed
10

Thierry Martinez's avatar
Thierry Martinez committed
11 12 13
:- devdoc('\\section{Public API}').


Thierry Martinez's avatar
Thierry Martinez committed
14 15 16 17 18 19 20 21 22 23 24 25
solve(Options, Table) :-
  \+ memberchk(jacobian: _, Options),
  !,
  memberchk(equations: Equations, Options),
  jacobian(Equations, Jacobian),
  solve([jacobian: Jacobian | Options], Table).

solve(Options, Table) :-
  CFilename = 'ode.inc',
  ExecutableFilename = 'ode',
  write_gsl_file(CFilename, Options),
  compile_c_program(ExecutableFilename),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
26
  call_subprocess(ExecutableFilename, [], []),
Thierry Martinez's avatar
Thierry Martinez committed
27 28 29
  csv_read_file('ode.csv', Table).


Thierry Martinez's avatar
Thierry Martinez committed
30 31 32
:- devdoc('\\section{Private predicates}').


Thierry Martinez's avatar
Thierry Martinez committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
write_gsl_file(CFilename, Options) :-
  setup_call_cleanup(
      open(CFilename, write, Stream),
      write_gsl_stream(Stream, Options),
      close(Stream)
  ).

write_gsl_stream(Stream, Options) :-
  with_output_to(
      Stream,
      write_gsl(Options)
  ).

write_gsl(Options) :-
  write_headers(Options),
  write_equations(Options),
  write_jacobian(Options),
  write_initial_values(Options),
  write_initial_parameter_values(Options),
Thierry Martinez's avatar
Thierry Martinez committed
52
  write_events(Options),
Thierry Martinez's avatar
Thierry Martinez committed
53
  write_print_headers(Options),
Thierry Martinez's avatar
Thierry Martinez committed
54 55 56 57 58 59 60
  write_print(Options).

write_headers(Options) :-
  memberchk(equations: Equations, Options),
  length(Equations, VariableCount),
  memberchk(initial_parameter_values: InitialParameterValues, Options),
  length(InitialParameterValues, ParameterCount),
Thierry Martinez's avatar
Thierry Martinez committed
61 62 63 64 65 66 67
  (
    memberchk(events: Events, Options)
  ->
    length(Events, EventCount)
  ;
    EventCount = 0
  ),
Thierry Martinez's avatar
Thierry Martinez committed
68 69
  write_header('VARIABLE_COUNT', '~d', VariableCount),
  write_header('PARAMETER_COUNT', '~d', ParameterCount),
Thierry Martinez's avatar
Thierry Martinez committed
70
  write_header('EVENT_COUNT', '~d', EventCount),
71
  write_option('METHOD', 'gsl_odeiv2_step_~a', method, Options),
72 73
  write_option('ERROR_EPSILON_ABSOLUTE', '~f', error_epsilon_absolute, Options),
  write_option('ERROR_EPSILON_RELATIVE', '~f', error_epsilon_relative, Options),
Thierry Martinez's avatar
Thierry Martinez committed
74
  write_option('INITIAL_STEP_SIZE', '~f', initial_step_size, Options),
75
  write_option('MAXIMUM_STEP_SIZE', '~f', maximum_step_size, Options),
Thierry Martinez's avatar
Thierry Martinez committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  write_option('TIME_INITIAL', '~f', time_initial, Options),
  write_option('TIME_FINAL', '~f', time_final, Options).

write_header(Variable, Format, Value) :-
  format('#define ~a ', [Variable]),
  format(Format, [Value]),
  nl.

write_option(Variable, Format, Option, Options) :-
  memberchk(Option: Value, Options),
  write_header(Variable, Format, Value).

write_equations(Options) :-
  memberchk(equations: Equations, Options),
  write('
int
functions(double t, const double x[], double f[], void *data)
{
  double *p = data;
'),
  \+ (
    nth0(I, Equations, Equation),
    \+ (
      write_equation(I, Equation)
    )
  ),
  write('  \c
  return GSL_SUCCESS;
}
').

Thierry Martinez's avatar
Thierry Martinez committed
107

Thierry Martinez's avatar
Thierry Martinez committed
108 109 110 111 112
write_equation(I, Equation) :-
  format('  f[~d] = ', [I]),
  write_arithmetic_expression(Equation),
  write(';\n').

Thierry Martinez's avatar
Thierry Martinez committed
113

Thierry Martinez's avatar
Thierry Martinez committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
write_jacobian(Options) :-
  memberchk(jacobian: Jacobian, Options),
  length(Jacobian, VariableCount),
  format('
int
jacobian(double t, const double x[], double *dfdx, double dfdt[], void *data)
{
  double *p = data;
  gsl_matrix_view dfdx_mat = gsl_matrix_view_array(dfdx, ~d, ~d);
  gsl_matrix *m = &dfdx_mat.matrix;
', [VariableCount, VariableCount]),
  \+ (
    nth0(I, Jacobian, JacobianLine),
    nth0(J, JacobianLine, Expression),
    \+ (
      write_jacobian_cell(I, J, Expression)
    )
  ),
  write('  \c
  dfdt[0] = 0.0;
  dfdt[1] = 0.0;
  return GSL_SUCCESS;
}
').

Thierry Martinez's avatar
Thierry Martinez committed
139

Thierry Martinez's avatar
Thierry Martinez committed
140 141 142 143 144
write_jacobian_cell(I, J, Expression) :-
  format('  gsl_matrix_set(m, ~d, ~d, ', [I, J]),
  write_arithmetic_expression(Expression),
  write(');\n').

Thierry Martinez's avatar
Thierry Martinez committed
145 146 147
write_arithmetic_expression(Value) :-
  number(Value),
  !,
Thierry Martinez's avatar
Thierry Martinez committed
148
  format('~f', [Value]).
Thierry Martinez's avatar
Thierry Martinez committed
149 150

write_arithmetic_expression([VariableIndex]) :-
Thierry Martinez's avatar
Thierry Martinez committed
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  format('x[~d]', [VariableIndex]).

write_arithmetic_expression(p(ParameterIndex)) :-
  format('p[~d]', [ParameterIndex]).

write_arithmetic_expression(- A) :-
  write('- '),
  write_arithmetic_expression_with_parentheses(A).

write_arithmetic_expression(A + B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' + '),
  write_arithmetic_expression_with_parentheses(B).

write_arithmetic_expression(A - B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' - '),
  write_arithmetic_expression_with_parentheses(B).

write_arithmetic_expression(A * B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' * '),
  write_arithmetic_expression_with_parentheses(B).

write_arithmetic_expression(A / B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' / '),
  write_arithmetic_expression_with_parentheses(B).

write_arithmetic_expression(A ^ B) :-
  write('pow('),
  write_arithmetic_expression(A),
  write(', '),
  write_arithmetic_expression(B),
  write(')').

write_arithmetic_expression(sqrt(A)) :-
  write('sqrt('),
  write_arithmetic_expression(A),
  write(')').

write_arithmetic_expression(cos(A)) :-
  write('cos('),
  write_arithmetic_expression(A),
  write(')').

write_arithmetic_expression(sin(A)) :-
  write('sin('),
  write_arithmetic_expression(A),
  write(')').

write_arithmetic_expression(exp(A)) :-
  write('exp('),
  write_arithmetic_expression(A),
  write(')').

write_arithmetic_expression(log(A)) :-
  write('log('),
  write_arithmetic_expression(A),
  write(')').

write_arithmetic_expression_with_parentheses(Expression) :-
  write('('),
  write_arithmetic_expression(Expression),
  write(')').


Thierry Martinez's avatar
Thierry Martinez committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
write_condition(A = B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' == '),
  write_arithmetic_expression_with_parentheses(B).

write_condition(A < B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' < '),
  write_arithmetic_expression_with_parentheses(B).

write_condition(A > B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' > '),
  write_arithmetic_expression_with_parentheses(B).

write_condition(A <= B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' <= '),
  write_arithmetic_expression_with_parentheses(B).

write_condition(A >= B) :-
  write_arithmetic_expression_with_parentheses(A),
  write(' >= '),
  write_arithmetic_expression_with_parentheses(B).

write_condition(true) :-
  write('true').

write_condition(false) :-
  write('false').

write_condition(A or B) :-
  write_condition_with_parentheses(A),
  write(' || '),
  write_condition_with_parentheses(B).

write_condition(A and B) :-
  write_condition_with_parentheses(A),
  write(' && '),
  write_condition_with_parentheses(B).

write_condition(not(A)) :-
  write('!'),
  write_condition_with_parentheses(A).


write_condition_with_parentheses(Condition) :-
  write('('),
  write_condition(Condition),
  write(')').


Thierry Martinez's avatar
Thierry Martinez committed
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
write_initial_values(Options) :-
  memberchk(initial_values: InitialValues, Options),
  write('
void
initial_values(double x[])
{
'),
  \+ (
    nth0(VariableIndex, InitialValues, InitialValue),
    \+ (
      format('  x[~d] = ~f;\n', [VariableIndex, InitialValue])
    )
  ),
  write('\c
}
').


write_initial_parameter_values(Options) :-
  memberchk(initial_parameter_values: InitialParameterValues, Options),
  write('
void
initial_parameter_values(double p[])
{
'),
  \+ (
    nth0(ParameterIndex, InitialParameterValues, ParameterValue),
    \+ (
      format('  p[~d] = ~f;\n', [ParameterIndex, ParameterValue])
    )
  ),
  write('\c
}
').


Thierry Martinez's avatar
Thierry Martinez committed
306 307 308 309 310 311 312 313 314 315
write_events(Options) :-
  (
    memberchk(events: Events, Options)
  ->
    true
  ;
    Events = []
  ),
  write('
void
Thierry Martinez's avatar
Thierry Martinez committed
316
events(int e[], double p[], double x[])
Thierry Martinez's avatar
Thierry Martinez committed
317 318 319
{
'),
  \+ (
Thierry Martinez's avatar
Thierry Martinez committed
320
    nth0(EventIndex, Events, (Condition -> ParameterValues)),
Thierry Martinez's avatar
Thierry Martinez committed
321 322 323
    \+ (
      format('  if (!e[~d] && (', [EventIndex]),
      write_condition(Condition),
Thierry Martinez's avatar
Thierry Martinez committed
324 325
      format(
        ')) {
Thierry Martinez's avatar
Thierry Martinez committed
326 327 328 329 330 331 332 333
    e[~d] = 1;',
        [EventIndex]
      ),
      \+ (
        member(ParameterIndex = Value, ParameterValues),
        \+ (
          format(
            '
Thierry Martinez's avatar
Thierry Martinez committed
334
    p[~d] = ',
Thierry Martinez's avatar
Thierry Martinez committed
335 336 337 338 339
            [ParameterIndex]
          ),
          write_arithmetic_expression(Value),
          write(';')
        )
Thierry Martinez's avatar
Thierry Martinez committed
340 341
      ),
      format(
Thierry Martinez's avatar
Thierry Martinez committed
342
        '
Thierry Martinez's avatar
Thierry Martinez committed
343
  }
Thierry Martinez's avatar
Thierry Martinez committed
344 345 346
  else if (e[~d] && !(',
        [EventIndex]
      ),
Thierry Martinez's avatar
Thierry Martinez committed
347
      write_condition(Condition),
Thierry Martinez's avatar
Thierry Martinez committed
348 349
      format(
        ')) {
Thierry Martinez's avatar
Thierry Martinez committed
350 351
    e[~d] = 0;
  }
Thierry Martinez's avatar
Thierry Martinez committed
352 353 354
',
        [EventIndex]
      )
Thierry Martinez's avatar
Thierry Martinez committed
355 356 357 358 359 360 361
    )
  ),
  write('\c
}
').


Thierry Martinez's avatar
Thierry Martinez committed
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
write_print_headers(Options) :-
  (
    memberchk(headers: Headers, Options)
  ->
    write('
void
print_headers(FILE *csv)
{
  fprintf(csv, "#Time'),
    \+ (
      member(Header, Headers),
      \+ (
        format(',\\"~a\\"', [Header])
      )
    ),
    write('\\n"'),
    write(');
}
')
  ;
    write('
void
print_headers(FILE *csv)
{
}
')
  ).


Thierry Martinez's avatar
Thierry Martinez committed
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
write_print(Options) :-
  memberchk(equations: Equations, Options),
  length(Equations, VariableCount),
  VariableMax is VariableCount - 1,
  memberchk(precision: Precision, Options),
  format('
void
print(FILE *csv, double t, double x[])
{
  fprintf(csv, "%.~de', [Precision]),
  \+ (
    between(1, VariableCount, _),
    \+ (
      format(',%.~de', [Precision])
    )
  ),
  write('\\n", t'),
  \+ (
    between(0, VariableMax, VariableIndex),
    \+ (
      format(', x[~d]', [VariableIndex])
    )
  ),
  write(');
}
').


compile_c_program(ExecutableFilename) :-
Thierry Martinez's avatar
Thierry Martinez committed
420 421
  library_path(LibraryPath),
  absolute_file_name('gsl_solver.c', GslSolverC, [relative_to(LibraryPath)]),
Thierry Martinez's avatar
Thierry Martinez committed
422 423
  call_subprocess(
      path(gcc),
Thierry Martinez's avatar
FOLTL  
Thierry Martinez committed
424 425 426
      ['-o', ExecutableFilename, '-I.', GslSolverC,
       '-lgsl', '-lgslcblas', '-lm'],
      []).