From 1cba4bc319c9ce517108b6226c5f05aa41c477b8 Mon Sep 17 00:00:00 2001 From: Michael Schellenberger Costa Date: Tue, 29 Dec 2015 20:43:02 +0100 Subject: [PATCH] General code cleanup. Removed dependency on boost libraries. --- Cortical_Column.cpp | 4 +-- Cortical_Column.h | 17 ++-------- saves.h => Data_Storage.h | 0 NM_TC.pro | 15 +++++---- Random_Stream.h | 81 +++++++++++++++++++++++++++++++++++++++++++++++ Stimulation.h | 22 +++---------- TC.cpp | 3 -- TC_mex.cpp | 5 +-- Thalamic_Column.cpp | 4 +-- Thalamic_Column.h | 18 ++--------- 10 files changed, 103 insertions(+), 66 deletions(-) rename saves.h => Data_Storage.h (100%) create mode 100644 Random_Stream.h diff --git a/Cortical_Column.cpp b/Cortical_Column.cpp index 5956877..541c3ba 100644 --- a/Cortical_Column.cpp +++ b/Cortical_Column.cpp @@ -44,10 +44,10 @@ void Cortical_Column::set_RNG(void) { /* Create RNG for each stream */ for (int i=0; i #include -#include -#include -#include +#include "Random_Stream.h" #include "Thalamic_Column.h" using std::vector; - class Thalamic_Column; -/****************************************************************************************************/ -/* Typedefs for RNG */ -/****************************************************************************************************/ -typedef boost::mt11213b ENG; /* Mersenne Twister */ -typedef boost::normal_distribution DIST; /* Normal Distribution */ -typedef boost::variate_generator GEN; /* Variate generator */ -/****************************************************************************************************/ -/* end */ -/****************************************************************************************************/ - /****************************************************************************************************/ /* Macro for vector initialization */ @@ -131,7 +118,7 @@ class Cortical_Column { x = _INIT(0.0); /* derivative of y */ /* Random number generators */ - vector MTRands; + vector MTRands; /* Container for noise */ vector Rand_vars; diff --git a/saves.h b/Data_Storage.h similarity index 100% rename from saves.h rename to Data_Storage.h diff --git a/NM_TC.pro b/NM_TC.pro index 43414a5..24487b9 100644 --- a/NM_TC.pro +++ b/NM_TC.pro @@ -6,15 +6,16 @@ CONFIG -= qt TARGET = TC.cpp SOURCES += Cortical_Column.cpp \ - TC.cpp \ - TC_mex.cpp \ + TC.cpp \ + TC_mex.cpp \ Thalamic_Column.cpp -HEADERS += Cortical_Column.h \ - ODE.h \ - saves.h \ - Stimulation.h \ - Thalamic_Column.h +HEADERS += Cortical_Column.h \ + Data_Storage.h \ + ODE.h \ + Random_Stream.h \ + Stimulation.h \ + Thalamic_Column.h QMAKE_CXXFLAGS += -std=c++11 -O3 diff --git a/Random_Stream.h b/Random_Stream.h new file mode 100644 index 0000000..abd6c10 --- /dev/null +++ b/Random_Stream.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2015 University of Lübeck + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * AUTHORS: Michael Schellenberger Costa: mschellenbergercosta@gmail.com + * Stefanie Gareis: gareis@inb.uni-luebeck.de + * + */ + +/****************************************************************************************************/ +/* Random number streams */ +/****************************************************************************************************/ +#pragma once +#include + +/****************************************************************************************************/ +/* Struct for normal distribution */ +/****************************************************************************************************/ +struct random_stream_normal +{ + /* Random number engine: Mersenne-Twister */ + std::mt19937_64 mt; + /* Random number generator: Normal-distribution */ + std::normal_distribution norm_dist; + + /* Constructors */ + random_stream_normal(){} + random_stream_normal(double mean, double stddev) + : mt(rand()) , norm_dist(mean, stddev) + {} + + /* Overwrites the function-call operator "( )" */ + double operator( )(void) { + return norm_dist(mt); + } +}; +/****************************************************************************************************/ +/* end */ +/****************************************************************************************************/ + +/****************************************************************************************************/ +/* Struct for uniform int distribution */ +/****************************************************************************************************/ +struct random_stream_uniform_int +{ + /* Random number engine: Mersenne-Twister */ + std::mt19937_64 mt; + /* Random number generator: Uniform integer-distribution */ + std::uniform_int_distribution<> uniform_dist; + + /* Constructors */ + random_stream_uniform_int(){} + random_stream_uniform_int(double lower_bound, double upper_bound) + : mt(rand()) , uniform_dist(lower_bound, upper_bound) + {} + + /* Overwrites the function-call operator "( )" */ + double operator( )(void) { + return uniform_dist(mt); + } +}; +/****************************************************************************************************/ +/* end */ +/****************************************************************************************************/ diff --git a/Stimulation.h b/Stimulation.h index 557b62a..f1fbf38 100644 --- a/Stimulation.h +++ b/Stimulation.h @@ -32,19 +32,11 @@ /* Implementation of the stimulation protocol */ /****************************************************************************************************/ #pragma once -#include +#include "Random_Stream.h" #include "Cortical_Column.h" #include "Thalamic_Column.h" /****************************************************************************************************/ -/* Typedefs for RNG */ -/****************************************************************************************************/ -typedef boost::random::uniform_int_distribution<> DIST_int; /* Uniform integer distribution */ -/****************************************************************************************************/ -/* end */ -/****************************************************************************************************/ - -/****************************************************************************************************/ /* Stimulation object */ /****************************************************************************************************/ class Stim { @@ -151,11 +143,10 @@ class Stim { Thalamic_Column* Thalamus; /* Data containers */ - vector marker_stimulation; + std::vector marker_stimulation; /* Random number generator in case of semi-periodic stimulation */ - ENG Generator; - DIST_int Uniform_Distribution; + random_stream_uniform_int Rand_Uniform; }; /****************************************************************************************************/ /* end */ @@ -205,10 +196,7 @@ void Stim::setup (double* var_stim) { /* If ISI is random create RNG */ if (ISI_range != 0){ /* Create the generator */ - Generator = ENG(rand()); - - /* Combine RNG with distribution */ - Uniform_Distribution = DIST_int(ISI-ISI_range, ISI+ISI_range); + Rand_Uniform = random_stream_uniform_int(ISI-ISI_range, ISI+ISI_range); } } else { /* In case of phase dependent stimulation, time_to_stim is the time from minimum detection to start of stimulation */ @@ -246,7 +234,7 @@ void Stim::check_stim (int time) { } /* After last stimulus in event update the timer with respect to (random) ISI*/ else { - time_to_stimuli += (ISI_range==0)? ISI : Uniform_Distribution(Generator); + time_to_stimuli += (ISI_range==0)? ISI : Rand_Uniform(); /* Reset the stimulus counter for next stimulation event */ count_stimuli = 1; diff --git a/TC.cpp b/TC.cpp index d816f96..bf49ad8 100644 --- a/TC.cpp +++ b/TC.cpp @@ -30,12 +30,9 @@ /****************************************************************************************************/ /* Main file for compilation tests */ -/* The Simulation requires the following boost libraries: Random */ /****************************************************************************************************/ #include #include -#include "Cortical_Column.h" -#include "Thalamic_Column.h" #include "ODE.h" diff --git a/TC_mex.cpp b/TC_mex.cpp index 4e803ac..7eedf77 100644 --- a/TC_mex.cpp +++ b/TC_mex.cpp @@ -32,16 +32,13 @@ /* Implementation of the simulation as MATLAB routine (mex compiler) */ /* mex command is given by: */ /* mex CXXFLAGS="\$CXXFLAGS -std=c++11" TC_mex.cpp Cortical_Column.cpp Thalamic_Column.cpp */ -/* The Simulation requires the following boost libraries: Random */ /****************************************************************************************************/ #include #include "mex.h" #include "matrix.h" -#include "Cortical_Column.h" +#include "Data_Storage.h" #include "ODE.h" -#include "saves.h" #include "Stimulation.h" -#include "Thalamic_Column.h" mxArray* GetMexArray(int N, int M); /****************************************************************************************************/ diff --git a/Thalamic_Column.cpp b/Thalamic_Column.cpp index 4f14a83..7da1485 100644 --- a/Thalamic_Column.cpp +++ b/Thalamic_Column.cpp @@ -44,8 +44,8 @@ void Thalamic_Column::set_RNG(void) { /* Create RNG for each stream */ for (int i=0; i #include -#include -#include -#include +#include "Random_Stream.h" #include "Cortical_Column.h" using std::vector; - class Cortical_Column; /****************************************************************************************************/ -/* Typedefs for RNG */ -/****************************************************************************************************/ -typedef boost::mt11213b ENG; /* Mersenne Twister */ -typedef boost::normal_distribution DIST; /* Normal Distribution */ -typedef boost::variate_generator GEN; /* Variate generator */ -/****************************************************************************************************/ -/* end */ -/****************************************************************************************************/ - - -/****************************************************************************************************/ /* Macro for vector initialization */ /****************************************************************************************************/ #ifndef _INIT @@ -158,7 +144,7 @@ class Thalamic_Column { m_h2 = _INIT(0.0); /* activation of h channel bound with protein */ /* Random number generators */ - vector MTRands; + vector MTRands; /* Container for noise */ vector Rand_vars;