Skip to main content

Python-based Phase Field Crystal (PFC) simulation tools

Project description

pyPFC: An Open-Source Python Package for Phase Field Crystal Simulations

A Python software package for setting up, running and processing Phase Field Crystal (PFC) simulations. The code uses PyTorch to allow execution on both CPUs and GPUs, depending on the available hardware. The PFC implementation in pyPFC relies on heavy use of FFTs and is set on a regular Cartesian 3D grid. Periodic boundary conditions are assumed along all coordinate axes.

PFC atoms

Control parameters

The general behavior of pyPFC is controlled by a set of parameters, collected in a Python dictionary. The parameters are described in the table below.

Parameter name Defaults to Description
alat 1.0 Lattice parameter (non-dimensional)
alpha [1, 1] C2 Gaussian peak widths, excluding the zero-mode peak
C20_amplitude 0.0 Amplitude of the zero-mode Gaussian peak in C2
C20_alpha 1.0 Width of the zero-mode Gaussian peak in C2
density_interp_order 2 Interpolation order for density maxima localization
density_merge_distance 0.1 Distance for merging density maxima (in units of lattice parameters)
density_threshold 0.5 Threshold for density maxima detection
device_number 0 GPU device number (if multiple GPUs are available)
device_type 'gpu' PyTorch device ('cpu' or 'gpu')
dtime 1.0e-3 Non-dimensional time increment
dtype_cpu np.double Floating-point precision of numpy arrays
dtype_gpu torch.float64 Floating-point precision of PyTorch tensors
evaluate_phase_field True Evaluate phase field (or not)
normalize_pf True Normalize the phase fields to [0,1], or not
npeaks 2 Number of Gaussian peaks, excluding the zero-mode peak, to use in C2
pf_gauss_var 0.1 Variance (sigma) of the Gaussian smoothing kernel used in phase field evaluations
pf_iso_level 0.5 Iso-level for phase field contouring
sigma 0.0 Temperature-like parameter (non-dimensional)
struct 'FCC' Crystal structure
torch_threads 8 Number of CPU threads to use if device_type is 'cpu'
torch_threads_interop 8 Number of interop threads to use if device_type is 'cpu'
update_scheme '1st_order' Time integration scheme ('1st_order', '2nd_order' or 'exponential')
update_scheme_params [1.0, 1.0, 1.0, None, None, None] Parameters in the time integration scheme: [g1, g2, g3, alpha, beta, gamma]
verbose True Verbose output (or not)

Description of Source Files

The software is built on classes, contained in separate modules/files, with an inheritance chain (from top to bottom) comprising:

File (*.py) Description
pypfc_grid Grid methods
pypfc_base Base methods
pypfc_pre Pre-processing methods
pypfc_io Data IO methods
pypfc Main class

In addition, pypfc_ovito.py provides custom interfaces to selected functionalities in OVITO, useful for post-processing of pyPFC simulation output.

Methods in the different classes are described in individual subsections below.

The class pypfc_grid

The class is initiated by supplying the arguments ndiv = [nx,ny,nz] and ddiv=[dx,dy,dz], where [nx,ny,nz] are the number of grid points and [dx,dy,dz] the grid spacing along each coordinate direction.

Class methods and their arguments

Method Description
set_ndiv(ndiv) Sets the number of grid points nx,ny,nz
get_ndiv Returns ndiv
set_ddiv(ddiv) Sets the grid spacing dx,dy,dz
get_ddiv Returns ddiv
get_dSize Returns the grid size dSize=ndiv*ddiv
copy_from(grid) Makes a duplicate of a grid class object

The class pypfc_base

The class is initiated by supplying the arguments ndiv and ddiv. A dictionary of configuration parameters can also be supplied through config.

Class methods and their arguments

