Constraint relaxation

Hello everyone,
Thank you for sharing valuable information and insights here. I am using LAMMPS (29 Aug 2024), KOKKOS mode with Kokkos version 4.3.1 to iteratively adjust bond lengths between specific pairs of atoms. For each pair of atoms, I want to compute the displacement vector, normalize it, and use it to incrementally move the atoms from each other over a set number of steps.
However, I’m running into an error when I try to reference atoms & coordinates using variables and applying them in the displace_atoms move command. I’ve tried different ways to define these variables, but I’ve continued receiving syntax errors. I would greatly appreciate your guidance on where my coding may be incorrect.

variable atom1_1 equal 190
variable atom1_2 equal 198 
...variable atom_1_30 equal 136
variable atom_2_1 equal 125
...variable atom_2_30 equal 189
# Set up iteration variables for adjustment
variable npairs equal 30
variable steps equal 28
variable dz equal 0.2
 # Iterative bond adjustment
 label step_loop
 variable s loop ${steps}
# Loop over pairs
label pair_loop
variable p loop ${npairs}
 # Define atom indices for the current pair
 variable i equal v_atom1_${p}
 variable j equal v_atom2_${p}
 # Clear and define atom_i and atom_j groups
 group atom_i id ${i}
 group atom_j id ${j}
 # Compute positions for atom_i and atom_j
compute pos_i atom_i property/atom x y z
compute pos_j atom_j property/atom x y z
# Variables to access the x, y, z coordinates for each atom
variable x_i equal c_pos_i[1]
variable y_i equal c_pos_i[2]
variable z_i equal c_pos_i[3]
variable x_j equal c_pos_j[1]
variable y_j equal c_pos_j[2]
variable z_j equal c_pos_j[3]
# Calculate distance squared
variable dist_squared equal ((${x_i}-${x_j})^2+(${y_i}-${y_j})^2+(${z_i}-${z_j})^2)
# Calculate distance
variable dist equal sqrt(dist_squared)
# Calculate displacements
variable dx equal (${x_i}-${x_j})
variable dy equal (${y_i}-${y_j})
variable dz equal (${z_i}-${z_j})
# Normalize the displacement vector components
variable nx equal ${dx}/${dist}
variable ny equal ${dy}/${dist}
variable nz equal ${dz}/${dist}
# Set a fixed displacement distance (dz) 
variable displacement equal 0.2
# Final displacement values along each component
variable disp_x equal ${displacement}*${nx}
variable disp_y equal ${displacement}*${ny}
variable disp_z equal ${displacement}*${nz}
# Displace atoms using calculated values
displace_atoms atom_i move ${disp_x} ${disp_y} ${disp_z} units box
displace_atoms atom_j move -${disp_x} -${disp_y} -${disp_z} units box  
 # Minimization and dump for each step..
  next s
  jump SELF step_loop
  uncompute pos_i
  uncompute pos_j
next p
jump SELF pair_loop
# Output thermodynamic information```

LAMMPS input script language was not meant to be a fully featured scripting language. What you are trying to do is far beyond its scope. Instead you could use the LAMMPS python module.

Using KOKKOS acceleration is making matters far more complex. Also, it is not clear how much acceleration you can get because your scripted operations will be very slow in comparison and could dominate the computational resource requirements. Please consider Amdahl’s law.

@M_a You could write your own custom fix to do this. It isn’t too much of a jump between programming in LAMMPS input script code vs C++. Lot’s of people have done it, and we can try to help.

LAMMPS input script language was not meant to be a fully featured scripting language.

@akohlmey Didn’t Anders Hafreager write a full Lennard Jones simulation in LAMMPS input script, and then complained it was too slow as an April fools joke? So there’s that :joy:

Indeed, fix adapt may serve as a source of inspiration.

But the quoted input script code is rife with problems like nested loops, access to computes without first “consuming” them, adding atoms to groups without first deleting the group where replacing the group is obviously intended.

One has to keep in mind that all input script operations are global and replicated on all processes.
As I said before, the Python module is the simpler approach. You extract all required data, then process it, if desired in parallel. Much cleaner and with a suitable programming language that doesn’t require tricks.

1 Like

I don’t remember. Should be in the archives then.

don’t remember. Should be in the archives then.

Yes here it is: LAMMPS is slower than expected - #7 by Anders_Hafreager1 :joy:

1 Like

Hi,

I appreciate your guidance.
I will try the LAMMPS python module as you mentioned.

Hi,

Thank you for your reply.
I will first try to use the LAMMPS python module and if I run into significant limitations, I’ll consider creating a custom fix in C++ as you suggested. Thank you for the offer to help and will keep that in mind as I move forward.