Skip to content

Commit

Permalink
Kernel tested
Browse files Browse the repository at this point in the history
  • Loading branch information
giadarol committed Apr 11, 2021
1 parent 8dff1c3 commit 4976313
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import time

import numpy as np

import xobjects as xo
from xfields.contexts import add_default_kernels

from pysixtrack.be_beamfields.gaussian_fields import get_Ex_Ey_Gx_Gy_gauss
from pysixtrack.mathlibs import MathlibDefault

ctx = xo.ContextCpu()
ctx = xo.ContextCpu(omp_num_threads=4)
#ctx = xo.ContextCupy()
#ctx = xo.ContextPyopencl()

print(ctx)


kernel_descriptions = {'q_gaussian_profile':{
'args':(
(('scalar', np.int32 ), 'n'),
(('array', np.float64), 'z'),
(('scalar', np.float64), 'z0'),
(('scalar', np.float64), 'z_min'),
(('scalar', np.float64), 'z_max'),
(('scalar', np.float64), 'beta'),
(('scalar', np.float64), 'q'),
(('scalar', np.float64), 'q_tol'),
(('scalar', np.float64), 'factor'),
(('array', np.float64), 'res'),
),
'num_threads_from_arg': 'n'
},}

ctx.add_kernels(src_files=['../../../xfields/src/qgaussian.h'],
kernel_descriptions=kernel_descriptions)

import matplotlib.pyplot as plt
plt.close('all')
plt.figure(1)
for qq in [0.95, 1., 1.05]:
z = np.linspace(-2., 2., 1000)
res = 0*z
ctx.kernels.q_gaussian_profile(
n=len(z),
z=z,
z0=0.5,
z_min=-0.8,
z_max=1.9,
beta=1./2./0.5**2,
q=qq,
q_tol=1e-10,
factor=1,
res=res)
plt.plot(z, res, label=f'q={qq}')

plt.legend(loc='best')
plt.show()
24 changes: 15 additions & 9 deletions xfields/src/qgaussian.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#ifndef QGAUSSIAN
#define QGAUSSIAN

#include <math.h> //only_for_context none


/*gpukern*/
void q_gaussian_profile(
const int n,
const int n,
/*gpuglmem*/ const double* z,
const double z0,
const double z_min,
const double z_max,
const double beta,
const double q,
const double q_tol,
const double factor,
const double z0,
const double z_min,
const double z_max,
const double beta,
const double q,
const double q_tol,
const double factor,
/*gpuglmem*/ double* res){

if (fabs(q-1.) < q_tol){
Expand All @@ -35,7 +38,8 @@ void q_gaussian_profile(
if (zi<z_max && zi>z_min){
double zi_m_z0 = zi - z0;
double q_exp_arg = -(beta*zi_m_z0*zi_m_z0 );
double q_exp_res = pow(q_exp_arg, exponent );
double q_exp_res = pow(
(1.+(1.-q)*q_exp_arg), exponent );
res[ii] = factor*q_exp_res;
}
else{
Expand All @@ -44,3 +48,5 @@ void q_gaussian_profile(
}//end_vectorize
}
}

#endif

0 comments on commit 4976313

Please sign in to comment.