00001 /*************************************************************************** 00002 * SpikeStream Simulation * 00003 * Copyright (C) 2007 by David Gamez * 00004 * david@davidgamez.eu * 00005 * Version 0.1 * 00006 * * 00007 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 ***************************************************************************/ 00022 00023 //SpikeStream includes 00024 #include "SimulationClock.h" 00025 #include "Debug.h" 00026 00027 //Other includes 00028 #include <cstring> 00029 #include <iostream> 00030 using namespace std; 00031 00032 00033 /*! Constructor. */ 00034 SimulationClock::SimulationClock(){ 00035 //Initialise variables 00036 timeStep = 0; 00037 simulationTime = 0.0; 00038 liveMode = false; 00039 timeStepDuration_millisec = 0.0;//This should be initialised from the global parameters table 00040 } 00041 00042 00043 /*! Destructor. */ 00044 SimulationClock::~SimulationClock(){ 00045 #ifdef MEMORY_DEBUG 00046 cout<<"DESTROYING SIMULATION CLOCK"<<endl; 00047 #endif//MEMORY_DEBUG 00048 } 00049 00050 00051 //---------------------------------------------------------------------------- 00052 //----------------------------- PUBLIC METHODS ------------------------------- 00053 //---------------------------------------------------------------------------- 00054 00055 /*! Returns the time that has elapsed since the beginning of the simulation. 00056 In live mode this is the actual time elapsed. In non-live mode this is the 00057 number of time steps multiplied by the time step duration. */ 00058 double SimulationClock::getSimulationTime(){ 00059 return simulationTime; 00060 } 00061 00062 00063 /*! Returns the current time step. */ 00064 unsigned int SimulationClock::getTimeStep(){ 00065 return timeStep; 00066 } 00067 00068 00069 /*! Returns the amount of time that the clock advances with each time step 00070 This is only meaningful in non live mode. */ 00071 double SimulationClock::getTimeStepDuration_ms(){ 00072 return timeStepDuration_millisec; 00073 } 00074 00075 00076 //------------------------------------------------------------------------------ 00077 //---------------------------- PRIVATE METHODS --------------------------------- 00078 //------------------------------------------------------------------------------ 00079 00080 /*! Increases the time step of the simulation and increases the simulation time. */ 00081 void SimulationClock::advance(){ 00082 ++timeStep; 00083 if(liveMode){ 00084 gettimeofday(¤tTimeStruct, NULL); 00085 int microSecondsElapsed = 1000000 * (currentTimeStruct.tv_sec - startTimeStruct.tv_sec) + ( currentTimeStruct.tv_usec - startTimeStruct.tv_usec); 00086 simulationTime = ((double)microSecondsElapsed)/ 1000.0; 00087 } 00088 else{ 00089 simulationTime += timeStepDuration_millisec; 00090 } 00091 } 00092 00093 00094 /*! Resets the clock. */ 00095 void SimulationClock::reset(){ 00096 //Reset time variables 00097 timeStep = 0; 00098 simulationTime = 0.0; 00099 00100 //In live mode re-initialise time structures 00101 if(liveMode){ 00102 gettimeofday(&startTimeStruct, NULL); 00103 gettimeofday(¤tTimeStruct, NULL); 00104 } 00105 } 00106 00107 00108 /*! In live mode the clock advances in real time. Otherwise the clock 00109 advances in its own simulation time by timeStepDuration_millisec. */ 00110 void SimulationClock::setLiveMode(bool lMode){ 00111 //Reset clock if live mode has changed 00112 if(liveMode != lMode) 00113 simulationTime = 0.0; 00114 00115 //Store new mode 00116 liveMode = lMode; 00117 00118 //Initialise time structures used to keep track of real time 00119 if(liveMode){ 00120 gettimeofday(&startTimeStruct, NULL); 00121 gettimeofday(¤tTimeStruct, NULL); 00122 } 00123 } 00124 00125 00126 /*! Sets the amount that the clock will advance with each time step in non 00127 live mode. */ 00128 void SimulationClock::setTimeStepDuration(double tsDuration){ 00129 timeStepDuration_millisec = tsDuration; 00130 } 00131 00132
1.4.4