Create molecules in fix.

Dear all:
I’m writing a fix involving the creation of new molecules (including bond, angle ….).
In pre_exchange() stage, I manage to create everything associated with the atom class, but looks like the global to local atom id map is not set as the error appears in ntopo_bond_all.cpp atom->map(tag[kk]) still yields -1.
And I don’t understand where the map is actually set.
The result should be atom->map(tag[kk]) =kk which I know already.
Could anyone explain where the map is actually set or if there are some similar codes relative to the creation of a molecular with bond, angle structures?
Thanks, a lot!

Part of my code is shown below.

// atom->avec->create_atom(l,xh); // create new atom
nlocal = atom->nlocal;
kk = nlocal - 1;
atom->molecule[kk] = static_cast(buf_recv[k+1]);
atom->mask[kk] = static_cast(buf_recv[k+3]);
for (j = 0; j < 3; ++j) // assign velocities
atom->v[kk][j] = buf_recv[k + 7 + j];
k += part_size;
if (molecule[kk]){ // assign molecular data
tag[kk] = static_cast(buf_recv[k++]);
atom->num_bond[kk] = static_cast(buf_recv[k++]);
for (m = 0; m < atom->num_bond[kk]; m++){
atom->bond_type[kk][m] = static_cast(buf_recv[k++]);
atom->bond_atom[kk][m] = static_cast(buf_recv[k++]);
atom->num_angle[kk] = static_cast(buf_recv[k++]);
for (m = 0; m < atom->num_angle[kk]; m++){
atom->angle_type[kk][m] = static_cast(buf_recv[k++]);
atom->angle_atom1[kk][m] = static_cast(buf_recv[k++]);
atom->angle_atom2[kk][m] = static_cast(buf_recv[k++]);
atom->angle_atom3[kk][m] = static_cast(buf_recv[k++]);

if (atom->map_style) {

please have a look at fix deposit or fix pour. both allow to insert molecules to the system from molecule templates created with the molecule command. that is the recommended way to add molecules to a the system and used in multiple places throughout LAMMPS.

there is not enough of your code shown to give any specific comments. in particular, there are no MPI calls shown, which would be crucial to the process.

as to the creation of the atom map. Atom::map_clear() deletes it, Atom::map_init() initialized/allocates it and Atom::map_set() updates it.

also, your casting with “static_cast” is a problem. please see other places in the LAMMPS code, where it uses the ubuf union to handle 64 bit integers as expected.