simelement.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #include "sys.hpp" // for libcwd
  2. #include "debug.hpp" // for libcwd
  3. #include "simelement.hpp"
  4. #include "simloop.hpp"
  5. #include "libcsim.hpp"
  6. #include "gslsingleton.h"
  7. SimElement::SimElement(SimLoop* _MainSimLoop,
  8. int _MacroTimeStep,
  9. SimElementType _type)
  10. :MainSimLoop(_MainSimLoop),
  11. MacroTimeStep(_MacroTimeStep),
  12. seType(_type),
  13. seTypeString(SimElementTypeStrings[seType]),
  14. IdNumber(0),
  15. active(true),
  16. dt(MainSimLoop->GetDeltaT()),
  17. rec(0),
  18. BinRec(0),
  19. SimTag(""),
  20. resetable(true)
  21. {
  22. /** @todo go back to passing SimLoop to every SimElement
  23. instead of using a global SimLoop. Better design.
  24. Because no restriction to a single SimLoop. Maybe we want to
  25. run two simulations in the same program?
  26. element can implement a addToSomLoop() method
  27. */
  28. cout << "\n\n Obsolete!!!!!!!!!!!!!!!!! Use GlobalMainSimLoop instead!!\n\n\n";
  29. DataDirectory = "";
  30. if (MainSimLoop != 0) {
  31. cout << "Get Global Settings from MainSimLoop\n";
  32. MainSimLoop->Hallo();
  33. gslr = GslSingleton::GetGslSingleton().GetGslRng();
  34. DataDirectory = MainSimLoop->GetDataDirectory();
  35. } else cout << "ERROR: SimElement needs MainSimLoop\n\n";
  36. }
  37. SimElement::SimElement(SimElementType _type)
  38. : seType(_type),
  39. active(true),
  40. seTypeString(SimElementTypeStrings[seType]),
  41. IdNumber(0),
  42. rec(0),
  43. BinRec(0),
  44. SimTag(""),
  45. resetable(true),
  46. MainSimLoop(0)
  47. {
  48. // cout << "SimElement: using GlobalMainSimLoop\n";
  49. MainSimLoop = GetGlobalSimLoop();
  50. if (MainSimLoop == 0) {
  51. cerr << "*******ERROR********ERROR**********ERROR**************\n";
  52. cerr << "* routine: SimElement::SimElement: \n";
  53. cerr << "* Global Main Sim Loop not initialized\n";
  54. cerr << "* you need the following line in your main program\n";
  55. cerr << "* before using SimElement objects\n";
  56. cerr << "* SimLoop* MainSimLoop = InitLibCSim(); \n";
  57. cerr << "************************************************************\n\n";
  58. exit(1);
  59. }
  60. MacroTimeStep = MainSimLoop->GetMacroTimeStep();
  61. dt=MainSimLoop->GetDeltaT();
  62. gslr = GslSingleton::GetGslSingleton().GetGslRng();
  63. DataDirectory = MainSimLoop->GetDataDirectory();
  64. Dout(dc::element,"SimElement::SimElement: generate new SimElement");
  65. Debug( libcw_do.inc_indent(2) );
  66. Dout(dc::element,"DataDirectory = " << DataDirectory);
  67. Debug( libcw_do.dec_indent(2) );
  68. }
  69. SimElement::~SimElement()
  70. {
  71. if (rec) delete rec;
  72. }
  73. int SimElement::proceede(int Time)
  74. {
  75. }
  76. int SimElement::prepare(int step)
  77. {
  78. if (BinRec) {
  79. if (step==0) BinRec->restart(SimTag);
  80. BinRec->WriteToFile();
  81. }
  82. }
  83. void SimElement::SetResetable(bool value)
  84. {
  85. resetable=value;
  86. }
  87. bool SimElement::Resetable()
  88. {
  89. return resetable;
  90. }
  91. int SimElement::reset(int t)
  92. {
  93. }
  94. void SimElement::SetParameter(ParaType p, double value)
  95. {
  96. //cout <<"ParaType " << p << "not supported by SimElement " << seTypeString << "\n";
  97. }
  98. int SimElement::GetMacroTimeStep()
  99. {
  100. return MacroTimeStep;
  101. }
  102. int SimElement::SetMacroTimeStep(int mst)
  103. {
  104. MacroTimeStep = mst;
  105. }
  106. float SimElement::GetDeltaT()
  107. {
  108. return dt;
  109. }
  110. void SimElement::Hallo()
  111. {
  112. Debug( libcw_do.inc_indent(2) );
  113. Dout(dc::element, Name);
  114. Debug( libcw_do.dec_indent(2) );
  115. }
  116. void SimElement::TurnOn()
  117. {
  118. active = true;
  119. }
  120. void SimElement::TurnOff()
  121. {
  122. active = false;
  123. }
  124. bool SimElement::On()
  125. {
  126. return active;
  127. }
  128. int SimElement::SaveSimInfo()
  129. {
  130. }
  131. int SimElement::WriteSimInfo(fstream &fw)
  132. {
  133. fw << "<" << seTypeString << " id=\"" << IdNumber << "\" Type=\"" << seType << "\" Name=\"" << Name << "\" /> \n";
  134. }
  135. int SimElement::WriteSimInfo(fstream &fw, const string &ChildInfo)
  136. {
  137. fw << "<" << seTypeString << " id=\"" << IdNumber << "\" Type=\"" << seType << "\" Name=\"" << Name << "\"> \n";
  138. fw << AddSpaces(ChildInfo);
  139. fw << "</" << seTypeString << "> \n";
  140. }
  141. string SimElement::GetSimInfo()
  142. {
  143. stringstream sstr;
  144. sstr << "<" << seTypeString << " id=\"" << IdNumber << "\" Type=\"" << seType << "\" Name=\"" << Name << "\"> \n";
  145. sstr << "</" << seTypeString << "> \n";
  146. return sstr.str();
  147. }
  148. string SimElement::GetSimInfo(const string &ChildInfo)
  149. {
  150. stringstream sstr;
  151. sstr << "<" << seTypeString << " id=\"" << IdNumber << "\" Type=\"" << seType << "\" Name=\"" << Name << "\"> \n";
  152. sstr << AddSpaces(ChildInfo);
  153. sstr << "</" << seTypeString << "> \n";
  154. return sstr.str();
  155. }
  156. void SimElement::SetName(const char* _name)
  157. {
  158. Name=_name;
  159. }
  160. void SimElement::SetDataDirectory(const char* _dirname)
  161. {
  162. DataDirectory = _dirname;
  163. }
  164. int SimElement::StartRecorder(char* RecName)
  165. {
  166. if (rec==0) {
  167. rec = new Recorder(RecName);
  168. } else cout << "Recorder allready running!!";
  169. }
  170. void SimElement::SetSimTag(const char* _tag)
  171. {
  172. SimTag = _tag;
  173. }
  174. long SimElement::calcMemoryConsumption()
  175. {
  176. Dout(dc::element, "calcMemoryConsumption() not implemented for " << seTypeString);
  177. return 0;
  178. }