Permalink
Newer
Older
100644 115 lines (102 sloc) 4.75 KB
Apr 24, 2014 @miscco Code cleanups.
1 /*
Feb 1, 2016 @miscco Updated the naming convention and switched to the Random_stream header
2 * Copyright (c) 2015 University of Lübeck
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 *
22 * AUTHORS: Michael Schellenberger Costa: mschellenbergercosta@gmail.com
23 *
24 * Based on: A thalamocortical neural mass model of the EEG during NREM sleep and its response
25 * to auditory stimulation.
26 * M Schellenberger Costa, A Weigenand, H-VV Ngo, L Marshall, J Born, T Martinetz,
27 * JC Claussen.
Mar 23, 2016 @miscco Fixed error in license and added option for noise free setting
28 * PLoS Computational Biology (in review).
Feb 1, 2016 @miscco Updated the naming convention and switched to the Random_stream header
29 */
Apr 24, 2014 @miscco Code cleanups.
30
Sep 13, 2016 @miscco Major cleanup and code modernization
31 /******************************************************************************/
32 /* Implementation of the simulation as MATLAB routine (mex compiler) */
33 /* mex command is given by: */
34 /* mex CXXFLAGS="\$CXXFLAGS -std=c++11 -O3" Thalamus_mex.cpp */
35 /* Thalamic_Column.cpp */
36 /******************************************************************************/
Dec 29, 2015 @miscco General code cleanup.
37 #include "mex.h"
38 #include "matrix.h"
Sep 13, 2016 @miscco Major cleanup and code modernization
39
40 #include <iterator>
41 #include <vector>
42
Dec 29, 2015 @miscco General code cleanup.
43 #include "Data_Storage.h"
Sep 13, 2016 @miscco Major cleanup and code modernization
44 #include "Thalamic_Column.h"
45 mxArray* GetMexArray(int N, int M);
Jan 16, 2013 @miscco Basic implementation of the model proposed in Steyn-Ross2009.
46
Sep 13, 2016 @miscco Major cleanup and code modernization
47 /******************************************************************************/
48 /* Fixed simulation settings */
49 /******************************************************************************/
50 extern const int onset = 20; /* Time until data is stored in s */
51 extern const int res = 1E4; /* Number of iteration steps per s */
52 extern const int red = 1E2; /* Number of iterations steps not saved */
53 extern const double dt = 1E3/res; /* Duration of a time step in ms */
54 extern const double h = sqrt(dt); /* Square root of dt for SRK iteration */
Jan 16, 2013 @miscco Basic implementation of the model proposed in Steyn-Ross2009.
55
Sep 13, 2016 @miscco Major cleanup and code modernization
56 /******************************************************************************/
57 /* Simulation routine */
58 /* lhs defines outputs */
59 /* rhs defines inputs */
60 /******************************************************************************/
Dec 29, 2015 @miscco General code cleanup.
61 void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
Sep 13, 2016 @miscco Major cleanup and code modernization
62 /* Initialize the seeder */
63 srand(time(NULL));
Dec 29, 2015 @miscco General code cleanup.
64
Sep 13, 2016 @miscco Major cleanup and code modernization
65 /* Fetch inputs */
66 const int T = (int) (mxGetScalar(prhs[0])); /* Duration of simulation in s */
67 const int Time = (T+onset)*res; /* Total number of iteration steps */
68 double* Param_Thalamus = mxGetPr (prhs[1]); /* Parameters of cortical module */
Dec 29, 2015 @miscco General code cleanup.
69
Sep 13, 2016 @miscco Major cleanup and code modernization
70 /* Initialize the populations */
71 Thalamic_Column Thalamus(Param_Thalamus);
Jan 16, 2013 @miscco Basic implementation of the model proposed in Steyn-Ross2009.
72
Sep 13, 2016 @miscco Major cleanup and code modernization
73 /* Create data containers */
74 std::vector<mxArray*> dataArray;
75 dataArray.reserve(3);
76 dataArray.push_back(GetMexArray(1, T*res/red)); // Vt
77 dataArray.push_back(GetMexArray(1, T*res/red)); // Vr
78 dataArray.push_back(GetMexArray(1, T*res/red)); // act_h
Apr 29, 2013 @miscco Cleaned up the code and structurized it
79
Sep 13, 2016 @miscco Major cleanup and code modernization
80 /* Pointer to the data blocks */
81 std::vector<double*> dataPointer;
82 dataPointer.reserve(dataArray.size());
83 for (auto &dataptr : dataArray) {
84 dataPointer.push_back(mxGetPr(dataptr));
85 }
Dec 29, 2015 @miscco General code cleanup.
86
Sep 13, 2016 @miscco Major cleanup and code modernization
87 /* Simulation */
88 int count = 0;
89 for (unsigned t=0; t < Time; ++t) {
90 Thalamus.iterate_ODE();
91 if(t >= onset*res && t%red == 0){
92 get_data(count, Thalamus, dataPointer);
93 ++count;
94 }
95 }
Dec 29, 2015 @miscco General code cleanup.
96
Sep 13, 2016 @miscco Major cleanup and code modernization
97 /* Return the data containers */
98 nlhs = dataArray.size()+1;
99 for (auto &dataptr : dataArray) {
100 plhs[std::distance(&dataptr, dataArray.data())] = dataptr;
101 }
102 return;
Jan 16, 2013 @miscco Basic implementation of the model proposed in Steyn-Ross2009.
103 }
Feb 1, 2016 @miscco Updated the naming convention and switched to the Random_stream header
104
Sep 13, 2016 @miscco Major cleanup and code modernization
105 /******************************************************************************/
106 /* Create MATLAB data containers */
107 /******************************************************************************/
108 mxArray* GetMexArray(int N, int M) {
109 mxArray* Array = mxCreateDoubleMatrix(0, 0, mxREAL);
110 mxSetM(Array, N);
111 mxSetN(Array, M);
112 mxSetData(Array, mxMalloc(sizeof(double)*M*N));
113 return Array;
Feb 1, 2016 @miscco Updated the naming convention and switched to the Random_stream header
114 }