-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspartan.cpp
420 lines (313 loc) · 12.9 KB
/
spartan.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <vector>
#include <cstdio>
#include <cstring>
#include <chrono>
#include <iostream>
#include <fstream>
#include <madness/mra/mra.h>
#include "density_projector3d.hpp"
using namespace madness;
typedef double real_t;
typedef Vector<double,3> coordT;
// Fortran functions subroutines called by spartan
extern "C" void get_dim_(int* nx, int* ny, int* nz, int* nparticles, int* nproc);
extern "C" void part_init_(const int* nx, const int* ny, const int* nz, const int* nparticles, const int* nproc, real_t* x, real_t* y, real_t* z, real_t* vx, real_t* vy, real_t* vz, real_t* mass);
extern "C" void project_density_(const int* nx, const int* ny, const int* nz, const int* nparticles, const real_t* x, const real_t* y, const real_t* z, real_t* mass, real_t* density, const int* step);
const double p( 1/( 4 * constants::pi ) );//*6.67384e-11));
///
/// @brief Initialization of madness parameters
///
/// @param[in] nx Upper limit of the box
///
/// Initializes madness parameters, with periodic BC, cubic cell and refinement of the mesh
///
void set_initial_parameters(const int& nx){
BoundaryConditions<3> bc(BC_PERIODIC);
FunctionDefaults<3>::set_cubic_cell(1.0, static_cast<double>(nx));
FunctionDefaults<3>::set_bc(bc);
FunctionDefaults<3>::set_apply_randomize(true);
FunctionDefaults<3>::set_autorefine(true);
FunctionDefaults<3>::set_refine(true);
}
///
/// @brief Set precision for the multiwavelet representation
///
/// @param[in] order Order of the wavelets used
/// @param[in] threshold Accuracy
///
void set_projection_precision(const int& order, const double& threshold){
FunctionDefaults<3>::set_k(order);
FunctionDefaults<3>::set_thresh(threshold);
}
///
/// @brief Print density to vtk file
///
/// @param[in] world Reference to the madness:world
/// @param[in] projected_density Density to be printed
/// @param[in] numpts Number of points for the plot
/// @param[in] nx Upper limit of the plot box
///
void print_density(World& world, const real_function_3d& projected_density, const int& numpts, const int& nx){
const char filename_density[] = "data/spartan_density.vts";
Vector<double, 3> plotlo, plothi;
Vector<long, 3> npoints;
for(int i(0); i < 3; ++i){
plotlo[i] = 1;
plothi[i] = (double) nx;
npoints[i] = numpts;
}
plotvtk_begin(world, filename_density, plotlo, plothi, npoints);
plotvtk_data(projected_density, "density", world, filename_density, plotlo, plothi, npoints);
plotvtk_end<3>(world, filename_density);
}
///
/// @brief Print potential to vtk file
///
/// @param[in] world Reference to the madness:world
/// @param[in] potential Potential to be printed
/// @param[in] numpts Number of points for the plot
/// @param[in] nx Upper limit of the plot box
///
void print_potential(World& world, const real_function_3d& potential, const int& numpts, const int& nx){
const char filename_potential[] = "data/spartan_potential.vts";
Vector<double, 3> plotlo, plothi;
Vector<long, 3> npoints;
for(int i(0); i < 3; ++i){
plotlo[i] = 1;
plothi[i] = (double) nx;
npoints[i] = numpts;
}
plotvtk_begin(world, filename_potential, plotlo, plothi, npoints);
plotvtk_data(potential, "potential", world, filename_potential, plotlo, plothi, npoints);
plotvtk_end<3>(world, filename_potential);
}
///
/// @brief Built multiwavelet representation of the density
///
/// @param[in] world Reference to the madness:world
/// @param[in] nx x-dimension of the mesh
/// @param[in] ny y-dimension of the mesh
/// @param[in] nz z-dimension of the mesh
/// @param[in, out] density Array (size nx*ny*nz) containing the projection of the particles on the uniform
/// @param[in, out] projected_density Function to be initialized
///
void build_projected_density(World& world, const int& nx, const int& ny, const int& nz, real_t* density, real_function_3d& projected_density){
if (world.rank() == 0) printf("\nDensity step\n");
// Compute the coefficients for the cubic B-spline interpolation
real_functor_3d density_functor = real_functor_3d(new DensityProjector(nx, ny, nz, &density[0]));
// Do the cubic B-spline interpolation
projected_density = real_factory_3d(world).functor(density_functor);
print_density(world, projected_density, 128, 128);
}
///
/// @brief Compute gravitational potential from density
///
/// @param[in] world Reference to the madness:world
/// @param[in] projected_density Multiwavelet representation of the density
/// @param[in, out] potential S
/// @param[in] precision Accuracy of the coulomb operator
/// @param[in] threshold Accuracy of the coulomb operator
///
///
void compute_potential(World& world, const real_function_3d& projected_density, real_function_3d& potential, const double& precision, const double& threshold){
if (world.rank() == 0) printf("\nPotential step\n");
real_convolution_3d coulomb_operator = CoulombOperator(world, precision, threshold);
potential = coulomb_operator(projected_density);
// // Set mean of potential to 0
// double integral = potential.norm2();
// double volume = FunctionDefaults<3>::get_cell_volume();
// double mean = integral*integral/volume;
// potential = potential - mean;
}
///
/// @brief Solve Poisson equation
///
/// @param[in] world Reference to the madness:world
/// @param[in] x x-coordinate of the particles
/// @param[in] y y-coordinate of the particles
/// @param[in] z z-coordinate of the particles
/// @param[in] nx x-dimension of the cubic uniform mesh
/// @param[in] ny y-dimension of the cubic uniform mesh
/// @param[in] nz z-dimension of the cubic uniform mesh
/// @param[in] nparticles Number of particles used
/// @param[in, out] density Cubic uniform mesh containing the projection of the density from the particles
///
/// Main function of the code, solves the Poisson equation. It starts by setting initial parameters (BC, accuracy),
/// build a multiwavelet representation of the density and then solves the Poisson equation for the gravitational
/// potential using the projection of the density.
///
real_function_3d solve_potential(World& world, real_t* x, real_t* y, real_t* z, const int& nx, const int& ny, const int& nz, const int& nparticles, real_t* density){
real_function_3d rho_interp;
real_function_3d phi;
coord_3d center;
real_function_3d temp;
set_initial_parameters(128);
set_projection_precision(9, 1e-7);
build_projected_density(world, nx, ny, nz, density, rho_interp);
compute_potential(world, rho_interp, phi, 1e-6, 1e-8);
print_potential(world, phi, 128, 128);
return phi;
}
///
/// @brief Compute the gradient of the potential
///
/// @param[in] world Reference to the madness:world
/// @param[in] potential Solution of Poisson equation
/// @param[in, out] gradient Gradient of the solution
///
void compute_gradient(World& world, const real_function_3d& potential, vector_real_function_3d& gradient){
real_derivative_3d Dx(world, 0), Dy(world, 1), Dz(world, 2);
gradient[0] = Dx(potential);
gradient[1] = Dy(potential);
gradient[2] = Dz(potential);
}
///
/// @brief Update the position of the particles
///
/// @param[in] world Reference to the madness:world
/// @param[in, out] x x-coordinate of the particles
/// @param[in, out] y y-coordinate of the particles
/// @param[in, out] z z-coordinate of the particles
/// @param[in, out] vx x-component of the velocity
/// @param[in, out] vy y-component of the velocity
/// @param[in, out] vz z-component of the velocity
/// @param[in, out] potential Solution of Poisson equation
/// @param[in, out] nparticles Number of particles used
/// @param[in] timestep Timestep of the solver
///
/// Update position and velocity of the particles by using a Leap-Frog scheme.
///
void update_particles(World& world, real_t* x, real_t* y, real_t* z, real_t* vx, real_t* vy, real_t* vz, const int& nparticles, const real_function_3d& potential, const real_t& timestep){
const int nx(128), ny(128), nz(128);
vector_real_function_3d gradient(3);
static const int upper_limit = nparticles;
compute_gradient(world, potential, gradient);
std::vector<real_t> grad_x, grad_y, grad_z;
grad_x.resize(upper_limit);
grad_y.resize(upper_limit);
grad_z.resize(upper_limit);
for(int particle = world.rank(); particle < upper_limit; particle += world.size()){
coordT position, velocity;
position[0] = x[particle]; position[1] = y[particle]; position[2] = z[particle];
velocity[0] = vx[particle]; velocity[1] = vy[particle]; velocity[2] = vz[particle];
double evaluation;
int ndir(3);
for(int direction(0); direction < ndir; ++direction){
evaluation = gradient[direction].eval(position);
velocity[direction] += evaluation * timestep;
}
for(int direction(0); direction < 3; ++direction){
position[direction] += velocity[direction] * timestep;
}
x[particle] = position[0] + ( position[0] > nx ? -(nx-1) : (position[0] < 1 ? (nx-1) : 0) );
y[particle] = position[1] + ( position[1] > ny ? -(ny-1) : (position[1] < 1 ? (ny-1) : 0) );
z[particle] = position[2] + ( position[2] > nz ? -(nz-1) : (position[2] < 1 ? (nz-1) : 0) );
vx[particle] = velocity[0]; vy[particle] = velocity[1]; vz[particle] = velocity[2];
}
world.gop.fence();
// // Test of update using future, maybe it could help, maybe not.
// for(int particle = world.rank(); particle < nparticles; particle += world.size()){
//
// coordT position;
//
// position[0] = x[particle]; position[1] = y[particle]; position[2] = z[particle];
//
// grad_x[particle] = gradient[0].eval(position);
// grad_y[particle] = gradient[1].eval(position);
// grad_z[particle] = gradient[2].eval(position);
//
// }
//
// // Force futures
// world.gop.fence();
// world.gop.sum(&grad_x[0], upper_limit);
// world.gop.sum(&grad_y[0], upper_limit);
// world.gop.sum(&grad_z[0], upper_limit);
//
// for(int particle = world.rank(); particle < upper_limit; particle += world.size()){
//
// coordT position, velocity;
// position[0] = x[particle]; position[1] = y[particle]; position[2] = z[particle];
// velocity[0] = vx[particle]; velocity[1] = vy[particle]; velocity[2] = vz[particle];
//
// velocity[0] += grad_x[particle] * timestep;
// velocity[1] += grad_y[particle] * timestep;
// velocity[2] += grad_z[particle] * timestep;
//
// for(int direction(0); direction < 3; ++direction){
// position[direction] += velocity[direction] * timestep;
// }
//
// x[particle] = position[0] + ( position[0] > nx ? -(nx-1) : (position[0] < 1 ? (nx-1) : 0) );
// y[particle] = position[1] + ( position[1] > ny ? -(ny-1) : (position[1] < 1 ? (ny-1) : 0) );
// z[particle] = position[2] + ( position[2] > nz ? -(nz-1) : (position[2] < 1 ? (nz-1) : 0) );
//
// vx[particle] = velocity[0]; vy[particle] = velocity[1]; vz[particle] = velocity[2];
// }
}
int main(int argc, char** argv){
int nx, ny, nz, nparticles, nproc;
std::vector<real_t> x, y, z, vx, vy, vz, mass, density;
if(argc == 2){
nx = atoi(argv[1]);
ny = nx;
nz = nx;
}
initialize(argc, argv);
World world(SafeMPI::COMM_WORLD);
startup(world, argc, argv);
if(world.rank() == 0){
get_dim_(&nx, &ny, &nz, &nparticles, &nproc);
}
// Broadcast dimensions to all MPI ranks
world.gop.broadcast(nx);
world.gop.broadcast(ny);
world.gop.broadcast(nz);
world.gop.broadcast(nparticles);
x.resize(nparticles);
y.resize(nparticles);
z.resize(nparticles);
vx.resize(nparticles);
vy.resize(nparticles);
vz.resize(nparticles);
mass.resize(nparticles);
density.resize(nx*ny*nz);
// Sanity
if (world.rank() == 0) printf("Dimensions: %i %i %i\n", nx, ny, nz);
if (world.rank() == 0) printf("Number of particles: %i\n", nparticles);
if (world.rank() == 0) printf("num_procs to write file: %i\n", nproc);
auto start_time = std::chrono::high_resolution_clock::now();
int step(0);
// Initialize density on uniform mesh
if(world.rank() == 0){
part_init_(&nx, &ny, &nz, &nparticles, &nproc, &x[0], &y[0], &z[0], &vx[0], &vy[0], &vz[0], &mass[0]);
project_density_(&nx, &ny, &nz, &nparticles, &x[0], &y[0], &z[0], &mass[0], &density[0], &step);
}
// Broadcast density to all ranks
world.gop.broadcast(&density[0], nx*ny*nz, 0);
world.gop.fence();
real_function_3d potential = solve_potential(world, &x[0], &y[0], &z[0], nx, ny, nz, nparticles, &density[0]);
// // Implementation of the complete solver, not just Poisson equation
// nstep = 3;
//
// for(int step(0); step < nstep; ++step){
//
//
// int step(0);
//
// real_function_3d potential = solve_potential(world, &x[0], &y[0], &z[0], nx, ny, nz, nparticles, &density[0]);
//
// update_particles(world, &x[0], &y[0], &z[0], &vx[0], &vy[0], &vz[0], nparticles, potential, timestep);
//
//
// world.gop.fence();
//
// memset(&density[0], 0, sizeof(real_t)*nx*ny*nz);
// }
//
world.gop.fence();
auto overall_time = std::chrono::high_resolution_clock::now();
if (world.rank() == 0) printf("\nOverall time: %f s\n\n", 1e-3*(float)std::chrono::duration_cast<std::chrono::milliseconds>(overall_time - start_time).count());
finalize();
return 0;
}