[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Help-mcsim] #include directives in Inline() code
From: |
Caroline Ring |
Subject: |
[Help-mcsim] #include directives in Inline() code |
Date: |
Wed, 22 Aug 2018 15:27:37 +0000 |
I would like to use interpolation methods from GSL (GNU Scientific Library)
(version 2.5) in MCSim (version 6.0.1). I have GSL installed, and MCSim knows
how to link against it (the “makemcsim” shell script includes flag -lgsl in the
gcc command). However, I still need to put the appropriate #include directive
into the model code, to include the appropriate GSL header file(s). It does not
seem that this can be done as part of an Inline() statement; the #include
directive appears to get stripped out during the process of generating the
model.c file. What is the correct way to do this? I can accomplish it by
manually editing the model.c file generated by “mod” to add the #include
directive, then by manually compiling and linking the model. Is there any
better way?
Here is a simple reproducible example. (I’m setting off code blocks with
“--------“; the dashes aren’t included in the actual code.) (For background, I
am on Mac OS High Sierra version 10.13.6.)
File interp_test.model contains the following:
--------
States = {y};
Outputs = {rate};
rate_tmp = 0.5;
Dynamics{
Inline(#include <gsl/gsl_interp.h>);
Inline(unsigned n = 4;); #number of points in interpolation table
Inline(double x[4] = {0.0, 3.1, 10.1, 50};); #time-values for interpolation
Inline(double y[4] = {2.0, 5.0, 0.5, 0.1};); #response-values for interpolation
#set up GSL linear interpolation, following example at
https://lists.gnu.org/archive/html/help-gsl/2007-06/msg00019.html
Inline(gsl_interp *interpolation = gsl_interp_alloc (gsl_interp_linear,n););
Inline(gsl_interp_init(interpolation, x, y, n););
Inline(gsl_interp_accel * accelerator = gsl_interp_accel_alloc(););
Inline(rate_tmp = gsl_interp_eval(interpolation, x, y, *pdTime, accelerator););
#perform interpolation at current simulation time
rate = rate_tmp; #assign interpolated rate value
dt(y) = rate;
}
End.
---------
If I try to compile this using
$ makemcsim interp_test.model
I receive a bunch of errors about “undeclared identifiers” for all of the
gsl_interp items. When I examine the model.c file, I do not see “#include
<gsl/gsl_interp.h>” anywhere within it. The rest of the inline code is in the
CalcDeriv() function block as expected.
However, if I remove the following line in the interp_test.model file:
----
Inline(#include <gsl/gsl_interp.h>);
-----
And then create the C code using
$ mod interp_test.model
and then manually edit the resulting model.c file to add the directive
---------
#include <gsl/gsl_interp.h>
---------
And then compile and link with the statement
$ gcc -O3 -I/usr/local/include -L/usr/local/lib -g -O2 model.c -lmcsim -o
interp_test.mcsim -lm -lgsl -lgslcblas -llapack -Wall;
(i.e. the same command included in the “makemcsim” shell script), then the
model compiles without errors, and I can run it using the following input file:
--------
OutputFile("interp_test.out");
Integrate(Lsodes, 1e-6, 20, 1);
Simulation{
PrintStep(rate, y, 0, 20, 1)
}
END.
--------
And I get the following output:
---------
Results of Simulation 1
Time rate y
0 2.73171 0
1 3.46341 3.09756
2 4.19512 6.92683
3 4.92683 11.4878
4 4.42143 16.0464
5 3.77857 19.976
6 3.13571 23.4331
7 2.49286 26.2474
8 1.85 28.4189
9 1.20714 29.9474
10 0.564286 30.8331
11 0.490977 31.3928
12 0.480952 31.8809
13 0.470927 32.3564
14 0.460902 32.8223
15 0.450877 33.2782
16 0.440852 33.724
17 0.430827 34.1599
18 0.420802 34.5857
19 0.410777 35.0015
20 0.400752 35.4072
--------
So it seems to work as expected if I manually edit and then manually compile
and link the model, but it does not work if I use “makemcsim” to automate the
process.
I have tried placing the line
--------
Inline(#include <gsl/gsl_interp.h>);
--------
outside the Dynamics block, but it still does not appear in the resulting
model.c file. It really seems like that line is getting stripped out, although
I can't identify where it is happening in the code for "mod". Am I missing
something, or is the manual-edit process really the correct way to do this?
Thank you,
Caroline Ring
- [Help-mcsim] #include directives in Inline() code,
Caroline Ring <=