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
30 real** offload_array) {
31 // Do no initialization
32
33 int n = offload_data->n;
34 real rmin = (*offload_array)[0], rmax = (*offload_array)[0];
35 real zmin = (*offload_array)[n], zmax = (*offload_array)[n];
36 for(int i=0; i<n; i++) {
37 rmin = fmin(rmin, (*offload_array)[i]);
38 rmax = fmax(rmax, (*offload_array)[i]);
39 zmin = fmin(zmin, (*offload_array)[n+i]);
40 zmax = fmax(zmax, (*offload_array)[n+i]);
41 }
42
43 print_out(VERBOSE_IO, "\n2D wall model (wall_2D)\n");
44 print_out(VERBOSE_IO, "Number of wall elements = %d,"
45 " R extend = [%2.2f, %2.2f], z extend = [%2.2f, %2.2f]\n",
46 n, rmin, rmax, zmin, zmax);
47
48 return 0;
49}
50
63 real** offload_array) {
64 free(*offload_array);
65 *offload_array = NULL;
66}
67
79 real* offload_array) {
80 w->n = offload_data->n;
81 w->wall_r = &offload_array[0];
82 w->wall_z = &offload_array[offload_data->n];
83}
84
102 int hits = 0;
103 for(int i = 0; i < w->n; i++) {
104 real wr1, wr2, wz1, wz2;
105 if(i == w->n - 1) {
106 wz1 = w->wall_z[i] - z;
107 wz2 = w->wall_z[0] - z;
108 wr1 = w->wall_r[i] - r;
109 wr2 = w->wall_r[0] - r;
110 } else {
111 wz1 = w->wall_z[i] - z;
112 wz2 = w->wall_z[i+1] - z;
113 wr1 = w->wall_r[i] - r;
114 wr2 = w->wall_r[i+1] - r;
115 }
116 if(wz1 * wz2 < 0) {
117 real ri = wr1 + (wz1*(wr2-wr1)) / (wz1-wz2);
118 if(ri > 0) {
119 hits++;
120 }
121 }
122 }
123 return hits % 2;
124}
125
142int wall_2d_hit_wall(real r1, real phi1, real z1, real r2, real phi2, real z2,
143 wall_2d_data* w, real* w_coll) {
144 int tile = 0;
145 if(!wall_2d_inside(r2, z2, w)) {
146 tile = wall_2d_find_intersection(r1, z1, r2, z2, w, w_coll);
147 }
148 return tile;
149}
150
168 wall_2d_data* w, real* w_coll) {
169 int tile = 0;
170 real t0 = 2.0; // Helper variable to pick the closest intersection
171 for(int i=0; i<w->n; i++) {
172 real r3, z3, r4, z4;
173 if(i == w->n-1) {
174 r3 = w->wall_r[i];
175 z3 = w->wall_z[i];
176 r4 = w->wall_r[0];
177 z4 = w->wall_z[0];
178 } else {
179 r3 = w->wall_r[i];
180 z3 = w->wall_z[i];
181 r4 = w->wall_r[i+1];
182 z4 = w->wall_z[i+1];
183 }
184
185 real div = (r1 - r2) * (z3 - z4) - (z1 - z2) * (r3 - r4);
186 real t = ( (r1 - r3) * (z3 - z4) - (z1 - z3) * (r3 - r4) ) / div;
187 real u = ( (r1 - r3) * (z1 - z2) - (z1 - z3) * (r1 - r2) ) / div;
188 if(0 <= t && t <= 1.0 && 0 <= u && u <= 1.0 && t < t0) {
189 t0 = t;
190 tile = i + 1;
191 }
192 }
193 *w_coll = t0;
194 return tile;
195}
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:23
real * wall_z
Definition wall_2d.h:26
real * wall_r
Definition wall_2d.h:25
2D wall offload data
Definition wall_2d.h:13
void wall_2d_free_offload(wall_2d_offload_data *offload_data, real **offload_array)
Free offload array and reset parameters.
Definition wall_2d.c:62
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:167
void wall_2d_init(wall_2d_data *w, wall_2d_offload_data *offload_data, real *offload_array)
Initialize 2D wall data struct on target.
Definition wall_2d.c:78
int wall_2d_inside(real r, real z, wall_2d_data *w)
Check if coordinates are within 2D polygon wall.
Definition wall_2d.c:101
int wall_2d_init_offload(wall_2d_offload_data *offload_data, real **offload_array)
Load 2D wall data and prepare parameters.
Definition wall_2d.c:29
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:142
Header file for wall_2d.c.