Refactor. Fix bug in atom initialization.

This commit is contained in:
Jan Eitzinger 2020-08-19 09:00:35 +02:00
parent e7869286d7
commit 71ea8dbb0e
10 changed files with 108 additions and 45 deletions

View File

@ -7,7 +7,7 @@ ANSI_CFLAGS += -std=c99
ANSI_CFLAGS += -pedantic ANSI_CFLAGS += -pedantic
ANSI_CFLAGS += -Wextra ANSI_CFLAGS += -Wextra
CFLAGS = -O3 #-g $(ANSI_CFLAGS) CFLAGS = -O3 $(ANSI_CFLAGS) #-g
ASFLAGS = -masm=intel ASFLAGS = -masm=intel
CXXFLAGS = $(CFLAGS) CXXFLAGS = $(CFLAGS)
FCFLAGS = FCFLAGS =

View File

@ -64,8 +64,7 @@ void* reallocate (
size_t newBytesize, size_t newBytesize,
size_t oldBytesize) size_t oldBytesize)
{ {
void* newarray; void* newarray = allocate(alignment, newBytesize);
newarray = allocate(alignment, newBytesize);
if(ptr != NULL) { if(ptr != NULL) {
memcpy(newarray, ptr, oldBytesize); memcpy(newarray, ptr, oldBytesize);

View File

@ -24,15 +24,27 @@
* *
* ======================================================================================= * =======================================================================================
*/ */
#include <stdlib.h>
#include <stdio.h>
#include <math.h> #include <math.h>
#include <parameter.h>
#include <allocate.h>
#include <atom.h> #include <atom.h>
#include <allocate.h>
#include <util.h> #include <util.h>
#define DELTA 20000 #define DELTA 20000
void initAtom(Atom *atom)
{
atom->x = NULL; atom->y = NULL; atom->z = NULL;
atom->vx = NULL; atom->vy = NULL; atom->vz = NULL;
atom->fx = NULL; atom->fy = NULL; atom->fz = NULL;
atom->Natoms = 0;
atom->Nlocal = 0;
atom->Nghost = 0;
atom->Nmax = 0;
}
void createAtom(Atom *atom, Parameter *param) void createAtom(Atom *atom, Parameter *param)
{ {
double xlo = 0.0; double xhi = param->xprd; double xlo = 0.0; double xhi = param->xprd;
@ -128,9 +140,9 @@ void growAtom(Atom *atom)
int nold = atom->Nmax; int nold = atom->Nmax;
atom->Nmax += DELTA; atom->Nmax += DELTA;
atom->x = (double*) reallocate(atom->x, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double)); atom->x = (double*) reallocate(atom->x, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double));
atom->y = (double*) reallocate(atom->y, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double)); atom->y = (double*) reallocate(atom->y, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double));
atom->z = (double*) reallocate(atom->z, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double)); atom->z = (double*) reallocate(atom->z, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double));
atom->vx = (double*) reallocate(atom->vx, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double)); atom->vx = (double*) reallocate(atom->vx, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double));
atom->vy = (double*) reallocate(atom->vy, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double)); atom->vy = (double*) reallocate(atom->vy, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double));
atom->vz = (double*) reallocate(atom->vz, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double)); atom->vz = (double*) reallocate(atom->vz, ALIGNMENT, atom->Nmax * sizeof(double), nold * sizeof(double));

View File

@ -36,6 +36,7 @@ typedef struct {
double *fx, *fy, *fz; double *fx, *fy, *fz;
} Atom; } Atom;
extern void initAtom(Atom*);
extern void createAtom(Atom*, Parameter*); extern void createAtom(Atom*, Parameter*);
extern void growAtom(Atom*); extern void growAtom(Atom*);
#endif #endif

View File

@ -39,7 +39,7 @@ typedef struct {
} Neighbor; } Neighbor;
extern void initNeighbor(Neighbor*, Parameter*); extern void initNeighbor(Neighbor*, Parameter*);
extern void setupNeighbor(Parameter*); extern void setupNeighbor();
extern void binatoms(Atom*); extern void binatoms(Atom*);
extern void buildNeighbor(Atom*, Neighbor*); extern void buildNeighbor(Atom*, Neighbor*);
#endif #endif

View File

