Simulator

Simulator. Simulates the dynamics of a model (on the CPU or GPU).

class moldyn.simulation.runner.Simulation(model=None, simulation=None, prefer_gpu=True)[source]

Simulator for a model.

Parameters:
  • model (builder.Model) – Model to simulate. The original model object is copied, and thus preserved, which allows it to serve as a reference.
  • simulation (Simulation) – Simulation to copy. The simulation is copied, and the computing module reinitialized.
  • prefer_gpu (bool) – Specifies if GPU should be used to compute inter-atomic forces. Defaults to True, as it generally results in a significant speed gain.
model

Model that is simulated.

Warning

Works as usual, but changing its properties will not affect the simulation as intended. You should construct another simulation from this model to correctly take in account the changes. This is due to the fact that some values (eg. Lennard-Jones parameters) are treated as constants during shader initialisation, in order to speed up the calculations.

Type:builder.Model
current_iter

Number of iterations already computed, since initialisation.

Type:int
context

ModernGL context used to build and run compute shader.

Type:moderngl.Context
F

Last computed forces applied to atoms. Initialized to zeros.

Warning

Changing the values will affect behavior of the model.

Type:numpy.ndarray
F_f(t)[source]
Parameters:t – Time in seconds.
Returns:External forces applied at time t (2-component vector).
Return type:numpy.ndarray
iter(n=1, callback=None)[source]

Iterates one or more simulation steps.

Basically, follows the Position-Verlet method to update positions and speeds, and computes inter-atomic forces derived from Lennard-Jones potential.

Parameters:
  • n (int) – Number of iterations to perform.
  • callback (callable) – A callback function that must take the Simulation object as first argument. It is called at the end of each iteration.

Note

Setting n is significantly faster than calling iter() several times.

Example

model.iter(5)
set_Fx_ramps(t, Fx)[source]

Creates a function based on ramps and uses it for external forces control along axis x. See set_T_ramps for further details.

Parameters:
  • t (array) – Time.
  • Fx (array) – Associated forces.
set_Fy_ramps(t, Fy)[source]

Creates a function based on ramps and uses it for external forces control along axis y. See set_T_ramps for further details.

Parameters:
  • t (array) – Time.
  • Fy (array) – Associated forces.
set_T_f(f)[source]

Sets function that controls temperature.

Parameters:f (callable) – Must take time (float) as an argument and return temperature (in K, float).
set_T_ramps(t, T)[source]

Creates a function based on ramps and uses it for temperature control. Values of the function are interpolated between points given in t and T. Temperature is supposed constant before the first point and after the last one.

Parameters:
  • t (array) – Time.
  • T (array) – Associated temperatures.

Inter-atomic compute modules

class moldyn.simulation.forces_CPU.ForcesComputeCPU(consts, compute_npart=None, compute_offset=0)[source]

Compute module. Runs on CPU. Uses numba for JIT compilation and multiprocessing for multicore. See ForcesComputeGPU for documentation.

class moldyn.simulation.forces_GPU.ForcesComputeGPU(consts, compute_npart=None)[source]

Compute module. Runs on GPU.

Parameters:consts (dict) – Dictionary containing constants used for calculations.
npart

Number of atoms.

Type:int
consts

Dictionary containing constants used for calculations, and some parameters to run the compute shader.

Type:dict
get_COUNT()[source]
Returns:Average near atoms (one could count this as bonds).
Return type:np.ndarray
get_F()[source]
Returns:Computed inter-atomic forces.
Return type:np.ndarray
get_PE()[source]
Returns:Computed potential energy.
Return type:np.ndarray
set_pos(pos)[source]

Set position array and start computing forces.

Parameters:pos (np.ndarray) – Array of positions.