Hi

I was wondering how can one define a “while loop” in LAMMPS?

I guess it can be a mixture of “variable loop” and “if” commands.

any ideas?

yours

Exactly, within the loop you can have an if statement that jumps to a spot outside the loop. For something like a convergence loop you could do the following until your convergence conditions are met

```
compute pe all pe/atom
compute ke all ke/atom
compute cpe all reduce ave c_pe
compute cke all reduce ave c_ke
label converge_loop
variable converge_step loop 10
# do things
# if per atom energy is less than x, force convergence to continue
variable totalstep equal c_cke+c_cpe
# if first step, previous total energy is sufficiently large to prevent convergence.
if "${converge_step}==1 " then "variable totalprev equal 30000.0"
# percent difference of n and n-1 step
variable diff equal abs(${totalprev}-${totalstep})/${totalprev}*100
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
print "Previous Total Energy : ${totalprev}"
print "Current Total Energy : ${totalstep}"
print "Percent Difference : ${diff}"
variable totalprev equal ${totalstep}
# if less than 1% difference, consider it converged
if "${diff}<1.0" then "jump SELF converge_break"
print "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"
next converge_step
jump SELF converge_loop
label converge_break
```

3 Likes

@tjbarrett Thanks for the tip. I have followed your instruction to create random atoms and delete them if too close (within critical distance). However I receive an error when jumping to first label. Any idea?

```
> label AddingLoopSart
>
> variable AddingLoopCount loop 1000
>
> if "${AddingLoopCount}==1 " then "variable OverlapCheck equal 0"
> create_atoms 1 random 1 123456789 AddRegion
> variable AddedSiaId equal ${BeforeAddCountAll}+1
> group AddedSiaGroup id ${AddedSiaId}
> delete_atoms overlap ${CriticalAtomicDist} AddedSiaGroup BeforeAddGroup
> variable AfterAddCountAll equal count(all)
> variable OverlapCheck equal ${AfterAddCountAll}-${BeforeAddCountAll}
> print 'AdditionIsOverlap!!!'
> if "${OverlapCheck}==1" then &
> "jump SELF AddingLoopBreak" &
> else &
> "next AddingLoopCount" &
> "jump SELF AddingLoopSart" &
>
> label AddingLoopBreak
```

ERROR: Label wasn’t found in input script (src/input.cpp:237)

Last command:

Are you using `lmp -in in.input`

or `lmp < in.input`

?

The latter is not always reliable, especially when running in parallel.

1 Like