@ -33,6 +33,7 @@
#include <math.h> #include <math.h>
#include <float.h> #include <float.h>
#include <timing.h>
#include <allocate.h> #include <allocate.h>
#include <neighbor.h> #include <neighbor.h>
#include <parameter.h> #include <parameter.h>
@ -42,16 +43,16 @@
#define HLINE "----------------------------------------------------------------------------\n" #define HLINE "----------------------------------------------------------------------------\n"
#define FACTOR 0.999 typedef enum {
#define SMALL 1.0e-6 TOTAL = 0,
#define DELTA 20000 NEIGH,
FORCE,
NUMTIMER
} timertype;
void init(Atom *atom, Parameter *param)
void init(Parameter *param)
{ {
atom->x = NULL; atom->y = NULL; atom->z = NULL;
atom->vx = NULL; atom->vy = NULL; atom->vz = NULL;
atom->fx = NULL; atom->fy = NULL; atom->fz = NULL;
param->epsilon = 1.0; param->epsilon = 1.0;
param->sigma6 = 1.0; param->sigma6 = 1.0;
param->rho = 0.8442; param->rho = 0.8442;
@ -73,6 +74,19 @@ void init(Atom *atom, Parameter *param)
param->zprd = param->nz * lattice; param->zprd = param->nz * lattice;
} }
void setup(Parameter *param, Atom *atom, Neighbor *neighbor){
initAtom(atom);
initNeighbor(neighbor, param);
initPbc();
setupNeighbor();
createAtom(atom, param);
setupThermo(param, atom->Natoms);
adjustThermo(param, atom);
setupPbc(atom, param);
updatePbc(atom, param);
buildNeighbor(atom, neighbor);
}
void initialIntegrate(Parameter *param, Atom *atom) void initialIntegrate(Parameter *param, Atom *atom)
{ {
double* x = atom->x; double* y = atom->y; double* z = atom->z; double* x = atom->x; double* y = atom->y; double* z = atom->z;
@ -101,7 +115,7 @@ void finalIntegrate(Parameter *param, Atom *atom)
} }
} }
void computeForce(Neighbor *neighbor, Atom *atom, Parameter *param) void computeForce(Parameter *param, Atom *atom, Neighbor *neighbor)
{ {
int Nlocal = atom->Natoms; int Nlocal = atom->Natoms;
int* neighs; int* neighs;
@ -164,26 +178,52 @@ void printAtomState(Atom *atom)
} }
} }
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
double timer[NUMTIMER];
double S;
Atom atom; Atom atom;
Neighbor neighbor; Neighbor neighbor;
Parameter param; Parameter param;
init(&atom, &param); init(&param);
initNeighbor(&neighbor, &param);
initPbc(); for(int i = 0; i < argc; i++)
setupNeighbor(&param); {
createAtom(&atom, &param); if((strcmp(argv[i], "-n") == 0) || (strcmp(argv[i], "--nsteps") == 0))
setupThermo(&param, atom.Natoms); {
adjustThermo(&param, &atom); param.ntimes = atoi(argv[++i]);
/* printAtomState(&atom); */ continue;
setupPbc(&atom, &param); }
updatePbc(&atom, &param); if((strcmp(argv[i], "-nx") == 0))
buildNeighbor(&atom, &neighbor); {
param.nx = atoi(argv[++i]);
continue;
}
if((strcmp(argv[i], "-ny") == 0))
{
param.ny = atoi(argv[++i]);
continue;
}
if((strcmp(argv[i], "-nz") == 0))
{
param.nz = atoi(argv[++i]);
continue;
}
if((strcmp(argv[i], "-h") == 0) || (strcmp(argv[i], "--help") == 0))
{
printf("MD Bench: A minimalistic re-implementation of miniMD\n");
printf(HLINE);
printf("-n / --nsteps <int>: set number of timesteps for simulation\n");
printf("-nx/-ny/-nz <int>: set linear dimension of systembox in x/y/z direction\n");
printf(HLINE);
exit(EXIT_SUCCESS);
}
}
setup(&param, &atom, &neighbor);
computeThermo(0, &param, &atom); computeThermo(0, &param, &atom);
computeForce(&neighbor, &atom, &param); computeForce(&param, &atom, &neighbor);
for(int n = 0; n < param.ntimes; n++) { for(int n = 0; n < param.ntimes; n++) {
@ -199,7 +239,7 @@ int main (int argc, char** argv)
buildNeighbor(&atom, &neighbor); buildNeighbor(&atom, &neighbor);
} }
computeForce(&neighbor, &atom, &param); computeForce(&param, &atom, &neighbor);
finalIntegrate(&param, &atom); finalIntegrate(&param, &atom);
if(!((n + 1) % param.nstat) && (n+1) < param.ntimes) { if(!((n + 1) % param.nstat) && (n+1) < param.ntimes) {

View File

@ -76,7 +76,7 @@ void initNeighbor(Neighbor *neighbor, Parameter *param)
neighbor->neighbors = NULL; neighbor->neighbors = NULL;
} }
void setupNeighbor(Parameter *param) void setupNeighbor()
{ {
double coord; double coord;
int mbinxhi, mbinyhi, mbinzhi; int mbinxhi, mbinyhi, mbinzhi;

View File

@ -39,17 +39,7 @@ static int *PBCx, *PBCy, *PBCz;
static void growPbc(); static void growPbc();
void growPbc() /* exported subroutines */
{
int nold = NmaxGhost;
NmaxGhost += DELTA;
BorderMap = (int*) reallocate(BorderMap, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
PBCx = (int*) reallocate(PBCx, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
PBCy = (int*) reallocate(PBCy, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
PBCz = (int*) reallocate(PBCz, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
}
void initPbc() void initPbc()
{ {
NmaxGhost = 0; NmaxGhost = 0;
@ -57,6 +47,8 @@ void initPbc()
PBCx = NULL; PBCy = NULL; PBCz = NULL; PBCx = NULL; PBCy = NULL; PBCz = NULL;
} }
/* update coordinates of ghost atoms */
/* uses mapping created in setupPbc */
void updatePbc(Atom *atom, Parameter *param) void updatePbc(Atom *atom, Parameter *param)
{ {
int nlocal = atom->Nlocal; int nlocal = atom->Nlocal;
@ -74,6 +66,8 @@ void updatePbc(Atom *atom, Parameter *param)
} }
} }
/* relocate atoms that have left domain according
* to periodic boundary conditions */
void updateAtomsPbc(Atom *atom, Parameter *param) void updateAtomsPbc(Atom *atom, Parameter *param)
{ {
double* x = atom->x; double* x = atom->x;
@ -105,6 +99,10 @@ void updateAtomsPbc(Atom *atom, Parameter *param)
} }
} }
/* setup periodic boundary conditions by
* defining ghost atoms around domain
* only creates mapping and coordinate corrections
* that are then enforced in updatePbc */
#define ADDGHOST(dx,dy,dz) Nghost++; BorderMap[Nghost] = i; PBCx[Nghost] = dx; PBCy[Nghost] = dy; PBCz[Nghost] = dz; #define ADDGHOST(dx,dy,dz) Nghost++; BorderMap[Nghost] = i; PBCx[Nghost] = dx; PBCy[Nghost] = dy; PBCz[Nghost] = dz;
void setupPbc(Atom *atom, Parameter *param) void setupPbc(Atom *atom, Parameter *param)
{ {
@ -159,3 +157,15 @@ void setupPbc(Atom *atom, Parameter *param)
// increase by one to make it the ghost atom count // increase by one to make it the ghost atom count
atom->Nghost = Nghost + 1; atom->Nghost = Nghost + 1;
} }
/* internal subroutines */
void growPbc()
{
int nold = NmaxGhost;
NmaxGhost += DELTA;
BorderMap = (int*) reallocate(BorderMap, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
PBCx = (int*) reallocate(PBCx, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
PBCy = (int*) reallocate(PBCy, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
PBCz = (int*) reallocate(PBCz, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
}

View File

@ -44,6 +44,7 @@ static double p_act;
static double e_act; static double e_act;
static int mstat; static int mstat;
/* exported subroutines */
void setupThermo(Parameter *param, int natoms) void setupThermo(Parameter *param, int natoms)
{ {
int maxstat = param->ntimes / param->nstat + 2; int maxstat = param->ntimes / param->nstat + 2;

View File

@ -325,7 +325,7 @@ void init(Neighbor *neighbor, Parameter *param)
param->dt = 0.005; param->dt = 0.005;
param->nx = 32; param->nx = 32;
param->ny = 32; param->ny = 32;
param->nz = 32; param->nz = 64;
param->cutforce = 2.5; param->cutforce = 2.5;
param->temp = 1.44; param->temp = 1.44;
param->nstat = 100; param->nstat = 100;