Help with fix atom/swap command

Dear Lammps user,

I want to use fix atom/swap command to swap Zr and Y atoms from time to time. However, I’ve got this error:
ERROR: All atoms of a swapped type must have same charge. (…/fix_atom_swap.cpp:259)
The charge of my atoms of same type is the same. I don’t know why this is happening. I copied my script at the end.

Also I had a look at the source code in fix_atom_swap.cpp, in if (atom->q_flag && !semi_grand_flag) section, here is my understanding: as soon as one atom is found in the defined group and swap type, the charge of this swap type is assigned, and “first” variable becomes to 0. Then the rest atoms of the same swap type will compare to the charge value. I’m confused on these two commands:

if (first) qtype[iswaptype] = -DBL_MAX;

MPI_Allreduce(&qtype[iswaptype],&qmax,1,MPI_DOUBLE,MPI_MAX,world);

if (first) qtype[iswaptype] = DBL_MAX;

MPI_Allreduce(&qtype[iswaptype],&qmin,1,MPI_DOUBLE,MPI_MIN,world);

What's the value of qmax and qmin? why make qtype equal to -DBL_MAX and DBL_MAX?

Thank you so much for your help!

---------- Initialize Simulation ---------------------

clear
units metal
dimension 3
boundary p p p
atom_style full

---------- Create Atomistic Structure ---------------------

kspace_style ewald 1.0e-8
pair_style buck/coul/long 10
read_data end300K1500K.dat
group oxygen type 1
group zirco type 2
group yttria type 3
group cation union zirco yttria
set type 2 charge 4
set type 1 charge -2
set type 3 charge 3

---------- Define Interatomic Potential ---------------------

pair_coeff 2 2 0 10 0 10
pair_coeff 1 1 13098.9 0.219670 49.2998 10
pair_coeff 1 2 1292.85 0.358388 19.3646 10
pair_coeff 2 3 0 10 0 10
pair_coeff 1 3 1642.724 0.353197 104.180 10
pair_coeff 3 3 0 10 0 10

#-----------NVT simulation 1500K---------------
reset_timestep 0
timestep 0.001
thermo_style custom step etotal pe temp press lx ly lz pxx pyy pzz

fix 4 all nvt temp 1500 1500 0.1
fix 5 cation atom/swap 20 10000 5829017 1500 types 2 3

thermo 2000

run 200000

Yeah, this piece of code is not right, but somehow worked if there is only one charge value, or one atom type in the group. It should also work for your case of two types, each with a different charge value. The block of if-tests that you are confused by should be outside the atom loop. They will be in the next update. See patch below.

Aidan

— MC/fix_atom_swap.cpp (revision 15185)
+++ MC/fix_atom_swap.cpp (working copy)
@@ -251,15 +251,15 @@
} else if (qtype[iswaptype] != atom->q[i])
error->one(FLERR,“All atoms of a swapped type must have the same charge.”);
}

  • MPI_Allreduce(&first,&firstall,1,MPI_INT,MPI_MIN,world);
  • if (firstall) error->all(FLERR,“At least one atom of each swapped type must be present to define charges.”);
  • if (first) qtype[iswaptype] = -DBL_MAX;
  • MPI_Allreduce(&qtype[iswaptype],&qmax,1,MPI_DOUBLE,MPI_MAX,world);
  • if (first) qtype[iswaptype] = DBL_MAX;
  • MPI_Allreduce(&qtype[iswaptype],&qmin,1,MPI_DOUBLE,MPI_MIN,world);
  • if (qmax != qmin) error->all(FLERR,“All atoms of a swapped type must have same charge.”);
    }
    }
  • MPI_Allreduce(&first,&firstall,1,MPI_INT,MPI_MIN,world);
  • if (firstall) error->all(FLERR,“At least one atom of each swapped type must be present to define charges.”);
  • if (first) qtype[iswaptype] = -DBL_MAX;
  • MPI_Allreduce(&qtype[iswaptype],&qmax,1,MPI_DOUBLE,MPI_MAX,world);
  • if (first) qtype[iswaptype] = DBL_MAX;
  • MPI_Allreduce(&qtype[iswaptype],&qmin,1,MPI_DOUBLE,MPI_MIN,world);
  • if (qmax != qmin) error->all(FLERR,“All atoms of a swapped type must have same charge.”);
    }
    }

Dear Aidan,

I’m still encountering the same error after modifying the above lines. I’m still unclear about the if (qmax != qmin) error->all(FLERR,“All atoms of a swapped type must have same charge.”);

What’s the purpose of checking qmax equals to qmin? Is qmax is charge of one type and qmin the charge of the other type? Because line 252 already makes sure one type of atom has the same charge.

Thank you for your help!

Regards,
Shenli

If qmax != qmin, then there exists at least two different charge values for atoms of that type. Atoms with different types can still have different charge values. If you can’t get this working in your code, you may have made the change incorrectly (move the block of code outside/below two curly brackets). Or I may have missed something else. Send me a very small example of your problem.

Dear Aidan,

I attach the data file and input file here for test. Thank you so much for your help!

Shenli

end300K1500K.dat (80.4 KB)

MC_MD_try.input (969 Bytes)

I did not get an error. Attached is the new source file.

fix_atom_swap.cpp (23 KB)