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.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_bin_index(x, nx, xmin, xmax) floor( nx * (x-xmin) / (xmax-xmin) )
22
24#define math_copy(a,b) do { a[0]=b[0];a[1]=b[1];a[2]=b[2]; } while(0)
25
27#define math_matcopy(a,b) do { a[0]=b[0];a[1]=b[1];a[2]=b[2];a[3]=b[3]; \
28 a[4]=b[4];a[5]=b[5];a[6]=b[6];a[7]=b[7];a[8]=b[8]; } while(0)
29
31#define math_dot(a,b) (a[0]*b[0]+a[1]*b[1]+a[2]*b[2])
32
34#define math_cross(a,b,c) do { c[0]=a[1]*b[2]-a[2]*b[1]; \
35 c[1]=a[2]*b[0]-a[0]*b[2];c[2]=a[0]*b[1]-a[1]*b[0]; } while(0)
36
42#define math_scalar_triple_product(a,b,c) \
43 ( - (a[2] * b[1] * c[0] ) \
44 + (a[1] * b[2] * c[0] ) \
45 + (a[2] * b[0] * c[1] ) \
46 - (a[0] * b[2] * c[1] ) \
47 - (a[1] * b[0] * c[2] ) \
48 + (a[0] * b[1] * c[2] ) )
49
51#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)
52
54#define math_prod(a,b) do { a[0]=a[0]*b;a[1]=a[1]*b;a[2]=a[2]*b; } while(0)
55
57#define math_matsumew(a,b) do { a[0]=a[0]+b[0];a[1]=a[1]+b[1];a[2]=a[2]+b[2]; \
58 a[3]=a[3]+b[3];a[4]=a[4]+b[4];a[5]=a[5]+b[5];a[6]=a[6]+b[6]; \
59 a[7]=a[7]+b[7];a[8]=a[8]+b[8]; } while(0)
60
62#define math_matprod(a,b) do { a[0]=a[0]*b;a[1]=a[1]*b;a[2]=a[2]*b; \
63 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; \
64 a[8]=a[8]*b } while(0)
65
67#define math_norm(a) (sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]))
68
70#define math_normc(a1, a2, a3) (sqrt(a1*a1+a2*a2+a3*a3))
71
73#define math_unit(a, b) do {real _n=sqrt(a[0]*a[0]+a[1]*a[1]+a[2]*a[2]); \
74 b[0]=a[0]/_n;b[1]=a[1]/_n;b[2]=a[2]/_n; } while(0)
75
77#define math_xyz2rpz(xyz, rpz) do { rpz[0]=sqrt(xyz[0]*xyz[0]+xyz[1]*xyz[1]); \
78 rpz[1]=atan2(xyz[1],xyz[0]);rpz[2]=xyz[2]; } while(0)
79
81#define math_rpz2xyz(rpz, xyz) do { xyz[0]=rpz[0]*cos(rpz[1]); \
82 xyz[1]=rpz[0]*sin(rpz[1]);xyz[2]=rpz[2]; } while(0)
83
86#define math_vec_rpz2xyz(vrpz, vxyz, phi) do { \
87 vxyz[0]=vrpz[0]*cos(phi)-vrpz[1]*sin(phi); \
88 vxyz[1]=vrpz[0]*sin(phi)+vrpz[1]*cos(phi); \
89 vxyz[2]=vrpz[2]; } while(0)
90
93#define math_vec_xyz2rpz(vxyz, vrpz, phi) do { \
94 vrpz[0]=vxyz[0]*cos(phi)+vxyz[1]*sin(phi); \
95 vrpz[1]=-vxyz[0]*sin(phi)+vxyz[1]*cos(phi); \
96 vrpz[2]=vxyz[2]; } while(0)
97
100#define math_determinant3x3( \
101 x1, x2, x3, \
102 y1, y2, y3, \
103 z1, z2, z3) \
104 (x1) * ( (y2)*(z3) - (y3)*(z2) ) + \
105 (x2) * ( (y3)*(z1) - (y1)*(z3) ) + \
106 (x3) * ( (y1)*(z2) - (y2)*(z1) )
107
108
110#define math_deg2rad(a) (a * math_degrad)
111
113#define math_rad2deg(a) (a * math_raddeg)
114
115DECLARE_TARGET
116real fmod(real x, real y);
117DECLARE_TARGET_END
118DECLARE_TARGET_SIMD
119void math_jac_rpz2xyz(real* rpz, real* xyz, real r, real phi);
120GPU_DECLARE_TARGET_SIMD
121void math_jac_xyz2rpz(real* xyz, real* rpz, real r, real phi);
122DECLARE_TARGET_END
123GPU_DECLARE_TARGET_SIMD
124void math_matmul(real* matA, real* matB, int d1, int d2, int d3, real* matC);
125DECLARE_TARGET_END
126DECLARE_TARGET_SIMD
128DECLARE_TARGET_SIMD
129int math_ipow(int a, int p);
130double math_simpson(double (*f)(double), double a, double b, double epsilon);
131void math_linspace(real* vec, real a, real b, int n);
132DECLARE_TARGET_SIMD
133int math_point_on_plane(real q[3], real t1[3], real t2[3], real t3[3]);
134DECLARE_TARGET_SIMD
136 real AP[3], real AB[3], real AC[3], real n[3], real *s, real *t);
137void math_uniquecount(int* in, int* unique, int* count, int n);
138DECLARE_TARGET_SIMD
139real* math_rsearch(const real key, const real* base, int num);
140DECLARE_TARGET_SIMD_UNIFORM(rv,zv,n)
141int math_point_in_polygon(real r, real z, real* rv, real* zv, int n);
142
143
144#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