Method Description
set_verbose(verbose) Set verbose output flag
get_verbose Get verbose output flag
set_dtype_cpu(dtype) Set the cpu floating point precision
get_dtype_cpu Get the cpu floating point precision
set_dtype_gpu(dtype) Set the gpu floating point precision
get_dtype_gpu Get the gpu floating point precision
set_device_type(device_type) Set the device type (cpu or gpu)
get_device_type Get the device type (cpu or gpu)
set_device_number(device_number) Set the GPU device number
get_device_number Get the GPU device number
set_k2_d(k2_d) Set sum of squared wave vector (on the device)
get_k2_d Get sum of squared wave vector (on the device)
set_torch_threads Set torch.get_num_threads() and torch.get_num_interop_threads()
get_torch_threads Get torch.get_num_threads() and torch.get_num_interop_threads()
get_time_stamp Get time stamp string: YYYY-MM-DD H:M
get_k(npoints, dspacing) Define a 1D wave vector at npoint points with spacing dspacing
evaluate_k2_d Evaluate the sum of squared wave vectors
get_integrated_field_in_volume(field, limits) Integrate a field variable within a volume defined by lower and upper coordinate limits
get_field_average_along_axis(field, axis) Evaluate the mean value of a field variable along a certain axis ('x', 'y' or 'z')
get_integrated_field_along_axis(field, axis) Integrate a field variable along a certain axis ('x', 'y' or 'z')
interpolate_atoms(intrpPos, pos, values, num_nnb, power) Interpolate values at given positions
interpolate_density_maxima(self, den, ene, pf) Interpolate density field maxima, plus energy and (optionally) also phase field(s)
get_phase_field_contour(self, pf, pf_zoom, evaluate_volume) Find the iso-contour surface of a 3D phase field using marching cubes and (optionally) the enclosed volume
get_rlv(struct, alat) Get the reciprocal lattice vectors for a particular crystal structure
evaluate_reciprocal_planes Establish the reciprocal vectors/planes for a particular crystal structure
evaluate_C2_d Establish the two-point correlation function (on the device) for a particular crystal structure
evaluate_directional_correlation_kernel(self, H0, Rot) Establish the directional correlation kernel for a particular crystal structure

The class pypfc_pre

The class is initiated by supplying the arguments ndiv and ddiv. A dictionary of configuration parameters can also be supplied through config.

Class methods and their arguments

Method Description
set_struct(struct) Set crystal structure
get_struct Get crystal structure
set_density(den) Set PFC density field
get_density Get PFC density field
set_energy(ene) Set PFC energy field
get_energy(ene) Get PFC energy field
get_ampl Get amplitudes of the density field approximation
get_nlns Get liquid/solid densities in the density field approximation
set_sigma(sigma) Set sigma
get_sigma Get sigma
set_npeaks(npeaks) Set the number of Gaussian peaks in the pair correlation function
get_npeaks Set the number of Gaussian peaks in the pair correlation function
do_single_crystal(xtalRot, params, model) Define a centered crystal in a periodic 3D domain
do_bicrystal(xtalRot, params, liq_width, model) Define a centered crystal, embedded inside a matrix crystal, in a periodic 3D domain
do_polycrystal(xtalRot, params, liq_width, model) Define a polycrystal in a periodic 3D domain
generate_density_field(crd, g) Define a 3D PFC density field
evaluate_ampl_dens Get the amplitudes and densities for different density field expansions

The class pypfc_io

The class is initiated by supplying the arguments ndiv and ddiv. A dictionary of configuration parameters can also be supplied through config.

Class methods and their arguments

Method Description
write_extended_xyz(filename, coord, atom_data, atom_data_labels, simulation_time, gz) Save PFC data in extended XYZ format
read_extended_xyz(filename, nfields) Read PFC data in extended XYZ format from file
write_vtk_points(filename, coord, scalarData, scalarDataName, vectorData, vectorDataName, tensorData, tensorDataName) Save 3D point data to a VTK file
write_vtk_structured_grid(filename, arrayData, arrayName) Save 3D field data to a VTK file
save_pickle(filename, data) Save a list of data objects to a binary pickle file
load_pickle(filename, ndata) Load data objetcs from a binary pickle file
write_info_file(filename=, output_path) Write simulation setup information to a file
append_to_info_file(info, filename=, output_path) Append linea to a text file

The class pypfc

The class is initiated by supplying the arguments ndiv and ddiv. A dictionary of configuration parameters can also be supplied through config.

Class methods and their arguments

