Merge pull request #4088 from sakibmatin/debug

Fix for force calculation and memory bug (atom name definition) in mliap.
This commit is contained in:
Axel Kohlmeyer
2024-02-26 16:46:36 -05:00
committed by GitHub
4 changed files with 42 additions and 29 deletions

View File

@ -13,6 +13,7 @@ from libc.stdint cimport uintptr_t
cimport cython cimport cython
from cpython.ref cimport PyObject from cpython.ref cimport PyObject
from libc.stdlib cimport malloc, free from libc.stdlib cimport malloc, free
from libc.string cimport memcpy
cdef extern from "lammps.h" namespace "LAMMPS_NS": cdef extern from "lammps.h" namespace "LAMMPS_NS":
@ -451,15 +452,24 @@ cdef public object mliap_unified_connect_kokkos(char *fname, MLIAPDummyModel * m
cdef int nelements = <int>len(unified.element_types) cdef int nelements = <int>len(unified.element_types)
cdef char **elements = <char**>malloc(nelements * sizeof(char*)) cdef char **elements = <char**>malloc(nelements * sizeof(char*))
cdef char * c_str
cdef char * s
cdef ssize_t slen
if not elements: if not elements:
raise MemoryError("failed to allocate memory for element names") raise MemoryError("failed to allocate memory for element names")
cdef char *elem_name
for i, elem in enumerate(unified.element_types): for i, elem in enumerate(unified.element_types):
elem_name_bytes = elem.encode('UTF-8') py_str = elem.encode('UTF-8')
elem_name = elem_name_bytes s = py_str
elements[i] = &elem_name[0] slen = len(py_str)
c_str = <char *>malloc((slen+1)*sizeof(char))
if not c_str:
raise MemoryError("failed to allocate memory for element names")
memcpy(c_str, s, slen)
c_str[slen] = 0
elements[i] = c_str
unified_int.descriptor.set_elements(elements, nelements) unified_int.descriptor.set_elements(elements, nelements)
unified_int.model.nelements = nelements unified_int.model.nelements = nelements

View File

@ -315,7 +315,6 @@ void LAMMPS_NS::update_pair_forces(MLIAPDataKokkosDevice *data, double *fij)
int i = pair_i[ii]; int i = pair_i[ii];
int j = j_atoms[ii]; int j = j_atoms[ii];
// must not count any contribution where i is not a local atom // must not count any contribution where i is not a local atom
if (i < nlocal) {
Kokkos::atomic_add(&f[i*3+0], fij[ii3+0]); Kokkos::atomic_add(&f[i*3+0], fij[ii3+0]);
Kokkos::atomic_add(&f[i*3+1], fij[ii3+1]); Kokkos::atomic_add(&f[i*3+1], fij[ii3+1]);
Kokkos::atomic_add(&f[i*3+2], fij[ii3+2]); Kokkos::atomic_add(&f[i*3+2], fij[ii3+2]);
@ -352,7 +351,6 @@ void LAMMPS_NS::update_pair_forces(MLIAPDataKokkosDevice *data, double *fij)
Kokkos::atomic_add(&d_vatom(j,3), 0.5*v[3]); Kokkos::atomic_add(&d_vatom(j,3), 0.5*v[3]);
Kokkos::atomic_add(&d_vatom(j,4), 0.5*v[4]); Kokkos::atomic_add(&d_vatom(j,4), 0.5*v[4]);
Kokkos::atomic_add(&d_vatom(j,5), 0.5*v[5]); Kokkos::atomic_add(&d_vatom(j,5), 0.5*v[5]);
}
} }
} }
}); });
@ -382,11 +380,9 @@ void LAMMPS_NS::update_atom_energy(MLIAPDataKokkosDevice *data, double *ei)
Kokkos::parallel_reduce(nlocal, KOKKOS_LAMBDA(int i, double &local_sum){ Kokkos::parallel_reduce(nlocal, KOKKOS_LAMBDA(int i, double &local_sum){
double e = ei[i]; double e = ei[i];
// must not count any contribution where i is not a local atom
if (i < nlocal) { d_eatoms[i] = e;
d_eatoms[i] = e; local_sum += e;
local_sum += e;
}
},*data->energy); },*data->energy);
} }

View File

@ -254,10 +254,8 @@ void LAMMPS_NS::update_pair_energy(MLIAPData *data, double *eij)
double e = 0.5 * eij[ii]; double e = 0.5 * eij[ii];
// must not count any contribution where i is not a local atom // must not count any contribution where i is not a local atom
if (i < nlocal) { data->eatoms[i] += e;
data->eatoms[i] += e; e_total += e;
e_total += e;
}
} }
data->energy = e_total; data->energy = e_total;
} }
@ -277,17 +275,14 @@ void LAMMPS_NS::update_pair_forces(MLIAPData *data, double *fij)
int i = data->pair_i[ii]; int i = data->pair_i[ii];
int j = data->jatoms[ii]; int j = data->jatoms[ii];
// must not count any contribution where i is not a local atom f[i][0] += fij[ii3];
if (i < nlocal) { f[i][1] += fij[ii3 + 1];
f[i][0] += fij[ii3]; f[i][2] += fij[ii3 + 2];
f[i][1] += fij[ii3 + 1]; f[j][0] -= fij[ii3];
f[i][2] += fij[ii3 + 2]; f[j][1] -= fij[ii3 + 1];
f[j][0] -= fij[ii3]; f[j][2] -= fij[ii3 + 2];
f[j][1] -= fij[ii3 + 1];
f[j][2] -= fij[ii3 + 2];
if (data->vflag) data->pairmliap->v_tally(i, j, &fij[ii3], data->rij[ii]); if (data->vflag) data->pairmliap->v_tally(i, j, &fij[ii3], data->rij[ii]);
}
} }
} }

View File

@ -8,6 +8,7 @@ import lammps.mliap
cimport cython cimport cython
from cpython.ref cimport PyObject from cpython.ref cimport PyObject
from libc.stdlib cimport malloc, free from libc.stdlib cimport malloc, free
from libc.string cimport memcpy
cdef extern from "lammps.h" namespace "LAMMPS_NS": cdef extern from "lammps.h" namespace "LAMMPS_NS":
@ -387,15 +388,26 @@ cdef public object mliap_unified_connect(char *fname, MLIAPDummyModel * model,
cdef int nelements = <int>len(unified.element_types) cdef int nelements = <int>len(unified.element_types)
cdef char **elements = <char**>malloc(nelements * sizeof(char*)) cdef char **elements = <char**>malloc(nelements * sizeof(char*))
cdef char * c_str
cdef char * s
cdef ssize_t slen
if not elements: if not elements:
raise MemoryError("failed to allocate memory for element names") raise MemoryError("failed to allocate memory for element names")
cdef char *elem_name
for i, elem in enumerate(unified.element_types): for i, elem in enumerate(unified.element_types):
elem_name_bytes = elem.encode('UTF-8') py_str = elem.encode('UTF-8')
elem_name = elem_name_bytes
elements[i] = &elem_name[0] s = py_str
slen = len(py_str)
c_str = <char *>malloc((slen+1)*sizeof(char))
if not c_str:
raise MemoryError("failed to allocate memory for element names")
memcpy(c_str, s, slen)
c_str[slen] = 0
elements[i] = c_str
unified_int.descriptor.set_elements(elements, nelements) unified_int.descriptor.set_elements(elements, nelements)
unified_int.model.nelements = nelements unified_int.model.nelements = nelements