void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* Initialize the seeder */ srand(time(NULL)); /* Fetch inputs */ const int T = (int) (mxGetScalar(prhs[0])); /* Duration of simulation in s */ const int Time = (T+onset)*res; /* Total number of iteration steps */ double* Param_Cortex = mxGetPr (prhs[1]); /* Parameters of cortical module */ double* Param_Thalamus = mxGetPr (prhs[2]); /* Parameters of thalamic module */ double* Connections = mxGetPr (prhs[3]); /* Connectivity values C <-> T */ double* var_stim = mxGetPr (prhs[4]); /* Parameters of stimulation protocol */ /* Initialize the populations */ Cortical_Column Cortex = Cortical_Column(Param_Cortex, Connections); Thalamic_Column Thalamus = Thalamic_Column(Param_Thalamus, Connections); /* Link both modules */ Cortex.get_Thalamus(Thalamus); Thalamus.get_Cortex(Cortex); /* Initialize the stimulation protocol */ Stim Stimulation(Cortex, Thalamus, var_stim); /* Create data containers */ std::vector<mxArray*> dataArray; dataArray.reserve(4); dataArray.push_back(GetMexArray(1, T*res/red)); // Vt dataArray.push_back(GetMexArray(1, T*res/red)); // Vr dataArray.push_back(GetMexArray(1, T*res/red)); // Ca dataArray.push_back(GetMexArray(1, T*res/red)); // act_h /* Pointer to the data blocks */ std::vector<double*> dataPointer; dataPointer.reserve(dataArray.size()); for (mxArray* dataptr : dataArray) { dataPointer.push_back(mxGetPr(dataptr)); } /* Simulation */ int count = 0; for (unsigned t=0; t < Time; ++t) { ODE (Cortex, Thalamus); Stimulation.check_stim(t); if(t >= onset*res && t%red == 0){ get_data(count, Cortex, Thalamus, dataPointer); ++count; } } /* Return the data containers */ size_t numOutputs = 0; for (mxArray* dataptr : dataArray) { plhs[numOutputs++] = dataptr; } plhs[numOutputs++] = get_marker(Stimulation); return; }
/* Numerical integration of the ODE using 2nd order Runge Kutta. Returns length^2 of the update, so that we can detect if the step size needs reducing. */ static double Iterate(dvector *p, dvector(*ODE)(Par par, double x, double y, double z), Par par, double step) { dvector k1, k2, k3; k1 = ODE(par, p->x, p->y, p->z); k1.x *= step; k1.y *= step; k1.z *= step; k2 = ODE(par, p->x + k1.x, p->y + k1.y, p->z + k1.z); k2.x *= step; k2.y *= step; k2.z *= step; k3.x = (k1.x + k2.x) / 2.0; k3.y = (k1.y + k2.y) / 2.0; k3.z = (k1.z + k2.z) / 2.0; p->x += k3.x; p->y += k3.y; p->z += k3.z; return k3.x*k3.x + k3.y*k3.y + k3.z*k3.z; }