ASCOT5
Loading...
Searching...
No Matches
wall_2d.c
Go to the documentation of this file.
1
5#include <stdio.h>
6#include <stdlib.h>
7#include <math.h>
8#include "../ascot5.h"
9#include "../print.h"
10#include "wall_2d.h"
11
22int wall_2d_init(wall_2d_data* data, int nelements, real* r, real* z) {
23
24 data->n = nelements;
25 data->wall_r = (real*) malloc( nelements * sizeof(real) );
26 data->wall_z = (real*) malloc( nelements * sizeof(real) );
27 real rmin = r[0], rmax = r[0];
28 real zmin = z[0], zmax = z[0];
29 for(int i=0; i < nelements; i++) {
30 rmin = fmin(rmin, r[i]);
31 rmax = fmax(rmax, r[i]);
32 zmin = fmin(zmin, z[i]);
33 zmax = fmax(zmax, z[i]);
34 data->wall_r[i] = r[i];
35 data->wall_z[i] = z[i];
36 }
37
38 print_out(VERBOSE_IO, "\n2D wall model (wall_2D)\n");
39 print_out(VERBOSE_IO, "Number of wall elements = %d,"
40 " R extend = [%2.2f, %2.2f], z extend = [%2.2f, %2.2f]\n",
41 nelements, rmin, rmax, zmin, zmax);
42 return 0;
43}
44
51 free(data->wall_r);
52 free(data->wall_z);
53}
54
61 GPU_MAP_TO_DEVICE(
62 data->wall_r[0:data->n],
63 data->wall_z[0:data->n]
64 )
65}
66
84 int hits = 0;
85 for(int i = 0; i < w->n; i++) {
86 real wr1, wr2, wz1, wz2;
87 if(i == w->n - 1) {
88 wz1 = w->wall_z[i] - z;
89 wz2 = w->wall_z[0] - z;
90 wr1 = w->wall_r[i] - r;
91 wr2 = w->wall_r[0] - r;
92 } else {
93 wz1 = w->wall_z[i] - z;
94 wz2 = w->wall_z[i+1] - z;
95 wr1 = w->wall_r[i] - r;
96 wr2 = w->wall_r[i+1] - r;
97 }
98 if(wz1 * wz2 < 0) {
99 real ri = wr1 + (wz1*(wr2-wr1)) / (wz1-wz2);
100 if(ri > 0) {
101 hits++;
102 }
103 }
104 }
105 return hits % 2;
106}
107
124int wall_2d_hit_wall(real r1, real phi1, real z1, real r2, real phi2, real z2,
125 wall_2d_data* w, real* w_coll) {
126 int tile = 0;
127 if(!wall_2d_inside(r2, z2, w)) {
128 tile = wall_2d_find_intersection(r1, z1, r2, z2, w, w_coll);
129 }
130 return tile;
131}
132
150 wall_2d_data* w, real* w_coll) {
151 int tile = 0;
152 real t0 = 2.0; // Helper variable to pick the closest intersection
153 for(int i=0; i<w->n; i++) {
154 real r3, z3, r4, z4;
155 if(i == w->n-1) {
156 r3 = w->wall_r[i];
157 z3 = w->wall_z[i];
158 r4 = w->wall_r[0];
159 z4 = w->wall_z[0];
160 } else {
161 r3 = w->wall_r[i];
162 z3 = w->wall_z[i];
163 r4 = w->wall_r[i+1];
164 z4 = w->wall_z[i+1];
165 }
166
167 real div = (r1 - r2) * (z3 - z4) - (z1 - z2) * (r3 - r4);
168 real t = ( (r1 - r3) * (z3 - z4) - (z1 - z3) * (r3 - r4) ) / div;
169 real u = ( (r1 - r3) * (z1 - z2) - (z1 - z3) * (r1 - r2) ) / div;
170 if(0 <= t && t <= 1.0 && 0 <= u && u <= 1.0 && t < t0) {
171 t0 = t;
172 tile = i + 1;
173 }
174 }
175 *w_coll = t0;
176 return tile;
177}
Main header file for ASCOT5.
double real
Definition ascot5.h:85
Header file for math.c.
Macros for printing console output.
#define print_out(v,...)
Print to standard output.
Definition print.h:31
@ VERBOSE_IO
Definition print.h:20
2D wall data parameters
Definition wall_2d.h:15
real * wall_z
Definition wall_2d.h:18
real * wall_r
Definition wall_2d.h:17
void wall_2d_offload(wall_2d_data *data)
Offload data to the accelerator.
Definition wall_2d.c:60
int wall_2d_find_intersection(real r1, real z1, real r2, real z2, wall_2d_data *w, real *w_coll)
Find intersection between the wall element and line segment.
Definition wall_2d.c:149
int wall_2d_inside(real r, real z, wall_2d_data *w)
Check if coordinates are within 2D polygon wall.
Definition wall_2d.c:83
void wall_2d_free(wall_2d_data *data)
Free allocated resources.
Definition wall_2d.c:50
int wall_2d_init(wall_2d_data *data, int nelements, real *r, real *z)
Load 2D wall data and prepare parameters.
Definition wall_2d.c:22
int wall_2d_hit_wall(real r1, real phi1, real z1, real r2, real phi2, real z2, wall_2d_data *w, real *w_coll)
Check if trajectory from (r1, phi1, z1) to (r2, phi2, z2) intersects the wall.
Definition wall_2d.c:124
Header file for wall_2d.c.