GCMC Class¶
The GCMC class serves as the main interface for handling Grand Canonical Monte Carlo (GCMC) simulation logic.
- class flames.gcmc.GCMC(model, framework_atoms, adsorbate_atoms, temperature, pressure, device, vdw_radii, vdw_factor=0.6, framework_energy=None, adsorbate_energy=None, max_translation=1.5, max_rotation=1.5707963267948966, max_deltaE=1.555, save_frequency=100, save_rejected=False, output_to_file=True, output_folder=None, debug=False, fugacity_coeff=1.0, random_seed=None, cutoff_radius=6.0, automatic_supercell=True, criticalTemperature=None, criticalPressure=None, acentricFactor=None, void_fraction=0.0, LLM=False, move_weights={'deletion': 0.2, 'insertion': 0.2, 'reinsertion': 0.2, 'rotation': 0.2, 'translation': 0.2})[source]
Bases:
BaseSimulatorBase class for Grand Canonical Monte Carlo (GCMC) simulations using ASE.
This class employs Monte Carlo simulations under the grand canonical ensemble (\(μVT\)) to study the adsorption of molecules in a framework material. It allows for movements such as insertion, deletion, translation, and rotation of adsorbate molecules within the framework.
Currently, it supports any ASE-compatible calculator for energy calculations.
- Parameters:
model (ase.calculators.calculator.Calculator) – The calculator to use for energy calculations. Can be any ASE-compatible calculator. The output of the calculator should be in eV.
framework_atoms (ase.Atoms) – The framework structure as an ASE Atoms object.
adsorbate_atoms (ase.Atoms) – The adsorbate structure as an ASE Atoms object.
temperature (float) – Temperature of the ideal reservoir in Kelvin.
pressure (float) – Pressure of the ideal reservoir in Pascal.
device (str) – Device to run the simulation on, e.g.,
'cpu'or'cuda'.vdw_radii (np.ndarray) – Van der Waals radii for the atoms in the framework and adsorbate. Should be an array of the same length as the number of atomic numbers in ASE.
max_deltaE (float, optional) – Maximum energy difference (in eV) to consider for acceptance criteria. This is used to avoid overflow due to problematic calculations. Default is
1.555eV (approx. 150 kJ/mol).vdw_factor (float, optional) – Factor to scale the Van der Waals radii. Default is
0.6.framework_energy (float or None, optional) – Pre-calculated potential energy of the empty framework in eV. If not provided, it will be calculated during initialization.
adsorbate_energy (float or None, optional) – Pre-calculated potential energy of the adsorbate molecule in eV. If not provided, it will be calculated during initialization.
max_translation (float, optional) – Maximum translation distance. Default is
1.5.max_rotation (float, optional) – Maximum rotation angle (in radians). Default is
90degrees (converted to radians).save_frequency (int, optional) – Frequency at which to save the simulation state and results. Default is
100.save_rejected (bool, optional) – If
True, saves the rejected moves in a trajectory file. Default isFalse.output_to_file (bool, optional) – If
True, writes the output to a file namedoutput_{temperature}_{pressure}.outin theresultsdirectory. Default isTrue.output_folder (str or None, optional) – Folder to save the output files. If
None, a folder namedresults_<T>_<P>will be created.debug (bool, optional) – If
True, prints detailed debug information during the simulation. Default isFalse.fugacity_coeff (float, optional) – Fugacity coefficient to correct the pressure. Default is
1.0. Only used ifcriticalTemperature,criticalPressure, andacentricFactorare not provided.random_seed (int or None, optional) – Random seed for reproducibility. Default is
Noneand will generate a random seed automatically if not provided.cutoff_radius (float, optional) – Interaction potential cut-off radius used to estimate the minimum unit cell. Default is
6.0.automatic_supercell (bool, optional) – If
True, automatically creates a supercell based on the cutoff radius. Default isTrue.max_length (float or None, optional) – Maximum length (in Angstroms) for any side of the supercell. If
None, no maximum length is enforced. This can be used to limit the size of the supercell for computational efficiency. Default isNone.criticalTemperature (float, optional) – Critical temperature of the adsorbate in Kelvin.
criticalPressure (float, optional) – Critical pressure of the adsorbate in Pascal.
acentricFactor (float, optional) – Acentric factor of the adsorbate.
void_fraction (float, optional) – Void fraction of the adsorbate.
LLM (bool, optional) – Use Leftmost Local Minima (LLM) on the determination of the equilibration point by pyMSER. This can underestimate the equilibration point in some situations, but generate good averages for well-behaved scenarios.
move_weights (dict, optional) – A dictionary containing the move weights for
'insertion','deletion','translation','rotation', and'reinsertion'. Default is equal weights for all moves. Example:{'insertion': 0.3, 'deletion': 0.3, 'translation': 0.2, 'rotation': 0.2, 'reinsertion': 0.0}
- equilibrate(batch_size=False, run_ADF=False, uncertainty='uSD', production_start=0)[source]
Use pyMSER to get the equilibrated statistics of the simulation.
- Parameters:
LLM (bool) – If True, use the Leftmost-Local Minima (LLM) method to determine the equilibration time. This is only recommended for high-throughput simulations, and sometimes can underestimate the true equilibration point. Default is True.
batch_size (int) – Batch size to use for speedup the equilibration process. Default is 100.
run_ADF (bool) – If True, run the Augmented Dickey-Fuller (ADF) test to confirm for stationarity. Default is False.
uncertainty (str) – The type of uncertainty to use for the equilibration process. Default is “uSD”. Options are: - “uSD”: uncorrelated Standard Deviation - “uSE”: uncorrelated Standard Error - “SD”: Standard Deviation - “SE”: Standard Error
production_start (int) – The step to start the production analysis from. Default is 0.
- Return type:
- get_average_ads_energy()[source]
Compute the average adsorption energy per adsorbed molecule.
- The adsorption energy is calculated as:
E_ads_avg = [E_total - (N_ads * E_adsorbate) - E_framework] / N_ads
- where:
E_total: current total energy of the system (simulation)
N_ads: number of adsorbed molecules
E_adsorbate: energy of an isolated adsorbate molecule
E_framework: energy of the empty framework
The result is converted from simulation units to kJ/mol per adsorbate.
- Returns:
The average adsorption energy per molecule in kJ/mol. Returns 0.0 if no molecules are adsorbed (N_ads == 0).
- Return type:
- get_framework_mass()
Calculate the mass of the framework in kg.
- Returns:
The mass of the framework in kg.
- Return type:
- get_ideal_supercell()
Get the ideal supercell dimensions based on the cutoff radius.
- Returns:
An array of three integers representing the number of unit cells in each dimension.
- Return type:
np.ndarray
- insert_adsorbates(n_adsorbates, max_attempts=1000)[source]
Insert a given number of adsorbate molecules into the framework at random positions without overlap.
- load_state(state_file)[source]
Load the state of the simulation from a file.
- npt(nsteps, time_step=0.5, mode='iso_shape', driver='MTKNPT', set_momenta=True, output_interval=100, movie_interval=100, calculator=None, **kwargs)
Run a NPT simulation using the Berendsen thermostat and barostat.
- Parameters:
nsteps (int) – Number of steps to run the NPT simulation.
time_step (float, optional) – Time step for the NPT simulation (default is 0.5 fs).
mode (str, optional) – The mode of the NPT simulation (default is “iso_shape”). Can be one of “iso_shape”, “aniso_shape”, or “aniso_flex”.
driver (str, optional) – The driver to use for the NPT simulation. Can be Berendsen, NoseHoover or MTKNPT (default is “MTKNPT”).
set_momenta (bool, optional) – Whether to set the atomic momenta to a Maxwell-Boltzmann distribution of the simulation temperature.
output_interval (int, optional) – The interval for logging output (default is 100 steps).
movie_interval (int, optional) – The interval for saving trajectory frames (default is 100 step).
calculator (ase.calculators.calculator.Calculator or None, optional) – The calculator to use for energy calculations. If None, the default model will be used.
kwargs (optional) – Arguments passed to the ase molecular dynamics class.
- nvt(nsteps, time_step=0.5, set_momenta=True, output_interval=100, movie_interval=100, calculator=None, **kwargs)
Run a NVT simulation using the Berendsen thermostat.
- Parameters:
nsteps (int) – Number of steps to run the NVT simulation.
time_step (float, optional) – Time step for the NVT simulation (default is 0.5 fs).
set_momenta (bool, optional) – Whether to set the atomic momenta to a Maxwell-Boltzmann distribution of the simulation temperature.
output_interval (int, optional) – The interval for logging output (default is 100 steps).
movie_interval (int, optional) – The interval for saving trajectory frames (default is 100 step).
calculator (ase.calculators.calculator.Calculator or None, optional) – The calculator to use for energy calculations. If None, the default model will be used.
kwargs (optional) – Arguments passed to the ase molecular dynamics class.
- optimize_adsorbate(max_steps=1000, max_force=0.05)
Optimize the adsorbate structure using the provided calculator.
- Parameters:
- Returns:
The optimized adsorbate structure.
- Return type:
ase.Atoms
- optimize_framework(max_steps=1000, opt_cell=True, fix_symmetry=True, hydrostatic_strain=True, symm_tol=0.001, max_force=0.05)
Optimize the framework structure using the provided calculator.
- restart()[source]
Restart the simulation from the last state.
This method loads the last saved state from the trajectory file and restores the simulation to that state. It also loads the uptake, total energy, and total adsorbates lists from the saved files if they exist.
- Return type:
- save_results(file_name=None, batch_size=False, run_ADF=False, uncertainty='uSD')[source]
Save a json file with the main results of the simulation.
- Parameters:
file_name (str) – Name of the output file. Default is ‘GCMC_Results.json’.
LLM (bool) – If True, use the Leftmost-Local Minima (LLM) method to determine the equilibration time. This is only recommended for high-throughput simulations, and sometimes can underestimate the true equilibration point. Default is True.
batch_size (int) – Batch size to use for speedup the equilibration process. Default is False, which means 2% of the total number of steps.
run_ADF (bool) – If True, run the Augmented Dickey-Fuller (ADF) test to confirm for stationarity. Default is False.
uncertainty (str) – The type of uncertainty to use for the equilibration process. Default is “uSD”. Options are: - “uSD”: uncorrelated Standard Deviation - “uSE”: uncorrelated Standard Error - “SD”: Standard Deviation - “SE”: Standard Error
- Return type:
- set_adsorbate(adsorbate_atoms, adsorbate_energy=None, n_adsorbates=0)
Set the adsorbate structure for the simulation.
- Parameters:
- Return type:
- set_framework(framework_atoms, framework_energy=None)
Set the framework structure for the simulation.
- set_state(state)
Set the current state of the simulation.
- Parameters:
state (ase.Atoms) – The current state of the simulation as an ASE Atoms object.
- Return type:
- step(iteration)[source]
Perform a single Grand Canonical Monte Carlo step. It will randomly select a move based on the move weights and attempt to perform it. The uptake, total energy, and total adsorbates lists are updated accordingly.
- try_deletion()[source]
Try to delete an adsorbate molecule from the framework. This method randomly selects an adsorbate molecule and try to apply the deletion.
If there are no adsorbates, it returns False.
- Returns:
True if the deletion was accepted, False otherwise.
- Return type:
- try_insertion()[source]
Try to insert a new adsorbate molecule into the framework. This method randomly places the adsorbate in the framework and checks for van der Waals overlap. If there is no overlap, it calculates the new potential energy and decides whether to accept the insertion based on the acceptance criteria.
- Returns:
True if the insertion was accepted, False otherwise.
- Return type:
- try_reinsertion()[source]
Try to delete and reinsert an adsorbate molecule. This method randomly selects an adsorbate molecule, deletes it, and tries to reinsert it at a new random position within the framework.
If there are no adsorbates, it returns False.
- Returns:
True if the reinsertion was accepted, False otherwise.
- Return type:
- try_rotation()[source]
Try to rotate an adsorbate molecule within the framework. This method randomly selects an adsorbate molecule and applies a random rotation. It checks for van der Waals overlap and calculates the new potential energy.
- Returns:
True if the rotation was accepted, False otherwise.
- Return type:
- try_translation()[source]
Try to translate an adsorbate molecule within the framework. This method randomly selects an adsorbate molecule and applies a random translation. It checks for van der Waals overlap and calculates the new potential energy.
- Returns:
True if the translation was accepted, False otherwise.
- Return type:
- property base_iteration: int
Get the base iteration for the GCMC simulation.
- Returns:
The base iteration count.
- Return type:
- property move_weights: dict
Get the move weights for the GCMC simulation.
- Returns:
A dictionary containing the move weights for each type of movement.
- Return type: