OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
csys.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
38#pragma once
39
40#include "types.hpp"
41
42namespace pfc {
43namespace csys {
44
45using pfc::types::Bool3;
47using pfc::types::Real3;
48
87template <typename Tag> struct CoordinateSystem;
88
115template <typename CoordTag>
116struct CoordinateSystemDefaults; // intentionally left undefined
117
118// Coordinate system tags.
119// struct LineTag {};
120// struct PlaneTag {};
121// struct PolarTag {};
122// struct CylindricalTag {};
123// struct SphericalTag {};
124// struct Polar2DTag {};
125// struct Toroidal2DTag {};
126// struct LogPolar3DTag {};
127
147struct CartesianTag {};
148
166 static constexpr Real3 offset = {0.0, 0.0, 0.0};
167 static constexpr Real3 spacing = {1.0, 1.0, 1.0};
168 static constexpr Bool3 periodic = {true, true, true};
169 std::size_t dimensions = 3;
170};
171
199template <> struct CoordinateSystem<CartesianTag> {
200 const Real3 m_offset;
201 const Real3 m_spacing;
202 const Bool3 m_periodic;
203
209 : m_offset(offset), m_spacing(spacing), m_periodic(periodic) {
210 for (std::size_t i = 0; i < 3; ++i) {
211 if (spacing[i] <= 0.0) {
212 throw std::invalid_argument("Spacing must be positive.");
213 }
214 };
215 };
216
217 bool operator==(const CoordinateSystem<CartesianTag> &other) const {
218 return m_offset == other.m_offset && m_spacing == other.m_spacing &&
219 m_periodic == other.m_periodic;
220 };
221
222 bool operator!=(const CoordinateSystem<CartesianTag> &other) const {
223 return !(*this == other);
224 };
225};
226
227using CartesianCS = CoordinateSystem<CartesianTag>;
228
234inline const Real3 &get_offset(const CartesianCS &cs) noexcept {
235 return cs.m_offset;
236};
237
245inline double get_offset(const CartesianCS &cs, int i) { return cs.m_offset.at(i); }
246
252inline const Real3 &get_spacing(const CartesianCS &cs) noexcept {
253 return cs.m_spacing;
254};
255
263inline double get_spacing(const CartesianCS &cs, int i) {
264 return cs.m_spacing.at(i);
265}
266
272inline const Bool3 &get_periodic(const CartesianCS &cs) noexcept {
273 return cs.m_periodic;
274};
275
283inline bool is_periodic(const CartesianCS &cs, int i) { return cs.m_periodic.at(i); }
284
291inline const Real3 to_coords(const CartesianCS &cs, const Int3 &idx) noexcept {
292 Real3 xyz;
293 const auto &offset = get_offset(cs);
294 const auto &spacing = get_spacing(cs);
295 for (int i = 0; i < 3; ++i) {
296 xyz[i] = offset[i] + idx[i] * spacing[i];
297 }
298 return xyz;
299}
300
307inline const Int3 to_index(const CartesianCS &cs, const Real3 &xyz) noexcept {
308 Int3 idx;
309 const auto &offset = get_offset(cs);
310 const auto &spacing = get_spacing(cs);
311 for (int i = 0; i < 3; ++i) {
312 idx[i] = static_cast<int>((xyz[i] - offset[i]) / spacing[i]);
313 }
314 return idx;
315}
316
317} // namespace csys
318} // namespace pfc
std::array< int, 3 > Int3
Type aliases for clarity.
Definition types.hpp:45
const Real3 & get_spacing(const CartesianCS &cs) noexcept
Get the spacing of the coordinate system.
Definition csys.hpp:252
const Int3 to_index(const CartesianCS &cs, const Real3 &xyz) noexcept
Convert physical coordinates to grid indices.
Definition csys.hpp:307
const Bool3 & get_periodic(const CartesianCS &cs) noexcept
Get the periodicity of the coordinate system.
Definition csys.hpp:272
const Real3 to_coords(const CartesianCS &cs, const Int3 &idx) noexcept
Convert grid indices to physical coordinates.
Definition csys.hpp:291
const Real3 & get_offset(const CartesianCS &cs) noexcept
Get the offset of the coordinate system.
Definition csys.hpp:234
bool is_periodic(const CartesianCS &cs, int i)
Check if the coordinate system is periodic in a specific dimension.
Definition csys.hpp:283
Tag type for the 3D Cartesian coordinate system.
Definition csys.hpp:147
Trait class for providing default parameters for coordinate systems.
Definition csys.hpp:116
Specialization of the coordinate system for 3D Cartesian space.
Definition csys.hpp:199
CoordinateSystem(const Real3 &offset=CoordinateSystemDefaults< CartesianTag >::offset, const Real3 &spacing=CoordinateSystemDefaults< CartesianTag >::spacing, const Bool3 &periodic=CoordinateSystemDefaults< CartesianTag >::periodic)
Constructs a 3D Cartesian coordinate system.
Definition csys.hpp:205
const Bool3 m_periodic
Periodicity flags for each dimension.
Definition csys.hpp:202
const Real3 m_offset
Physical coordinate of grid index (0, 0, 0)
Definition csys.hpp:200
const Real3 m_spacing
Physical spacing between grid points.
Definition csys.hpp:201
Primary template for defining coordinate systems by tag.
Definition csys.hpp:87
Represents the global simulation domain (the "world").
Definition world.hpp:91
Common type aliases used throughout OpenPFC.