9 #define M_PI 3.14159265358979323846
34 template<
typename T,
typename U,
typename V>
51 template<
typename T,
typename U,
typename V>
64 T
pdfGauss(std::vector<T> vars, std::vector<T> scales);
88 template<
typename T,
typename U,
typename V,
typename W,
typename G>
89 void initGauss(T gdict, U refldict, V *res_field, V *res_current);
109 template<
typename T,
typename U,
typename V,
typename W,
typename G>
129 template<
typename T,
typename U,
typename V,
typename W>
130 void calcJM(T *res_field, T *res_current, V refldict,
int mode);
132 template<
typename T,
typename U,
typename V>
135 std::array<V, 3> nomChief = {0, 0, 1};
136 std::array<V, 3> zero = {0, 0, 0};
140 int nTot = 1 + rdict.nRing * 4 * rdict.nRays;
147 if (rdict.nRays > 0) {d_alpha = 2 * M_PI / (4 * rdict.nRays);}
151 std::array<V, 3> rotation;
152 std::array<V, 3> direction;
162 std::array<V, 3> pos;
164 for (
int i=1; i<nTot; i++)
166 fr->x[i] = rdict.x0 * cos(alpha) / rdict.nRing * n;
167 fr->y[i] = rdict.y0 * sin(alpha) / rdict.nRing * n;
170 rotation[0] = rdict.angy0 * sin(alpha) / rdict.nRing * n;
171 rotation[1] = rdict.angx0 * cos(alpha) / rdict.nRing * n;
172 rotation[2] = 2 * alpha;
174 ut.
matRot(rotation, nomChief, zero, direction);
176 fr->dx[i] = direction[0];
177 fr->dy[i] = direction[1];
178 fr->dz[i] = direction[2];
181 if (i ==
int(nTot / rdict.nRing) * n)
189 template<
typename T,
typename U,
typename V>
193 std::array<V, 3> nomChief = {0, 0, 1};
194 std::array<V, 3> zero = {0, 0, 0};
198 if (grdict.seed != -1) {
Random<V> rando(grdict.seed);}
200 fr->size = grdict.nRays;
202 std::array<V, 3> rotation;
203 std::array<V, 3> direction;
204 std::array<V, 3> pos;
207 std::vector<V> scales{grdict.x0, grdict.y0, grdict.angx0, grdict.angy0};
220 std::vector<V> xi(4, 0);
223 while (n_suc < grdict.nRays)
231 for (
int k = 0; k<4; k++) {xi[k] = xi[k] * thres * scales[k];}
232 if (pdfGauss<V>(xi, scales) > yi)
235 rotation = {xi[3], xi[2], 0};
236 ut.
matRot(rotation, nomChief, zero, direction);
237 pos = {xi[0], xi[1], 0};
239 fr->x[n_suc] = pos[0];
240 fr->y[n_suc] = pos[1];
241 fr->z[n_suc] = pos[2];
243 fr->dx[n_suc] = direction[0];
244 fr->dy[n_suc] = direction[1];
245 fr->dz[n_suc] = direction[2];
252 T
pdfGauss(std::vector<T> vars, std::vector<T> scales)
254 T norm = 1 / (M_PI*M_PI * scales[0] * scales[1] * scales[2] * scales[3]);
256 return norm * exp(-vars[0]*vars[0] / (scales[0]*scales[0])) * exp(-vars[1]*vars[1] / (scales[1]*scales[1])) *
257 exp(-vars[2]*vars[2] / (scales[2]*scales[2])) * exp(-vars[3]*vars[3] / (scales[3]*scales[3]));
261 template<
typename T,
typename U,
typename V,
typename W,
typename G>
262 void initGauss(T gdict, U refldict, V *res_field, V *res_current)
264 int nTot = refldict.n_cells[0] * refldict.n_cells[1];
270 reflc.x =
new G[nTot];
271 reflc.y =
new G[nTot];
272 reflc.z =
new G[nTot];
274 reflc.nx =
new G[nTot];
275 reflc.ny =
new G[nTot];
276 reflc.nz =
new G[nTot];
278 reflc.area =
new G[nTot];
282 bool transform =
true;
286 G zRx = M_PI * gdict.w0x*gdict.w0x * gdict.n / gdict.lam;
287 G zRy = M_PI * gdict.w0y*gdict.w0y * gdict.n / gdict.lam;
288 G k = 2 * M_PI / gdict.lam;
297 std::complex<G> j(0, 1);
299 std::complex<G> field_atPoint;
300 std::array<std::complex<G>, 3> efield;
301 std::array<G, 3> n_source;
302 std::array<std::complex<G>, 3> m;
304 for (
int i=0; i<nTot; i++)
306 wzx = gdict.w0x * sqrt(1 + (reflc.z[i] / zRx)*(reflc.z[i] / zRx));
307 wzy = gdict.w0y * sqrt(1 + ((reflc.z[i] - gdict.dxyz) / zRy)*((reflc.z[i] - gdict.dxyz) / zRy));
308 Rzx_inv = reflc.z[i] / (reflc.z[i]*reflc.z[i] + zRx*zRx);
309 Rzy_inv = (reflc.z[i] - gdict.dxyz) / ((reflc.z[i] - gdict.dxyz)*(reflc.z[i] - gdict.dxyz) + zRy*zRy);
310 phizx = atan(reflc.z[i] / zRx);
311 phizy = atan((reflc.z[i] - gdict.dxyz) / zRy);
316 field_atPoint = gdict.E0 * exp(-(reflc.x[i]/wzx)*(reflc.x[i]/wzx) - (reflc.y[i]/wzy)*(reflc.y[i]/wzy) -
317 j*M_PI/gdict.lam * (reflc.x[i]*reflc.x[i]*Rzx_inv + reflc.y[i]*reflc.y[i]*Rzy_inv) - j*k*reflc.z[i] + j*(phizx - phizy)*0.5);
319 efield[0] = field_atPoint * gdict.pol[0];
320 efield[1] = field_atPoint * gdict.pol[1];
321 efield[2] = field_atPoint * gdict.pol[2];
323 n_source[0] = reflc.nx[i];
324 n_source[1] = reflc.ny[i];
325 n_source[2] = reflc.nz[i];
327 ut.
ext(n_source, efield, m);
329 res_field->r1x[i] = efield[0].real();
330 res_field->i1x[i] = efield[0].imag();
332 res_field->r1y[i] = efield[1].real();
333 res_field->i1y[i] = efield[1].imag();
335 res_field->r1z[i] = efield[2].real();
336 res_field->i1z[i] = efield[2].imag();
339 res_field->r2x[i] = 0;
340 res_field->i2x[i] = 0;
342 res_field->r2y[i] = 0;
343 res_field->i2y[i] = 0;
345 res_field->r2z[i] = 0;
346 res_field->i2z[i] = 0;
349 res_current->r1x[i] = 0;
350 res_current->i1x[i] = 0;
352 res_current->r1y[i] = 0;
353 res_current->i1y[i] = 0;
355 res_current->r1z[i] = 0;
356 res_current->i1z[i] = 0;
359 res_current->r2x[i] = -2*m[0].real();
360 res_current->i2x[i] = -2*m[0].imag();
362 res_current->r2y[i] = -2*m[1].real();
363 res_current->i2y[i] = -2*m[1].imag();
365 res_current->r2z[i] = -2*m[2].real();
366 res_current->i2z[i] = -2*m[2].imag();
380 template<
typename T,
typename U,
typename V,
typename W,
typename G>
383 int nTot = refldict.n_cells[0] * refldict.n_cells[1];
387 reflc.x =
new G[nTot];
388 reflc.y =
new G[nTot];
389 reflc.z =
new G[nTot];
391 reflc.nx =
new G[nTot];
392 reflc.ny =
new G[nTot];
393 reflc.nz =
new G[nTot];
395 reflc.area =
new G[nTot];
398 bool transform =
true;
401 G zRx = M_PI * sgdict.w0x*sgdict.w0x * sgdict.n / sgdict.lam;
402 G zRy = M_PI * sgdict.w0y*sgdict.w0y * sgdict.n / sgdict.lam;
403 G k = 2 * M_PI / sgdict.lam;
411 std::complex<G> j(0, 1);
413 std::complex<G> efield;
415 for (
int i=0; i<nTot; i++)
417 wzx = sgdict.w0x * sqrt(1 + (reflc.z[i] / zRx)*(reflc.z[i] / zRx));
418 wzy = sgdict.w0y * sqrt(1 + ((reflc.z[i] - sgdict.dxyz) / zRy)*((reflc.z[i] - sgdict.dxyz) / zRy));
419 Rzx_inv = reflc.z[i] / (reflc.z[i]*reflc.z[i] + zRx*zRx);
420 Rzy_inv = (reflc.z[i] - sgdict.dxyz) / ((reflc.z[i] - sgdict.dxyz)*(reflc.z[i] - sgdict.dxyz) + zRy*zRy);
421 phizx = atan(reflc.z[i] / zRx);
422 phizy = atan((reflc.z[i] - sgdict.dxyz) / zRy);
424 efield = sgdict.E0 * sqrt(2 / (M_PI * wzx * wzy)) * exp(-(reflc.x[i]/wzx)*(reflc.x[i]/wzx) - (reflc.y[i]/wzy)*(reflc.y[i]/wzy) -
425 j*M_PI/sgdict.lam * (reflc.x[i]*reflc.x[i]*Rzx_inv + reflc.y[i]*reflc.y[i]*Rzy_inv) - j*k*reflc.z[i] + j*(phizx - phizy)*0.5);
427 res_field->x[i] = efield.real();
428 res_field->y[i] = efield.imag();
442 template<
typename T,
typename U,
typename V,
typename W>
443 void calcJM(T *res_field, T *res_current, V refldict,
int mode)
446 int nTot = refldict.n_cells[0] * refldict.n_cells[1];
450 reflc.x =
new U[nTot];
451 reflc.y =
new U[nTot];
452 reflc.z =
new U[nTot];
454 reflc.nx =
new U[nTot];
455 reflc.ny =
new U[nTot];
456 reflc.nz =
new U[nTot];
458 reflc.area =
new U[nTot];
460 bool transform =
true;
465 std::array<std::complex<U>, 3> field;
466 std::array<U, 3> n_source;
468 std::array<std::complex<U>, 3> js;
469 std::array<std::complex<U>, 3> ms;
474 for (
int i=0; i<nTot; i++)
476 field[0] = {res_field->r1x[i], res_field->i1x[i]};
477 field[1] = {res_field->r1y[i], res_field->i1y[i]};
478 field[2] = {res_field->r1z[i], res_field->i1z[i]};
480 n_source[0] = reflc.nx[i];
481 n_source[1] = reflc.ny[i];
482 n_source[2] = reflc.nz[i];
484 ut.
ext(n_source, field, ms);
486 res_current->r2x[i] = -ms[0].real();
487 res_current->i2x[i] = -ms[0].imag();
489 res_current->r2y[i] = -ms[1].real();
490 res_current->i2y[i] = -ms[1].imag();
492 res_current->r2z[i] = -ms[2].real();
493 res_current->i2z[i] = -ms[2].imag();
495 field[0] = {res_field->r2x[i], res_field->i2x[i]};
496 field[1] = {res_field->r2y[i], res_field->i2y[i]};
497 field[2] = {res_field->r2z[i], res_field->i2z[i]};
499 ut.
ext(n_source, field, js);
501 res_current->r1x[i] = js[0].real();
502 res_current->i1x[i] = js[0].imag();
504 res_current->r1y[i] = js[1].real();
505 res_current->i1y[i] = js[1].imag();
507 res_current->r1z[i] = js[2].real();
508 res_current->i1z[i] = js[2].imag();
515 for (
int i=0; i<nTot; i++)
517 field[0] = {res_field->r1x[i], res_field->i1x[i]};
518 field[1] = {res_field->r1y[i], res_field->i1y[i]};
519 field[2] = {res_field->r1z[i], res_field->i1z[i]};
521 n_source[0] = reflc.nx[i];
522 n_source[1] = reflc.ny[i];
523 n_source[2] = reflc.nz[i];
525 ut.
ext(n_source, field, ms);
527 res_current->r2x[i] = -2*ms[0].real();
528 res_current->i2x[i] = -2*ms[0].imag();
530 res_current->r2y[i] = -2*ms[1].real();
531 res_current->i2y[i] = -2*ms[1].imag();
533 res_current->r2z[i] = -2*ms[2].real();
534 res_current->i2z[i] = -2*ms[2].imag();
536 res_current->r1x[i] = 0;
537 res_current->i1x[i] = 0;
539 res_current->r1y[i] = 0;
540 res_current->i1y[i] = 0;
542 res_current->r1z[i] = 0;
543 res_current->i1z[i] = 0;
550 for (
int i=0; i<nTot; i++)
552 field[0] = {res_field->r2x[i], res_field->i2x[i]};
553 field[1] = {res_field->r2y[i], res_field->i2y[i]};
554 field[2] = {res_field->r2z[i], res_field->i2z[i]};
556 n_source[0] = reflc.nx[i];
557 n_source[1] = reflc.ny[i];
558 n_source[2] = reflc.nz[i];
560 ut.
ext(n_source, field, js);
562 res_current->r1x[i] = 2*js[0].real();
563 res_current->i1x[i] = 2*js[0].imag();
565 res_current->r1y[i] = 2*js[1].real();
566 res_current->i1y[i] = 2*js[1].imag();
568 res_current->r1z[i] = 2*js[2].real();
569 res_current->i1z[i] = 2*js[2].imag();
571 res_current->r2x[i] = 0;
572 res_current->i2x[i] = 0;
574 res_current->r2y[i] = 0;
575 res_current->i2y[i] = 0;
577 res_current->r2z[i] = 0;
578 res_current->i2z[i] = 0;
void initScalarGauss(T sgdict, U refldict, V *res_field)
Definition: BeamInit.h:381
T pdfGauss(std::vector< T > vars, std::vector< T > scales)
Definition: BeamInit.h:252
void initFrame(T rdict, U *fr)
Definition: BeamInit.h:133
void calcJM(T *res_field, T *res_current, V refldict, int mode)
Definition: BeamInit.h:443
void initGauss(T gdict, U refldict, V *res_field, V *res_current)
Definition: BeamInit.h:262
void initRTGauss(T grdict, U *fr)
Definition: BeamInit.h:190
void generateGrid(reflparams refl, reflcontainer *container, bool transform, bool spheric)
Definition: InterfaceReflector.cpp:880
Header for reflector generation interface.
Structs used within PyPO.
Linear algebra functions for the CPU version of PyPO.
T generateUniform(T lower=-1.0)
Definition: Random.h:77
void ext(const std::array< T, 3 > &v1, const std::array< T, 3 > &v2, std::array< T, 3 > &out)
Definition: Utils.h:170
void matRot(const std::array< T, 3 > &rot, const std::array< T, 3 > &v1, const std::array< T, 3 > &cRot, std::array< T, 3 > &out)
Definition: Utils.h:613