OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
16_strong_types_demo.cpp File Reference

Demonstration of strong type aliases for geometric quantities. More...

#include <iomanip>
#include <iostream>
#include <openpfc/core/strong_types.hpp>
#include <openpfc/core/types.hpp>
Include dependency graph for 16_strong_types_demo.cpp:

Functions

void print_domain_info (GridSize size, GridSpacing spacing, PhysicalOrigin origin)
 Print domain information using strong types.
 
IndexBounds calculate_subdomain_bounds (GridSize total_size, LocalOffset local_offset, GridSize local_size)
 Calculate subdomain bounds using strong types.
 
PhysicalCoords index_to_physical (Int3 index, PhysicalOrigin origin, GridSpacing spacing)
 Map index to physical coordinates.
 
int main ()
 

Detailed Description

Demonstration of strong type aliases for geometric quantities.

This example shows how strong types make code more self-documenting and catch argument order mistakes at compile time.

What You'll Learn

  • How to use strong types (GridSize, GridSpacing, PhysicalOrigin)
  • Benefits of type safety in function signatures
  • Zero-cost abstraction (compiles to same assembly as raw types)
  • Backward compatibility with existing code

Key Concepts

Problem: Raw Int3 and Real3 types don't distinguish between different kinds of geometric quantities:

Int3 size = {64, 64, 64};
Int3 offset = {0, 0, 0};
// Easy to confuse! Both are just Int3

Solution: Strong types make the distinction explicit:

GridSize size({64, 64, 64});
LocalOffset offset({0, 0, 0});
// Compiler knows these are different!

Function Documentation

◆ calculate_subdomain_bounds()

IndexBounds calculate_subdomain_bounds ( GridSize  total_size,
LocalOffset  local_offset,
GridSize  local_size 
)

Calculate subdomain bounds using strong types.

This function demonstrates type safety - you cannot accidentally pass spacing where offset is expected.

◆ print_domain_info()

void print_domain_info ( GridSize  size,
GridSpacing  spacing,
PhysicalOrigin  origin 
)

Print domain information using strong types.

Function signature is self-documenting - no need to check docs to understand what each parameter means.

Parameters
sizeGrid dimensions
spacingPhysical spacing between grid points
originPhysical origin of coordinate system
Here is the call graph for this function: