Defining regions in a loop

Dear Lammps users,

Apologies in advance if this is a trivial doubt. I am simulating a phase separating lennard jones system in a slab geometry, where one axis is larger than the other two. I want to define 10 regions parallel to the longer axis in the box and compute the pressure in those regions.

The following lammps script work but is very tedious-

region          reg1 block 0 32 0 32 0 16 units box
region          reg2 block 0 32 0 32 16 32 units box
region          reg3 block 0 32 0 32 32 48 units box
region          reg4 block 0 32 0 32 48 64 units box

group           region1 region reg1
group           region2 region reg2
group           region3 region reg3
group           region4 region reg4

compute         temp1 region1 temp
compute         temp2 region2 temp
compute         temp3 region3 temp
compute         temp4 region4 temp

compute         press1 region1 stress/atom temp1
compute         press2 region2 stress/atom temp2
compute         press3 region3 stress/atom temp3
compute         press4 region4 stress/atom temp4

compute         pressure1 region1 reduce sum c_press1[1] c_press1[2] c_press1[3]
compute         pressure2 region2 reduce sum c_press2[1] c_press2[2] c_press2[3]
compute         pressure3 region3 reduce sum c_press3[1] c_press3[2] c_press3[3]
compute         pressure4 region4 reduce sum c_press4[1] c_press4[2] c_press4[3]

fix             file1 region1 ave/time 100 1 100 c_temp1 c_pressure1[1] c_pressure1[2] c_pressure1[3] file temp_pressure1.txt
fix             file2 region2 ave/time 100 1 100 c_temp2 c_pressure2[1] c_pressure2[2] c_pressure2[3] file temp_pressure2.txt
fix             file3 region3 ave/time 100 1 100 c_temp3 c_pressure3[1] c_pressure3[2] c_pressure3[3] file temp_pressure3.txt
fix             file4 region4 ave/time 100 1 100 c_temp4 c_pressure4[1] c_pressure4[2] c_pressure4[3] file temp_pressure4.txt

This works for 4 regions but I want to implement it for 10 regions. When i try to do the same thing using loop variable, i am not able to define the IDs for different command inside the loop. Is there a way to implement this with loops? I tried the following but it wasn’t working.

variable        length equal 64/10
label           loop
variable        i loop 10

variable        zzlo equal v_length*(i-1)
variable        zzhi equal v_length*i
region          region${i} block 0 32 0 32 ${zzlo} ${zzhi} units box

compute         temp${i} region${i} temp
compute         press${i} region${i} stress/atom
compute         pressure${i} region${i} reduce sum c_press${i}[1] c_press${i}[2] c_press${i}[3]

fix             file${i} region${i} ave/time 100 1 100 c_temp${i} c_pressure${i} file temp_pressure${i}.txt

next            i
jump            SELF loop

I get the following error-
ERROR: Variable zzlo: Invalid thermo keyword ‘i’ in variable formula (src/variable.cpp:2053)
Last command: region region$i block 0 32 0 32 {zzlo} {zzhi} units box

Thanks in advance

Please look carefully at this error message. The error is not with the region command but with the definition of the variable it references.

Both of these are incorrect. A variable is immediately expanded with ${name} and referenced in an expression with v_name. Your commands do neither and hence the error.

Besides, the whole process could be much simplified by using compute chunk/atom to set up a 1-d binning and compute reduce/chunk to get the stress per bin. No loop required.

Thank you so much. I got this to work using the following commands

compute spatialchunk all chunk/atom bin/1d z lower 6.4 units box
compute tempchunk all temp/chunk spatialchunk temp
compute stress all stress/atom NULL
compute stressChunk all reduce/chunk spatialchunk sum c_stress[1] c_stress[2] c_stress[3]

One issue that I am still facing with this approach is that I can’t use the temperature of each chunk in the calculation of local pressure, which I could do earlier. Is there a way to implement that?


Try fix ave/chunk, which has both temp options and options to average / sum per-atom quantities.