[lammps-users] Way to count specific atomic type in a specific region

Hi!

I’m trying to find a way to count the number of atoms of type n and m in a box which has different regions with different compositions of n/m per region. I can count the number of atoms in each region easily, as well as all types easily using the group command in conjunction with count(group), but I haven’t found a way to do something of the sort in a combined fashion.

To clarify, I’d like to be able to do something similar to :
group 2_in_region_1 region region1 type 2
print(There are $(count(2_in_region_1)) atoms of type 2 in region 1)

I tried browsing through the forum and documentation but to no avail, any pointers would be helpful!

Thanks a lot for your time!

Please look at the documentation for the variable command again and more carefully.
Under “region commands” there is a “count(group, region)” that seems to be doing exactly what you are asking for.

Thank you for your answer, I’ve been trying to use it unsuccessfully and can’t seem to understand why.

When using the function I repeatedly receive the following error :

ERROR: Region ID [given ID] in variable formula does not exist (src/variable.cpp:4050)

However I know the given region ID exists, as it was previously used to create atoms, even using the name all (as is mentioned in the variable command documentation) returns the same error.

Here’s how I’m calling it for example :
group gAl type 1
print “Al in region : $(count(gAl, all))”

If this can help I’m using the 29 oct 2020 release.

Thanks again for your help!
Antoine.

you have defined a group gAl but not a region. where is the definition of the “all” region?

Thanks for your answer, sorry I should have been clearer in my example

Here’s a longer example that returns the same error :

region whole block 0 10 0 10 0 20 units lattice
create_box 3 whole

region box1 block 0 10 0 10 0 12 units lattice
create_atoms 1 region box1

set region box1 type/fraction 3 $(1-v_fAls) 1734535

group gAl type 1

print “Al in region : $(count(gAl, box1))”

If I understand correctly the region command, with these situations, I have created two region IDs, the first one being “whole” and the second one being “box1”, the fact that the create_atoms and set commands both work seems to confirm this, yet, the print function will return the error stated beforehand, that the region ID “box1” doesn’t exist.

I suppose there’s something I’m misunderstanding, but am unable to put my finger on it.

Thanks for your time and your help!
Antoine.

You have to look at the error message very, very carefully. Here is what I get (after fixing a few things that keeps your input from being run):

ERROR: Region ID ’ box1’ in variable formula does not exist (src/variable.cpp:4048)

Did you notice the blank in front of “box1”?

The documented syntax is “count(group,region)” and not “count(group, region)”
You’ll get the same kind of error, just on the group, if you enter:

print “Al in region : $(count( gAl,box1))”

Wow thank you, I guess I was too used to most languages using “,” and ", " together, I hadn’t thought it could be as simple as that.

That fixes the issue! Good to know! :slight_smile:

Thanks!

That is the reason why the error messages have single quotes around the ids. :wink:

I just had a closer look at the relevant code and it seems that it is only used in cases where leading and trailing whitespace can be safely eliminated and with the current development code, this is a minimal modification (it used to be much more painful to do that, which is probably why it wasn’t done in the beginning).

So the next LAMMPS release will thus no longer produce errors on such inputs (unless we find a case where this must not be done).

Makes perfect sense!

Happy to know the change might occur in the future! Keep up the good work! :slight_smile:

Thanks again!