OpenPFC  0.1.4
Phase Field Crystal simulation framework
Loading...
Searching...
No Matches
17_custom_coordinate_system.cpp File Reference

Example: Defining custom coordinate systems without modifying OpenPFC. More...

#include <cmath>
#include <iostream>
#include "openpfc/openpfc.hpp"
Include dependency graph for 17_custom_coordinate_system.cpp:

Classes

struct  PolarTag
 Tag for polar coordinate system. More...
 
struct  PolarCoordinateSystem
 Polar coordinate system parameters. More...
 
struct  SphericalTag
 Tag for spherical coordinate system. More...
 
struct  SphericalCoordinateSystem
 Spherical coordinate system parameters. More...
 

Functions

Real3 polar_to_coords (const PolarCoordinateSystem &cs, const Int3 &indices, const Int3 &size)
 
Int3 polar_to_indices (const PolarCoordinateSystem &cs, const Real3 &coords, const Int3 &size)
 Convert Cartesian coordinates to grid indices (Cartesian → Polar)
 
Real3 spherical_to_coords (const SphericalCoordinateSystem &cs, const Int3 &indices, const Int3 &size)
 Spherical → Cartesian coordinate transformation.
 
Int3 spherical_to_indices (const SphericalCoordinateSystem &cs, const Real3 &coords, const Int3 &size)
 Cartesian → Spherical coordinate transformation.
 
void example_polar_coordinates ()
 Demonstrate polar coordinate system usage.
 
void example_spherical_coordinates ()
 Demonstrate spherical coordinate system usage.
 
void show_extension_pattern ()
 Summary of the extension pattern.
 
int main ()
 

Variables

constexpr double M_PI = 3.14159265358979323846
 

Detailed Description

Example: Defining custom coordinate systems without modifying OpenPFC.

This example demonstrates OpenPFC's extensibility by showing how to add custom coordinate systems without modifying the library source code.

We implement two complete coordinate systems:

  1. Polar coordinates (2D: r, θ) - simpler case
  2. Spherical coordinates (3D: r, θ, φ) - complete 3D case

Key Technique: ADL (Argument-Dependent Lookup)

OpenPFC uses ADL to find coordinate transformation functions. You define functions in your namespace (or pfc namespace), and ADL ensures they're found automatically.

Philosophy: Laboratory, Not Fortress

This example proves you can extend OpenPFC without forking or modifying its source code. This is the "laboratory" philosophy in action.

How to Use This Example

  1. Read through the polar coordinate implementation (simpler)
  2. Study the spherical coordinate implementation (complete)
  3. Copy the pattern for your own coordinate system
  4. No OpenPFC source code modifications required!

Function Documentation

◆ example_polar_coordinates()

void example_polar_coordinates ( )

Demonstrate polar coordinate system usage.

Shows:

  • Creating polar coordinate system
  • Converting grid indices to Cartesian coordinates
  • Round-trip transformation (indices → coords → indices)
  • Verification of mathematical properties
Examples
/home/runner/work/OpenPFC/OpenPFC/examples/17_custom_coordinate_system.cpp.
Here is the call graph for this function:

◆ example_spherical_coordinates()

void example_spherical_coordinates ( )

Demonstrate spherical coordinate system usage.

Shows complete 3D spherical coordinate transformations with verification.

Examples
/home/runner/work/OpenPFC/OpenPFC/examples/17_custom_coordinate_system.cpp.
Here is the call graph for this function:

◆ polar_to_indices()

Int3 polar_to_indices ( const PolarCoordinateSystem cs,
const Real3 &  coords,
const Int3 &  size 
)
inline

Convert Cartesian coordinates to grid indices (Cartesian → Polar)

Inverse transformation of polar_to_coords().

Given Cartesian coordinates (x, y, z):

  1. Convert to polar: r = √(x² + y²), θ = atan2(y, x)
  2. Map to grid indices: i = round(r), j = round(θ)
Parameters
csPolar coordinate system
coordsCartesian coordinates (x, y, z)
sizeGrid dimensions [n_r, n_θ, 1]
Returns
Grid indices (i_r, i_θ, 0)
Note
z coordinate is ignored (2D system)
Warning
Indices may be outside grid range - caller should check bounds
Examples
/home/runner/work/OpenPFC/OpenPFC/examples/17_custom_coordinate_system.cpp.
Here is the caller graph for this function:

◆ show_extension_pattern()

void show_extension_pattern ( )

Summary of the extension pattern.

Shows the complete recipe for adding custom coordinate systems.

Examples
/home/runner/work/OpenPFC/OpenPFC/examples/17_custom_coordinate_system.cpp.

◆ spherical_to_coords()

Real3 spherical_to_coords ( const SphericalCoordinateSystem cs,
const Int3 &  indices,
const Int3 &  size 
)
inline

Spherical → Cartesian coordinate transformation.

Transforms grid indices in spherical coordinates to Cartesian (x,y,z).

Transformation:

  • x = r * sin(θ) * cos(φ)
  • y = r * sin(θ) * sin(φ)
  • z = r * cos(θ)
Parameters
csSpherical coordinate system
indicesGrid indices (i_r, i_theta, i_phi)
sizeGrid dimensions [n_r, n_θ, n_φ]
Returns
Cartesian coordinates (x, y, z)
Note
Found via ADL - no namespace qualification needed

Special points:

  • theta = 0: North pole (0, 0, r)
  • theta = pi: South pole (0, 0, -r)
  • theta = pi/2, phi = 0: Point on +x axis (r, 0, 0)
  • theta = pi/2, phi = pi/2: Point on +y axis (0, r, 0)
Examples
/home/runner/work/OpenPFC/OpenPFC/examples/17_custom_coordinate_system.cpp.
Here is the caller graph for this function:

◆ spherical_to_indices()

Int3 spherical_to_indices ( const SphericalCoordinateSystem cs,
const Real3 &  coords,
const Int3 &  size 
)
inline

Cartesian → Spherical coordinate transformation.

Inverse of spherical_to_coords().

Transformation:

  • r = √(x² + y² + z²)
  • theta = acos(z / r)
  • phi = atan2(y, x)
Parameters
csSpherical coordinate system
coordsCartesian coordinates (x, y, z)
sizeGrid dimensions [n_r, n_theta, n_phi]
Returns
Grid indices (i_r, i_theta, i_phi)
Warning
Singular at origin (r=0) - undefined theta and phi
Indices may be outside grid - caller should validate
Examples
/home/runner/work/OpenPFC/OpenPFC/examples/17_custom_coordinate_system.cpp.
Here is the caller graph for this function: