2.8. Reference of the Python package¶
The functions and classes of the package act, see Using ACT from Python, with the documentation in the code. The classes of ACT are there when ACT was built with -DACT_PYTHON=ON, as in the conda package alexandria-python.
2.8.1. Functions¶
- act.build_arguments(command: CommandInfo, options: Mapping[str, Any], workdir: PathLike, base: PathLike | None = None) Tuple[List[str], Dict[str, List[Path]]]¶
Turn keyword arguments into the command line of a command.
The rules are: - A value None means that the option is not given. - A boolean option takes True or False and becomes -name or -noname. - Numbers, enums and strings are checked against the type of the option. - An input file must exist. Relative paths are relative to
baseand are made absolute, since the program runs inworkdir.- An output file can be a path, or True for the default name inworkdir,or False to leave an optional file out. - An option that takes several values takes a list.- Parameters:
command – The command.
options – The options by their name on the command line or their name as a Python keyword argument.
workdir – The directory the program will run in.
base – The directory that relative paths are relative to, the current directory if None.
- Returns:
The arguments after the name of the command, and for each output file option the files the program is expected to write. That includes the files with default names that the program always writes.
- Raises:
ActOptionError – for an unknown option or a value it cannot take.
ActNotFoundError – for an input file that does not exist.
- act.classes_available() bool¶
Whether this package has the classes of ACT, such as act.ForceField, which use the code of ACT directly instead of running the alexandria program.
- Returns:
True if ACT was built with them.
- act.data_directory() str¶
The directory of the installation in which the classes of ACT look for the data files of ACT, such as the properties of the elements. The directory in the environment variable ACTDATA, if set, comes first.
- Returns:
The directory, empty if this installation has none or was built without the classes.
- act.program_info(refresh: bool = False) ProgramInfo¶
The commands of alexandria and their options.
The program is asked for the description the first time; after that the answer is kept for as long as Python runs and the program file stays the same. A program that is replaced by a newer one at the same path is asked again, such that a process that runs for a long time, like the server of a user interface, does not go on with the options of the program it started with.
- Parameters:
refresh – Ask the program again in any case.
- Returns:
The description.
- Raises:
ActNotFoundError – if the program cannot be found.
ActError – if the program does not give a description: it is too old for this interface, or it fails, and then the message has the reason.
- act.read_molprop(file: str | os.PathLike) act.MolProps¶
Read the compounds of a molprop file with their reference data.
Raises ActFileError if the file cannot be read.
- act.run(command: str, *, workdir: PathLike | None = None, processes: int = 1, timeout: float | None = None, check: bool = True, env: Mapping[str, str] | None = None, on_progress: Callable[[Any], None] | None = None, stop: Any = None, **options: Any) Result¶
Run a command of alexandria and return what it produced.
For example
import act res = act.run("minimize", ff="ACS-pg1.xml", f="acetone.sdf") print(res.data["rmsd"]["value"], res.file("c"))
The functions in act.commands do the same with the name of the command as the name of the function: act.commands.minimize(ff=…, f=…).
If the command can write its results in JSON format (option -json), it is asked to, unless json=False is given.
- Parameters:
command – The name of the command, such as minimize.
workdir – The directory to run in, which is made if it does not exist and is kept. Without it the command runs in a temporary directory that is removed with the result.
processes – The number of processes; more than one starts the program with the MPI launcher.
timeout – Seconds after which the program is stopped, None for act.config.timeout.
check – Raise ActError if the program fails. With False the result is returned and its returncode tells what happened.
env – Environment variables to set for the program, on top of those of Python.
on_progress – A function that is called while the program runs, for commands with the option -progress, such as simulate. It gets one argument, the object that the program wrote: for simulate a dict with the step, the time, the temperature and the energies. An exception in the function stops the program.
stop – An object with a method is_set(), such as a threading.Event, with which another thread can stop the program: when is_set() returns True the program is stopped and ActCancelledError is raised.
options – The options of the command, see build_arguments() for the rules and act.program_info() for the options of each command.
- Returns:
The result.
- Raises:
ActOptionError – for an unknown command or option, or a wrong value.
ActNotFoundError – if the program or an input file does not exist.
ActTimeoutError – if the program ran too long.
ActCancelledError – if the program was stopped through
stop.ActError – if the program failed and check is True.
- act.commands¶
Create a module object.
The name must be a string; the optional doc argument can have any type.
- act.config¶
Settings of the Python interface: where the program is and how it is started on several processes.
There is one object of this class, act.config. The settings can also be given with environment variables: ACT_ALEXANDRIA for the program and ACT_MPIEXEC for the launcher of MPI programs.
2.8.2. Classes¶
- class act.CommandInfo¶
One command of alexandria, such as minimize, with its options.
- find_option(name: str) OptionInfo¶
Look up an option by its name on the command line or by its name as a Python keyword argument.
- Parameters:
name – The name, without dash.
- Returns:
The option.
- Raises:
ActOptionError – if the command has no such option. The message lists options with similar names.
- static from_json(name: str, data: Dict[str, Any]) CommandInfo¶
Make a command from its description.
- Parameters:
name – The name of the command.
data – The description of the command.
- Returns:
The command.
- has_option(name: str) bool¶
Whether the command has an option.
- Parameters:
name – The name on the command line, without dash.
- Returns:
True if it has.
- class act.ForceField¶
A force field: its particle types and the parameters of its interactions.
Read one from a file with ForceField.read(file).
- property filename¶
The file the force field was read from.
- property interactions¶
The names of the interactions that the force field has parameters for.
- parameter(interaction: str, identifier: str, name: str) act.Parameter¶
One parameter: of an interaction, for the types of the identifier as in the force field file (such as c3 or c3_b~h_b), by its name (such as sigma).
Raises ActInputError if there is no such parameter.
- parameters(interaction: str) list[act.Parameter]¶
The parameters of an interaction, such as VANDERWAALS, for all its entries.
An empty list if the force field does not have the interaction. Raises ActInputError if ACT has no interaction of that name.
- particle_type(name: str) act.ParticleType¶
A particle type with its kind, its options and its parameters.
Raises ActInputError if the force field has no particle type of that name.
- property particle_types¶
The names of the particle types: atoms, shells and virtual sites.
- property polarizable¶
Whether the force field has particles that are polarizable.
- static read(file: str | os.PathLike) act.ForceField¶
Read a force field from an XML file.
A name without a directory is also looked for in the library of ACT. Raises ActFileError if the file cannot be read, and ActInputError if what is in it is not a consistent force field.
- set_parameter(interaction: str, identifier: str, name: str, *, value: float | None = None, minimum: float | None = None, maximum: float | None = None, mutability: str | None = None) None¶
Change the value, the bounds or the mutability (Bounded or Fixed) of a parameter.
What is not given stays as it is. Raises ActInputError, and changes nothing, if the value would be outside the bounds, the minimum above the maximum, or a parameter negative that cannot be.
- set_particle_parameter(particle: str, name: str, *, value: float | None = None, minimum: float | None = None, maximum: float | None = None, mutability: str | None = None) None¶
Change a parameter of a particle type, such as its charge, see set_parameter.
- validate() list¶
The problems with the parameters, as edit_ff -validate lists them: an empty list if there are none, otherwise for each problem its kind, where it is and what is wrong.
- write(file: str | os.PathLike, compress: bool = False) None¶
Write the force field to an XML file, compressed if asked.
Raises ActFileError if the file cannot be written.
- class act.Minimization¶
The result of Molecule.minimize.
- property converged¶
Whether the forces are within the tolerance.
- property coordinates¶
The position of each particle in nm in the structure that was found.
- property energy_after¶
The energy by term in kJ/mol of the structure that was found.
- property energy_before¶
The energy by term in kJ/mol of the structure it started from.
- property interaction_energy¶
For a molecule of two compounds the interaction energy by term in kJ/mol of the structure that was found, as alexandria minimize gives it, with the correction of the induction energy as a term of its own (Molecule. interaction_energy adds it to the induction energy); None otherwise.
- property rmsd¶
The root mean square distance in nm between the atoms before and after, with the structures on top of each other.
- property status¶
OK, or why the minimizer stopped, such as Too many steps. The structure is then the one with the lowest energy that was found.
- class act.MolProps¶
The compounds of a molprop file with their reference data.
Read them with act.read_molprop(file). A compound is found by its place or by its name: molprops[0] or molprops[“water”].
- __getitem__(compound: object) object¶
A compound, by its place or its name: its name, formula, charge, the compounds it consists of (fragments), and the number of calculations and of values of each property.
- __len__() int¶
The number of compounds.
- calculations(compound: object) list¶
The calculations or experiments of a compound, by its place or its name. For each: reference, conformation, method, basisset, program, jobtype, datafile, the atoms, their coordinates in nm, and the properties, each with observable, type, value, error, unit, temperature and, for a vector or tensor, vector.
- exclude(compounds: Sequence[object]) act.MolProps¶
A new set without the given compounds, by place or name. Raises KeyError or IndexError for one that is not there.
- property filename¶
The file the compounds were read from.
- merge(other: act.MolProps) act.MolProps¶
A new set with the compounds of both, sorted by name, and the calculations of a compound that both have merged into one, as alexandria edit_mp merges molprop files.
- property names¶
The names of the compounds, in the order of the file.
- select(compounds: Sequence[object]) act.MolProps¶
A new set with the given compounds, by place or name, in the order given. Raises KeyError or IndexError for one that is not there.
- property summary¶
The numbers of compounds, monomers, dimers and calculations, and the number of values of each property, as alexandria edit_mp reports them.
- validate() object¶
The check of alexandria edit_mp -validate: every calculation of a compound has the same atoms in the same order, and the compounds of a dimer are in the same order everywhere. Returns ok, the number of problems, the names of the compounds that have them (invalid) and the messages.
- write(file: str | os.PathLike, compress: bool = False) None¶
Write the compounds to a molprop file, compressed if asked.
Raises ActFileError if the file cannot be written.
- class act.Molecule¶
A molecule for a force field: its particles with their charges, its bonds, its structure and its energy.
Make one with Molecule.from_smiles(smiles, forcefield) or Molecule.from_file(file, forcefield).
- property atoms¶
The particles: atoms, shells and virtual sites. For each its name, element, type in the force field, kind (particletype), mass and charge, in the order of the coordinates.
- property bonds¶
The bonds: for each the two particles, counted from zero, and the bond order.
- property charge¶
The total charge in e.
- property charges¶
The charge of each particle in e, a numpy array.
- property coordinates¶
The position of each particle in nm, a numpy array of shape (particles, 3). It is a copy: changing it does not change the molecule.
- energy() dict¶
The energy in kJ/mol for the structure of the molecule: a dict with the terms of the force field and their sum, EPOT.
- property forcefield¶
The force field the molecule was made for.
- forces() numpy.typing.NDArray[numpy.float64]¶
The force on each particle in kJ/mol nm, a numpy array of shape (particles, 3).
- property formula¶
The chemical formula.
- static from_file(file: str | os.PathLike, forcefield: object) act.Molecule¶
Read a molecule from a structure file, such as an sdf or pdb file, and make the topology for the force field and the charges.
Raises ActFileError if the file is not there, and ActInputError if no molecule can be made of it with this force field.
- static from_smiles(smiles: str, forcefield: object) act.Molecule¶
Make a molecule from a SMILES string: a structure, the topology for the force field and the charges, as the programs of ACT make them.
Raises ActInputError if the string is no molecule or the force field has no types for its atoms.
- generate_charges(algorithm: str | None = None) None¶
Make the charges again, for the structure the molecule has now.
algorithm is EEM or SQE for the charge model of the force field, or None for the charges of the particle types as they are; as when the molecule was made if not given. The charges of ACT depend on the structure, so after a minimization they are other ones than before.
- interaction_energy() dict¶
The interaction energy in kJ/mol between the two compounds of a dimer, for the structure it has: the energy of the dimer minus those of the compounds, with the shells relaxed in each, by term, as alexandria simulate -sp reports it. Raises ActInputError for a molecule that is not made of two compounds.
- property mass¶
The mass in g/mol.
- minimize(*, algorithm: str | None = None, max_iter: int | None = None, tolerance: float | None = None, freeze: Sequence[int] = [], update: bool = True) act.Minimization¶
Minimize the energy with respect to the positions of the atoms, as alexandria minimize does, and give the molecule the structure that is found.
algorithm is LBFGS, Steep or Newton (-minalg), max_iter the largest number of iterations (-maxiter, 0 until convergence) and tolerance the mean square force at which to stop (-toler); what is not given is as in the program, which checks the settings in the same way. The particles in freeze, numbered as in atoms, stay where they are. With update=False the molecule keeps its structure. Returns a Minimization with whether it converged, the energies before and after, for a dimer the interaction energy, the RMSD and the structure. A minimization that does not converge is told there, not raised.
- property name¶
The name of the molecule, empty if it has none.
- normal_modes(*, minimize: bool = True, algorithm: str | None = None, max_iter: int | None = None, tolerance: float | None = None) act.NormalModes¶
The vibrations of the molecule, as alexandria nma computes them: frequencies, infrared intensities, the motion of the particles and the thermochemistry.
The energy is minimized first, with algorithm, max_iter and tolerance as in minimize and the settings of alexandria nma, and the molecule keeps that structure; give minimize=False for a molecule that is at its minimum already. Raises ActError if the minimization does not converge. Returns a NormalModes.
- set_charges(charges: Sequence[float]) None¶
Give each particle a charge in e, in the order of atoms.
- simulate(*, steps: int, timestep: float = 0.0002, temperature: float = 0.0, seed: int = 0, energy_interval: int = 1, frame_interval: int = 1, on_energies: object = None) act.Trajectory¶
Run a molecular dynamics simulation, as alexandria simulate does, from the structure of the molecule, which stays as it is.
steps is the number of steps (-nsteps); with 0 there is no dynamics, and the energies of the structure as it is are those of step 0, as simulate -sp gives them of timestep ps (-deltat). With a temperature in K (-temp) the atoms get velocities for it, drawn with the seed (-seed, 0 for another one each time); without, they start at rest. The energies are kept every energy_interval steps and the structure every frame_interval steps, 0 for never. on_energies is a function that gets a dict with step, time, temperature and energies each time the energies are due; if it returns False the simulation ends there. Returns a Trajectory. Ctrl-C ends the simulation at the next energies.
- write_openmm(file: str | os.PathLike, *, structure: str | os.PathLike | None = None, drude_mass: float = 0.1, min_training: int = 1, number_types: bool = True, shells: bool = False, residue_names: str = '') None¶
Write the force field file with which OpenMM simulates this molecule, as alexandria gentop -openmm does.
structure is a pdb file to write as well, with the structure the molecule has now and the names of the force field file, with the shells if shells is True. drude_mass is the mass in g/mol of a shell, which is taken from its atom (-mDrude). A parameter is written if it was trained on min_training data points at least (-ntrain). With number_types each atom gets a type of its own (-numberAtypes). residue_names names the residues of the compounds, as -resnames does, for instance “methanol=MOH”; the others get a name from their IUPAC name. The settings are checked as gentop checks them.
- class act.NormalModes¶
The result of Molecule.normal_modes.
- property frequencies¶
The frequencies of the vibrations in cm^-1, from low to high, a numpy array.
- property intensities¶
The infrared intensity of each vibration, in intensity_unit, a numpy array.
- property intensity_unit¶
The unit of the intensities.
- property minimization¶
The Minimization that was done first, None if there was none.
- property modes¶
For each vibration its frequency, intensity, reduced_mass in g/mol and displacements: how each particle moves, a numpy array of x, y and z for each, normalized as quantum chemistry programs print them.
- spectrum(linewidth: float = 24.0) tuple¶
The infrared spectrum: a line of the given width in cm^-1 at each frequency.
Returns the frequencies, whole numbers of cm^-1, and the intensities, which add up to one, as alexandria nma -ir writes them, both as numpy arrays.
- property thermochemistry¶
The zero point energy, entropy, heat capacity and so on, each with value and unit, as alexandria nma reports them.
- class act.OptionInfo¶
One option of a command.
- static from_json(data: Dict[str, Any]) OptionInfo¶
Make an option from its description.
- Parameters:
data – One element of the array “options” of a command.
- Returns:
The option.
- Raises:
ActError – if the description is not understood.
- property is_file¶
Whether the option is a file name.
- Returns:
True for options of type file.
- property python_name¶
The name of the option as a Python keyword argument.
- Returns:
The name, see python_name().
- class act.Parameter¶
A parameter of a force field, as it was when it was asked for.
Change it with ForceField.set_parameter or set_particle_parameter.
- property identifier¶
The types it is for, as in the force field file, or the particle type.
- property interaction¶
The interaction it belongs to, empty for a parameter of a particle type.
- property maximum¶
The largest value that training gives it.
- property minimum¶
The smallest value that training gives it.
- property mutability¶
Whether training changes it: Bounded, Fixed, ACM or Dependent.
- property name¶
The name of the parameter, such as sigma.
- property nonnegative¶
Whether it may not be negative.
- property ntrain¶
The number of data points it was trained on.
- property uncertainty¶
The uncertainty of the value.
- property unit¶
The unit.
- property value¶
The value, in the unit.
- class act.ParticleType¶
A particle type of a force field, as it was when it was asked for.
- property description¶
What the force field says about it.
- property kind¶
Atom, Shell or VSite.
- property name¶
The name.
- property options¶
The options: the element, and the types it has for each interaction.
- property parameters¶
Its own parameters, such as the charge and the mass.
- class act.ProgramInfo¶
All the commands of the alexandria program.
- find_command(name: str) CommandInfo¶
Look up a command.
- Parameters:
name – The name of the command, such as minimize.
- Returns:
The command.
- Raises:
ActOptionError – if there is no such command.
- static from_file(path: os.PathLike[str] | str) ProgramInfo¶
Read the description from a file written by `alexandria help -export json`.
- Parameters:
path – The file.
- Returns:
The description.
- Raises:
ActError – if the file is not valid JSON or not understood.
- static from_json(data: Dict[str, Any]) ProgramInfo¶
Make the description of the program from the JSON document.
- Parameters:
data – The parsed document.
- Returns:
The description.
- Raises:
ActError – if the document has another layout than this module understands.
- class act.Result¶
What a command of alexandria produced.
If the command ran in a temporary directory, that directory is removed when the result is no longer used, or when close() is called. Copy the files that should be kept, or give a directory with the argument workdir of the command.
- close() None¶
Remove the temporary directory of the run, if it has one.
- property data¶
The results without the outer key.
- Returns:
The JSON file of a command has one key, the name of the command, with everything below it. This is the content below that key, or None if there is no JSON file.
- file(option: str) Path | None¶
The output file of an option.
- Parameters:
option – The name of the option, such as c for the structure.
- Returns:
The first file of the option, None if it was not written.
- property json¶
The results the program wrote in JSON format.
- Returns:
The parsed file of the option -json, None if the command has no such option or the file was not written.
- Raises:
ActError – if the file is not valid JSON.
- property log¶
The log file of the program.
- Returns:
The text of the file of option -g, empty if there is none.
- property ok¶
Whether the program finished without error.
- Returns:
True if the exit status is zero.
- class act.Trajectory¶
The result of Molecule.simulate.
- property energies¶
The energies in kJ/mol at these times: for each term of the force field, for the potential (EPOT), kinetic (EKIN) and total energy (ETOT) a numpy array.
- property energy_steps¶
The steps of which there are energies, a numpy array.
- property energy_times¶
The times in ps of the energies, a numpy array.
- property frame_steps¶
The steps of which there is a structure, a numpy array.
- property frames¶
The structures, a numpy array of shape (structures, particles, 3), in nm.
- property steps¶
The number of steps that were asked for.
- property temperature¶
The temperature in K at these times, a numpy array.
- property times¶
The times in ps of the structures, a numpy array.
- property timestep¶
The time step in ps.
2.8.3. Errors¶
- exception act.ActCancelledError¶
The program was stopped because the caller asked for it.
Derived from ActError.
- exception act.ActError¶
Base class of all errors of the Python interface to ACT.
When a program of ACT fails, the exception carries what is known about the run, such that a caller can show it or store it.
Derived from Exception.
- exception act.ActFileError¶
A file that the classes of ACT cannot read or write: it is not there, it is not what it should be, or the place cannot be written.
Derived from ActError, OSError.
- exception act.ActInputError¶
Input that the classes of ACT do not accept, such as a force field with a value that ACT does not know.
Derived from ActError, ValueError.
- exception act.ActInternalError¶
Something that should not happen inside ACT, which is a bug to report. The process is still alive.
Derived from ActError, RuntimeError.
- exception act.ActNotFoundError¶
The alexandria program, or a file it needs, cannot be found.
Derived from ActError, FileNotFoundError.
- exception act.ActOptionError¶
An option that the program does not have, or a value that the option does not accept. Raised before a program is started.
Derived from ActError, ValueError.
- exception act.ActTimeoutError¶
The program ran longer than the time it was given and was stopped.
Derived from ActError, TimeoutError.