Merge pull request #4088 from sakibmatin/debug
Fix for force calculation and memory bug (atom name definition) in mliap.
This commit is contained in:
@ -13,6 +13,7 @@ from libc.stdint cimport uintptr_t
|
||||
cimport cython
|
||||
from cpython.ref cimport PyObject
|
||||
from libc.stdlib cimport malloc, free
|
||||
from libc.string cimport memcpy
|
||||
|
||||
|
||||
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 char **elements = <char**>malloc(nelements * sizeof(char*))
|
||||
cdef char * c_str
|
||||
cdef char * s
|
||||
cdef ssize_t slen
|
||||
|
||||
if not elements:
|
||||
raise MemoryError("failed to allocate memory for element names")
|
||||
|
||||
cdef char *elem_name
|
||||
for i, elem in enumerate(unified.element_types):
|
||||
elem_name_bytes = elem.encode('UTF-8')
|
||||
elem_name = elem_name_bytes
|
||||
elements[i] = &elem_name[0]
|
||||
py_str = elem.encode('UTF-8')
|
||||
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.model.nelements = nelements
|
||||
|
||||
|
||||
@ -315,7 +315,6 @@ void LAMMPS_NS::update_pair_forces(MLIAPDataKokkosDevice *data, double *fij)
|
||||
int i = pair_i[ii];
|
||||
int j = j_atoms[ii];
|
||||
// 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+1], fij[ii3+1]);
|
||||
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,4), 0.5*v[4]);
|
||||
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){
|
||||
double e = ei[i];
|
||||
// must not count any contribution where i is not a local atom
|
||||
if (i < nlocal) {
|
||||
d_eatoms[i] = e;
|
||||
local_sum += e;
|
||||
}
|
||||
|
||||
d_eatoms[i] = e;
|
||||
local_sum += e;
|
||||
},*data->energy);
|
||||
}
|
||||
|
||||
|
||||
@ -254,10 +254,8 @@ void LAMMPS_NS::update_pair_energy(MLIAPData *data, double *eij)
|
||||
double e = 0.5 * eij[ii];
|
||||
|
||||
// must not count any contribution where i is not a local atom
|
||||
if (i < nlocal) {
|
||||
data->eatoms[i] += e;
|
||||
e_total += e;
|
||||
}
|
||||
data->eatoms[i] += e;
|
||||
e_total += e;
|
||||
}
|
||||
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 j = data->jatoms[ii];
|
||||
|
||||
// must not count any contribution where i is not a local atom
|
||||
if (i < nlocal) {
|
||||
f[i][0] += fij[ii3];
|
||||
f[i][1] += fij[ii3 + 1];
|
||||
f[i][2] += fij[ii3 + 2];
|
||||
f[j][0] -= fij[ii3];
|
||||
f[j][1] -= fij[ii3 + 1];
|
||||
f[j][2] -= fij[ii3 + 2];
|
||||
f[i][0] += fij[ii3];
|
||||
f[i][1] += fij[ii3 + 1];
|
||||
f[i][2] += fij[ii3 + 2];
|
||||
f[j][0] -= fij[ii3];
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -8,6 +8,7 @@ import lammps.mliap
|
||||
cimport cython
|
||||
from cpython.ref cimport PyObject
|
||||
from libc.stdlib cimport malloc, free
|
||||
from libc.string cimport memcpy
|
||||
|
||||
|
||||
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 char **elements = <char**>malloc(nelements * sizeof(char*))
|
||||
cdef char * c_str
|
||||
cdef char * s
|
||||
cdef ssize_t slen
|
||||
|
||||
if not elements:
|
||||
raise MemoryError("failed to allocate memory for element names")
|
||||
|
||||
cdef char *elem_name
|
||||
for i, elem in enumerate(unified.element_types):
|
||||
elem_name_bytes = elem.encode('UTF-8')
|
||||
elem_name = elem_name_bytes
|
||||
elements[i] = &elem_name[0]
|
||||
py_str = elem.encode('UTF-8')
|
||||
|
||||
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.model.nelements = nelements
|
||||
|
||||
|
||||
Reference in New Issue
Block a user