So I want to understand how fix is called in lammps because i have written the bonds change only it the initialization. I don’t understand when running the code the print statement is printed two times. Once before setting up verlet run and once after the run after writing a restart file.
#include "fix_bond_data.h"
#include "atom.h"
#include "update.h"
#include "comm.h"
#include "error.h"
#include <stdio.h>
#include "fix.h"
#include <iostream>
using namespace LAMMPS_NS;
FixBondData::FixBondData(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) {
// Constructor: initialize any parameters if needed
}
FixBondData::~FixBondData() {
// Destructor: clean up resources if needed
}
void FixBondData::init() {
// Initialization: This function is called once at the start
process_bond_data(); // Call the bond data processing function once during initialization
}
void FixBondData::process_bond_data() {
// Access the bond data from LAMMPS
for (int i = 0; i < atom->nlocal; ++i) { // Loop through local atoms
int nbonds = atom->num_bond[i]; // Get number of bonds for atom i
for (int j = 0; j < nbonds; ++j) {
int atom1 = i; // Current atom
int atom2 = atom->map(atom->bond_atom[i][j]); // Get the bonded atom ID
int bond_type = atom->bond_type[i][j]; // Get the bond type
// Process bond data (atom1 is bonded to atom2 with bond_type)
printf("Bond between atom %d and atom %d (type %d)\n", atom1, atom2, bond_type);
}
}
}
int FixBondData::setmask() {
int mask = 0;
// No need for POST_FORCE since we are only running once at the start
return mask; // Return an empty mask
}
For reference my input file for lammps looks like this
dimension 2
units lj
atom_style bond
# Read initial configuration
read_data init_config.txt
# Define bonds (springs)
bond_style harmonic
bond_coeff 1 200.0 1.0
# Define the neighbor list
neighbor 0.3 bin
neigh_modify delay 5
group vertices id 1 2 3 4 5 6 7 8 9 10 11 20 21 30 31 40 41 50 51 60 61 70 71 80 81 90 91 92 93 94 95 96 97 98 99 100
# Fix the vertex atoms so they don't move
fix freeze_vertices vertices setforce 0.0 0.0 0.0
# Integrate motion of all atoms except the fixed vertices
group mobile subtract all vertices
fix 1 mobile nve
fix 2 mobile langevin 1.0 1.0 1.0 48279
fix my_bond_data all bond/data
# Output atom properties
dump 1 all custom 100 ../lammps/trajectory/atoms.lammpstrj id type x y z
# Specify simulation settings
thermo 100
# Run the simulation for the required duration
run 10000
# Write a restart file
write_data ../lammps/restart.txt
And the output I am getting looks something like this
`Bond between atom 94 and atom 95 (type 1)
Bond between atom 95 and atom 96 (type 1)
Bond between atom 96 and atom 97 (type 1)
Bond between atom 97 and atom 98 (type 1)
Bond between atom 98 and atom 99 (type 1)
Setting up Verlet run ...
.......
......
Timesteps part
......
Histogram: 1 0 0 0 0 0 0 0 0 0
Neighs: 0 ave 0 max 0 min
Histogram: 1 0 0 0 0 0 0 0 0 0
Total # of neighbors = 0
Ave neighs/atom = 0
Ave special neighs/atom = 26.06
Neighbor list builds = 1997
Dangerous builds = 1996
System init for write_data ...
Bond between atom 0 and atom 10 (type 1)
Bond between atom 0 and atom 1 (type 1)
Bond between atom 1 and atom 2 (type 1)
.....
.....
`