Lammps "if statement"

From the following code, in the section (Compress by moving wall and check p3 <-222GPa), I want compress the structure by a moving wall from “z_low2” to “zlow3” in 80000 steps. Meanwhile I want to check the value of “p3” after every 100 steps to check if it is less than 222GPa. If it is less than 222GPa I want to break that run and go to the next step.

But, this code is compressing the wall from z_low2 to z_low3 in 100 steps. #help

The code:

dimension 3
boundary p p f
atom_style atomic
units metal
timestep 0.00025 #0.25 fs

#--------------------------------Geometrty Define--------------------------------#

read_data Graphene_5L.lmp

group structure type 1


pair_style lcbop
pair_coeff * * C.lcbop C

#--------------------------------Compute Variable--------------------------------#

variable t equal 300
variable shear_rate equal 0.00573913
variable p equal 0.0

variable zhigh equal 18.60
variable zlow_1 equal 9.40
variable zlow_2 equal 5.40
variable zlow_3 equal 2.60
variable zlow_4 equal 3.9125
variable zlow_5 equal 3.9055
variable Lz0 equal 17.0

compute 2 structure stress/atom NULL
compute 3 structure reduce sum c_2[1] c_2[2] c_2[3] c_2[4] c_2[5] c_2[6]
compute 4 structure pe/atom
compute 5 structure reduce sum c_4
compute mytemp structure temp

#-----------------------------------Setup Walls----------------------------------#

fix w_low all wall/reflect zlo -4.0
fix w_high all wall/reflect zhi ${zhigh}

#----------------------------Velocity And Temperature----------------------------#
#min_style cg
#minimize 1.0e-20 1.0e-20 100000 100000

#write_data mindata.lmp

velocity all create $t 489247

thermo 100
thermo_style custom step temp lx ly lz c_5 c_3[3] press pzz pxz pyz pe etotal

dump 1 all custom 1000 relaxation.dump id type x y z c_4 c_2[3]

fix 1 all npt temp $t t (100.0dt) x $p p (1000.0dt) y $p p (1000.0*dt)
run 25000
unfix 1

fix 2 all nvt temp $t t (100.0*dt)
run 25000
unfix w_high
undump 1
write_data relaxed.lmp

reset_timestep 0

variable p1 equal “-(0.0001pxx)(lz/v_Lz0)”
variable p2 equal “-(0.0001pyy)(lz/v_Lz0)”
variable p3 equal “-(0.0001pzz)(lz/v_Lz0)”
variable p4 equal “-(0.0001pxy)(lz/v_Lz0)”
variable p5 equal “-(0.0001pxz)(lz/v_Lz0)”
variable p6 equal “-(0.0001pyz)(lz/v_Lz0)”

variable en_pe equal pe
variable en_tot equal etotal

change_box all triclinic
#------------------------- Compress By Moving Walls -------------------------#

variable ramp equal ramp({zhigh},{zlow_1})
fix 3 all wall/reflect zhi v_ramp

fix result_stress all print 100 “{p1} {p2} {p3} {p4} {p5} {p6}” file Diamond_Stress.txt screen no
fix result_en all print 100 “{en_pe} {en_tot}” file Diamond_Energy.txt screen no

run 45000
variable ramp delete
#--------------------------------- Compression ----------------------------------#
dump 2 all custom 100 Compress.dump id type x y z c_2[1] c_2[2] c_2[3] c_2[4] c_2[5] c_2[6]
variable ramp equal ramp({zlow_1},{zlow_2})

run 115000

#—Compress By Moving Walls (look for p3 <-222GPa and break the run) ----#

variable ramp delete
variable ramp equal ramp({zlow_2},{zlow_3})
label loop
variable a loop 80000

run 100

if “${p3} < -222.0” then “jump SELF break”
next a
jump SELF loop
label break

#-------------------------Relaxed in Compressed Position-------------------------#
variable ramp delete
variable ramp equal ${zlow_5}

run 100000

#---------------------------Decompress By Moving Walls---------------------------#
variable ramp delete
variable ramp equal ramp({zlow_5},{zlow_2})
run 268000

variable ramp delete
variable ramp equal ramp({zlow_2},{zhigh})
run 110000
unfix 2
unfix w_low
unfix 3

change_box all boundary p p p
fix relax_final all npt temp $t t (100.0dt) x $p p (1000.0dt) y $p p (1000.0dt) z $p p (1000.0dt)
run 25000
unfix relax_final

print “All Done!”

Hi Rashidul,

Welcome to the LAMMPS forums. Please read these guidelines to help you make the most of the forum.

In particular, you should include your script between three backticks,

like this,

to activate the “code mode” that properly handles special characters.

Also, because nanosystems are very small, their local pressure fluctuates significantly over time (search the forum for “pressure fluctuates” or “pressure varies” to see what other people have asked before). It is not feasible for you to just move the walls, take a single measurement, and stop when the measurement happens to meet your present value (due to the fluctuations). Instead, when you would like to simulate a nanosystem confined with a specific pressure, you should do one of two things:

  • run a long equilibration simulation where an average extra force is added to each wall to simulate its imposition of pressure (using pressure = force divided by area), or
  • establish a “bulk region” where the confined nanofluid looks somewhat like the bulk, and determine the wall separation where the average density of the bulk region equals the average density of a truly bulk, fully periodic NPT simulation of the same fluid (again over a long equilibration).
1 Like

In addition to the very valid concerns mentioned by @srtee there is one more issue at play here and this has nothing to do with the “if” command as listed in the subject line.

That is documented behavior and thus the flaw is not in LAMMPS but in your input file.

The ramp function will ramp up from the first to the last step of a run command. It doesn’t know that you want to ramp up slower in multiple stages across multiple run commands. For that you would need to use the “start” and “stop” keywords of the run command. For details, see the LAMMPS manual.

BTW: there is also the fix halt command to stop a simulation run if a condition is reached.

1 Like

Thanks. I learnt about the fix halt command from the forum. It worked.