main.cpp 5.68 KB
Newer Older
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<double,double> > & Old, grid_dist_id<3,double,aggregate<double,double> > & 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<U>(key) = 1.0;
		Old.template get<V>(key) = 0.0;

		// Old values U and V
		New.template get<U>(key) = 0.0;
		New.template get<V>(key) = 0.0;

		++it;
	}

incardon's avatar
incardon committed
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});
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<U>(key) = 0.5 + (((double)std::rand())/RAND_MAX -0.5)/10.0;
                Old.template get<V>(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's avatar
incardon committed
83
        size_t sz[3] = {128,128,128};
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's avatar
incardon committed
101
        size_t timeSteps = 5000;
102 103

	// K and F (Physical constant in the equation)
incardon's avatar
incardon committed
104 105
        double K = 0.014;
        double F = 0.053;
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<double,double>> Old(sz,domain,g,bc);

	// New grid with the decomposition of the old grid
        grid_dist_id<3, double, aggregate<double,double>> 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<U,V>();

	// 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<U>(key) = Old.get<U>(key) + uFactor * (
										Old.get<U>(key.move(x,1)) +
										Old.get<U>(key.move(x,-1)) +
										Old.get<U>(key.move(y,1)) +
										Old.get<U>(key.move(y,-1)) +
										Old.get<U>(key.move(z,1)) +
										Old.get<U>(key.move(z,-1)) -
										6.0*Old.get<U>(key)) +
										- deltaT * Old.get<U>(key) * Old.get<V>(key) * Old.get<V>(key) +
										- deltaT * F * (Old.get<U>(key) - 1.0);


			// update based on Eq 2
			New.get<V>(key) = Old.get<V>(key) + vFactor * (
										Old.get<V>(key.move(x,1)) +
										Old.get<V>(key.move(x,-1)) +
										Old.get<V>(key.move(y,1)) +
										Old.get<V>(key.move(y,-1)) +
										Old.get<V>(key.move(z,1)) +
                                                                                Old.get<V>(key.move(z,-1)) -
										6*Old.get<V>(key)) +
										deltaT * Old.get<U>(key) * Old.get<V>(key) * Old.get<V>(key) +
										- deltaT * (F+K) * Old.get<V>(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<U,V>();

		// Every 30 time step we output the configuration for
		// visualization
		if (i % 60 == 0)
		{
incardon's avatar
incardon committed
197
			Old.write_frame("output",count,VTK_WRITER | FORMAT_BINARY);
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
}