OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
strong_types.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
149#pragma once
150
151#include <array>
152#include <openpfc/core/types.hpp>
153#include <type_traits>
154
155namespace pfc {
156
157// ============================================================================
158// Strong Types for Discrete (Index) Space
159// ============================================================================
160
176struct GridSize {
177 Int3 value;
178
183 GridSize(const Int3 &v) : value(v) {}
184
189 const Int3 &get() const noexcept { return value; }
190
195 operator const Int3 &() const noexcept { return value; }
196
202 bool operator==(const GridSize &other) const noexcept {
203 return value == other.value;
204 }
205
211 bool operator!=(const GridSize &other) const noexcept {
212 return value != other.value;
213 }
214};
215
231 Int3 value;
232
237 LocalOffset(const Int3 &v) : value(v) {}
238
243 const Int3 &get() const noexcept { return value; }
244
249 operator const Int3 &() const noexcept { return value; }
250
256 bool operator==(const LocalOffset &other) const noexcept {
257 return value == other.value;
258 }
259
265 bool operator!=(const LocalOffset &other) const noexcept {
266 return value != other.value;
267 }
268};
269
285 Int3 value;
286
291 GlobalOffset(const Int3 &v) : value(v) {}
292
297 const Int3 &get() const noexcept { return value; }
298
303 operator const Int3 &() const noexcept { return value; }
304
310 bool operator==(const GlobalOffset &other) const noexcept {
311 return value == other.value;
312 }
313
319 bool operator!=(const GlobalOffset &other) const noexcept {
320 return value != other.value;
321 }
322};
323
341 Int3 lower;
342 Int3 upper;
343
349 IndexBounds(const Int3 &lo, const Int3 &hi) : lower(lo), upper(hi) {}
350};
351
352// ============================================================================
353// Strong Types for Physical (Coordinate) Space
354// ============================================================================
355
371 Real3 value;
372
377 GridSpacing(const Real3 &v) : value(v) {}
378
383 const Real3 &get() const noexcept { return value; }
384
389 operator const Real3 &() const noexcept { return value; }
390
396 bool operator==(const GridSpacing &other) const noexcept {
397 return value == other.value;
398 }
399
405 bool operator!=(const GridSpacing &other) const noexcept {
406 return value != other.value;
407 }
408};
409
425 Real3 value;
426
431 PhysicalOrigin(const Real3 &v) : value(v) {}
432
437 const Real3 &get() const noexcept { return value; }
438
443 operator const Real3 &() const noexcept { return value; }
444
450 bool operator==(const PhysicalOrigin &other) const noexcept {
451 return value == other.value;
452 }
453
459 bool operator!=(const PhysicalOrigin &other) const noexcept {
460 return value != other.value;
461 }
462};
463
479 Real3 value;
480
485 PhysicalCoords(const Real3 &v) : value(v) {}
486
491 const Real3 &get() const noexcept { return value; }
492
497 operator const Real3 &() const noexcept { return value; }
498
504 bool operator==(const PhysicalCoords &other) const noexcept {
505 return value == other.value;
506 }
507
513 bool operator!=(const PhysicalCoords &other) const noexcept {
514 return value != other.value;
515 }
516};
517
535 Real3 lower;
536 Real3 upper;
537
543 PhysicalBounds(const Real3 &lo, const Real3 &hi) : lower(lo), upper(hi) {}
544};
545
546// ============================================================================
547// Compile-Time Assertions (Zero-Cost Verification)
548// ============================================================================
549
550// Verify zero-cost: same size as underlying types
551static_assert(sizeof(GridSize) == sizeof(Int3),
552 "GridSize must be same size as Int3 (zero-cost)");
553static_assert(sizeof(LocalOffset) == sizeof(Int3),
554 "LocalOffset must be same size as Int3 (zero-cost)");
555static_assert(sizeof(GlobalOffset) == sizeof(Int3),
556 "GlobalOffset must be same size as Int3 (zero-cost)");
557static_assert(sizeof(GridSpacing) == sizeof(Real3),
558 "GridSpacing must be same size as Real3 (zero-cost)");
559static_assert(sizeof(PhysicalOrigin) == sizeof(Real3),
560 "PhysicalOrigin must be same size as Real3 (zero-cost)");
561static_assert(sizeof(PhysicalCoords) == sizeof(Real3),
562 "PhysicalCoords must be same size as Real3 (zero-cost)");
563
564// Verify trivial copyability (required for performance)
565static_assert(std::is_trivially_copyable_v<GridSize>,
566 "GridSize must be trivially copyable");
567static_assert(std::is_trivially_copyable_v<LocalOffset>,
568 "LocalOffset must be trivially copyable");
569static_assert(std::is_trivially_copyable_v<GlobalOffset>,
570 "GlobalOffset must be trivially copyable");
571static_assert(std::is_trivially_copyable_v<IndexBounds>,
572 "IndexBounds must be trivially copyable");
573static_assert(std::is_trivially_copyable_v<GridSpacing>,
574 "GridSpacing must be trivially copyable");
575static_assert(std::is_trivially_copyable_v<PhysicalOrigin>,
576 "PhysicalOrigin must be trivially copyable");
577static_assert(std::is_trivially_copyable_v<PhysicalCoords>,
578 "PhysicalCoords must be trivially copyable");
579static_assert(std::is_trivially_copyable_v<PhysicalBounds>,
580 "PhysicalBounds must be trivially copyable");
581
582// Verify standard layout (required for interop)
583static_assert(std::is_standard_layout_v<GridSize>,
584 "GridSize must have standard layout");
585static_assert(std::is_standard_layout_v<LocalOffset>,
586 "LocalOffset must have standard layout");
587static_assert(std::is_standard_layout_v<GlobalOffset>,
588 "GlobalOffset must have standard layout");
589static_assert(std::is_standard_layout_v<IndexBounds>,
590 "IndexBounds must have standard layout");
591static_assert(std::is_standard_layout_v<GridSpacing>,
592 "GridSpacing must have standard layout");
593static_assert(std::is_standard_layout_v<PhysicalOrigin>,
594 "PhysicalOrigin must have standard layout");
595static_assert(std::is_standard_layout_v<PhysicalCoords>,
596 "PhysicalCoords must have standard layout");
597static_assert(std::is_standard_layout_v<PhysicalBounds>,
598 "PhysicalBounds must have standard layout");
599
600} // namespace pfc
Core type definitions for World parameters.
Global subdomain offset in global coordinate system.
Definition strong_types.hpp:284
const Int3 & get() const noexcept
Get underlying value.
Definition strong_types.hpp:297
bool operator==(const GlobalOffset &other) const noexcept
Equality comparison.
Definition strong_types.hpp:310
Int3 value
Underlying array value.
Definition strong_types.hpp:285
GlobalOffset(const Int3 &v)
Construct from Int3 (implicit for backward compatibility)
Definition strong_types.hpp:291
bool operator!=(const GlobalOffset &other) const noexcept
Inequality comparison.
Definition strong_types.hpp:319
Grid dimensions (number of grid points per dimension)
Definition strong_types.hpp:176
bool operator!=(const GridSize &other) const noexcept
Inequality comparison.
Definition strong_types.hpp:211
const Int3 & get() const noexcept
Get underlying value.
Definition strong_types.hpp:189
GridSize(const Int3 &v)
Construct from Int3 (implicit for backward compatibility)
Definition strong_types.hpp:183
Int3 value
Underlying array value.
Definition strong_types.hpp:177
bool operator==(const GridSize &other) const noexcept
Equality comparison.
Definition strong_types.hpp:202
Physical spacing between grid points.
Definition strong_types.hpp:370
bool operator!=(const GridSpacing &other) const noexcept
Inequality comparison.
Definition strong_types.hpp:405
GridSpacing(const Real3 &v)
Construct from Real3 (implicit for backward compatibility)
Definition strong_types.hpp:377
bool operator==(const GridSpacing &other) const noexcept
Equality comparison.
Definition strong_types.hpp:396
const Real3 & get() const noexcept
Get underlying value.
Definition strong_types.hpp:383
Real3 value
Underlying array value.
Definition strong_types.hpp:371
Index space bounds (min and max indices)
Definition strong_types.hpp:340
IndexBounds(const Int3 &lo, const Int3 &hi)
Construct from lower and upper bounds.
Definition strong_types.hpp:349
Int3 lower
Lower bounds (inclusive)
Definition strong_types.hpp:341
Int3 upper
Upper bounds (inclusive)
Definition strong_types.hpp:342
Local subdomain offset in local coordinate system.
Definition strong_types.hpp:230
const Int3 & get() const noexcept
Get underlying value.
Definition strong_types.hpp:243
bool operator==(const LocalOffset &other) const noexcept
Equality comparison.
Definition strong_types.hpp:256
LocalOffset(const Int3 &v)
Construct from Int3 (implicit for backward compatibility)
Definition strong_types.hpp:237
Int3 value
Underlying array value.
Definition strong_types.hpp:231
bool operator!=(const LocalOffset &other) const noexcept
Inequality comparison.
Definition strong_types.hpp:265
Physical space bounds (min and max coordinates)
Definition strong_types.hpp:534
Real3 upper
Upper bounds.
Definition strong_types.hpp:536
Real3 lower
Lower bounds.
Definition strong_types.hpp:535
PhysicalBounds(const Real3 &lo, const Real3 &hi)
Construct from lower and upper bounds.
Definition strong_types.hpp:543
Physical coordinates in space.
Definition strong_types.hpp:478
bool operator!=(const PhysicalCoords &other) const noexcept
Inequality comparison.
Definition strong_types.hpp:513
Real3 value
Underlying array value.
Definition strong_types.hpp:479
PhysicalCoords(const Real3 &v)
Construct from Real3 (implicit for backward compatibility)
Definition strong_types.hpp:485
const Real3 & get() const noexcept
Get underlying value.
Definition strong_types.hpp:491
bool operator==(const PhysicalCoords &other) const noexcept
Equality comparison.
Definition strong_types.hpp:504
Physical origin of coordinate system.
Definition strong_types.hpp:424
const Real3 & get() const noexcept
Get underlying value.
Definition strong_types.hpp:437
PhysicalOrigin(const Real3 &v)
Construct from Real3 (implicit for backward compatibility)
Definition strong_types.hpp:431
Real3 value
Underlying array value.
Definition strong_types.hpp:425
bool operator==(const PhysicalOrigin &other) const noexcept
Equality comparison.
Definition strong_types.hpp:450
bool operator!=(const PhysicalOrigin &other) const noexcept
Inequality comparison.
Definition strong_types.hpp:459
Represents the global simulation domain (the "world").
Definition world.hpp:91