Advertisement
Guest User

Untitled

a guest
Mar 28th, 2020
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. //
  2. // diffusion1d.cc
  3. //
  4. // 1d diffusion with periodic boundary conditions
  5. //
  6. // Compile with make using provided Makefile
  7. //
  8.  
  9. #include <fstream>
  10. #include <rarray>
  11. #include "diffusion1d_output.h"
  12. #include "diffusion1d_timestep.h"
  13. #include "parameters.h"
  14. #include <omp.h>
  15. #include <iostream>
  16. #include <string>
  17.  
  18. // the main function drives the simulation
  19. int main(int argc, char *argv[])
  20. {
  21. // Simulation parameters
  22. double L; // system length
  23. double D; // diffusion constant
  24. double T; // time
  25. double dx; // spatial resolution
  26. double dt; // temporal resolution (time step)
  27. int Z; // number of walkers (dummy variable, not used)
  28. std::string datafile; // filename for output
  29. double time_between_output;
  30.  
  31. // Read parameters from a file given on the command line.
  32. // If no file was given, use "params.ini".
  33. std::string paramFilename = argc>1?argv[1]:"params.ini";
  34. read_parameters(paramFilename, L, D, T, dx, dt, Z, datafile, time_between_output);
  35.  
  36. // Compute derived parameters
  37. const int numSteps = int(T/dt + 0.5); // number of steps to take
  38. const int N = int(L/dx + 0.5); // number of grid points
  39. const int outputEvery = int(time_between_output/dt + 0.5); // how many steps between output
  40. const int outputcols = 48; // number of columns for sparkline output
  41.  
  42. // Allocate density data
  43. rvector<double> P(N);
  44.  
  45. // Setup initial conditions for P
  46. P.fill(0.0);
  47. P[N/2] = 1.0;
  48.  
  49. // Setup initial time
  50. double time = 0.0;
  51.  
  52. // Open a file for data output
  53. std::ofstream file;
  54. diffusion1d_output_init(file, datafile);
  55.  
  56. // Initial output
  57. diffusion1d_output(file, 0, time, P, outputcols);
  58.  
  59. #pragma omp parallel default(none) shared(P,D,dt,dx,file,time)
  60. {
  61. for(int step = 1; step <= numSteps; step++) {
  62. //int t = omp_get_thread_num();
  63. diffusion1d_timestep(P, D, dt, dx);
  64. // Compute next time point
  65.  
  66. #pragma omp single
  67. {
  68. // Update time
  69. time += dt;
  70.  
  71. // Periodically add data to the file
  72. if (step % outputEvery == 0 and step > 0)
  73. diffusion1d_output(file, step, time, P, outputcols);
  74. }
  75. }
  76.  
  77. }
  78.  
  79. // Close file
  80. diffusion1d_output_finish(file);
  81.  
  82. // All done
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement