Demonstrates type-safe World construction using strong types.
Demonstrates type-safe World construction using strong typesThis example shows how to use strong types (GridSize, PhysicalOrigin, GridSpacing) for clear, self-documenting, and type-safe World creation. Strong types prevent parameter confusion at compile time.
#include <iomanip>
#include <iostream>
int main() {
using namespace pfc;
std::cout << "OpenPFC Strong Types Example\n";
std::cout << "=============================\n\n";
std::cout << "1. Basic World creation with strong types:\n";
{
auto world = create(size,
origin, spacing);
std::cout << " Grid size: " << get_size(world)[0] << "³\n";
std::cout << " Physical origin: (" << get_origin(world)[0] << ", "
<< get_origin(world)[1] << ", " << get_origin(world)[2] << ")\n";
std::cout << " Grid spacing: " << get_spacing(world)[0] << "\n";
}
std::cout << "\n";
std::cout << "2. Inline construction:\n";
{
auto world =
std::cout << " Created 128³ grid with spacing 0.5\n";
std::cout << " Domain extends from " << get_origin(world)[0] << " to ";
}
std::cout << "\n";
std::cout << "3. Non-uniform grid (different sizes and spacing):\n";
{
auto world = create(size,
origin, spacing);
std::cout << " Grid: " << get_size(world)[0] << "×" << get_size(world)[1]
<< "×" << get_size(world)[2] << "\n";
std::cout << " Spacing: dx=" << get_spacing(world)[0]
<< ", dy=" << get_spacing(world)[1] << ", dz=" << get_spacing(world)[2]
<< "\n";
Real3
size_phys = {get_size(world)[0] * get_spacing(world)[0],
get_size(world)[1] * get_spacing(world)[1],
get_size(world)[2] * get_spacing(world)[2]};
}
std::cout << "\n";
std::cout << "4. Type safety demonstration:\n";
{
std::cout << " ✓ Correct: create(size, origin, spacing)\n";
std::cout << " ✗ Wrong parameter orders rejected at compile time\n";
}
std::cout << "\n";
std::cout << "5. Zero overhead verification:\n";
{
std::cout << " sizeof(GridSize) == sizeof(Int3): "
<< (
sizeof(
GridSize) ==
sizeof(Int3) ?
"✓" :
"✗") <<
"\n";
std::cout << " sizeof(PhysicalOrigin) == sizeof(Real3): "
std::cout << " sizeof(GridSpacing) == sizeof(Real3): "
<< (
sizeof(
GridSpacing) ==
sizeof(Real3) ?
"✓" :
"✗") <<
"\n";
std::cout << " Strong types compile away completely!\n";
}
std::cout << "\n";
std::cout << "6. Backward compatibility with raw types:\n";
{
Int3 size = {32, 32, 32};
Real3 offset = {0.0, 0.0, 0.0};
Real3 spacing = {1.0, 1.0, 1.0};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
auto world = create(size, offset, spacing);
#pragma GCC diagnostic pop
std::cout << " Old API still works (with deprecation warning)\n";
std::cout << " Migration: Just wrap parameters in strong types!\n";
}
std::cout << "\n";
std::cout << "7. Helper functions use strong types internally:\n";
{
std::cout << " uniform(64) creates 64³ grid\n";
auto world2 = uniform(128, 0.5);
std::cout << " uniform(128, 0.5) creates 128³ grid with spacing 0.5\n";
auto world3 = from_bounds({100, 100, 100}, {0, 0, 0}, {10, 10, 10});
std::cout << " from_bounds() computes spacing automatically\n";
}
std::cout << "\n";
std::cout << "8. Coordinate transformations:\n";
{
auto world =
Real3
center = to_coords(world, {32, 32, 32});
std::cout <<
" Center index (32,32,32) maps to physical (" <<
center[0] <<
","
Real3
corner = to_coords(world, {0, 0, 0});
std::cout <<
" Origin index (0,0,0) maps to physical (" <<
corner[0] <<
","
}
std::cout << "\n";
std::cout << "Strong types make code safer and more readable!\n";
return 0;
}
Namespace for decomposition-related classes and functions.
Definition decomposition.hpp:56
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
World class definition and unified interface.