models_base¶
Base model classes used across kMCpy.
- class kmcpy.models.base.BaseModel(*args, **kwargs)[source]¶
Base class for models in kmcpy.
This base class provides common serialization and loading conventions for model objects. Scientific operations such as
compute,build, andcompute_probabilityare optional because different model classes have different roles in a KMC workflow.Constructor convention (pymatgen-style): - as_dict and from_dict handle structured data. - to and from_file handle file I/O.
- name¶
Name of the model instance.
- Type:
str, optional
- apply_event(*, event, simulation_state)[source]¶
Commit an accepted event to optional model-side state.
Stateless models can ignore this hook. Stateful external adapters should update only the changed sites here so their internal state stays aligned with kMCpy’s
State.- Return type:
None
- compute_probability(*args, **kwargs)[source]¶
Compute an event rate/probability for KMC, when supported.
- classmethod from_config(config)[source]¶
Load the configured model.
Called on
BaseModel, this dispatches to the concrete model class declared by the model file orconfig.model_type. Called on a concrete subclass, it loads that subclass directly fromconfig.model_file.
- abstractmethod classmethod from_dict(d)[source]¶
Create a model object from a dictionary representation.
- initialize_state(*, simulation_state, event_lib=None, structure=None, config=None, active_site_order=None)[source]¶
Initialize optional stateful model caches from the KMC state.
Stateless models can ignore this hook. Stateful adapters can use it to build their own occupancy representation once, instead of rebuilding it during every event-rate evaluation.
- Return type:
None
- classmethod load(file_path)¶
Loads a class from a provided json file.
- Parameters:
file_path (os.PathLike) – The json file to load from.
- Returns:
An instance of the class being reloaded.
- Return type:
MSONable
- save(json_path, mkdir=True, json_kwargs=None, pickle_kwargs=None, strict=True)¶
Utility that uses the standard tools of MSONable to convert the class to json format, but also save it to disk. In addition, this method intelligently uses pickle to individually pickle class objects that are not serializable, saving them separately. This maximizes the readability of the saved class information while allowing _any_ class to be at least partially serializable to disk.
For a fully MSONable class, only a class.json file will be saved to the location {save_dir}/class.json. For a partially MSONable class, additional information will be saved to the save directory at {save_dir}. This includes a pickled object for each attribute that e serialized.
- Parameters:
file_path (os.PathLike) – The file to which to save the json object. A pickled object of the same name but different extension might also be saved if the class is not entirely MSONable.
mkdir (bool) – If True, makes the provided directory, including all parent directories.
json_kwargs (dict) – Keyword arguments to pass to the serializer.
pickle_kwargs (dict) – Keyword arguments to pass to pickle.dump.
strict (bool) – If True, will not allow you to overwrite existing files.
- to_json()¶
Returns a json string representation of the MSONable object.
- Return type:
str
- unsafe_hash()¶
Returns an hash of the current object. This uses a generic but low performance method of converting the object to a dictionary, flattening any nested keys, and then performing a hash on the resulting object
- classmethod validate_monty_v1(_MSONable__input_value)¶
Pydantic validator with correct signature for pydantic v1.x
- classmethod validate_monty_v2(_MSONable__input_value, _)¶
Pydantic validator with correct signature for pydantic v2.x