How to compute a custom force for each particle MD simulation #1717
-
Hello, I am new to hoomd-blue and I would like to compute forces and energy for each particles that are not in the provided functions. I did the research online but could not find any working answers (maybe I did it wrong). Can anyone help me please ? Here is the code that I can provide and I would like to run a simple MD simulation of a polymer : Codeimport numpy as np import hoomd cpu = hoomd.device.CPU() from scipy.special import lambertw def compute_b_lambertw(l0): Exponential_force_particle_energy functiondef exponential_force_particle_energy(positions, bonds, l0, b, particle_index):
Example usageparticle_index = 0 # Replace with the desired particle index Define your potential parametersl0 = 1.0 Define system parametersdimension = 3 Example bonded pairsbonds = [(i, (i + 1) % num_particles) for i in range(num_particles - 1)] Example initial positionspositions = np.random.rand(num_particles, dimension) def exponential_force_energy_all(positions, bonds, l0, b):
Example usageforces_all_particles, energies_all_particles = exponential_force_energy_all(positions, bonds, l0, b) Print forces and energies for all particlesfor i in range(num_particles): integrator = hoomd.md.Integrator(dt = 0.005, integrate_rotational_dof=False, forces=exponential_force_energy_all(positions,bonds,l0,b)[0], constraints=None, methods=None, rigid=None) returns : ValueError: Validation failed: Error raised in conversion: Value [14.122901 17.188808 16.567728] is not convertable into any of these types (<class 'hoomd.md.force.Force'>,) Thanks for your help, Adrien Berard |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
As the error message states, members of |
Beta Was this translation helpful? Give feedback.
As the error message states, members of
forces
must be subclasses ofhoomd.md.force.Force
and override the appropriate methods. The guide How to apply arbitrary forces in MD in the documentation gives an overview of methods to apply arbitrary forces. Follow the links for details on each method.