Re: Is there a way to stop a MD run before the END_OF_RUN timestep?

From: Giacomo Fiorin (
Date: Tue May 26 2020 - 15:14:19 CDT

Hi, I'm not sure that calling "exit" from calc_colvar_forces is safe. This
function is, like tclForces' calcforces, a callback: it is called by the
C++ code through the Tcl interpreter, but the Tcl interpreter is also still
waiting on the outside for the completion of the "run" command. When you
call "exit", you are effectively calling it from inside another Tcl
command, i.e. "run".

You can query the internal state of the Colvars module at any time, for as
long as you don't need to update the variables. So try placing the test in
the main NAMD script:

while true {
   run 1000
   if {$minC != $anchor1} {
     print $ts $minC $minV
     print "hit new colvar"
     cv printframe
     cv save stop
     output stop

Based on the scripting doc:
"exit" will save the NAMD output files, but you can always use "output"
directly as well.

Note the finite number of steps (1000 for example). As Josh suggested, you
lose performance majorly if you don't run a bunch of steps in sequence.

Also, look up the implementations of AMS (adaptive multilevel splitting),
SEEKR and SCmile, each of which is a Tcl set of script that uses at some
point geometric estimators provided by Colvars to decide when to move or
stop. There are most likely approaches to take inspiration from.


On Tue, May 26, 2020 at 3:46 PM Cardenas, Alfredo E <>

> Hi Josh,
> Thanks for the link to the scripting guide. I have tried some variants of
> what you suggested but I can't make it work. Here is part of the conf file
> that contain the evaluation of the colvars. Actually the script needs to
> evaluate ten's to hundred's of different colvars (distances) and it stops
> when a given colvar (that initially has the lowest value of the list of
> colvars, associated with anchor 1 in the example below) is not the lowest
> anymore. At that moment, the run stop with an exit. To capture coor and vel
> for the exit event, restartfreq would have to be small, that makes the run
> very slow.
> colvars on
> colvarsConfig colvar_free.conf
> set anchor1 1
> proc calc_colvar_forces { ts } {
> global anchor1 anchor2
> set list [cv list]
> set i 1
> set minV [lindex $list 0]
> set minC $i
> foreach colv $list {
> set col [ expr abs([cv colvar $colv value])]
> if {$col < $minV} {
> set minV $col
> set minC $i
> }
> incr i
> }
> if {$minC != $anchor1} {
> print $ts $minC $minV
> cv printframe
> cv save stop
> print "hit new colvar"
> #run 1
> #print " after run 1"
> #### pick exit command depands on the cluster ###
> exit
> }
> }
> run 50000
> It would be fine with me if the coor and vel are written one step after
> the "hit new colvar" so that is why I tried to place run 1 before the exit
> command. If I uncomment the "run 1" line I get an error:
> TCL: 202 3 1.11704898925451
> colvars: The restart output state file will be
> "pet_dopc.restart.colvars.state".
> colvars: The final output state file will be "stop.colvars.state".
> colvars: Saving collective variables state to "stop.colvars.state".
> TCL: hit new colvar
> colvars: Error while executing calc_colvar_forces:
> colvars: number of steps must be a multiple of stepsPerCycle
> FATAL ERROR: Error in the collective variables module (see above for
> details): No such file or directory
> [Partition 0][Node 0] End of program
> The stepsPerCycle=20 in my simulation, and according to the script guide I
> don't think I can change that value with scripting. Actually I also tried
> "run 20". In that case the run seems stalled without exiting
> TCL: hit new colvar
> TCL: Original numsteps 50000 will be ignored.
> TCL: Running for 20 steps
> Maybe calc_colvar_forces procedure is incompatible with setting different
> run values? But also run 1 seems to have additional problems because I
> don't know how to change stepsperCycle or as you mentioned pairlistfreq.
> Using small values for those parameters from the beginning would not be
> good. By the way, I am using NAMD 2.14b1
> Best,
> Alfredo
> ------------------------------
> *From:* Josh Vermaas <>
> *Sent:* Monday, May 25, 2020 5:35 PM
> *To:* NAMD list <>; Cardenas, Alfredo E <
> *Subject:* Re: namd-l: Is there a way to stop a MD run before the
> END_OF_RUN timestep?
> Hi Alfredo,
> Based on my reading of the scripting guide (
> you could do something like this, having an outer loop that runs at a less
> frequent restart rate until you get close. Then you'd use the "output"
> feature to force the writing of the .coor, .vel, and .xsc files:
> set isfar 1
> while { $isfar } {
> run 1000
> #Insert some code to check the cv value and whatever threshold you are
> interested in.
> #I assume you have this already, and that you are waiting for the value to
> be greater than the threshold.
> if { $cvvalueyoucomputed > $somethreshold } {
> set isfar 0
> }
> while { $isfar == 0 } {
> #Now that you are close, run 1 step at a time, and force outputs if needed.
> run 1
> #Insert some code to check the cv value and whatever threshold you are
> interested in.
> if { $cvvalueyoucomputed <= $somethreshold } {
> #This lets you go back to efficient running if the simulation slides back.
> set isfar 1
> }
> if { $cvvalueyoucomputed > $targetthreshold } {
> #If you reach your target, write a set of output files immediately, and
> kill the simulation.
> output targetreached
> exit
> }
> }
> }
> The trick is going to be that as soon as you make it into the inner loop,
> your performance is going to be garbage, and the other things that are
> required so that you can run 1 step at a time (I think your pairlistfreq
> needs to be 1) also make your performance not great. When I wanted to kill
> simulations after they had made sufficient progress, I'd just only check
> every 1000 steps or so, since the amount of extra compute time that I saved
> having pairlistfreq be something reasonable more than made up for
> overrunning my target.
> -Josh
> On Sun, May 24, 2020 at 11:37 PM Cardenas, Alfredo E <
>> wrote:
> Hi,
> I use certain TCL script in a NAMD conf file to evaluate certain colvars
> during the run. When certain values of the colvars are reached the
> simulation is kill. We need to save the coor, vel and xsc files at the
> timestep when the colvar condition is satisfied. To do that we use a very
> small restartfreq value and that evidently slows the simulation. I wonder
> if there is a way to trick NAMD, to change on the flight the END_OF_RUN or
> FILE_OUTPUT values so it could write those files when the colvars
> conditions are satisfied without the need to write restart files too often.
> If not, any other suggestion to do this will be appreciated. For example,
> there are ways to print out complete coor, vel and xsc in TCL scripts?
> Thanks,
> Alfredo

Giacomo Fiorin
Associate Professor of Research, Temple University, Philadelphia, PA
Research collaborator, National Institutes of Health, Bethesda, MD

This archive was generated by hypermail 2.1.6 : Fri Dec 31 2021 - 23:17:09 CST