quadgrid 0.1
simple cartesian quad grid with particles for c++/octave
Loading...
Searching...
No Matches
drift_diffusion.h
Go to the documentation of this file.
1#ifndef DRIFT_DIFFUSION_H
2#define DRIFT_DIFFUSION_H
3
4#include <json.hpp>
5#include <particles.h>
6
7#include <fstream>
8#include <iostream>
9#include <map>
10#include <random>
11#include <vector>
12
13#include "counter.h"
14#include <timer.h>
15#include <quadgrid_config.h>
16
17
18template<typename PVAR_t>
19class
20stepper {
21private :
22 PVAR_t x;
23 PVAR_t y;
24 PVAR_t vx;
25 PVAR_t vy;
26
27 real_t dt;
28
29public :
30
31 stepper (PVAR_t x_, PVAR_t y_,
32 PVAR_t vx_, PVAR_t vy_,
33 real_t dt_)
34 : x(x_), y(y_), vx(vx_), vy(vy_), dt{dt_} { }
35
37
41
42 DEVICE
43 void operator() (int n) {
44
45 //update particles positions
46 x[n] += vx[n] * dt;
47 y[n] += vy[n] * dt;
48
49 // Apply boundary conditions (unelastic walls)
50 y[n] = fmin (1.999, fmax (0.001, y[n]));
51 x[n] = fmin (1.999, fmax (0.001, x[n]));
52 }
53
54};
55
56
57template<typename PVAR_t, typename GVAR_t, typename P2C_t>
58class
60
62 const PVAR_t x;
63 const PVAR_t y;
64 const PVAR_t dprop1;
65 const PVAR_t dprop2;
66 const P2C_t ptcl_to_grd;
67 const idx_t nrows;
68 const real_t hx;
69 const real_t hy;
70 GVAR_t M;
71 GVAR_t gvar;
73
74public:
75
76 p2g_step1 (const PVAR_t x_, const PVAR_t y_, GVAR_t M_,
77 GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_,
78 const real_t hx_, const real_t hy_, const PVAR_t dprop1_, const PVAR_t dprop2_, bool apply_mass_)
79 : x(x_), y(y_), M(M_), gvar(gvar_),
80 ptcl_to_grd(ptcl_to_grd_), nrows(nrows_), hx(hx_), hy(hy_),
81 dprop1(dprop1_), dprop2(dprop2_), apply_mass(apply_mass_) {};
82
83
84 DEVICE
85 void
87
88 using qgt = quadgrid_t<GVAR_t>;
89 real_t N = 0.0;
90 auto xx = x[ip];
91 auto yy = y[ip];
92 auto r = qgt::gind2row (ptcl_to_grd[ip], nrows);
93 auto c = qgt::gind2col (ptcl_to_grd[ip], nrows);
94 for (idx_t inode = 0; inode < 4; ++inode) {
95 N = apply_mass ? qgt::shp (xx, yy, inode, c, r, hx, hy)/M[qgt::gt(inode, c, r, nrows)] :
96 qgt::shp (xx, yy, inode, c, r, hx, hy);
97 atomicAdd(&(gvar[qgt::gt(inode, c, r, nrows)]), N*dprop1[ip]*dprop2[ip]);
98 }
99 }
100
101};
102
103
104template<typename PVAR_t, typename GVAR_t, typename P2C_t>
105class
107
109 const PVAR_t x;
110 const PVAR_t y;
111 const PVAR_t dpropx;
112 const PVAR_t dpropy;
113 const P2C_t ptcl_to_grd;
115 const real_t hx;
116 const real_t hy;
117 const real_t D;
118 GVAR_t M;
119 GVAR_t gvar;
120
122
123public :
124
125 p2gd_step2 (const PVAR_t x_, const PVAR_t y_, const GVAR_t M_,
126 GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_,
127 const real_t hx_, const real_t hy_, const real_t D_, const PVAR_t dpropx_, const PVAR_t dpropy_, bool apply_mass_)
128 : x(x_), y(y_), M(M_), gvar(gvar_),
129 ptcl_to_grd(ptcl_to_grd_), nrows(nrows_), hx(hx_), hy(hy_),
130 D(D_), dpropx(dpropx_), dpropy(dpropy_), apply_mass(apply_mass_) {};
131
132 DEVICE
133 void
134 operator() (idx_t ip) {
135 using qgt = quadgrid_t<GVAR_t>;
136 real_t Nx = 0.0, Ny = 0.0;
137 auto xx = x[ip];
138 auto yy = y[ip];
139 auto r = qgt::gind2row (ptcl_to_grd[ip], nrows);
140 auto c = qgt::gind2col (ptcl_to_grd[ip], nrows);
141 for (idx_t inode = 0; inode < 4; ++inode) {
142 Nx = apply_mass ? qgt::shg (xx, yy, 0, inode, c, r, hx, hy) / M[qgt::gt(inode, c, r, nrows)] :
143 qgt::shg (xx, yy, 0, inode, c, r, hx, hy);
144 Ny = apply_mass ? qgt::shg (xx, yy, 1, inode, c, r, hx, hy) / M[qgt::gt(inode, c, r, nrows)] :
145 qgt::shg (xx, yy, 1, inode, c, r, hx, hy);
146
147 atomicAdd(&(gvar[qgt::gt(inode, c, r, nrows)]), (Nx*dpropx[ip] + Ny*dpropy[ip])*D);
148 }
149 }
150
151};
152
153
154template<typename GVAR_t, typename PVAR_t, typename P2C_t>
155class
156g2p_step3 {
157
159 PVAR_t x;
160 PVAR_t y;
161 const GVAR_t M;
162 const GVAR_t gvar1;
163 const GVAR_t gvar2;
164 const P2C_t ptcl_to_grd;
166 const real_t hx;
167 const real_t hy;
168 PVAR_t dprop;
170
171public :
172
173 g2p_step3 (PVAR_t x_, PVAR_t y_, const GVAR_t M_,
174 const GVAR_t gvar1_, const GVAR_t gvar2_, const P2C_t ptcl_to_grd_, const idx_t nrows_,
175 const real_t hx_, const real_t hy_, PVAR_t dprop_, bool apply_mass_)
176 : x(x_), y(y_), M(M_), gvar1(gvar1_), gvar2(gvar2_),
177 ptcl_to_grd(ptcl_to_grd_), nrows(nrows_), hx(hx_), hy(hy_),
178 dprop(dprop_), apply_mass(apply_mass_) {};
179
180 DEVICE
181 void
182 operator() (idx_t ip) {
183 using qgt = quadgrid_t<GVAR_t>;
184 real_t N = 0.0;
185 auto xx = x[ip];
186 auto yy = y[ip];
187 auto r = qgt::gind2row (ptcl_to_grd[ip], nrows);
188 auto c = qgt::gind2col (ptcl_to_grd[ip], nrows);
189 for (idx_t inode = 0; inode < 4; ++inode) {
190 N = apply_mass ? qgt::shp (xx, yy, inode, c, r, hx, hy) * M[qgt::gt(inode, c, r, nrows)] :
191 qgt::shp (xx, yy, inode, c, r, hx, hy);
192 dprop[ip] += N * (gvar1[qgt::gt(inode, c, r, nrows)] + gvar2[qgt::gt(inode, c, r, nrows)]);
193 }
194 }
195};
196
197
198template<typename GVAR_t, typename PVAR_t, typename P2C_t>
199class
201
203 PVAR_t x;
204 PVAR_t y;
205 const GVAR_t M;
206 const GVAR_t gvar1, gvar2, gvar3, gvar4;
207 const P2C_t ptcl_to_grd;
209 const real_t hx;
210 const real_t hy;
211 PVAR_t dprop;
213
214public :
215
216 g2pd_step4 (PVAR_t x_, PVAR_t y_, const GVAR_t M_,
217 const GVAR_t gvar1_, const GVAR_t gvar2_, const GVAR_t gvar3_, const GVAR_t gvar4_, const P2C_t ptcl_to_grd_, const idx_t nrows_,
218 const real_t hx_, const real_t hy_, PVAR_t dprop_,
219 bool apply_mass_)
220 : x(x_), y(y_), M(M_), gvar1(gvar1_), gvar2(gvar2_), gvar3(gvar3_), gvar4(gvar4_),
221 ptcl_to_grd(ptcl_to_grd_), nrows(nrows_), hx(hx_), hy(hy_),
222 dprop(dprop_), apply_mass(apply_mass_) {};
223
224 DEVICE
225 void
226 operator() (idx_t ip) {
227 using qgt = quadgrid_t<GVAR_t>;
228 real_t Nx = 0.0, Ny = 0.0;
229 auto xx = x[ip];
230 auto yy = y[ip];
231 auto r = qgt::gind2row (ptcl_to_grd[ip], nrows);
232 auto c = qgt::gind2col (ptcl_to_grd[ip], nrows);
233
234 for (idx_t inode = 0; inode < 4; ++inode) {
235 Nx = apply_mass ?
236 qgt::shg (xx, yy, 0, inode, c, r, hx, hy) * M[qgt::gt(inode, c, r, nrows)] :
237 qgt::shg (xx, yy, 0, inode, c, r, hx, hy);
238 Ny = apply_mass ?
239 qgt::shg (xx, yy, 1, inode, c, r, hx, hy) * M[qgt::gt(inode, c, r, nrows)] :
240 qgt::shg (xx, yy, 1, inode, c, r, hx, hy);
241 dprop[ip] += Nx * (gvar1[qgt::gt(inode, c, r, nrows)] + gvar2[qgt::gt(inode, c, r, nrows)]) +
242 Ny * (gvar3[qgt::gt(inode, c, r, nrows)] + gvar4[qgt::gt(inode, c, r, nrows)]);;
243
244 }
245 }
246};
247
248template<typename PVAR_t>
249class
251 PVAR_t rho, divV;
254public:
255 updateRho(PVAR_t rho_, PVAR_t divV_, real_t dt_):
256 rho(rho_), divV(divV_), dt(dt_){};
257
258 DEVICE
259 void
260 operator() (idx_t ip){
261 rho[ip] = rho[ip]/(1 + dt*divV[ip]);
262 }
263
264};
265
266
267#endif
void atomicAdd(T *x, const T y)
Definition atomicAdd.h:9
particles_t::idx_t idx_t
const real_t hx
g2p_step3(PVAR_t x_, PVAR_t y_, const GVAR_t M_, const GVAR_t gvar1_, const GVAR_t gvar2_, const P2C_t ptcl_to_grd_, const idx_t nrows_, const real_t hx_, const real_t hy_, PVAR_t dprop_, bool apply_mass_)
const idx_t nrows
const GVAR_t gvar1
const GVAR_t M
const real_t hy
const GVAR_t gvar2
const P2C_t ptcl_to_grd
const real_t hy
const idx_t nrows
const GVAR_t gvar3
const real_t hx
particles_t::idx_t idx_t
const P2C_t ptcl_to_grd
g2pd_step4(PVAR_t x_, PVAR_t y_, const GVAR_t M_, const GVAR_t gvar1_, const GVAR_t gvar2_, const GVAR_t gvar3_, const GVAR_t gvar4_, const P2C_t ptcl_to_grd_, const idx_t nrows_, const real_t hx_, const real_t hy_, PVAR_t dprop_, bool apply_mass_)
const GVAR_t M
const GVAR_t gvar2
const GVAR_t gvar1
const GVAR_t gvar4
const PVAR_t y
const PVAR_t x
const PVAR_t dprop1
const real_t hx
const idx_t nrows
const PVAR_t dprop2
const P2C_t ptcl_to_grd
DEVICE void operator()(idx_t ip)
const real_t hy
p2g_step1(const PVAR_t x_, const PVAR_t y_, GVAR_t M_, GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_, const real_t hx_, const real_t hy_, const PVAR_t dprop1_, const PVAR_t dprop2_, bool apply_mass_)
particles_t::idx_t idx_t
const idx_t nrows
particles_t::idx_t idx_t
const PVAR_t y
const PVAR_t dpropy
const PVAR_t x
const real_t hx
const real_t D
const PVAR_t dpropx
const P2C_t ptcl_to_grd
p2gd_step2(const PVAR_t x_, const PVAR_t y_, const GVAR_t M_, GVAR_t gvar_, const P2C_t ptcl_to_grd_, const idx_t nrows_, const real_t hx_, const real_t hy_, const real_t D_, const PVAR_t dpropx_, const PVAR_t dpropy_, bool apply_mass_)
const real_t hy
Functor class for moving particles.
std::vector< double > & vx
std::vector< double > & y
stepper(PVAR_t x_, PVAR_t y_, PVAR_t vx_, PVAR_t vy_, real_t dt_)
std::vector< double > & x
std::vector< double > & vy
particles_t::idx_t idx_t
updateRho(PVAR_t rho_, PVAR_t divV_, real_t dt_)
#define DEVICE
double real_t
quadgrid_t< vector_t< real_t > >::idx_t idx_t
datatype for indexing into vectors of properties
Definition particles.h:32