OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
world_helpers.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2025 VTT Technical Research Centre of Finland Ltd
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
25#pragma once
26
27#include "strong_types.hpp"
28#include "types.hpp"
29#include "world.hpp"
30#include "world_factory.hpp"
31#include <stdexcept>
32#include <string>
33
34namespace pfc {
35namespace world {
36
37using pfc::types::Bool3;
39using pfc::types::Real3;
40
55inline CartesianWorld uniform(int size) {
56 if (size <= 0) {
57 throw std::invalid_argument("Grid size must be positive, got: " +
58 std::to_string(size));
59 }
60 return create(GridSize({size, size, size}), PhysicalOrigin({0.0, 0.0, 0.0}),
61 GridSpacing({1.0, 1.0, 1.0}));
62}
63
78inline CartesianWorld uniform(int size, double spacing) {
79 if (size <= 0) {
80 throw std::invalid_argument("Grid size must be positive, got: " +
81 std::to_string(size));
82 }
83 if (spacing <= 0.0) {
84 throw std::invalid_argument("Spacing must be positive, got: " +
85 std::to_string(spacing));
86 }
87 return create(GridSize({size, size, size}), PhysicalOrigin({0.0, 0.0, 0.0}),
88 GridSpacing({spacing, spacing, spacing}));
89}
90
115inline CartesianWorld from_bounds(Int3 size, Real3 lower, Real3 upper,
116 Bool3 periodic = {true, true, true}) {
117 // Validate inputs
118 for (int i = 0; i < 3; ++i) {
119 if (size[i] <= 0) {
120 throw std::invalid_argument("Grid size must be positive in all dimensions");
121 }
122 if (upper[i] <= lower[i]) {
123 throw std::invalid_argument("Upper bound must be greater than lower bound");
124 }
125 }
126
127 // Compute spacing based on periodicity
128 Real3 spacing;
129 for (int i = 0; i < 3; ++i) {
130 if (periodic[i]) {
131 spacing[i] = (upper[i] - lower[i]) / size[i];
132 } else {
133 spacing[i] = (upper[i] - lower[i]) / (size[i] - 1);
134 }
135 }
136
137 return create(GridSize(size), PhysicalOrigin(lower), GridSpacing(spacing));
138}
139
154inline CartesianWorld with_spacing(Int3 size, Real3 spacing) {
155 // Validate
156 for (int i = 0; i < 3; ++i) {
157 if (size[i] <= 0) {
158 throw std::invalid_argument("Grid size must be positive");
159 }
160 if (spacing[i] <= 0.0) {
161 throw std::invalid_argument("Spacing must be positive");
162 }
163 }
164
165 return create(GridSize(size), PhysicalOrigin({0.0, 0.0, 0.0}),
166 GridSpacing(spacing));
167}
168
182inline CartesianWorld with_origin(Int3 size, Real3 origin) {
183 // Validate
184 for (int i = 0; i < 3; ++i) {
185 if (size[i] <= 0) {
186 throw std::invalid_argument("Grid size must be positive");
187 }
188 }
189
190 return create(GridSize(size), PhysicalOrigin(origin),
191 GridSpacing({1.0, 1.0, 1.0}));
192}
193
194} // namespace world
195} // namespace pfc
std::array< int, 3 > Int3
Type aliases for clarity.
Definition types.hpp:45
CartesianWorld with_origin(Int3 size, Real3 origin)
Create grid with custom origin but unit spacing.
Definition world_helpers.hpp:182
CartesianWorld from_bounds(Int3 size, Real3 lower, Real3 upper, Bool3 periodic={true, true, true})
Create grid from physical bounds (automatically computes spacing).
Definition world_helpers.hpp:115
CartesianWorld uniform(int size)
Create uniform grid with unit spacing at origin.
Definition world_helpers.hpp:55
auto create(const World< T > &world, const heffte::box3d< int > &box)
Construct a new World object from an existing one and a box.
Definition decomposition.hpp:62
CartesianWorld with_spacing(Int3 size, Real3 spacing)
Create grid with default origin but custom spacing.
Definition world_helpers.hpp:154
Strong type aliases for geometric quantities.
Grid dimensions (number of grid points per dimension)
Definition strong_types.hpp:176
Physical spacing between grid points.
Definition strong_types.hpp:370
Physical origin of coordinate system.
Definition strong_types.hpp:424
Represents the global simulation domain (the "world").
Definition world.hpp:91
Common type aliases used throughout OpenPFC.
World class definition and unified interface.
World creation and factory functions.