Passing a filestream, how?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2006
Posts: 12
Reputation: Daan is an unknown quantity at this point 
Solved Threads: 0
Daan Daan is offline Offline
Newbie Poster

Passing a filestream, how?

 
0
  #1
Aug 3rd, 2006
Hello :-)

I'm trying to pass a filestream to a function:

  1. template <class T>
  2. void grid<T>::print(std::ofstream file)
  3. { for (int i = 0; i < h; i++)
  4. { for (int j = 0; j < w; j++)
  5. file << cell[i][j] << "\t";
  6. file << std::endl;
  7. }
  8. }
so that I can do something like this:

  1. somegrid.print(densities);
However, mu compiler gives me a lot of errors I don't understand. Can you tell me how to do it right?

Thank you!

Daan

Output from g++:

  1. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd: In copy constructor ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’:
  2. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/bits/ios_base.h:779: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
  3. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd:55: error: within this context
  4. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd: In copy constructor ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)’:
  5. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd:92: warning: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)’ first required here
  6. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd: In copy constructor ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’:
  7. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/streambuf:772: error: ‘std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]’ is private
  8. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd:86: error: within this context
  9. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd: In copy constructor ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)’:
  10. /usr/lib/gcc/i486-linux-gnu/4.0.4/../../../../include/c++/4.0.4/iosfwd:92: warning: synthesized method ‘std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)’ first required here
  11. SK.cpp: In function ‘int main()’:
  12. SK.cpp:394: warning: synthesized method ‘std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream(const std::basic_ofstream<char, std::char_traits<char> >&)’ first required here
  13. SK.cpp:394: warning: initializing argument 1 of ‘void grid<T>::print(std::ofstream) [with T = double]
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Passing a filestream, how?

 
0
  #2
Aug 3rd, 2006
Can you give the complete code.
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 65
Reputation: GloriousEremite will become famous soon enough GloriousEremite will become famous soon enough 
Solved Threads: 14
GloriousEremite GloriousEremite is offline Offline
Junior Poster in Training

Re: Passing a filestream, how?

 
0
  #3
Aug 3rd, 2006
Try passing by reference
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 12
Reputation: Daan is an unknown quantity at this point 
Solved Threads: 0
Daan Daan is offline Offline
Newbie Poster

Re: Passing a filestream, how?

 
0
  #4
Aug 3rd, 2006
There are quit a couple of files, but I'll give you the two most important.

