gflownet.envs.crystals.composition
Classes to represent material compositions (stoichiometry)
Attributes
Classes
Module Contents
- class gflownet.envs.crystals.composition.Composition(elements=94, max_diff_elem=5, min_diff_elem=2, min_atoms=2, max_atoms=20, min_atom_i=1, max_atom_i=16, oxidation_states=None, alphabet=None, required_elements=(), space_group=None, do_charge_check=False, do_spacegroup_check=True, **kwargs)[source]
Bases:
gflownet.envs.base.GFlowNetEnv- Parameters:
elements (list or int) – Elements that will be used for construction of crystal. Either list, in which case every value should indicate the atomic number of an element, or int, in which case n consecutive atomic numbers will be used. Note that we assume this will correspond to real atomic numbers, i.e. start from 1, not 0.
max_diff_elem (int) – Maximum number of unique elements in the crystal
min_diff_elem (int) – Minimum number of unique elements in the crystal
min_atoms (int) – Minimum number of atoms that needs to be used to construct a crystal
max_atoms (int) – Maximum number of atoms that can be used to construct a crystal
min_atom_i (int) – Minimum number of elements of each kind that needs to be used to construct a crystal
max_atom_i (int) – Maximum number of elements of each kind that can be used to construct a crystal
oxidation_states ((optional) dict) – Mapping from ints (representing elements) to lists of different oxidation states
alphabet ((optional) dict) – Mapping from ints (representing elements) to strings containing human-readable elements’ names
required_elements ((optional) list) – List of elements that must be present in a crystal for it to represent a valid end state
space_group ((optional) int) – International number of a space group to be used for compatibility check, using pyxtal.symmetry.Group.check_compatible().
do_charge_check (bool) – Whether to do neutral charge check and forbid compositions for which neutral charge is not possible.
do_spacegroup_check (bool) – Whether to do a space group compatibility check and forbid compositions with incompatible Wyckoff positions with the given space group.
- set_space_group(space_group)[source]
Sets the space group.
- Parameters:
space_group (int) – Space group number.
- get_action_space()[source]
Constructs list with all possible actions. An action is described by a tuple (element, n), indicating that the count of element will be set to n.
- get_mask_invalid_actions_forward(state=None, done=None)[source]
Returns a vector of length the action space + 1: True if forward action is invalid given the current state, False otherwise.
- states2proxy(states)[source]
Prepares a batch of states in “environment format” for the proxy: The output is a tensor of dtype long with N_ELEMENTS_ORACLE + 1 columns, where the positions of self.elements are filled with the number of atoms of each element in the state.
- Parameters:
states (list or tensor) – A batch of states in environment format, either as a list of states or as a single tensor.
- Returns:
A tensor containing all the states in the batch.
- Return type:
torchtyping.TensorType[batch, state_proxy_dim]
- states2policy(states)[source]
Prepares a batch of states in “environment format” for the policy model: in order to not waste memory and for backward compatibility, the policy state only contains the number of atoms of the allowed elements.
- For example, if self.elements is [1, 2, 3, 4]:
states: [{2: 1, 4: 2}, {1: 3}] states2policy(states): tensor([[0, 1, 0, 2], [3, 0, 0, 0]])
- Parameters:
states (list) – A batch of states in environment format, that is a list of dictionaries.
- Returns:
A tensor containing all the states in the batch.
- Return type:
torchtyping.TensorType[batch, policy_input_dim]
- state2readable(state=None)[source]
Transforms the state, represented as a dictionary of element: n_atoms key-value pairs, into a human-readable version: a non-reduced formula, following the Hill system.
See: https://en.wikipedia.org/wiki/Chemical_formula#Hill_system
Example
- state: {1: 2, 3: 1}
1: atomic number of H 3: atomic number of Li
output: H2Li1
- readable2state(readable)[source]
Converts the readable representation of a state (a chemical formula) into the environment format.
Example
readable: H2Li1 self.alphabet: {1: “H”, 2: “He”, 3: “Li”, 4: “Be”} output: {1: 2, 3: 1}
- get_parents(state=None, done=None, action=None)[source]
Determines all parents and actions that lead to a state.
- Parameters:
state (dict) – Representation of a state as a dictionary of element: n_atoms key-value pairs. Elements whose atomic number is not a key of the dictionary have zero atoms.
done (bool) – Whether the trajectory is done. If None, done is taken from instance.
action (None) – Ignored
- Returns:
parents (list) – List of parents in state format
actions (list) – List of actions that lead to state for each parent in parents
- step(action)[source]
Executes step given an action.
- Parameters:
action (tuple) – Action to be executed. See: get_action_space()
- Returns:
self.state (list) – The state after executing the action
action (tuple) – Action executed
valid (bool) – False, if the action is not allowed for the current state.
- Return type:
Tuple[List[int], Tuple[int, int], bool]
- get_n_atoms_per_element(state=None)[source]
Returns the number of atoms per element.
The result is returned as a list of integers, with no particular order. That is, the output does not allow to identify which element has how many atoms, since the intended use is mainly to check the compatibility with a space group.
- Parameters:
state (list) – A state in environment format. If None, self.state is used.
- Returns:
list – A list of integers containing the number of atoms per element in the state.
- Return type:
List[int]