45 cbl::data::Field3D::Field3D (
const double deltaR,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ)
47 set_parameters(deltaR, minX, maxX, minY, maxY, minZ, maxZ);
54 cbl::data::Field3D::Field3D (
const int nx,
const int ny,
const int nz,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ)
56 set_parameters(nx, ny, nz, minX, maxX, minY, maxY, minZ, maxZ);
72 double DeltaX = m_MaxX-m_MinX;
73 double DeltaY = m_MaxY-m_MinY;
74 double DeltaZ = m_MaxZ-m_MinZ;
77 m_nX = (m_nX%2==0) ? m_nX : m_nX+1;
78 m_deltaX = DeltaX/m_nX;
81 m_nY = (m_nY%2==0) ? m_nY : m_nY+1;
82 m_deltaY = DeltaY/m_nY;
85 m_nZ = (m_nZ%2==0) ? m_nZ : m_nZ+1;
86 m_deltaZ = DeltaZ/m_nZ;
88 m_Volume = DeltaX*DeltaY*DeltaZ;
92 m_nCells = m_nX*m_nY*m_nZ;
93 m_nCells_Fourier = m_nX*m_nY*m_nZF;
97 double xfact = 2*
par::pi/DeltaX;
98 double yfact = 2*
par::pi/DeltaY;
99 double zfact = 2*
par::pi/DeltaZ;
101 for(
int i=0; i<m_nX; i++){
102 m_X.push_back(m_MinX+(i+0.5)*m_deltaX);
103 m_kX.push_back( (i<=m_nX/2) ? xfact*i : -(m_nX-i)*xfact);
106 for(
int i=0; i<m_nY; i++){
107 m_Y.push_back(m_MinY+(i+0.5)*m_deltaY);
108 m_kY.push_back( (i<=m_nY/2) ? yfact*i : -(m_nY-i)*yfact);
111 for(
int i=0; i<m_nZ; i++)
112 m_Z.push_back(m_MinZ+(i+0.5)*m_deltaZ);
114 for(
int i=0; i<m_nZF; i++)
115 m_kZ.push_back(zfact*i);
123 void cbl::data::Field3D::set_parameters (
const int nx,
const int ny,
const int nz,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ)
132 m_nX = (nx%2==0) ? nx : nx+1;
133 m_nY = (ny%2==0) ? ny : ny+1;
134 m_nZ = (nz%2==0) ? nz : nz+1;
138 double DeltaX = m_MaxX-m_MinX;
139 double DeltaY = m_MaxY-m_MinY;
140 double DeltaZ = m_MaxZ-m_MinZ;
142 m_deltaX = DeltaX/m_nX;
143 m_deltaY = DeltaY/m_nY;
144 m_deltaZ = DeltaZ/m_nZ;
146 m_Volume = DeltaX*DeltaY*DeltaZ;
150 m_nCells = m_nX*m_nY*m_nZ;
151 m_nCells_Fourier = m_nX*m_nY*m_nZF;
155 double xfact = 2*
par::pi/DeltaX;
156 double yfact = 2*
par::pi/DeltaY;
157 double zfact = 2*
par::pi/DeltaZ;
159 for(
int i=0; i<m_nX; i++){
160 m_X.push_back(m_MinX+(i+0.5)*m_deltaX);
161 m_kX.push_back( (i<=m_nX/2) ? xfact*i : -(m_nX-i)*xfact);
164 for(
int i=0; i<m_nY; i++){
165 m_Y.push_back(m_MinY+(i+0.5)*m_deltaY);
166 m_kY.push_back( (i<=m_nY/2) ? yfact*i : -(m_nY-i)*yfact);
169 for(
int i=0; i<m_nZ; i++)
170 m_Z.push_back(m_MinZ+(i+0.5)*m_deltaZ);
172 for(
int i=0; i<m_nZF; i++)
173 m_kZ.push_back(zfact*i);
180 cbl::data::ScalarField3D::ScalarField3D (
const double deltaR,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ) :
Field3D(deltaR, minX, maxX, minY, maxY, minZ, maxZ)
198 cbl::data::ScalarField3D::ScalarField3D (
const int nx,
const int ny,
const int nz,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ) :
Field3D(nx, ny, nz, minX, maxX, minY, maxY, minZ, maxZ)
220 for (
int i=0; i<m_nCells_Fourier; i++) {
221 m_field_FourierSpace[i][0] = 0;
222 m_field_FourierSpace[i][1] = 0;
225 fftw_plan real2complex;
226 real2complex = fftw_plan_dft_r2c_3d(m_nX, m_nY, m_nZ, m_field, m_field_FourierSpace, FFTW_ESTIMATE);
227 fftw_execute(real2complex);
228 fftw_destroy_plan(real2complex);
230 for(
int i=0;i<m_nCells_Fourier;i++){
231 m_field_FourierSpace[i][0] = m_field_FourierSpace[i][0]/m_nCells;
232 m_field_FourierSpace[i][1] = m_field_FourierSpace[i][1]/m_nCells;
243 for (
int i=0; i<m_nCells; i++)
246 fftw_plan complex2real;
247 complex2real = fftw_plan_dft_c2r_3d(m_nX, m_nY, m_nZ, m_field_FourierSpace, m_field, FFTW_ESTIMATE);
248 fftw_execute(complex2real);
249 fftw_destroy_plan(complex2real);
259 FourierTransformField();
261 double xfact = 2*
par::pi/(m_nX*m_deltaX);
262 double yfact = 2*
par::pi/(m_nY*m_deltaY);
263 double zfact = 2*
par::pi/(m_nZ*m_deltaZ);
265 double ks2 = kernel_size*kernel_size;
267 for (
int i=0; i<m_nX; i++) {
268 double kx = (i<=m_nX/2) ? xfact*i : -(m_nX-i)*xfact;
269 for (
int j=0; j<m_nY; j++) {
270 double ky = (j<=m_nY/2) ? yfact*j : -(m_nY-j)*yfact;
271 for (
int k=0; k<m_nZF; k++) {
273 double kk2 = kx*kx+ky*ky+kz*kz;
274 long int index = inds_to_index_Fourier(i,j,k);
275 m_field_FourierSpace[index][0] = m_field_FourierSpace[index][0]*exp(-0.5*kk2*ks2);
276 m_field_FourierSpace[index][1] = m_field_FourierSpace[index][1]*exp(-0.5*kk2*ks2);
281 FourierAntiTransformField();
291 m_field[inds_to_index(i,j,k)] = (add) ? m_field[inds_to_index(i,j,k)] + value : value;
300 m_field_FourierSpace[inds_to_index_Fourier(i,j,k)][0] = (add) ? m_field_FourierSpace[inds_to_index_Fourier(i,j,k)][0] + value: value;
309 m_field_FourierSpace[inds_to_index_Fourier(i,j,k)][1] = (add) ? m_field_FourierSpace[inds_to_index_Fourier(i,j,k)][1] + value: value;
318 return m_field[inds_to_index(i,j,k)];
327 return m_field_FourierSpace[inds_to_index_Fourier(i,j,k)][0];
336 return m_field_FourierSpace[inds_to_index_Fourier(i,j,k)][1];
345 int i = min(
int((pos[0]-m_MinX)/m_deltaX), m_nX-1);
346 int j = min(
int((pos[1]-m_MinY)/m_deltaY), m_nY-1);
347 int k = min(
int((pos[2]-m_MinZ)/m_deltaZ), m_nZ-1);
349 return m_field[inds_to_index(i,j,k)];
358 vector<double> vv(m_field, m_field+m_nCells);
368 for(
int i=0;i<m_nCells;i++)
371 for(
int i=0;i<m_nCells_Fourier;i++){
372 m_field_FourierSpace[i][0] = 0;
373 m_field_FourierSpace[i][1] = 0;
380 cbl::data::VectorField3D::VectorField3D (
const double deltaR,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ) :
Field3D(deltaR, minX, maxX, minY, maxY, minZ, maxZ)
414 cbl::data::VectorField3D::VectorField3D (
const int nx,
const int ny,
const int nz,
const double minX,
const double maxX,
const double minY,
const double maxY,
const double minZ,
const double maxZ) :
Field3D(nx, ny, nz, minX, maxX, minY, maxY, minZ, maxZ)
452 for(
int i=0;i<m_nCells_Fourier;i++){
453 m_field_FourierSpace[0][i][0] = 0;
454 m_field_FourierSpace[0][i][1] = 0;
455 m_field_FourierSpace[1][i][0] = 0;
456 m_field_FourierSpace[1][i][1] = 0;
457 m_field_FourierSpace[2][i][0] = 0;
458 m_field_FourierSpace[2][i][1] = 0;
461 fftw_plan real2complex;
463 real2complex = fftw_plan_dft_r2c_3d(m_nX, m_nY, m_nZ, m_field[0], m_field_FourierSpace[0], FFTW_ESTIMATE);
464 fftw_execute(real2complex);
465 fftw_destroy_plan(real2complex);
467 real2complex = fftw_plan_dft_r2c_3d(m_nX, m_nY, m_nZ, m_field[1], m_field_FourierSpace[1], FFTW_ESTIMATE);
468 fftw_execute(real2complex);
469 fftw_destroy_plan(real2complex);
471 real2complex = fftw_plan_dft_r2c_3d(m_nX, m_nY, m_nZ, m_field[2], m_field_FourierSpace[2], FFTW_ESTIMATE);
472 fftw_execute(real2complex);
473 fftw_destroy_plan(real2complex);
475 for(
int i=0;i<m_nCells_Fourier;i++){
476 m_field_FourierSpace[0][i][0] = m_field_FourierSpace[0][i][0]/m_nCells;
477 m_field_FourierSpace[0][i][1] = m_field_FourierSpace[0][i][1]/m_nCells;
478 m_field_FourierSpace[1][i][0] = m_field_FourierSpace[1][i][0]/m_nCells;
479 m_field_FourierSpace[1][i][1] = m_field_FourierSpace[1][i][1]/m_nCells;
480 m_field_FourierSpace[2][i][0] = m_field_FourierSpace[2][i][0]/m_nCells;
481 m_field_FourierSpace[2][i][1] = m_field_FourierSpace[2][i][1]/m_nCells;
492 for (
int i=0; i<m_nCells; i++) {
498 fftw_plan complex2real;
500 complex2real = fftw_plan_dft_c2r_3d(m_nX, m_nY, m_nZ, m_field_FourierSpace[0], m_field[0], FFTW_ESTIMATE);
501 fftw_execute(complex2real);
502 fftw_destroy_plan(complex2real);
504 complex2real = fftw_plan_dft_c2r_3d(m_nX, m_nY, m_nZ, m_field_FourierSpace[1], m_field[1], FFTW_ESTIMATE);
505 fftw_execute(complex2real);
506 fftw_destroy_plan(complex2real);
508 complex2real = fftw_plan_dft_c2r_3d(m_nX, m_nY, m_nZ, m_field_FourierSpace[2], m_field[2], FFTW_ESTIMATE);
509 fftw_execute(complex2real);
510 fftw_destroy_plan(complex2real);
520 m_field[0][inds_to_index(i,j,k)] = (add) ? m_field[0][inds_to_index(i,j,k)]+value[0]: value[0];
521 m_field[1][inds_to_index(i,j,k)] = (add) ? m_field[1][inds_to_index(i,j,k)]+value[1]: value[1];
522 m_field[2][inds_to_index(i,j,k)] = (add) ? m_field[2][inds_to_index(i,j,k)]+value[2]: value[2];
531 m_field_FourierSpace[0][inds_to_index_Fourier(i,j,k)][0] = (add) ? m_field_FourierSpace[0][inds_to_index(i,j,k)][0]+value[0] : value[0];
532 m_field_FourierSpace[1][inds_to_index_Fourier(i,j,k)][0] = (add) ? m_field_FourierSpace[1][inds_to_index(i,j,k)][0]+value[1] : value[1];
533 m_field_FourierSpace[2][inds_to_index_Fourier(i,j,k)][0] = (add) ? m_field_FourierSpace[2][inds_to_index(i,j,k)][0]+value[2] : value[2];
542 m_field_FourierSpace[0][inds_to_index_Fourier(i,j,k)][1] = (add) ? m_field_FourierSpace[0][inds_to_index(i,j,k)][1]+value[0] : value[0];
543 m_field_FourierSpace[1][inds_to_index_Fourier(i,j,k)][1] = (add) ? m_field_FourierSpace[1][inds_to_index(i,j,k)][1]+value[1] : value[1];
544 m_field_FourierSpace[2][inds_to_index_Fourier(i,j,k)][1] = (add) ? m_field_FourierSpace[2][inds_to_index(i,j,k)][1]+value[2] : value[2];
553 return {m_field[0][inds_to_index(i,j,k)], m_field[1][inds_to_index(i,j,k)], m_field[2][inds_to_index(i,j,k)]};
562 return {m_field_FourierSpace[0][inds_to_index_Fourier(i,j,k)][0], m_field_FourierSpace[1][inds_to_index_Fourier(i,j,k)][0], m_field_FourierSpace[2][inds_to_index_Fourier(i,j,k)][0]};
571 return {m_field_FourierSpace[0][inds_to_index_Fourier(i,j,k)][1], m_field_FourierSpace[1][inds_to_index_Fourier(i,j,k)][1], m_field_FourierSpace[2][inds_to_index_Fourier(i,j,k)][1]};
580 int i = min(
int((pos[0]-m_MinX)/m_deltaX), m_nX-1);
581 int j = min(
int((pos[1]-m_MinY)/m_deltaY), m_nY-1);
582 int k = min(
int((pos[2]-m_MinZ)/m_deltaZ), m_nZ-1);
584 return {m_field[0][inds_to_index(i,j,k)], m_field[1][inds_to_index(i,j,k)], m_field[2][inds_to_index(i,j,k)]};
593 for(
int i=0;i<m_nCells;i++){
598 for(
int i=0;i<m_nCells_Fourier;i++){
599 m_field_FourierSpace[0][i][0] = 0;
600 m_field_FourierSpace[0][i][1] = 0;
601 m_field_FourierSpace[1][i][0] = 0;
602 m_field_FourierSpace[1][i][1] = 0;
603 m_field_FourierSpace[2][i][0] = 0;
604 m_field_FourierSpace[2][i][1] = 0;
int m_nCells
number of cells
Field3D()=default
default constructor
void set_parameters(const double deltaR, const double minX, const double maxX, const double minY, const double maxY, const double minZ, const double maxZ)
set the parameters
virtual std::vector< double > ScalarField_FourierSpace_real() const
get the value of the scalar field, Fourier space, real part
virtual std::vector< double > ScalarField_FourierSpace_complex() const
get the value of the scalar field, Fourier space, complex part
int m_nCells_Fourier
number of cells, Fourier space
ScalarField3D()=default
default constructor
std::vector< double > ScalarField() const
get the values of the scalar field
void set_ScalarField_FourierSpace_complex(const double value, const int i, const int j, const int k, const bool add=0)
set the value of the scalar field in Fourier space, real part
void FourierTransformField()
perform the Fourier transform on the field
void FourierAntiTransformField()
perform the anti-Fourier transform on the field
double * m_field
scalar field
void GaussianConvolutionField(const double kernel_size)
perform a smoothing of the field with a gaussian kernel
void reset()
set to 0 the fields
void set_ScalarField_FourierSpace_real(const double value, const int i, const int j, const int k, const bool add=0)
set the value of the scalar field in Fourier space, real part
void set_ScalarField(const double value, const int i, const int j, const int k, const bool add=0)
set the value of the scalar field
fftw_complex * m_field_FourierSpace
fourier transform of the scalar field
void reset()
set to 0 the fields
void FourierTransformField()
perform the anti-Fourier transform on the field
void set_VectorField_FourierSpace_real(const std::vector< double > value, const int i, const int j, const int k, const bool add=0)
set the value of the vector field, Fourier space, real part
void set_VectorField_FourierSpace_complex(const std::vector< double > value, const int i, const int j, const int k, const bool add=0)
set the value of the vector field, Fourier space, complex part
std::vector< double * > m_field
vector field
std::vector< fftw_complex * > m_field_FourierSpace
vector field in fourier space
std::vector< double > VectorField_FourierSpace_complex(const int i, const int j, const int k) const
get the value of the vector field, Fourier space, complex part
void set_VectorField(const std::vector< double > value, const int i, const int j, const int k, const bool add=0)
set the value of the vectorr field
std::vector< double > VectorField_FourierSpace_real(const int i, const int j, const int k) const
get the value of the vector field, Fourier space, real part
VectorField3D()=default
default constructor
std::vector< double > VectorField(const int i, const int j, const int k) const
get the value of the vector field
void FourierAntiTransformField()
perform the anti-Fourier transform on the field
static const double pi
: the ratio of a circle's circumference to its diameter
The global namespace of the CosmoBolognaLib