Atomfile command cannot read the complete data as expected

Hi LAMMPS developers and users,

I want to create a variable from an atom file with the variable atomfile command. I’ve read carefully the document and wrote a proper data file as requested. Weirdly, I cannot read the complete data because there are many zero values. Here is the minimal working example:

atom_style atomic
units si
atom_modify map array
region simulation_box block 0 1 0 1 0 1
create_box 1 simulation_box
create_atoms 1 single 0.1 0.1 0.1
create_atoms 1 single 0.3 0.5 0.7
create_atoms 1 single 0.5 0.7 0.2
mass 1 1
variable F atomfile test.csv
dump mydmp all custom 1 test.dump id xs ys zs v_F
run 1

The csv file is

# ID	FP
1	0.1
2	0.2
3	0.3

The output dump file is

ITEM: TIMESTEP
0
ITEM: NUMBER OF ATOMS
3
ITEM: BOX BOUNDS pp pp pp
0.0000000000000000e+00 1.0000000000000000e+00
0.0000000000000000e+00 1.0000000000000000e+00
0.0000000000000000e+00 1.0000000000000000e+00
ITEM: ATOMS id xs ys zs v_F
1 0.1 0.1 0.1 0
2 0.3 0.5 0.7 0.2
3 0.5 0.7 0.2 0

It is found that only atom #2 got the proper value, and the F value of atoms #1 and #3 did not change.
Interestingly, when I changed the csv file to

# ID	FP
1	0.1
3	0.2
2	0.3

and the dump file became

... 
(omitted for simplicity)
ITEM: ATOMS id xs ys zs v_F
1 0.1 0.1 0.1 0
2 0.3 0.5 0.7 0
3 0.5 0.7 0.2 0.2

This time only atom #3 got the proper value. So weird it is… How can I fix this bug?

Any help or comment will be appreciated. Thanks in advance

The documentation says the following about the format of atomfile variable files:

The rules for formatting the file are as follows. Each time a set of per-atom values is read, a non-blank line is searched for in the file. The file is read line by line but only up to 254 characters are used. The rest are ignored. A comment character “#” can be used anywhere on a line and all text following and the “#” character are ignored; text starting with the comment character is stripped. Blank lines are skipped. The first “word” of a non-blank line, delimited by white-space, is read as the count N of per-atom lines to immediately follow. N can be the total number of atoms in the system, or only a subset. The next N lines have the following format

So in your file the first word in the first non-blank non-comment line “1 0.1” is read and N is set to 1. That is your atomfile is expected to have 1 entry per record. In your first example, this would be “2 0.2” in the second example this would be “3 0.2” and that is exactly what is happening. All other atoms will have a value of 0.0.

The problem is you obviously should write your files in a different format, e.g. like this:

3
# ID   FP
1     0.1
2     0.2
3     0.3
1 Like

Thanks @akohlmey . I used the data file provided by you. But it gave another error:

ERROR: Invalid atomfile line ‘# ID FP’: Not a valid integer number: ‘#’ (src/variable.cpp:5293)
Last command: variable F atomfile test.csv

If I replace the comment line with a blank line:

3

1     0.1
2     0.2
3     0.3

I get a similar error:

ERROR: Invalid atomfile line ‘’: No more tokens (src/variable.cpp:5293)
Last command: variable F atomfile test.csv

The error can only be avoided if there is no comment or blank line as follows:

3
1     0.1
2     0.2
3     0.3

Using the format above I can assign the values properly. I don’t understand why the comment line was not recognized. Anyway, thank u for reminding me that the first line should be the count N.

The format below is OK. No error is reported.

# ID FP
3
1     0.1
2     0.2
3     0.3

Maybe comment/blank lines are only allowed to appear before count N.

That is an “undocumented feature” (aka unintended behavior).
The parser for the atomfile variable data is using some old style string processing and needs to be updated to use the modernized tools for parsing that we have written which simplify and unify the processing of text so that comments and empty lines can be added anywhere.

1 Like

A pull request with the necessary updates has been submitted on GitHub.

1 Like

:+1:
Hope it will be improved soon.

All the best,
Jasmine Feng