ASCOT5
Loading...
Searching...
No Matches
math.h
Go to the documentation of this file.
1
5#ifndef MATH_H
6#define MATH_H
7
8#include "offload_acc_omp.h"
9#include <math.h>
10#include "ascot5.h"
11
13#define math_degrad 0.0174532925199432957692
15#define math_raddeg 57.295779513082320876798
16
18#define math_maxSimpsonDepth 20
19
21#define math_copy(a,b) do { a[0]=b[0];a[1]=b[1];a[2]=b[2]; } while(0)
22
24#define math_matcopy(a,b) do { a[0]=b[0];a[1]=b[1];a[2]=b[2];a[3]=b[3]; \
25 a[4]=b[4];a[5]=b[5];a[6]=b[6];a[7]=b[7];a[8]=b[8]; } while(0)
26
28#define math_dot(a,b) (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
29
31#define math_cross(a,b,c) do { c[0]=a[1]*b[2]-a[2]*b[1]; \
32 c[1]=a[2]*b[0]-a[0]*b[2];c[2]=a[0]*b[1]-a[1]*b[0]; } while(0)
33
39#define math_scalar_triple_product(a,b,c) \
40 ( - (a[2] * b[1] * c[0] ) \
41 + (a[1] * b[2] * c[0] ) \
42 + (a[2] * b[0] * c[1] ) \
43 - (a[0] * b[2] * c[1] ) \
44 - (a[1] * b[0] * c[2] ) \
45 + (a[0] * b[1] * c[2] ) )
46
48#define math_sumew(a,b) do { a[0]=a[0]+b[0];a[1]=a[1]+b[1];a[2]=a[2]+b[2] } while(0)
49
51#define math_prod(a,b) do { a[0]=a[0]*b;a[1]=a[1]*b;a[2]=a[2]*b; } while(0)
52
54#define math_matsumew(a,b) do { a[0]=a[0]+b[0];a[1]=a[1]+b[1];a[2]=a[2]+b[2]; \
55 a[3]=a[3]+b[3];a[4]=a[4]+b[4];a[5]=a[5]+b[5];a[6]=a[6]+b[6]; \
56 a[7]=a[7]+b[7];a[8]=a[8]+b[8]; } while(0)
57
59#define math_matprod(a,b) do { a[0]=a[0]*b;a[1]=a[1]*b;a[2]=a[2]*b; \
60 a[3]=a[3]*b;a[4]=a[4]*b;a[5]=a[5]*b;a[6]=a[6]*b;a[7]=a[7]*b; \
61 a[8]=a[8]*b } while(0)
62
64#define math_norm(a) (sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]))
65
67#define math_normc(a1, a2, a3) (sqrt(a1*a1+a2*a2+a3*a3))
68
70#define math_unit(a, b) do {real _n=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]); \
71 b[0]=a[0]/_n;b[1]=a[1]/_n;b[2]=a[2]/_n; } while(0)
72
74#define math_xyz2rpz(xyz, rpz) do { rpz[0]=sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]); \
75 rpz[1]=atan2(xyz[1],xyz[0]);rpz[2]=xyz[2]; } while(0)
76
78#define math_rpz2xyz(rpz, xyz) do { xyz[0]=rpz[0]*cos(rpz[1]); \
79 xyz[1]=rpz[0]*sin(rpz[1]);xyz[2]=rpz[2]; } while(0)
80
83#define math_vec_rpz2xyz(vrpz, vxyz, phi) do { \
84 vxyz[0]=vrpz[0]*cos(phi)-vrpz[1]*sin(phi); \
85 vxyz[1]=vrpz[0]*sin(phi)+vrpz[1]*cos(phi); \
86 vxyz[2]=vrpz[2]; } while(0)
87
90#define math_vec_xyz2rpz(vxyz, vrpz, phi) do { \
91 vrpz[0]=vxyz[0]*cos(phi)+vxyz[1]*sin(phi); \
92 vrpz[1]=-vxyz[0]*sin(phi)+vxyz[1]*cos(phi); \
93 vrpz[2]=vxyz[2]; } while(0)
94
97#define math_determinant3x3( \
98 x1, x2, x3, \
99 y1, y2, y3, \
100 z1, z2, z3) \
101 (x1) * ( (y2)*(z3) - (y3)*(z2) ) + \
102 (x2) * ( (y3)*(z1) - (y1)*(z3) ) + \
103 (x3) * ( (y1)*(z2) - (y2)*(z1) )
104
105
107#define math_deg2rad(a) (a * math_degrad)
108
110#define math_rad2deg(a) (a * math_raddeg)
111
112DECLARE_TARGET
113real fmod(real x, real y);
114DECLARE_TARGET_END
115DECLARE_TARGET_SIMD
116void math_jac_rpz2xyz(real* rpz, real* xyz, real r, real phi);
117GPU_DECLARE_TARGET_SIMD
118void math_jac_xyz2rpz(real* xyz, real* rpz, real r, real phi);
119DECLARE_TARGET_END
120GPU_DECLARE_TARGET_SIMD
121void math_matmul(real* matA, real* matB, int d1, int d2, int d3, real* matC);
122DECLARE_TARGET_END
123DECLARE_TARGET_SIMD
125DECLARE_TARGET_SIMD
126int math_ipow(int a, int p);
127double math_simpson(double (*f)(double), double a, double b, double epsilon);
128void math_linspace(real* vec, real a, real b, int n);
129DECLARE_TARGET_SIMD
130int math_point_on_plane(real q[3], real t1[3], real t2[3], real t3[3]);
131DECLARE_TARGET_SIMD
133 real AP[3], real AB[3], real AC[3], real n[3], real *s, real *t);
134void math_uniquecount(int* in, int* unique, int* count, int n);
135DECLARE_TARGET_SIMD
136real* math_rsearch(const real key, const real* base, int num);
137DECLARE_TARGET_SIMD_UNIFORM(rv,zv,n)
138int math_point_in_polygon(real r, real z, real* rv, real* zv, int n);
139
140
141#endif
Main header file for ASCOT5.
double real
Definition ascot5.h:85
Header file for math.c.
void math_uniquecount(int *in, int *unique, int *count, int n)
Find unique numbers and their frequency in given array.
Definition math.c:276
DECLARE_TARGET_END DECLARE_TARGET_SIMD real math_normal_rand()
Generate normally distributed random numbers.
Definition math.c:188
DECLARE_TARGET_SIMD void math_barycentric_coords_triangle(real AP[3], real AB[3], real AC[3], real n[3], real *s, real *t)
Find barycentric coordinates for a given point.
Definition math.c:360
double math_simpson(double(*f)(double), double a, double b, double epsilon)
Adaptive Simpsons rule for integral.
Definition math.c:232
int math_point_in_polygon(real r, real z, real *rv, real *zv, int n)
Check if coordinates are within polygon.
Definition math.c:427
DECLARE_TARGET_SIMD real * math_rsearch(const real key, const real *base, int num)
Search for array element preceding a key value.
Definition math.c:405
GPU_DECLARE_TARGET_SIMD void math_jac_xyz2rpz(real *xyz, real *rpz, real r, real phi)
Convert a Jacobian from cartesian to cylindrical coordinates.
Definition math.c:103
DECLARE_TARGET_END DECLARE_TARGET_SIMD void math_jac_rpz2xyz(real *rpz, real *xyz, real r, real phi)
Convert a Jacobian from cylindrical to cartesian coordinates.
Definition math.c:44
DECLARE_TARGET_SIMD int math_ipow(int a, int p)
Calculate a^p where both a and p are integers (p >= 0)
Definition math.c:210
DECLARE_TARGET_SIMD int math_point_on_plane(real q[3], real t1[3], real t2[3], real t3[3])
Find if a point is on a given plane.
Definition math.c:324
DECLARE_TARGET real fmod(real x, real y)
Compute the modulus of two real numbers.
Definition math.c:22
DECLARE_TARGET_END GPU_DECLARE_TARGET_SIMD void math_matmul(real *matA, real *matB, int d1, int d2, int d3, real *matC)
Matrix multiplication.
Definition math.c:159
void math_linspace(real *vec, real a, real b, int n)
Generate linearly spaced vector.
Definition math.c:251