Skip to content

Commit 10aaf71

Browse files
committed
examples: boris: tweaking logging and data output
1 parent b6a533e commit 10aaf71

File tree

6 files changed

+75
-58
lines changed

6 files changed

+75
-58
lines changed

examples/boris/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ foreach(example ${boris_examples})
1717
${3rdparty_DEPENDEND_LIBS}
1818
simple_physics_solver
1919
)
20-
add_dependencies(${example} ${pfasst_DEPENDEND_TARGETS} simple_physics_solver)
20+
if(${pfasst_NUM_DEPENDEND_TARGETS} GREATER 0)
21+
add_dependencies(${example} ${pfasst_DEPENDEND_TARGETS})
22+
endif()
23+
add_dependencies(${example} simple_physics_solver)
2124
if(pfasst_INSTALL_EXAMPLES)
2225
install(TARGETS ${example} RUNTIME DESTINATION bin)
2326
endif()

examples/boris/bindings/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
add_library(simple_physics_solver
32
simple_physics_solver.cpp
43
)

examples/boris/bindings/simple_physics_solver.cpp

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ using namespace std;
77

88
#define UNUSED(expr) (void)(expr)
99

10+
1011
namespace simple_physics_solver
1112
{
1213
SimplePhysicsSolverConfig::SimplePhysicsSolverConfig(const double omega_e, const double omega_b,
@@ -48,22 +49,21 @@ namespace simple_physics_solver
4849
double* forces)
4950
{
5051
UNUSED(t);
51-
cout << "SimplePhysicsSolver::evaluate_external_e_field(t=" << t << ")" << endl;
52+
// cout << "SimplePhysicsSolver::evaluate_external_e_field(t=" << t << ")" << endl;
5253
double pre_factor = (- config->epsilon) * (config->omega_e * config->omega_e);
5354
double factor = double(0.0);
5455
for (size_t i = 0; i < num_particles; ++i) {
55-
cout << " particle " << i << endl;
56+
// cout << " particle " << i << endl;
5657
factor = pre_factor / (charges[i] / masses[i]);
57-
// BUG!!!
58-
cout << " position: ";
59-
internal::print_vec(positions + (i * DIM));
60-
cout << endl << " factor: " << factor << endl;
58+
// cout << " position: ";
59+
// internal::print_vec(positions + (i * DIM));
60+
// cout << endl << " factor: " << factor << endl;
6161
internal::scale_mat_mul_vec(config->external_e_field_matrix, positions + (i * DIM), factor, forces + (i * DIM));
62-
cout << " force: ";
63-
internal::print_vec(forces + (i * DIM));
64-
cout << endl;
62+
// cout << " force: ";
63+
// internal::print_vec(forces + (i * DIM));
64+
// cout << endl;
6565
}
66-
cout << "DONE SimplePhysicsSolver::evaluate_external_e_field(t=" << t << ")" << endl;
66+
// cout << "DONE SimplePhysicsSolver::evaluate_external_e_field(t=" << t << ")" << endl;
6767
}
6868

6969
void evaluate_internal_e_field(const double* positions, const double* charges, const double* masses,
@@ -72,48 +72,48 @@ namespace simple_physics_solver
7272
double* exyz, double* phis)
7373
{
7474
UNUSED(masses); UNUSED(t);
75-
cout << "SimplePhysicsSolver::evaluate_internal_e_field(t=" << t << ")" << endl;
75+
// cout << "SimplePhysicsSolver::evaluate_internal_e_field(t=" << t << ")" << endl;
7676
double r = double(0.0),
7777
r3 = double(0.0),
7878
dist = double(0.0);
7979

8080
// computing forces on particle i
8181
for (size_t i = 0; i < num_particles; ++i) {
82-
cout << " particle " << i << endl;
82+
// cout << " particle " << i << endl;
8383

8484
// null result values
8585
phis[i] = double(0.0);
8686
for (size_t d = 0; d < DIM; ++d) { exyz[i * DIM + d] = double(0.0); }
8787

8888
// effects of particle j on particle i
8989
for (size_t j = 0; j < num_particles; ++j) {
90-
cout << " particle " << j << endl;
90+
// cout << " particle " << j << endl;
9191
if (j == i) {
92-
cout << " skipping" << endl;
92+
// cout << " skipping" << endl;
9393
continue;
9494
}
9595
dist = double(0.0);
9696
for (size_t d = 0; d < DIM; ++d) {
9797
dist += (positions[i * DIM + d] - positions[j * DIM + d]) * (positions[i * DIM + d] - positions[j * DIM + d]);
9898
}
99-
cout << " dist = " << dist << endl;
99+
// cout << " dist = " << dist << endl;
100100
r = sqrt(dist * dist + config->sigma2);
101-
cout << " r = " << r << " (= sqrt(dist^2+" << config->sigma2 << ")" << endl;
101+
// cout << " r = " << r << " (= sqrt(dist^2+" << config->sigma2 << ")" << endl;
102102
phis[i] += charges[j] / r;
103103

104104
r3 = r * r * r;
105105
for (size_t d = 0; d < DIM; ++d) {
106106
exyz[i * DIM + d] += positions[j * DIM + d] / r3 * charges[j];
107-
cout << " exyz[" << i * DIM + d << "] += " << positions[j * DIM + d] << " / " << r3 << " * " << charges[j] << "(q) => " << exyz[i * DIM + d] << endl;
107+
// cout << " exyz[" << i * DIM + d << "] += " << positions[j * DIM + d] << " / " << r3 << " * " << charges[j] << "(q) => " << exyz[i * DIM + d] << endl;
108108
}
109109
}
110110

111-
cout << " exyz = ";
112-
internal::print_vec(exyz+(i*DIM));
113-
cout << endl
114-
<< " phi_i = " << phis[i] << endl;
111+
// cout << " exyz = ";
112+
// internal::print_vec(exyz+(i*DIM));
113+
// cout << endl
114+
// << " phi_i = " << phis[i] << endl;
115115
}
116-
cout << "DONE SimplePhysicsSolver::evaluate_internal_e_field(t=" << t << ")" << endl;
116+
// cout << "DONE SimplePhysicsSolver::evaluate_internal_e_field(t=" << t << ")" << endl;
117117
}
118118

119119

@@ -122,22 +122,22 @@ namespace simple_physics_solver
122122
const SimplePhysicsSolverConfig* config,
123123
double* forces)
124124
{
125-
cout << "SimplePhysicsSolver::evaluate_e_field(t=" << t << ")" << endl;
125+
// cout << "SimplePhysicsSolver::evaluate_e_field(t=" << t << ")" << endl;
126126
double* external = new double[num_particles * DIM];
127127
double* internal = new double[num_particles * DIM];
128128
double* phis = new double[num_particles];
129129
evaluate_external_e_field(positions, charges, masses, num_particles, t, config, external);
130130
evaluate_internal_e_field(positions, charges, masses, num_particles, t, config, internal, phis);
131-
cout << " -> e_forces = [ " << endl;
131+
// cout << " -> e_forces = [ " << endl;
132132
for (size_t i = 0; i < num_particles; ++i) {
133-
cout << " ";
133+
// cout << " ";
134134
for (size_t d = 0; d < DIM; ++d) {
135135
forces[i * DIM + d] = external[i * DIM + d] + internal[i * DIM + d];
136136
}
137-
internal::print_vec(forces + (i * DIM));
138-
cout << endl;
137+
// internal::print_vec(forces + (i * DIM));
138+
// cout << endl;
139139
}
140-
cout << " ]" << endl;
140+
// cout << " ]" << endl;
141141
delete[] external;
142142
delete[] internal;
143143
delete[] phis;
@@ -178,7 +178,7 @@ namespace simple_physics_solver
178178
const size_t num_particles, const double t,
179179
const SimplePhysicsSolverConfig* config)
180180
{
181-
cout << "SimplePhysicsSolver::compute_energy(t=" << t << ")" << endl;
181+
// cout << "SimplePhysicsSolver::compute_energy(t=" << t << ")" << endl;
182182
double e_kin = double(0.0);
183183
double e_pot = double(0.0);
184184

@@ -190,33 +190,33 @@ namespace simple_physics_solver
190190
evaluate_internal_e_field(positions, charges, masses, num_particles, t, config, exyz, phis);
191191

192192
for (size_t i = 0; i < num_particles; ++i) {
193-
cout << " particle " << i << endl;
193+
// cout << " particle " << i << endl;
194194
// potential energy (induced by external electric field, position and internal electric field (i.e. phis))
195195
internal::scale_mat_mul_vec(config->external_e_field_matrix, positions + (i * DIM),
196196
(- config->epsilon * config->omega_e * config->omega_e / double(2.0) * (charges[i] / masses[i])),
197197
temp);
198198
e_pot += (charges[i] * phis[i]) - internal::scalar_prod(temp, positions + (i * DIM));
199-
cout << " e_pot += " << charges[i] << " * " << phis[i] << " - <";
200-
internal::print_vec(temp);
201-
cout << ", ";
202-
internal::print_vec(positions + (i * DIM));
203-
cout << "> (=" << internal::scalar_prod(temp, positions + (i * DIM)) << ")" << endl;
199+
// cout << " e_pot += " << charges[i] << " * " << phis[i] << " - <";
200+
// internal::print_vec(temp);
201+
// cout << ", ";
202+
// internal::print_vec(positions + (i * DIM));
203+
// cout << "> (=" << internal::scalar_prod(temp, positions + (i * DIM)) << ")" << endl;
204204

205205
// kinetic energy (induced by velocity)
206206
v2 = internal::scalar_prod(velocities + (i * DIM), velocities + (i * DIM));
207207
e_kin += masses[i] / double(2.0) * v2;
208-
cout << " e_kin += " << masses[i] << "(m) / 2.0" << " * <";
209-
internal::print_vec(velocities + (i * DIM));
210-
cout << " , ";
211-
internal::print_vec(velocities + (i * DIM));
212-
cout << "> (=" << v2 << ")" << endl;
208+
// cout << " e_kin += " << masses[i] << "(m) / 2.0" << " * <";
209+
// internal::print_vec(velocities + (i * DIM));
210+
// cout << " , ";
211+
// internal::print_vec(velocities + (i * DIM));
212+
// cout << "> (=" << v2 << ")" << endl;
213213
}
214214

215215
delete[] exyz;
216216
delete[] phis;
217217
delete[] temp;
218-
cout << " -> energy = " << e_kin << " + " << e_pot << endl;
219-
cout << "DONE SimplePhysicsSolver::compute_energy(t=" << t << ")" << endl;
218+
// cout << " -> energy = " << e_kin << " + " << e_pot << endl;
219+
// cout << "DONE SimplePhysicsSolver::compute_energy(t=" << t << ")" << endl;
220220
return e_kin + e_pot;
221221
}
222222

