Skip to content

Commit 4154be9

Browse files
committed
Refactor examples to utilize World object for improved context handling and streamline decomposition and spacing access
1 parent 6f0fac2 commit 4154be9

10 files changed

+96
-57
lines changed

examples/01_hello_world.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ int main() {
3939

4040
// Retrieve world properties
4141
cout << "World properties:" << endl;
42-
cout << "Dimensions: " << world.get_Lx() << " x " << world.get_Ly() << " x " << world.get_Lz() << endl;
43-
cout << "Origin: (" << world.get_x0() << ", " << world.get_y0() << ", " << world.get_z0() << ")" << endl;
44-
cout << "Discretization: dx = " << world.get_dx() << ", dy = " << world.get_dy() << ", dz = " << world.get_dz()
45-
<< endl;
42+
auto size = world.size();
43+
cout << "Dimensions: " << size[0] << " x " << size[1] << " x " << size[2] << endl;
44+
auto world_origin = world.origin();
45+
cout << "Origin: (" << world_origin[0] << ", " << world_origin[1] << ", " << world_origin[2] << ")" << endl;
46+
auto spacing = world.spacing();
47+
cout << "Spacing: dx = " << spacing[0] << ", dy = " << spacing[1] << ", dz = " << spacing[2] << endl;
4648

4749
// Simpler way:
4850
cout << world << endl;

examples/02_domain_decomposition.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
#include <iostream>
55
#include <mpi.h>
6+
#include <openpfc/core/decomposition.hpp>
67
#include <openpfc/core/world.hpp>
7-
#include <openpfc/decomposition.hpp>
88

99
using namespace std;
1010
using namespace pfc;

examples/03_parallel_fft.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
#include <iostream>
55
#include <vector>
66

7+
#include <openpfc/core/decomposition.hpp>
78
#include <openpfc/core/world.hpp>
8-
#include <openpfc/decomposition.hpp>
99
#include <openpfc/fft.hpp>
1010

1111
using namespace std;
@@ -113,7 +113,8 @@ int main(int argc, char *argv[]) {
113113
// Construct world, decomposition and fft
114114
World world({8, 1, 1});
115115
Decomposition decomposition(world, comm);
116-
FFT fft(decomposition, comm);
116+
auto plan_options = heffte::default_options<heffte::backend::fftw>();
117+
FFT fft(decomposition, comm, plan_options, world);
117118

118119
// Create two vectors; in contains input data and results are stored to out
119120
vector<double> in(fft.size_inbox());

examples/04_diffusion_model.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <iostream>
66
#include <limits>
77

8+
#include <openpfc/core/decomposition.hpp>
89
#include <openpfc/core/world.hpp>
9-
#include <openpfc/decomposition.hpp>
1010
#include <openpfc/fft.hpp>
1111
#include <openpfc/model.hpp>
1212

@@ -125,9 +125,11 @@ class Diffusion : public Model {
125125
for (int k = i_low[2]; k <= i_high[2]; k++) {
126126
for (int j = i_low[1]; j <= i_high[1]; j++) {
127127
for (int i = i_low[0]; i <= i_high[0]; i++) {
128-
double x = w.x0 + i * w.dx;
129-
double y = w.y0 + j * w.dy;
130-
double z = w.z0 + k * w.dz;
128+
auto origin = w.origin();
129+
auto spacing = w.spacing();
130+
double x = origin[0] + i * spacing[0];
131+
double y = origin[1] + j * spacing[1];
132+
double z = origin[2] + k * spacing[2];
131133
psi[idx] = exp(-(x * x + y * y + z * z) / (4.0 * D));
132134
idx += 1;
133135
}
@@ -141,16 +143,17 @@ class Diffusion : public Model {
141143
if (rank0) cout << "Prepare operators" << endl;
142144
idx = 0;
143145
double pi = std::atan(1.0) * 4.0;
144-
double fx = 2.0 * pi / (w.dx * w.Lx);
145-
double fy = 2.0 * pi / (w.dy * w.Ly);
146-
double fz = 2.0 * pi / (w.dz * w.Lz);
146+
auto spacing = w.spacing();
147+
auto size = w.size();
148+
double fx = 2.0 * pi / (spacing[0] * size[0]);
149+
double fy = 2.0 * pi / (spacing[1] * size[1]);
150+
double fz = 2.0 * pi / (spacing[2] * size[2]);
147151
for (int k = o_low[2]; k <= o_high[2]; k++) {
148152
for (int j = o_low[1]; j <= o_high[1]; j++) {
149153
for (int i = o_low[0]; i <= o_high[0]; i++) {
150-
// Laplacian operator -k^2
151-
double ki = (i <= w.Lx / 2) ? i * fx : (i - w.Lx) * fx;
152-
double kj = (j <= w.Ly / 2) ? j * fy : (j - w.Ly) * fy;
153-
double kk = (k <= w.Lz / 2) ? k * fz : (k - w.Lz) * fz;
154+
double ki = (i <= size[0] / 2) ? i * fx : (i - size[0]) * fx;
155+
double kj = (j <= size[1] / 2) ? j * fy : (j - size[1]) * fy;
156+
double kk = (k <= size[2] / 2) ? k * fz : (k - size[2]) * fz;
154157
double kLap = -(ki * ki + kj * kj + kk * kk);
155158
opL[idx++] = 1.0 / (1.0 - dt * kLap);
156159
}
@@ -227,8 +230,9 @@ void run() {
227230
// Construct world, decomposition, fft and model
228231
World world({Lx, Ly, Lz}, {x0, y0, z0}, {dx, dy, dz});
229232
Decomposition decomp(world);
230-
FFT fft(decomp);
231-
Diffusion model(fft);
233+
auto plan_options = heffte::default_options<heffte::backend::fftw>();
234+
FFT fft(decomp, MPI_COMM_WORLD, plan_options, world);
235+
Diffusion model(world);
232236

233237
// Define time
234238
double t = 0.0;

examples/05_simulator.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include <iostream>
66
#include <limits>
77
#include <memory>
8+
#include <openpfc/core/decomposition.hpp>
89
#include <openpfc/core/world.hpp>
9-
#include <openpfc/decomposition.hpp>
1010
#include <openpfc/fft.hpp>
1111
#include <openpfc/field_modifier.hpp>
1212
#include <openpfc/model.hpp>
@@ -59,9 +59,11 @@ class GaussianIC : public FieldModifier {
5959
for (int k = low[2]; k <= high[2]; k++) {
6060
for (int j = low[1]; j <= high[1]; j++) {
6161
for (int i = low[0]; i <= high[0]; i++) {
62-
double x = w.x0 + i * w.dx;
63-
double y = w.y0 + j * w.dy;
64-
double z = w.z0 + k * w.dz;
62+
auto origin = w.origin();
63+
auto spacing = w.spacing();
64+
double x = origin[0] + i * spacing[0];
65+
double y = origin[1] + j * spacing[1];
66+
double z = origin[2] + k * spacing[2];
6567
field[idx] = exp(-(x * x + y * y + z * z) / (4.0 * D));
6668
idx += 1;
6769
}
@@ -102,16 +104,17 @@ class Diffusion : public Model {
102104

103105
if (is_rank0()) std::cout << "Prepare operators" << std::endl;
104106
size_t idx = 0;
105-
double fx = 2.0 * PI / (w.dx * w.Lx);
106-
double fy = 2.0 * PI / (w.dy * w.Ly);
107-
double fz = 2.0 * PI / (w.dz * w.Lz);
107+
auto spacing = w.spacing();
108+
auto size = w.size();
109+
double fx = 2.0 * PI / (spacing[0] * size[0]);
110+
double fy = 2.0 * PI / (spacing[1] * size[1]);
111+
double fz = 2.0 * PI / (spacing[2] * size[2]);
108112
for (int k = low[2]; k <= high[2]; k++) {
109113
for (int j = low[1]; j <= high[1]; j++) {
110114
for (int i = low[0]; i <= high[0]; i++) {
111-
// Laplacian operator -k^2
112-
double ki = (i <= w.Lx / 2) ? i * fx : (i - w.Lx) * fx;
113-
double kj = (j <= w.Ly / 2) ? j * fy : (j - w.Ly) * fy;
114-
double kk = (k <= w.Lz / 2) ? k * fz : (k - w.Lz) * fz;
115+
double ki = (i <= size[0] / 2) ? i * fx : (i - size[0]) * fx;
116+
double kj = (j <= size[1] / 2) ? j * fy : (j - size[1]) * fy;
117+
double kk = (k <= size[2] / 2) ? k * fz : (k - size[2]) * fz;
115118
double kLap = -(ki * ki + kj * kj + kk * kk);
116119
opL[idx++] = 1.0 / (1.0 - dt * kLap);
117120
}
@@ -180,8 +183,9 @@ void run() {
180183
World world(dimensions, origin, discretization);
181184

182185
Decomposition decomp(world);
183-
FFT fft(decomp);
184-
Diffusion model(fft);
186+
auto plan_options = heffte::default_options<heffte::backend::fftw>();
187+
FFT fft(decomp, MPI_COMM_WORLD, plan_options, world);
188+
Diffusion model(world);
185189

186190
// Define time
187191
double t0 = 0.0;

examples/08_discrete_fields.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// SPDX-License-Identifier: AGPL-3.0-or-later
33

44
#include <iostream>
5+
#include <openpfc/core/decomposition.hpp>
56
#include <openpfc/core/world.hpp>
6-
#include <openpfc/decomposition.hpp>
77
#include <openpfc/discrete_field.hpp>
88
#include <openpfc/utils.hpp>
99

@@ -48,14 +48,21 @@ int main() {
4848
Decomposition decomp3(world, 2, 4);
4949
Decomposition decomp4(world, 3, 4);
5050
std::cout << decomp1 << std::endl;
51-
DiscreteField<double, 3> field1(decomp1.get_inbox_size(), decomp1.get_inbox_offset(), world.get_origin(),
52-
world.get_discretization());
53-
DiscreteField<double, 3> field2(decomp2.get_inbox_size(), decomp2.get_inbox_offset(), world.get_origin(),
54-
world.get_discretization());
55-
DiscreteField<double, 3> field3(decomp3.get_inbox_size(), decomp3.get_inbox_offset(), world.get_origin(),
56-
world.get_discretization());
57-
DiscreteField<double, 3> field4(decomp4.get_inbox_size(), decomp4.get_inbox_offset(), world.get_origin(),
58-
world.get_discretization());
51+
const std::array<int, 3> &inbox_size1 = decomp1.get_inbox_size();
52+
const std::array<int, 3> &inbox_offset1 = decomp1.get_inbox_offset();
53+
DiscreteField<double, 3> field1(inbox_size1, inbox_offset1, world.origin(), world.spacing());
54+
55+
const std::array<int, 3> &inbox_size2 = decomp2.get_inbox_size();
56+
const std::array<int, 3> &inbox_offset2 = decomp2.get_inbox_offset();
57+
DiscreteField<double, 3> field2(inbox_size2, inbox_offset2, world.origin(), world.spacing());
58+
59+
const std::array<int, 3> &inbox_size3 = decomp3.get_inbox_size();
60+
const std::array<int, 3> &inbox_offset3 = decomp3.get_inbox_offset();
61+
DiscreteField<double, 3> field3(inbox_size3, inbox_offset3, world.origin(), world.spacing());
62+
63+
const std::array<int, 3> &inbox_size4 = decomp4.get_inbox_size();
64+
const std::array<int, 3> &inbox_offset4 = decomp4.get_inbox_offset();
65+
DiscreteField<double, 3> field4(inbox_size4, inbox_offset4, world.origin(), world.spacing());
5966
std::cout << field1 << std::endl;
6067
std::cout << field2 << std::endl;
6168
std::cout << field3 << std::endl;

examples/09_parallel_fft_high_level.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ int main(int argc, char *argv[]) {
3535
// Array<double, 3> output2(decomp);
3636
// std::cout << output2 << std::endl; // this is {4, 3, 2}
3737

38-
// Create FFT object and perform parallel FFT
39-
FFT fft(decomp);
38+
auto plan_options = heffte::default_options<heffte::backend::fftw>();
39+
FFT fft(decomp, MPI_COMM_WORLD, plan_options, world);
40+
4041
fft.forward(input, output);
4142

4243
// Display results

examples/10_ui_register_ic.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ void from_json(const json &params, MyIC &ic) {
4040
// Define model
4141
class MyModel : public Model {
4242
public:
43+
/**
44+
* @brief Constructs a MyModel instance with the given World object.
45+
*
46+
* @param world The World object to initialize the model.
47+
*/
48+
explicit MyModel(const World &world) : Model(world) {
49+
// Additional initialization if needed
50+
}
51+
4352
void initialize(double) override { std::cout << "initialize()" << std::endl; }
4453
void step(double) override { std::cout << "MyModel.step()" << std::endl; }
4554
};

examples/11_write_results.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int argc, char **argv) {
2626
writer.set_field_name("density");
2727
writer.set_domain(world.get_size(), field.get_size(), field.get_offset());
2828
writer.set_origin(world.get_origin());
29-
writer.set_spacing(world.get_discretization());
29+
writer.set_spacing(world.spacing());
3030
std::cout << "Writing results to file: " << writer.get_uri() << "\n";
3131
writer.initialize();
3232
writer.write(field.get_array().get_data());

examples/12_cahn_hilliard.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class CahnHilliard : public Model {
1919
double D = 1.0; // Diffusion coefficient
2020

2121
public:
22+
/**
23+
* @brief Constructs a CahnHilliard instance with the given World object.
24+
*
25+
* @param world The World object to initialize the model.
26+
*/
27+
explicit CahnHilliard(const World &world) : Model(world) {
28+
// Additional initialization if needed
29+
}
30+
2231
void initialize(double dt) override {
2332
FFT &fft = get_fft();
2433
const Decomposition &decomp = get_decomposition();
@@ -37,16 +46,17 @@ class CahnHilliard : public Model {
3746
std::array<int, 3> o_high = decomp.outbox.high;
3847
size_t idx = 0;
3948
double pi = std::atan(1.0) * 4.0;
40-
double fx = 2.0 * pi / (w.dx * w.Lx);
41-
double fy = 2.0 * pi / (w.dy * w.Ly);
42-
double fz = 2.0 * pi / (w.dz * w.Lz);
49+
auto spacing = w.spacing();
50+
auto size = w.size();
51+
double fx = 2.0 * pi / (spacing[0] * size[0]);
52+
double fy = 2.0 * pi / (spacing[1] * size[1]);
53+
double fz = 2.0 * pi / (spacing[2] * size[2]);
4354
for (int k = o_low[2]; k <= o_high[2]; k++) {
4455
for (int j = o_low[1]; j <= o_high[1]; j++) {
4556
for (int i = o_low[0]; i <= o_high[0]; i++) {
46-
// Laplacian operator -k^2
47-
double ki = (i <= w.Lx / 2) ? i * fx : (i - w.Lx) * fx;
48-
double kj = (j <= w.Ly / 2) ? j * fy : (j - w.Ly) * fy;
49-
double kk = (k <= w.Lz / 2) ? k * fz : (k - w.Lz) * fz;
57+
double ki = (i <= size[0] / 2) ? i * fx : (i - size[0]) * fx;
58+
double kj = (j <= size[1] / 2) ? j * fy : (j - size[1]) * fy;
59+
double kk = (k <= size[2] / 2) ? k * fz : (k - size[2]) * fz;
5060
double kLap = -(ki * ki + kj * kj + kk * kk);
5161
double L = kLap * (-D - D * gamma * kLap);
5262
opL[idx] = std::exp(L * dt);
@@ -104,8 +114,9 @@ int main(int argc, char **argv) {
104114
// Construct world, decomposition, fft and model
105115
World world({Lx, Ly, Lz}, {x0, y0, z0}, {dx, dy, dz});
106116
Decomposition decomp(world);
107-
FFT fft(decomp);
108-
CahnHilliard model;
117+
auto plan_options = heffte::default_options<heffte::backend::fftw>();
118+
FFT fft(decomp, MPI_COMM_WORLD, plan_options, world);
119+
CahnHilliard model(world);
109120
model.set_fft(fft);
110121

111122
// Define time
@@ -129,9 +140,9 @@ int main(int argc, char **argv) {
129140
// set uri as format cahn_hilliard_%04i.vti, where %04i is replaced by file_count
130141
writer.set_uri(sprintf("cahn_hilliard_%04i.vti", file_count));
131142
writer.set_field_name("concentration");
132-
writer.set_domain(world.get_size(), decomp.get_inbox_size(), decomp.get_inbox_offset());
133-
writer.set_origin(world.get_origin());
134-
writer.set_spacing(world.get_discretization());
143+
writer.set_domain(world.size(), decomp.get_inbox_size(), decomp.get_inbox_offset());
144+
writer.set_origin(world.origin());
145+
writer.set_spacing(world.spacing());
135146
writer.initialize();
136147
writer.write(field);
137148

0 commit comments

Comments
 (0)