Nested commands

The way commands described in a LAMMPS input file is somehow different from C language and other programming languages. I was wondering if it’s possible to create nested commands (such as nested if command or nested loops as regularly done in the C language) in an input file. I gave it a try and included the following:

if "$(abs(v_T2-v_T1)) >1" then &
	"if "${grossBrokenBonds}>=1.000000" then &
		"variable       T2 equal ${stemp}" &
		"variable       stemp equal (v_T1+v_T2)/2" &
	elif "${grossBrokenBonds}<1.000000 && ${T1}>${T2}" &
		"variable       T1 equal ${stemp}" &
		"variable       stemp equal ${T1}+400 " &
	elif "${grossBrokenBonds}<1.000000 && ${T1}<${T2} && $i>1" &
		"variable       T1 equal ${stemp}" &
		"variable       stemp equal (v_T1+v_T2)/2"" 
else "quit"

However it didn’t work out and it ended to an error. But, when I rewrote it in its regular way as follows:

if "${grossBrokenBonds}>=1.000000 && $(abs(v_T2-v_T1)) >1" then &
	"variable       T2 equal ${stemp}" &
	"variable       stemp equal (v_T1+v_T2)/2" &
elif "${grossBrokenBonds}<1.000000 && ${T1}>${T2}" &
	"print 'the system is still stable because the broken bonds =${grossBrokenBonds}'" &
	"variable       T1 equal ${stemp}" &
	"variable       stemp equal ${T1}+400 " &
elif "${grossBrokenBonds}<1.000000 && ${T1}<${T2} && $(abs(v_T2-v_T1)) >1 && $i>1" &
	"variable       T1 equal ${stemp}" &
	"variable       stemp equal (v_T1+v_T2)/2" &
elif "$(abs(v_T2-v_T1)) <1"  &
	quit 

it worked with no issues. Is this an indication that nested commands in LAMMPS are not possible? or is their any misstake in my script ?

This is the error message I received:

ERROR: Input line quote not followed by white-space (src/input.cpp:506)
Last command: if "$(abs(v_T2-v_T1)) >1" then    "if "${grossBrokenBonds}>=1.000000" then            "variable       T2 equal ${stemp}"              "variable       stemp equal (v_T1+v_T2)/2"      elif "${grossBrokenBonds}<1.000000 && ${T1}>${T2}"              "variable       T1 equal ${stemp}"          "variable       stemp equal ${T1}+400 "         elif "${grossBrokenBonds}<1.000000 && ${T1}<${T2} && $i>1"          "variable       T1 equal ${stemp}"              "variable       stemp equal (v_T1+v_T2)/2"" else "quit"

The error message already tells you what is wrong.

Please also note the following:

  • LAMMPS scripting is not meant to replace a proper scripting language
  • scripting commands are executed immediately, so you cannot define an if statement and expect that to be executed repeatedly during a run
  • you have to watch very carefully to follow the formatting and parsing rules with complex input. it is strongly advised to build such constructs in steps rather than the whole thing and then debug it
  • if you need more flexible scripting don’t use LAMMPS script but the python module and start LAMMPS calculations from python

Do you mind if you know what I made wrong to tell me what it is? I’ve been trying everything to fix it and I failed.

Count the quotation marks and look at the actually processed command line (as echoed by LAMMPS) without continuation characters.

You can also tell that things are not quite right from the syntax highlighting here in the quoted code block.

1 Like