examples/boris/bindings/simple_physics_solver.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ using namespace std;
88
#define DIM 3
99
#endif
1010

11+
1112
namespace simple_physics_solver
1213
{
1314
class SimplePhysicsSolverConfig

examples/boris/boris_sdc.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ namespace pfasst
2626
const double mass = 1.0;
2727
const double charge = 1.0;
2828

29-
const double epsilon = config::get_value<scalar>("epsilon", -1.0);
30-
3129
auto quad = quadrature::quadrature_factory<double>(nnodes, quadrature::QuadratureType::GaussLobatto);
3230
auto factory = make_shared<ParticleCloudFactory<double>>(nparticles, 3, mass, charge);
3331

3432
shared_ptr<bindings::WrapperInterface<double, double>> impl_solver = \
3533
make_shared<bindings::WrapperSimplePhysicsSolver<double, double>>();
3634
bindings::setup(dynamic_pointer_cast<bindings::WrapperSimplePhysicsSolver<double, double>>(impl_solver));
37-
auto sweeper = make_shared<BorisSweeper<double, double>>(impl_solver);
35+
36+
string data_file = "s" + to_string(nsteps) + "_i" + to_string(niters) + "_dt" + to_string(dt) + "_m" + to_string(nnodes) + "_p" + to_string(nparticles) + ".csv";
37+
auto sweeper = make_shared<BorisSweeper<double, double>>(impl_solver, data_file);
3838

3939
sweeper->set_quadrature(quad);
4040
sweeper->set_factory(factory);
@@ -50,9 +50,9 @@ namespace pfasst
5050

5151
shared_ptr<ParticleCloud<double>> q0 = dynamic_pointer_cast<ParticleCloud<double>>(sweeper->get_state(0));
5252
q0->distribute_around_center(center);
53+
LOG(INFO) << OUT::green << "Initial Particle: " << *(dynamic_pointer_cast<ParticleCloud<double>>(sweeper->get_state(0)));
5354

5455
sweeper->set_initial_energy();
55-
LOG(INFO) << OUT::green << "Initial Particle: " << *(dynamic_pointer_cast<ParticleCloud<double>>(sweeper->get_state(0)));
5656
sdc.run();
5757

5858
return sweeper->get_errors();

examples/boris/boris_sweeper.hpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#include <cmath>
66
#include <complex>
77
#include <cstdlib>
8-
#include <vector>
8+
#include <iostream>
99
#include <map>
10+
#include <vector>
1011
using namespace std;
1112

1213
#include <Eigen/Core>
@@ -137,6 +138,8 @@ namespace pfasst
137138
Matrix<time> qx_mat;
138139
Matrix<time> qt_mat;
139140

141+
ofstream data_stream;
142+
140143
acceleration_type build_rhs(const size_t m, const bool previous = false) const
141144
{
142145
// VLOG_FUNC_START("BorisSweeper") << "m=" << m << ", previous=" << boolalpha << previous;
@@ -188,13 +191,16 @@ namespace pfasst
188191

189192
public:
190193
//! @{
191-
BorisSweeper(shared_ptr<bindings::WrapperInterface<scalar, time>>& impl_solver)
194+
BorisSweeper(shared_ptr<bindings::WrapperInterface<scalar, time>>& impl_solver,
195+
const string& data_file)
192196
: impl_solver(impl_solver)
193197
, errors()
194198
, exact_updated(false)
195199
, f_evals(0)
200+
, data_stream(data_file, ios::out | ios::trunc)
196201
{
197202
VLOG_FUNC_START("BorisSweeper");
203+
assert(data_stream.is_open() && data_stream.good());
198204
}
199205

200206
BorisSweeper(const BorisSweeper<scalar, time>& other) = delete;
@@ -240,11 +246,17 @@ namespace pfasst
240246
VLOG_FUNC_START("BorisSweeper");
241247
auto p0 = this->particles.front();
242248
this->initial_energy = this->impl_solver->energy(p0, this->get_controller()->get_time());
249+
LOG(INFO) << OUT::green << "Initial Energy:" << this->initial_energy;
243250
for (size_t p = 0; p < p0->size(); ++p) {
244-
cout << "data,0,0," << p << "," << fixed << setprecision(LOG_PRECISION)
245-
<< p0->positions()[p][0] << "," << p0->positions()[p][1] << "," << p0->positions()[p][2] << ","
246-
<< p0->velocities()[p][0] << "," << p0->velocities()[p][1] << "," << p0->velocities()[p][2] << endl;
251+
this->data_stream << "0,0," << p << ","
252+
<< fixed << setprecision(LOG_PRECISION)
253+
<< p0->positions()[p][0] << "," << p0->positions()[p][1] << "," << p0->positions()[p][2] << ","
254+
<< p0->velocities()[p][0] << "," << p0->velocities()[p][1] << "," << p0->velocities()[p][2] << endl;
247255
}
256+
auto center = p0->center_of_mass();
257+
this->data_stream << "0,0,-1,"
258+
<< fixed << setprecision(LOG_PRECISION)
259+
<< center[0] << "," << center[1] << "," << center[2] << ",0,0,0" << endl;
248260
VLOG_FUNC_END("BorisSweeper");
249261
}
250262
//! @}
@@ -356,13 +368,15 @@ namespace pfasst
356368
this->errors.insert(pair<error_index, ErrorTuple<scalar>>(nk, e_tuple));
357369

358370
for (size_t p = 0; p < end->size(); ++p) {
359-
cout << "data," << n+1 << "," << k << "," << p << "," << fixed << setprecision(LOG_PRECISION)
360-
<< end->positions()[p][0] << "," << end->positions()[p][1] << "," << end->positions()[p][2] << ","
361-
<< end->velocities()[p][0] << "," << end->velocities()[p][1] << "," << end->velocities()[p][2] << endl;
371+
this->data_stream << n+1 << "," << k << "," << p << ","
372+
<< fixed << setprecision(LOG_PRECISION)
373+
<< end->positions()[p][0] << "," << end->positions()[p][1] << "," << end->positions()[p][2] << ","
374+
<< end->velocities()[p][0] << "," << end->velocities()[p][1] << "," << end->velocities()[p][2] << endl;
362375
}
363376
auto center = end->center_of_mass();
364-
cout << "data," << n+1 << "," << k << ",-1," << fixed << setprecision(LOG_PRECISION)
365-
<< center[0] << "," << center[1] << "," << center[2] << ",0,0,0" << endl;
377+
this->data_stream << n+1 << "," << k << ",-1,"
378+
<< fixed << setprecision(LOG_PRECISION)
379+
<< center[0] << "," << center[1] << "," << center[2] << ",0,0,0" << endl;
366380
}
367381

368382
virtual error_map<scalar> get_errors() const

0 commit comments

Comments
 (0)