main.cpp 5.68 KB
 incardon committed Apr 30, 2017 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 #include "Grid/grid_dist_id.hpp" #include "data_type/aggregate.hpp" #include "timer.hpp" /*! * \page Grid_3_gs Grid 3 Gray Scott in 3D * * # Solving a gray scott-system in 3D # {#e3_gs_gray_scott} * * This example is just an extension of the 2D Gray scott example. * Here we show how to solve a non-linear reaction diffusion system in 3D * * \see \ref Grid_2_solve_eq * * \snippet Grid/3_gray_scott/main.cpp constants * */ //! \cond [constants] \endcond constexpr int U = 0; constexpr int V = 1; constexpr int x = 0; constexpr int y = 1; constexpr int z = 2; void init(grid_dist_id<3,double,aggregate > & Old, grid_dist_id<3,double,aggregate > & New, Box<3,double> & domain) { auto it = Old.getDomainIterator(); while (it.isNext()) { // Get the local grid key auto key = it.get(); // Old values U and V Old.template get(key) = 1.0; Old.template get(key) = 0.0; // Old values U and V New.template get(key) = 0.0; New.template get(key) = 0.0; ++it; }  incardon committed Jun 03, 2017 49 50 51 52 53 54 55 56 57 58  long int x_start = Old.size(0)*1.55f/domain.getHigh(0); long int y_start = Old.size(1)*1.55f/domain.getHigh(1); long int z_start = Old.size(1)*1.55f/domain.getHigh(2); long int x_stop = Old.size(0)*1.85f/domain.getHigh(0); long int y_stop = Old.size(1)*1.85f/domain.getHigh(1); long int z_stop = Old.size(1)*1.85f/domain.getHigh(2); grid_key_dx<3> start({x_start,y_start,z_start}); grid_key_dx<3> stop ({x_stop,y_stop,z_stop});  incardon committed Apr 30, 2017 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82  auto it_init = Old.getSubDomainIterator(start,stop); while (it_init.isNext()) { auto key = it_init.get(); Old.template get(key) = 0.5 + (((double)std::rand())/RAND_MAX -0.5)/10.0; Old.template get(key) = 0.25 + (((double)std::rand())/RAND_MAX -0.5)/20.0; ++it_init; } } //! \cond [end fun] \endcond int main(int argc, char* argv[]) { openfpm_init(&argc,&argv); // domain Box<3,double> domain({0.0,0.0},{2.5,2.5,2.5}); // grid size  incardon committed Jun 03, 2017 83  size_t sz[3] = {128,128,128};  incardon committed Apr 30, 2017 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100  // Define periodicity of the grid periodicity<3> bc = {PERIODIC,PERIODIC,PERIODIC}; // Ghost in grid unit Ghost<3,long int> g(1); // deltaT double deltaT = 1; // Diffusion constant for specie U double du = 2*1e-5; // Diffusion constant for specie V double dv = 1*1e-5; // Number of timesteps  incardon committed Jun 03, 2017 101  size_t timeSteps = 5000;  incardon committed Apr 30, 2017 102 103  // K and F (Physical constant in the equation)  incardon committed Jun 03, 2017 104 105  double K = 0.014; double F = 0.053;  incardon committed Apr 30, 2017 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196  //! \cond [init lib] \endcond /*! * \page Grid_3_gs Grid 3 Gray Scott * * Here we create 2 distributed grid in 2D Old and New. In particular because we want that * the second grid is distributed across processors in the same way we pass the decomposition * of the Old grid to the New one in the constructor with **Old.getDecomposition()**. Doing this, * we force the two grid to have the same decomposition. * * \snippet Grid/3_gray_scott/main.cpp init grid * */ //! \cond [init grid] \endcond grid_dist_id<3, double, aggregate> Old(sz,domain,g,bc); // New grid with the decomposition of the old grid grid_dist_id<3, double, aggregate> New(Old.getDecomposition(),sz,g); // spacing of the grid on x and y double spacing[3] = {Old.spacing(0),Old.spacing(1),Old.spacing(2)}; init(Old,New,domain); // sync the ghost size_t count = 0; Old.template ghost_get(); // because we assume that spacing[x] == spacing[y] we use formula 2 // and we calculate the prefactor of Eq 2 double uFactor = deltaT * du/(spacing[x]*spacing[x]); double vFactor = deltaT * dv/(spacing[x]*spacing[x]); for (size_t i = 0; i < timeSteps; ++i) { if (i % 300 == 0) std::cout << "STEP: " << i << std::endl; auto it = Old.getDomainIterator(); while (it.isNext()) { auto key = it.get(); // update based on Eq 2 New.get(key) = Old.get(key) + uFactor * ( Old.get(key.move(x,1)) + Old.get(key.move(x,-1)) + Old.get(key.move(y,1)) + Old.get(key.move(y,-1)) + Old.get(key.move(z,1)) + Old.get(key.move(z,-1)) - 6.0*Old.get(key)) + - deltaT * Old.get(key) * Old.get(key) * Old.get(key) + - deltaT * F * (Old.get(key) - 1.0); // update based on Eq 2 New.get(key) = Old.get(key) + vFactor * ( Old.get(key.move(x,1)) + Old.get(key.move(x,-1)) + Old.get(key.move(y,1)) + Old.get(key.move(y,-1)) + Old.get(key.move(z,1)) + Old.get(key.move(z,-1)) - 6*Old.get(key)) + deltaT * Old.get(key) * Old.get(key) * Old.get(key) + - deltaT * (F+K) * Old.get(key); // Next point in the grid ++it; } // Here we copy New into the old grid in preparation of the new step // It would be better to alternate, but using this we can show the usage // of the function copy. To note that copy work only on two grid of the same // decomposition. If you want to copy also the decomposition, or force to be // exactly the same, use Old = New Old.copy(New); // After copy we synchronize again the ghost part U and V Old.ghost_get(); // Every 30 time step we output the configuration for // visualization if (i % 60 == 0) {  incardon committed Jun 03, 2017 197  Old.write_frame("output",count,VTK_WRITER | FORMAT_BINARY);  incardon committed Apr 30, 2017 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220  count++; } } //! \cond [time stepping] \endcond /*! * \page Grid_3_gs Grid 3 Gray Scott * * ## Finalize ## * * Deinitialize the library * * \snippet Grid/3_gray_scott/main.cpp finalize * */ //! \cond [finalize] \endcond openfpm_finalize(); //! \cond [finalize] \endcond }