Here's SK_.cpp:

  1. /* SK.cpp: simulates a reaction-diffusion model of chemical warfare between microbes. */
  2.  
  3. #include "GRID.hpp"
  4. #include <math.h>
  5. #include <time.h>
  6. //#include <vector>
  7. #include "gnuplot_i.h"
  8. #include "XGRID.hpp"
  9. //#include <fstream>
  10. //#include <string>
  11. //#include <typeinfo>
  12. //#include <unistd.h>
  13. #include <limits>
  14. using namespace std;
  15.  
  16. typedef double number; // choose double
  17. number EPS = numeric_limits<number>::epsilon();
  18. // resolves issues with casting numbers that
  19. double dummy; // cannot be expressed exactly as a binary
  20.  
  21. /////////////////
  22. // GENERAL STUFF:
  23. /////////////////
  24.  
  25. const number height = 1.0; // height of competition arena
  26. const number width = 1.0; // its width
  27. const bool wrap = true; // borders _must_ be connected
  28. const number dx = 0.01; // step size for integration in space (same as dy)
  29. const number dt = 0.003; // step size for integration in time
  30. const int h =
  31. static_cast<int>(height/dx); // number of gridcells in one direction
  32. const int w =
  33. static_cast<int>(width/dx); // number of gridcells in other direction
  34. const number end_time = 1000; // number of generations to be simulated
  35. const unsigned int time_steps =
  36. static_cast<int>(end_time/dt); // number of time steps
  37.  
  38. /////////////
  39. // REPORTING:
  40. /////////////
  41.  
  42. // choose multiples of dt:
  43. const number print_time = 100*dt; // when to print numbers to screen
  44. float frac = modf(print_time/dt,&dummy);
  45. const unsigned int print_step =
  46. modf(print_time/dt,&dummy)+EPS < 1 ? static_cast<int>(print_time/dt) :
  47. static_cast<int>(print_time/dt)+1;
  48. const number save_totals_time = 10*dt;// when to save total densities to file
  49. const unsigned int save_totals_step =
  50. modf(save_totals_time/dt,&dummy)+EPS < 1 ? static_cast<int>(save_totals_time/dt) :
  51. static_cast<int>(save_totals_time/dt)+1;
  52. const number plot_totals_time = 1000*dt;// when to plot total densities with gnuplot
  53. const unsigned int plot_totals_step =
  54. modf(plot_totals_time/dt,&dummy)+EPS < 1 ? static_cast<int>(plot_totals_time/dt) :
  55. static_cast<int>(plot_totals_time/dt)+1;
  56. const number plot_time = dt; // when to plot local densities with XGRID
  57. const unsigned int plot_step =
  58. modf(plot_time/dt,&dummy)+EPS < 1 ? static_cast<int>(plot_time/dt) :
  59. static_cast<int>(plot_time/dt)+1;
  60. const number scale_time = 3333*dt; // when to update the colorscale for plotting with XGRID
  61. const unsigned int scale_step =
  62. modf(scale_time/dt,&dummy)+EPS < 1 ? static_cast<int>(scale_time/dt) :
  63. static_cast<int>(scale_time/dt)+1;
  64. const number save_densities_time = 10*dt;// when to save density grids to file
  65. const unsigned int save_densities_step =
  66. modf(save_densities_time/dt,&dummy)+EPS < 1 ? static_cast<int>(save_densities_time/dt) :
  67. static_cast<int>(save_densities_time/dt)+1;
  68.  
  69. /////////////
  70. // VARIABLES:
  71. /////////////
  72.  
  73. // sensitive cells:
  74. grid<number> S(h,w,wrap); // density
  75. grid<number> dSdt(h,w,wrap); // rate of change
  76. grid<number> dSdt_react(h,w,wrap); // rate of change due to growth, death and killing
  77. grid<number> dSdt_diff_x(h,w,wrap); // rate of change due to diffusion in x-direction (inflow)
  78. grid<number> dSdt_diff_y(h,w,wrap); // rate of change due to diffusion in y-direction (inflow)
  79.  
  80. // killer cells:
  81. grid<number> K(h,w,wrap); // density
  82. grid<number> dKdt(h,w,wrap); // rate of change
  83. grid<number> dKdt_react(h,w,wrap); // rate of change due to growth, death and killing
  84. grid<number> dKdt_diff_x(h,w,wrap); // rate of change due to diffusion in x-direction (inflow)
  85. grid<number> dKdt_diff_y(h,w,wrap); // rate of change due to diffusion in y-direction (inflow)
  86.  
  87. // resource:
  88. grid<number> R(h,w,wrap);
  89. grid<number> dRdt(h,w,wrap);
  90. grid<number> dRdt_react(h,w,wrap);
  91. grid<number> dRdt_diff_x(h,w,wrap);
  92. grid<number> dRdt_diff_y(h,w,wrap);
  93.  
  94. // toxin:
  95. grid<number> T(h,w,wrap); // density
  96. grid<number> dTdt(h,w,wrap); // rate of change
  97. grid<number> dTdt_react(h,w,wrap); // rate of change due to production and degradation;
  98. grid<number> dTdt_diff_x(h,w,wrap); // rate of change due to diffusion in x-direction (inflow)
  99. grid<number> dTdt_diff_y(h,w,wrap); // rate of change due to diffusion in y-direction (inflow)
  100.  
  101.  
  102. //////////////
  103. // PARAMETERS:
  104. //////////////
  105.  
  106. const number y_cell = 1; // yield of cells per unit of resource
  107. const number p = 0.25; // fraction of resource allocated by killer
  108. // to the production of toxin
  109. const number q = 0; // fraction of resource allocated by killer
  110. // to immunity against toxin
  111. const number y_tox = 0.6; // yield of toxin per unit of resource
  112. const number epsilon = 0.1; // resource uptake rate
  113. const number chi = 0.1; // killing rate of toxin
  114. const number d = 0.1; // natural death rate
  115. const number R_batch = 0.0; // concentration of resource in batch
  116. const number R_bit = 100*dx*dx*dt; //0.0015; // size of resource bit
  117. const number R_in = 100; // expected ammount of incoming resource
  118. // per unit time per unit area
  119. const number R_chance = 1; // chance that resource bit will fall in a gridcell during dt
  120. //(R_in/(R_bit*static_cast<number>(h)*static_cast<number>(w))*dt)*height*width;
  121. const number transfer = 0.01; // fraction of cells that is transferred
  122. const number D_cell = 0.025; // diffusion constant for cells
  123. const number D_R = 0.25; // diffusion constant for resource
  124. const number D_tox = 0.1; // diffusion constant for toxin
  125. const number delta = 0.01; // additional death rate due to VLP
  126. const number psi = 0.3; // degradation rate of toxin
  127.  
  128.  
  129. /////////////////////
  130. // INITIAL DENSITIES:
  131. /////////////////////
  132.  
  133. const number init_dens_S = 1000; // initial cell density averaged over space
  134. const number init_dens_K = 10;
  135. unsigned int init_pop_S = // total initial population size
  136. static_cast<int>(init_dens_S*height*width);
  137. unsigned int init_pop_K =
  138. static_cast<int>(init_dens_K*height*width);
  139.  
  140. int main()
  141. {
  142. // INITIALIZATION:
  143. ////////////////////////////////////////////////////////////
  144.  
  145. srand(time(NULL));
  146.  
  147. S.setall(0);
  148. K.setall(0);
  149. R.setall(R_batch/(h*w));
  150. T.setall(0);
  151.  
  152. number p_total = p + q;
  153. //number K_S = R_batch*y_cell/d;
  154.  
  155. // spread the competitors randomly over space:
  156. for (unsigned int k = 0; k < init_pop_S; k++)
  157. S[rand()%h][rand()%w] += 1.0;
  158. for (unsigned int k = 0; k < init_pop_K; k++)
  159. K[rand()%h][rand()%w] += 1.0;
  160.  
  161. /* // introduce one S in the middle:
  162.   S[w/2][h/2] = 1;
  163. */
  164. /* // introduce some R in the middle:
  165.   R[w/2][h/2] = R_bit/(dx*dx);
  166. */
  167. // technical stuff:
  168.  
  169. // create text file for saving and plotting totals:
  170. ofstream totals("SK_totals.txt", ios::trunc);
  171. if (totals.good() == false)
  172. cout << "Totals file is not good!" << endl;
  173. // and one for saving local densities:
  174. ofstream densities("SK_densities.txt", ios::trunc);
  175. if (densities.good() == false)
  176. cout << "densities file is not good!" << endl;
  177.  
  178. // open pipe to gnuplot and stuff:
  179. gnuplot_ctrl* gp_handle;
  180. gp_handle = gnuplot_init();
  181. gnuplot_setstyle(gp_handle, "lines");
  182.  
  183. // connect to X server, make a window etc.:
  184. connect XSK;
  185. // for sensitives and killers:
  186. gridwindow WSK(XSK.display, h, w, 300/w);
  187. number minS = S.min();
  188. number maxS = S.max();
  189. int stepsS = 20;
  190. number minK = R.min();
  191. number maxK = R.max();
  192. int stepsK = 20;
  193. GC** GCSK = makecolorscale(XSK.display, stepsS, 0.0, 0.0, 1.0,
  194. stepsK, 1.0, 0.0, 0.0);
  195. // for resource:
  196. gridwindow WR(XSK.display, h, w, 300/w);
  197. number minR = 0;
  198. number maxR = R_bit/(dx*dx);
  199. int stepsR = 20;
  200. GC* GCR = makecolorscale(XSK.display, stepsR, 1.0, 1.0, 1.0);
  201. // for toxin:
  202. gridwindow WT(XSK.display, h, w, 300/w);
  203. number minT = 0;
  204. number maxT = R_bit/(dx*dx);
  205. int stepsT = 20;
  206. GC* GCT = makecolorscale(XSK.display, stepsT, 0.0, 1.0, 0.0);
  207.  
  208.  
  209. // print parameters to screen:
  210. cout << "height:\t\t" << height << "\n"
  211. << "width:\t\t" << width << "\n"
  212. << "dx:\t\t" << dx << "\n"
  213. << "dt:\t\t" << dt << "\n"
  214. << "epsilon:\t" << epsilon << "\n"
  215. << "y_cell:\t\t" << y_cell << "\n"
  216. << "R_batch:\t" << R_batch << "\n"
  217. << "R_in:\t\t" << R_in << "\n"
  218. << "R_bit:\t\t" << R_bit << "\n"
  219. << "psi:\t\t" << psi << "\n"
  220. << "D_cell:\t\t" << D_cell << "\n"
  221. << "D_R:\t\t" << D_R << "\n"
  222. << "D_tox:\t\t" << D_tox << "\n"
  223. << "p:\t\t" << p << "\n"
  224. << "d:\t\t" << d << "\n"
  225. << "y_tox:\t\t" << y_tox << "\n"
  226. << "chi:\t\t" << chi << "\n"
  227. << "init_dens_S:\t" << init_dens_S << "\n"
  228. << "init_dens_K:\t" << init_dens_K << "\n"
  229. << endl;
  230.  
  231. // save parameters to file:
  232. totals << "#height:\t" << height << "\n"
  233. << "#width:\t" << width << "\n"
  234. << "#dx:\t" << dx << "\n"
  235. << "#dt:\t" << dt << "\n"
  236. << "#epsilon:\t" << epsilon << "\n"
  237. << "#y_cell:\t" << y_cell << "\n"
  238. << "#R_batch:\t" << R_batch << "\n"
  239. << "#R_in:\t" << R_in << "\n"
  240. << "#R_bit:\t" << R_bit << "\n"
  241. << "#psi:\t" << psi << "\n"
  242. << "#D_cell:\t" << D_cell << "\n"
  243. << "#D_R:\t" << D_R << "\n"
  244. << "#D_tox:\t" << D_tox << "\n"
  245. << "#p:\t" << p << "\n"
  246. << "#p_total:\t" << p_total << "\n"
  247. << "#d:\t" << d << "\n"
  248. << "#y_tox\t" << y_tox << "\n"
  249. << "#chi:\t" << chi << "\n"
  250. << "#init_dens_S:\t" << init_dens_S << "\n"
  251. << "#init_dens_K:\t" << init_dens_K << "\n"
  252. << endl;
  253.  
  254. // ...
  255. cout << "t: " << "0" << "\t"
  256. << "S: " << S.gridsum() << "\t"
  257. << "K: " << K.gridsum() << "\t"
  258. << "R: " << R.gridsum() << "\t"
  259. << "T: " << T.gridsum() << "\t"
  260. << endl;
  261.  
  262. // ...
  263. totals << "#time\t" << "S\t" << "K\t" << "R\t" << "T\n"
  264. << 0 << "\t"
  265. << S.gridsum() << "\t"
  266. << K.gridsum() << "\t"
  267. << R.gridsum() << "\t"
  268. << T.gridsum() << "\t"
  269. << endl;
  270.  
  271. // ...
  272.  
  273. WSK.paint(GCSK, S, minS, maxS, stepsR,
  274. K, minK, maxK, stepsR );
  275. WR.paint(GCR, R, minR, maxR, stepsR);
  276. WT.paint(GCT, T, minT, maxT, stepsT);
  277. XSync(XSK.display,False);
  278.  
  279. // THE TIME LOOP:
  280. ////////////////////////////////////////////////////////////
  281.  
  282. for (unsigned int t_step = 1; t_step <= time_steps; t_step++)
  283. {
  284.  
  285. /* integration of the partial differential equations. The differential
  286.   equations are simply translated into difference equations: */
  287. //TODO: compute uptake (epsilon*R) just once.
  288. //TODO: compute reaction and diffusion in one step, might be faster.
  289.  
  290. for (int x = 0; x < h; x++) // the origin is in the _upper_ left corner
  291. for (int y = 0; y < w; y++)
  292. { // SENSITIVE:
  293. // growth and death:
  294. dSdt_react[x][y] = S[x][y]*(y_cell*epsilon*R[x][y]-d-chi*T[x][y]);
  295. // diffusion:
  296. dSdt_diff_x[x][y] = -D_cell*(S[x][y] - S[x-1][y])/dx;
  297. dSdt_diff_y[x][y] = -D_cell*(S[x][y] - S[x][y-1])/dx;
  298.  
  299. // KILLER:
  300. // growth and death:
  301. dKdt_react[x][y] = K[x][y]*((1.0-p_total)*y_cell*epsilon*R[x][y]-d-delta);
  302. // diffusion:
  303. dKdt_diff_x[x][y] = -D_cell*(K[x][y] - K[x-1][y])/dx;
  304. dKdt_diff_y[x][y] = -D_cell*(K[x][y] - K[x][y-1])/dx;
  305.  
  306. // RESOURCE:
  307. // supply and consumption:
  308. dRdt_react[x][y] = -epsilon*R[x][y]*(S[x][y]+K[x][y]);
  309. // diffusion:
  310. dRdt_diff_x[x][y] = -D_R*(R[x][y] - R[x-1][y])/dx;
  311. dRdt_diff_y[x][y] = -D_R*(R[x][y] - R[x][y-1])/dx;
  312.  
  313. // TOXIN:
  314. // production and degradation:
  315. dTdt_react[x][y] = epsilon*p*y_tox*(K[x][y])*R[x][y]-psi*T[x][y];
  316. // difussion:
  317. dTdt_diff_x[x][y] = -D_tox*(T[x][y] - T[x-1][y])/dx;
  318. dTdt_diff_y[x][y] = -D_tox*(T[x][y] - T[x][y-1])/dx;
  319. }
  320.  
  321. for (int x = 0; x < h; x++)
  322. for (int y = 0; y < w; y++)
  323. { // SENSITIVE net rate of change:
  324. dSdt[x][y] = dSdt_react[x][y] + dSdt_diff_x[x][y] - dSdt_diff_x[x+1][y]
  325. + dSdt_diff_y[x][y] - dSdt_diff_y[x][y+1];
  326.  
  327. // KILLER net rate of change:
  328. dKdt[x][y] = dKdt_react[x][y] + dKdt_diff_x[x][y] - dKdt_diff_x[x+1][y]
  329. + dKdt_diff_y[x][y] - dKdt_diff_y[x][y+1];
  330.  
  331. // RESOURCE net rate of change:
  332. dRdt[x][y] = dRdt_react[x][y] + dRdt_diff_x[x][y] - dRdt_diff_x[x+1][y]
  333. + dRdt_diff_y[x][y] - dRdt_diff_y[x][y+1];
  334.  
  335. // TOXIN net rate of change:
  336. dTdt[x][y] = dTdt_react[x][y] + dTdt_diff_x[x][y] - dTdt_diff_x[x+1][y]
  337. + dTdt_diff_y[x][y] - dTdt_diff_y[x][y+1];
  338.  
  339. // STOCHASTIC RESOURCE RAIN:
  340. if ( R_chance > static_cast<number>(rand())/static_cast<number>(RAND_MAX) )
  341. R[x][y] += (R_bit/(dx*dx));
  342. }
  343.  
  344. /* S.print();
  345.   cout << endl;
  346.   dSdt_diff_x.print();
  347.   cout << endl;
  348.   dSdt_diff_y.print();
  349.   cout << endl;
  350.   cout << endl;
  351.   */
  352. /* R.print();
  353.   cout << endl;
  354.   dRdt_diff_x.print();
  355.   cout << endl;
  356.   dRdt_diff_y.print();
  357.   cout << endl;
  358.   dRdt.print();
  359.   cout << endl;
  360.   */
  361. // multiply rates of change by dt and update densities:
  362. S += dSdt*dt;
  363. K += dKdt*dt;
  364. R += dRdt*dt;
  365. T += dTdt*dt;
  366.  
  367. // round fractional cells up or down:
  368. // NOTA BENE: this makes the model stochastic!
  369. for (int x = 0; x < w; x++)
  370. for (int y = 0; y < h; y++)
  371. if (S[x][y] - floor(S[x][y]) > (number)rand()/(number)RAND_MAX)
  372. S[x][y] = ceil(S[x][y]);
  373. else
  374. S[x][y] = floor(S[x][y]);
  375. for (int x = 0; x < w; x++)
  376. for (int y = 0; y < h; y++)
  377. if (K[x][y] - floor(K[x][y]) > (number)rand()/(number)RAND_MAX)
  378. K[x][y] = ceil(K[x][y]);
  379. else
  380. K[x][y] = floor(K[x][y]);
  381.  
  382.  
  383. if (t_step%print_step == 0)
  384. { cout << "t: " << static_cast<number>(t_step)*dt << "\t"
  385. << "S: " << S.gridsum() << "\t"
  386. << "K: " << K.gridsum() << "\t"
  387. << "R: " << R.gridsum() << "\t"
  388. << "T: " << T.gridsum() << "\t"
  389. << endl;
  390. }
  391.  
  392. if (t_step%save_densities_step == 0)
  393. { densities << static_cast<number>(t_step)*dt << "\n";
  394. densities << "S\n"; S.print(densities);
  395. densities << "K\n"; K.print(densities);
  396. densities << "R\n"; R.print(densities);
  397. densities << "T\n"; T.print(densities);
  398. densities << endl;
  399. }
  400.  
  401. if (t_step%save_totals_step == 0)
  402. { totals << static_cast<number>(t_step)*dt << "\t"
  403. << S.gridsum() << "\t"
  404. << K.gridsum() << "\t"
  405. << R.gridsum() << "\t"
  406. << T.gridsum() << "\t"
  407. << endl;
  408. }
  409.  
  410. if (t_step%plot_totals_step == 0)
  411. { gnuplot_cmd(gp_handle, "plot 'SK_totals.txt' u 1:2 title 'S' w lines,'SK_totals.txt' u 1:3 title 'K' w lines,'SK_totals.txt' u 1:4 title 'R' axis x1y2 w lines,'SK_totals.txt' u 1:5 title 'T' axis x1y2 w lines");
  412. }
  413.  
  414. if (t_step%scale_step == 0)
  415. { maxS = S.max();
  416. minS = S.min();
  417. maxK = K.max();
  418. minK = K.min();
  419. maxR = R.max();
  420. minR = R.min();
  421. maxT = T.max();
  422. minT = T.min();
  423. cout << "Updating colorscale: maxS = " << maxS << ", minS = " << minS << "\n"
  424. << " maxK = " << maxK << ", minK = " << minK << "\n"
  425. << " maxR = " << maxR << ", minR = " << minR << "\n"
  426. << " maxT = " << maxT << ", minT = " << minT << endl;
  427. }
  428.  
  429. if (t_step%plot_step == 0)
  430. {
  431. WSK.paint(GCSK, S, minS, maxS, stepsR,
  432. K, minK, maxK, stepsR );
  433. WR.paint(GCR, R, minR, maxR, stepsR);
  434. WT.paint(GCT, T, minT, maxT, stepsT);
  435. XSync(XSK.display,False);
  436.  
  437. }
  438. }
  439.  
  440. return 0;
  441. }