Method Description
set_alat(alat) Set lattice parameter (non-dimensional)
get_alat Get lattice parameter (non-dimensional)
set_dtime(dtime) Set time increment (non-dimensional)
get_dtime Get time increment (non-dimensional)
set_alpha(alpha) Set peak widths in C2
get_alpha Get peak widths in C2
set_C2_d(C2_d) Set C2 (on the device)
get_C2_d Get C2 (on the device)
set_H2_d(H0, Rot) Set H2 (in Fourier space, on the device)
set_update_scheme(update_scheme) Set time integration scheme
get_update_scheme Get time integration scheme
set_update_scheme_params(params) Set time integration scheme parameters
get_update_scheme_params Get time integration scheme parameters
get_energy Get the PFC energy field and its mean value
set_density(density) Set the PFC density field
get_density Get the PFC density field and its mean value
set_phase_field_kernel(H0, Rot) Set phase field kernel
set_phase_field_smoothing_kernel(pf_gauss_var) Set phase field smoothing kernel
cleanup Clean up variables
do_step_update Update the (X)PFC density field using the chosen time integration scheme
evaluate_energy Evaluate the PFC energy field
get_phase_field Evaluate the phase field using wavelet filtering

Package dependencies

The following Python packages are required:

  • ovito (if pypfc_ovito is to be used)
  • pyvtk
  • scikit-image
  • scipy
  • torch

Installation and Usage

Install using pip install pypfc and import with import pypfc and (optionally) import pypfc_ovito.

Licencing and Acknowledgment

This software is released under a GNU GPLv3 license.

References

Further details on PFC modeling and example applications can be found in:

  1. K.H. Blixt and H. Hallberg, Phase Field Crystal Modeling of Grain Boundary Migration: Mobility, Energy and Structural Variability, Acta Materialia, 297:121318, 2025
  2. H. Hallberg and K.H. Blixt, Assessing grain boundary variability through phase field crystal simulations, Physical Review Materials, 8(3):113605, 2024
  3. K.H. Blixt and H. Hallberg, Phase field crystal modeling of grain boundary structures in diamond cubic systems, Physical Review Materials, 8(3):033606, 2024
  4. H. Hallberg and K.H. Blixt, Multiplicity of grain boundary structures and related energy variations, Materials Today Communications, 38:107724, 2024
  5. H. Hallberg and K.H. Blixt, Evaluation of Nanoscale Deformation Fields from Phase Field Crystal Simulations, Metals, 12(10):1630, 2022
  6. K.H. Blixt and H. Hallberg, Grain boundary and particle interaction: Enveloping and pass-through mechanisms studied by 3D phase field crystal simulations, Materials & Design, 220:110845, 2022
  7. K.H. Blixt and H. Hallberg, Grain boundary stiffness based on phase field crystal simulations, Materials Letters, 318:132178, 2022
  8. K.H. Blixt and H. Hallberg, Evaluation of grain boundary energy, structure and stiffness from phase field crystal simulations, Modelling and Simulation in Materials Science and Engineering, 30(1):014002, 2022

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypfc-0.0.1.tar.gz (43.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypfc-0.0.1-py3-none-any.whl (47.7 kB view details)

Uploaded Python 3

File details

Details for the file pypfc-0.0.1.tar.gz.

File metadata

  • Download URL: pypfc-0.0.1.tar.gz
  • Upload date:
  • Size: 43.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypfc-0.0.1.tar.gz
Algorithm Hash digest
SHA256 b10e22916b5a2afecd894d6ce14b6e82d29e1c6a9125df2343025d6788b0b43b
MD5 6cceb6c9f95bb15f822cfe9b88ba5bad
BLAKE2b-256 dd5fd9a5390f036b1757eecab368c0ea555c02647cc014e31559b6f75db5e493

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypfc-0.0.1.tar.gz:

Publisher: python-publish.yml on HHallb/pyPFC

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pypfc-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pypfc-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 47.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pypfc-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 66dd82ab41cbd3352c1cb26edf2c75226f5134a7dc95ebb2a31a515d7f3e36ff
MD5 b081257895f7d3a944054362f1ead6ac
BLAKE2b-256 d32683032d20ee385ce8ef2f78820a7dc9e664af1cc943a2471aff834fa5d58e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypfc-0.0.1-py3-none-any.whl:

Publisher: python-publish.yml on HHallb/pyPFC

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page