Implementing a Harmonic Length Restraint for Linear Molecules

Some general comments:

What you subsequently describe is not a “harmonic force to restrain the total length of the molecule to a certain length”. That harmonic potential is very simply expressed as

U(\mathbf{r}_1, \mathbf{r}_n) = \frac{1}{2} k (|\mathbf{r}_1- \mathbf{r}_n| - r_0)^2

and is easily inserted into your simulation by adding a harmonic bond between the first and last beads of each molecule with the desired harmonic strength. Note how, since the length of the molecule does not (directly) depend on the locations of intermediate beads, a harmonic restraint on the molecule length can’t impose a direct force on those beads either for Hamiltonian consistency.

For more help, you should give the description of the forces in mathematical notation. When you say “positive contribution” and “negative contribution”, for example, that’s mathematically ambiguous (to me at least).

To “spread” the molecular length to an entire molecule, you can try something like this:

# group "ends" contains only terminal particles of molecules

compute mchunk all chunk/atom molecule
compute r2mols ends gyration/chunk mchunk
compute r2mol_peratom all chunk/spread/atom mchunk c_r2mols[4]

# r2mol_peratom now contains (half) the molecular
# end-to-end distance squared as a peratom value
# (possibly additionally scaled depending on particle masses)

Read the documentation carefully for compute gyration/chunk to see why this works (or should work – test before using!).

See this post: Propel polymer bead along bond vector - #4 by srtee for more very hacky advanced ways of using chunks.

This phrase is historically correlated with significant headaches, associated with a PI without sufficient knowledge of what’s “easy” and “hard” in MD, and thus suggesting things without a clear budget for either the programming cost or the physical-significance payout.

Your best bet is to find a local mentor or collaborator to keep you on the right path. A straightforward way to identify such a person is to find the administrator of the cluster you’re intending to run these simulations on, and ask them who to talk to.