And here's the header that has the print function:

  1. /* This is the C++ header file for GRID, a set of classes and functions for spatial modelling, e.g. for cellular automata and reaction-diffusion models. By Daan Gerla, July 2006. */
  2.  
  3. /*
  4. GRID.hpp was originally CM.hpp, a header for the program CellMachine (CM), a cellular automata (CA) program. CM differs from most other CA programs in that it easily allows for interaction between distant cells. The code was written with two dimensional CA's in mind. The functions provided in this header file are for creating CA grids and for reading and manipulating them, and for displaying the lattice with nice colors on the screen.
  5.  
  6. GRID.hpp (this file) provides the class "grid", which constructs a two dimensional array to hold the states of one layer of a CA. Different layers of a CA can be used to hold different properties of the cells of a CA, for instance, in a CA for simulating games from game theory, one layer can hold their strategies and another their payoffs. The class also provides functions for reading and setting states. The class is defined as a class template, so that a grid could be of any type, however it is probably most convienient to have a type that is some kind of number.
  7. Alternatively, an instance of the grid class can be thought of as storing the average value of a variable in a small square in a plane, when simulating a reaction-diffusion system. For instance, an array of size n could hold the local densities of n species. In this case, only neighbouring cells interact, so the routines for interaction between distant cells are not needed (except when the borders of the lattice wrap up to each other).
  8. */
  9.  
  10. #include <iostream>
  11. #include <cstdlib>
  12. #include <fstream>
  13.  
  14. ///////////////////////////////////////////////////////////
  15. // class for a layer of a 2D CA. The the resulting 2D array is dynamic, meaning the ammount of memory allocated for the array can be determined at run time.
  16. ///////////////////////////////////////////////////////////
  17. // Rows and collumns are numbered from 0 to height-1 or width-1!
  18. template <class T>
  19. class grid
  20. { private:
  21. T *pool;
  22. T *curPtr;
  23. public:
  24. T **cell;
  25. int h;
  26. int w;
  27. bool r;
  28. grid(int height, int width, bool wrap)
  29. { h = height;
  30. w = width;
  31. r = wrap;
  32. cell = new T*[h];
  33. pool = new T [h*w];
  34. curPtr = pool;
  35. for (int i = 0; i < h; i++)
  36. { *(cell + i) = curPtr;
  37. curPtr += w;
  38. }
  39. }
  40. grid()
  41. {
  42. }
  43. grid(const grid<T> &othergrid)
  44. { h = othergrid.h;
  45. w = othergrid.w;
  46. r = othergrid.r;
  47. cell = new T*[h];
  48. pool = new T [h*w];
  49. curPtr = pool;
  50. for (int i = 0; i < h; i++)
  51. { *(cell + i) = curPtr;
  52. curPtr += w;
  53. }
  54. for (int i = 0; i < h; i++)
  55. for (int j = 0; j < w; j++)
  56. cell[i][j] = othergrid.cell[i][j];
  57. }
  58. ~grid()
  59. { delete[] *cell;
  60. delete[] cell;
  61. }
  62. // class for overloading []:
  63. template <class U>
  64. class rowclass
  65. { public:
  66. U* row_ptr;
  67. grid<U>* grid_ptr;
  68. int w;
  69. bool r;
  70. rowclass(int row, grid<U> &inputgrid)
  71. { grid_ptr = &inputgrid;
  72. row_ptr = &inputgrid.cell[row][0];
  73. w = inputgrid.h;
  74. r = inputgrid.r;
  75. //TODO: can't we access w and r directly?
  76. }
  77. public:
  78. inline U &operator[](int col)
  79. { if (r == true)
  80. { while (col >= w)
  81. col = col - w;
  82. while (col < 0)
  83. col = col + w;
  84. }
  85. return row_ptr[col];
  86. }
  87. inline const U &operator[](int col) const //TODO: correct?
  88. { if (r == true)
  89. { while (col >= w)
  90. col = col - w;
  91. while (col < 0)
  92. col = col + w;
  93. }
  94. return row_ptr[col];
  95. }
  96. };
  97. // functions for overloading []:
  98. inline rowclass<T> operator[](int row)
  99. { if (r == true)
  100. { while (row >= h)
  101. row = row - h;
  102. while (row < 0)
  103. row = row + h;
  104. }
  105. rowclass<T> rowobject(row,*this);
  106. return rowobject;
  107. }
  108. inline const rowclass<T> operator[](int row) const //TODO: correct?
  109. { if (r == true)
  110. { while (row >= h)
  111. row = row - h;
  112. while (row < 0)
  113. row = row + h;
  114. }
  115. rowclass<T> rowobject(row,*this);
  116. return rowobject;
  117. }
  118. grid & operator=(const grid<T> &othergrid)
  119. { // TODO: check size.
  120. for (int i = 0; i < h; i++)
  121. for (int j = 0; j < w; j++)
  122. cell[i][j] = othergrid.cell[i][j];
  123. return *this;
  124. }
  125. grid operator+(const grid<T> &othergrid)
  126. { //TODO check size.
  127. grid<T> temp(h,w,r);
  128. for (int i = 0; i < h; i++)
  129. for (int j = 0; j < w; j++)
  130. temp[i][j] = cell[i][j] + othergrid.cell[i][j];
  131. return temp;
  132. }
  133. grid & operator+=(const grid<T> &othergrid)
  134. { //TODO check size.
  135. for (int i = 0; i < h; i++)
  136. for (int j = 0; j < w; j++)
  137. cell[i][j] += othergrid.cell[i][j];
  138. return *this;
  139. }
  140. grid operator*(const grid<T> &othergrid)
  141. { //TODO check size.
  142. grid<T> temp(h,w,r);
  143. for (int i = 0; i < h; i++)
  144. for (int j = 0; j < w; j++)
  145. temp[i][j] = cell[i][j] * othergrid.cell[i][j];
  146. return temp;
  147. }
  148. grid operator*(const float number)
  149. { grid<T> temp(h,w,r);
  150. for (int i = 0; i < h; i++)
  151. for (int j = 0; j < w; j++)
  152. temp[i][j] = cell[i][j] * number;
  153. return temp;
  154. }
  155. grid & operator*=(const grid<T> &othergrid)
  156. { //TODO check size.
  157. for (int i = 0; i < h; i++)
  158. for (int j = 0; j < w; j++)
  159. cell[i][j] *= othergrid.cell[i][j];
  160. return *this;
  161. }
  162. grid & operator*=(const float number)
  163. { //TODO check size.
  164. for (int i = 0; i < h; i++)
  165. for (int j = 0; j < w; j++)
  166. cell[i][j] *= number;
  167. return *this;
  168. }
  169. // other functions:
  170. //friend class CMwindow;
  171. T getstate (int row, int col);
  172. T getstate (int row, int col, int v_off, int h_off);
  173. void setstate (int row, int col, T state);
  174. void setstate (int row, int col, int v_off, int h_off, T state);
  175. void setall (T state);
  176. int getrow (int row);
  177. int getcol (int col);
  178. void copy (T **grid);
  179. //bool compare (T **grid);
  180. T neighborhoodsum(int row, int col, int v_off, int h_off);
  181. T gridsum ();
  182. //T neighborhoodhigh(int row, int col, int v_off, int h_off);
  183. int randcol ();
  184. int randrow ();
  185. int randneighborrow(int row, int h_off);
  186. int randneighborcol(int col, int v_off);
  187. int* randompickprop(int row, int col, int v_off, int h_off);
  188. //void display (Display* display, CMwindow &win, T min, T max, int steps, GC* gc);
  189. T max ();
  190. T min ();
  191. void print ();
  192. void print (std::ofstream file);
  193. };
  194.  
  195. // gets the the state of a cell, given its row and collumn. It does this for a 2D CA.
  196. template <class T>
  197. T grid<T>::getstate(int row, int col)
  198. { if (r == false)
  199. { if (row >= 0 && row < h && col >= 0 && col < w)
  200. return cell[row][col];
  201. else
  202. return 0;
  203. }
  204. else
  205. { while (row >= h)
  206. row = row - h;
  207. while (col >= w)
  208. col = col - w;
  209. while (row < 0)
  210. row = row + h;
  211. while (col < 0)
  212. col = col + w;
  213. }
  214. return cell[row][col];
  215. }
  216.  
  217. // gets the the state of a cell, given the cell's horizontal and vertical offset from the current row and collumn. It does this for a 2D CA.
  218. template <class T>
  219. T grid<T>::getstate(int row, int col, int v_off, int h_off)
  220. { int i, j;
  221. i = row + v_off;
  222. j = col + h_off;
  223. if (r == false)
  224. { if (i >= 0 && i < h && j >= 0 && j < w)
  225. return cell[i][j];
  226. else
  227. return 0;
  228. }
  229. else
  230. { while (i >= h)
  231. i = i - h;
  232. while (j >= w)
  233. j = j - w;
  234. while (i < 0)
  235. i = i + h;
  236. while (j < 0)
  237. j = j + w;
  238. }
  239. return cell[i][j];
  240. }
  241.  
  242. // sets the the state of a cell, given the cell's row and collumn. It does this for a 2D CA.
  243. template <class T>
  244. void grid<T>::setstate(int row, int col, T state)
  245. { if (r == false)
  246. if (row >= 0 && row < h && col >= 0 && col < w)
  247. cell[row][col] = state;
  248. else
  249. { std::cout << "Error. Trying to set out of bounds cell." << std::endl;
  250. exit(1);
  251. }
  252. else
  253. { while (row >= h)
  254. row = row - h;
  255. while (col >= w)
  256. col = col - w;
  257. while (row < 0)
  258. row = row + h;
  259. while (col < 0)
  260. col = col + w;
  261. }
  262. cell[row][col] = state;
  263. }
  264.  
  265. // sets the the state of a cell, given the cell's horizontal and vertical offset from the current row and collumn. It does this for a 2D CA.
  266. template <class T>
  267. void grid<T>::setstate(int row, int col,
  268. int v_off, int h_off, T state)
  269. { int i, j;
  270. i = row + v_off;
  271. j = col + h_off;
  272. if (r == false)
  273. if (i >= 0 && i < h && j >= 0 && j < w)
  274. cell[i][j] = state;
  275. else
  276. { std::cout << "Error. Trying to set out of bounds cell." << std::endl;
  277. exit(1);
  278. }
  279. else
  280. { while (i >= h)
  281. i = i - h;
  282. while (j >= w)
  283. j = j - w;
  284. while (i < 0)
  285. i = i + h;
  286. while (j < 0)
  287. j = j + w;
  288. }
  289. cell[i][j] = state;
  290. }
  291.  
  292. // sets all cells to a given state.
  293. template <class T>
  294. void grid<T>::setall(T state)
  295. { for(int i = 0; i < h; i++)
  296. for (int j = 0; j < w; j++)
  297. cell[i][j] = state;
  298. }
  299.  
  300. // gets cell coordinates.
  301. template <class T>
  302. int grid<T>::getrow(int row)
  303. { if (r == false)
  304. return row;
  305. if (r == true)
  306. { while (row >= h)
  307. row = row - h;
  308. while (row < 0)
  309. row = row + h;
  310. }
  311. return row;
  312. }
  313. template <class T>
  314. int grid<T>::getcol(int col)
  315. { if (r == false)
  316. return col;
  317. if (r == true)
  318. { while (col >= h)
  319. col = col - h;
  320. while (col < 0)
  321. col = col + h;
  322. }
  323. return col;
  324. }
  325.  
  326.  
  327. // copies the input grid.
  328. template <class T>
  329. void grid<T>::copy(T **grid)
  330. { for(int i = 0; i < h; i++)
  331. for (int j = 0; j < w; j++)
  332. cell[i][j] = grid[i][j];
  333. }
  334.  
  335. // sums the states of neighbors plus self of a given cell.
  336. template <class T>
  337. T grid<T>::neighborhoodsum(int row, int col, int v_off, int h_off)
  338. { T sum = 0;
  339. for(int i = row-v_off; i <= row+v_off; i++)
  340. for(int j = col-h_off; j <= col+h_off; j++)
  341. sum += getstate(i,j);
  342. return sum;
  343. }
  344.  
  345. // sums all states in the grid.
  346. template <class T>
  347. T grid<T>::gridsum()
  348. { T sum = 0;
  349. for (int i = 0; i < h; i++)
  350. for (int j = 0; j < w; j++)
  351. sum += cell[i][j];
  352. return sum;
  353. }
  354.  
  355. // picks cell coordinates randomly
  356. template <class T>
  357. int grid<T>::randrow()
  358. { return (rand()%h);
  359. }
  360. template <class T>
  361. int grid<T>::randcol()
  362. { return (rand()%w);
  363. }
  364.  
  365. // picks cell coordinates randomly from a neighborhood
  366. template <class T>
  367. int grid<T>::randneighborrow(int row, int h_off)
  368. { int randrow;
  369. if (rand()%2 == 1)
  370. randrow = getrow( row + (rand()%(h_off+1)) );
  371. else
  372. randrow = getrow( row - (rand()%(h_off+1)) );
  373. return randrow;
  374. }
  375. template <class T>
  376. int grid<T>::randneighborcol(int col, int v_off)
  377. { int randcol;
  378. if (rand()%2 == 1)
  379. randcol = getcol( col + (rand()%(v_off+1)) );
  380. else
  381. randcol = getcol( col - (rand()%(v_off+1)) );
  382. return randcol;
  383. }
  384.  
  385. // picks cell coordinates randomly from a neighborhood with a chance proportional to value
  386. template <class T>
  387. int* grid<T>::randompickprop(int row, int col, int v_off, int h_off)
  388. { int* rowcol = new int[2];
  389. T neighsum = neighborhoodsum(row,col,v_off,h_off);
  390. int randomnr = int(rand()%neighsum); //TODO: fix for floats
  391. T tempsum = 0;
  392. for(int i = row-v_off; i <= row+v_off; i++)
  393. for(int j = col-h_off; j <= col+h_off; j++)
  394. { tempsum += getstate(i,j);
  395. if (tempsum >= randomnr)
  396. { rowcol[1] = i;
  397. rowcol[2] = j;
  398. break;
  399. }
  400. }
  401. return rowcol;
  402. }
  403.  
  404. /*
  405. // displays the cells of a grid to screen
  406. template <class T>
  407. void grid<T>::display(Display* display, CMwindow &win, T min, T max, int steps, GC* gc)
  408. { for (int i = 0; i < h; i++)
  409.   { for (int j = 0; j < w; j++)
  410.   for (int step = 0; step < steps; step++)
  411.   if (cell[i][j] >= step*(max - min)/steps)
  412.   win.paintcell(i, j, display, gc[step]);
  413.   }
  414. }
  415. */
  416.  
  417. // returns the maximal value in the grid.
  418. template <class T>
  419. T grid<T>::max()
  420. { T maxval = cell[0][0];
  421. for (int i = 0; i < h; i++)
  422. for (int j = 0; j < w; j++)
  423. if (cell[i][j] > maxval)
  424. maxval = cell[i][j];
  425. return maxval;
  426. }
  427.  
  428. // returns the minimal value in the grid.
  429. template <class T>
  430. T grid<T>::min()
  431. { T minval = cell[0][0];
  432. for (int i = 0; i < h; i++)
  433. for (int j = 0; j < w; j++)
  434. if (cell[i][j] < minval)
  435. minval = cell[i][j];
  436. return minval;
  437. }
  438.  
  439.  
  440. // prints cell states of the entire grid to terminal.
  441. template <class T>
  442. void grid<T>::print()
  443. { for (int i = 0; i < h; i++)
  444. { for (int j = 0; j < w; j++)
  445. std::cout << cell[i][j] << "\t";
  446. std::cout << std::endl;
  447. }
  448. }
  449.  
  450. // prints cell states of the entire grid to file.
  451. template <class T>
  452. void grid<T>::print(std::ofstream file)
  453. { for (int i = 0; i < h; i++)
  454. { for (int j = 0; j < w; j++)
  455. file << cell[i][j] << "\t";
  456. file << std::endl;
  457. }
  458. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 12
Reputation: Daan is an unknown quantity at this point 
Solved Threads: 0
Daan Daan is offline Offline
Newbie Poster

Re: Passing a filestream, how?

 
0
  #5
Aug 3rd, 2006
Originally Posted by GloriousEremite
Try passing by reference
  1. void grid<T>::print(std::ofstream &file)
in stead of
  1. void grid<T>::print(std::ofstream file)
was all I did and it worked. Thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC