2020-08-18 14:27:28 +02:00
|
|
|
/*
|
2022-09-05 10:39:42 +02:00
|
|
|
* Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
|
|
|
|
* All rights reserved. This file is part of MD-Bench.
|
|
|
|
* Use of this source code is governed by a LGPL-3.0
|
|
|
|
* license that can be found in the LICENSE file.
|
2020-08-18 14:27:28 +02:00
|
|
|
*/
|
2022-08-09 18:53:53 +02:00
|
|
|
#include <stdbool.h>
|
2020-08-18 14:27:28 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
2022-08-09 18:53:53 +02:00
|
|
|
//---
|
2020-08-18 14:27:28 +02:00
|
|
|
#include <pbc.h>
|
|
|
|
#include <atom.h>
|
|
|
|
#include <allocate.h>
|
|
|
|
|
|
|
|
#define DELTA 20000
|
|
|
|
|
2022-08-11 16:42:41 +02:00
|
|
|
int NmaxGhost;
|
|
|
|
int *PBCx, *PBCy, *PBCz;
|
2020-08-18 14:27:28 +02:00
|
|
|
|
2021-11-03 00:57:24 +01:00
|
|
|
static void growPbc(Atom*);
|
2020-08-18 14:27:28 +02:00
|
|
|
|
2020-08-19 09:00:35 +02:00
|
|
|
/* exported subroutines */
|
2022-07-08 02:30:03 +02:00
|
|
|
void initPbc(Atom* atom) {
|
2020-08-18 14:27:28 +02:00
|
|
|
NmaxGhost = 0;
|
2021-11-03 00:57:24 +01:00
|
|
|
atom->border_map = NULL;
|
2020-08-18 14:27:28 +02:00
|
|
|
PBCx = NULL; PBCy = NULL; PBCz = NULL;
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:00:35 +02:00
|
|
|
/* update coordinates of ghost atoms */
|
|
|
|
/* uses mapping created in setupPbc */
|
2022-08-12 17:28:06 +02:00
|
|
|
void updatePbc_cpu(Atom *atom, Parameter *param, bool doReneighbor) {
|
2021-11-03 00:57:24 +01:00
|
|
|
int *border_map = atom->border_map;
|
2020-08-18 14:27:28 +02:00
|
|
|
int nlocal = atom->Nlocal;
|
2020-11-05 12:41:44 +01:00
|
|
|
MD_FLOAT xprd = param->xprd;
|
|
|
|
MD_FLOAT yprd = param->yprd;
|
|
|
|
MD_FLOAT zprd = param->zprd;
|
2020-08-18 14:27:28 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < atom->Nghost; i++) {
|
2021-11-03 00:57:24 +01:00
|
|
|
atom_x(nlocal + i) = atom_x(border_map[i]) + PBCx[i] * xprd;
|
|
|
|
atom_y(nlocal + i) = atom_y(border_map[i]) + PBCy[i] * yprd;
|
|
|
|
atom_z(nlocal + i) = atom_z(border_map[i]) + PBCz[i] * zprd;
|
2020-08-18 14:27:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:00:35 +02:00
|
|
|
/* relocate atoms that have left domain according
|
|
|
|
* to periodic boundary conditions */
|
2022-08-12 17:28:06 +02:00
|
|
|
void updateAtomsPbc_cpu(Atom *atom, Parameter *param) {
|
2020-11-05 12:41:44 +01:00
|
|
|
MD_FLOAT xprd = param->xprd;
|
|
|
|
MD_FLOAT yprd = param->yprd;
|
|
|
|
MD_FLOAT zprd = param->zprd;
|
2020-08-18 14:27:28 +02:00
|
|
|
|
|
|
|
for(int i = 0; i < atom->Nlocal; i++) {
|
2021-03-23 10:03:55 +01:00
|
|
|
if(atom_x(i) < 0.0) {
|
|
|
|
atom_x(i) += xprd;
|
|
|
|
} else if(atom_x(i) >= xprd) {
|
|
|
|
atom_x(i) -= xprd;
|
2020-08-18 14:27:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:03:55 +01:00
|
|
|
if(atom_y(i) < 0.0) {
|
|
|
|
atom_y(i) += yprd;
|
|
|
|
} else if(atom_y(i) >= yprd) {
|
|
|
|
atom_y(i) -= yprd;
|
2020-08-18 14:27:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 10:03:55 +01:00
|
|
|
if(atom_z(i) < 0.0) {
|
|
|
|
atom_z(i) += zprd;
|
|
|
|
} else if(atom_z(i) >= zprd) {
|
|
|
|
atom_z(i) -= zprd;
|
2020-08-18 14:27:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-19 09:00:35 +02:00
|
|
|
/* setup periodic boundary conditions by
|
|
|
|
* defining ghost atoms around domain
|
|
|
|
* only creates mapping and coordinate corrections
|
|
|
|
* that are then enforced in updatePbc */
|
2021-10-26 12:34:35 +02:00
|
|
|
#define ADDGHOST(dx,dy,dz) \
|
|
|
|
Nghost++; \
|
2021-11-03 00:57:24 +01:00
|
|
|
border_map[Nghost] = i; \
|
2021-10-26 12:34:35 +02:00
|
|
|
PBCx[Nghost] = dx; \
|
|
|
|
PBCy[Nghost] = dy; \
|
|
|
|
PBCz[Nghost] = dz; \
|
|
|
|
atom->type[atom->Nlocal + Nghost] = atom->type[i]
|
|
|
|
|
2022-07-08 02:30:03 +02:00
|
|
|
void setupPbc(Atom *atom, Parameter *param) {
|
2021-11-03 00:57:24 +01:00
|
|
|
int *border_map = atom->border_map;
|
2020-11-05 12:41:44 +01:00
|
|
|
MD_FLOAT xprd = param->xprd;
|
|
|
|
MD_FLOAT yprd = param->yprd;
|
|
|
|
MD_FLOAT zprd = param->zprd;
|
|
|
|
MD_FLOAT Cutneigh = param->cutneigh;
|
2020-08-18 14:27:28 +02:00
|
|
|
int Nghost = -1;
|
|
|
|
|
|
|
|
for(int i = 0; i < atom->Nlocal; i++) {
|
|
|
|
if (atom->Nlocal + Nghost + 7 >= atom->Nmax) {
|
|
|
|
growAtom(atom);
|
|
|
|
}
|
2022-07-08 02:30:03 +02:00
|
|
|
|
2020-08-18 14:27:28 +02:00
|
|
|
if (Nghost + 7 >= NmaxGhost) {
|
2021-11-03 00:57:24 +01:00
|
|
|
growPbc(atom);
|
|
|
|
border_map = atom->border_map;
|
2020-08-18 14:27:28 +02:00
|
|
|
}
|
|
|
|
|
2021-03-23 09:26:41 +01:00
|
|
|
MD_FLOAT x = atom_x(i);
|
|
|
|
MD_FLOAT y = atom_y(i);
|
|
|
|
MD_FLOAT z = atom_z(i);
|
2021-03-20 18:32:50 +01:00
|
|
|
|
2020-08-18 14:27:28 +02:00
|
|
|
/* Setup ghost atoms */
|
|
|
|
/* 6 planes */
|
2022-07-08 02:30:03 +02:00
|
|
|
if(param->pbc_x != 0) {
|
|
|
|
if (x < Cutneigh) { ADDGHOST(+1,0,0); }
|
|
|
|
if (x >= (xprd-Cutneigh)) { ADDGHOST(-1,0,0); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if(param->pbc_y != 0) {
|
|
|
|
if (y < Cutneigh) { ADDGHOST(0,+1,0); }
|
|
|
|
if (y >= (yprd-Cutneigh)) { ADDGHOST(0,-1,0); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if(param->pbc_z != 0) {
|
|
|
|
if (z < Cutneigh) { ADDGHOST(0,0,+1); }
|
|
|
|
if (z >= (zprd-Cutneigh)) { ADDGHOST(0,0,-1); }
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:27:28 +02:00
|
|
|
/* 8 corners */
|
2022-07-08 02:30:03 +02:00
|
|
|
if(param->pbc_x != 0 && param->pbc_y != 0 && param->pbc_z != 0) {
|
|
|
|
if (x < Cutneigh && y < Cutneigh && z < Cutneigh) { ADDGHOST(+1,+1,+1); }
|
|
|
|
if (x < Cutneigh && y >= (yprd-Cutneigh) && z < Cutneigh) { ADDGHOST(+1,-1,+1); }
|
2023-10-10 12:53:43 +02:00
|
|
|
if (x < Cutneigh && y < Cutneigh && z >= (zprd-Cutneigh)) { ADDGHOST(+1,+1,-1); }
|
2022-07-08 02:30:03 +02:00
|
|
|
if (x < Cutneigh && y >= (yprd-Cutneigh) && z >= (zprd-Cutneigh)) { ADDGHOST(+1,-1,-1); }
|
|
|
|
if (x >= (xprd-Cutneigh) && y < Cutneigh && z < Cutneigh) { ADDGHOST(-1,+1,+1); }
|
|
|
|
if (x >= (xprd-Cutneigh) && y >= (yprd-Cutneigh) && z < Cutneigh) { ADDGHOST(-1,-1,+1); }
|
|
|
|
if (x >= (xprd-Cutneigh) && y < Cutneigh && z >= (zprd-Cutneigh)) { ADDGHOST(-1,+1,-1); }
|
|
|
|
if (x >= (xprd-Cutneigh) && y >= (yprd-Cutneigh) && z >= (zprd-Cutneigh)) { ADDGHOST(-1,-1,-1); }
|
|
|
|
}
|
|
|
|
|
2020-08-18 14:27:28 +02:00
|
|
|
/* 12 edges */
|
2022-07-08 02:30:03 +02:00
|
|
|
if(param->pbc_x != 0 && param->pbc_z != 0) {
|
|
|
|
if (x < Cutneigh && z < Cutneigh) { ADDGHOST(+1,0,+1); }
|
|
|
|
if (x < Cutneigh && z >= (zprd-Cutneigh)) { ADDGHOST(+1,0,-1); }
|
|
|
|
if (x >= (xprd-Cutneigh) && z < Cutneigh) { ADDGHOST(-1,0,+1); }
|
|
|
|
if (x >= (xprd-Cutneigh) && z >= (zprd-Cutneigh)) { ADDGHOST(-1,0,-1); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if(param->pbc_y != 0 && param->pbc_z != 0) {
|
|
|
|
if (y < Cutneigh && z < Cutneigh) { ADDGHOST(0,+1,+1); }
|
|
|
|
if (y < Cutneigh && z >= (zprd-Cutneigh)) { ADDGHOST(0,+1,-1); }
|
|
|
|
if (y >= (yprd-Cutneigh) && z < Cutneigh) { ADDGHOST(0,-1,+1); }
|
|
|
|
if (y >= (yprd-Cutneigh) && z >= (zprd-Cutneigh)) { ADDGHOST(0,-1,-1); }
|
|
|
|
}
|
|
|
|
|
|
|
|
if(param->pbc_x != 0 && param->pbc_y != 0) {
|
|
|
|
if (y < Cutneigh && x < Cutneigh) { ADDGHOST(+1,+1,0); }
|
|
|
|
if (y < Cutneigh && x >= (xprd-Cutneigh)) { ADDGHOST(-1,+1,0); }
|
|
|
|
if (y >= (yprd-Cutneigh) && x < Cutneigh) { ADDGHOST(+1,-1,0); }
|
|
|
|
if (y >= (yprd-Cutneigh) && x >= (xprd-Cutneigh)) { ADDGHOST(-1,-1,0); }
|
|
|
|
}
|
2020-08-18 14:27:28 +02:00
|
|
|
}
|
|
|
|
// increase by one to make it the ghost atom count
|
|
|
|
atom->Nghost = Nghost + 1;
|
|
|
|
}
|
2020-08-19 09:00:35 +02:00
|
|
|
|
|
|
|
/* internal subroutines */
|
2022-08-11 16:42:41 +02:00
|
|
|
void growPbc(Atom* atom) {
|
2020-08-19 09:00:35 +02:00
|
|
|
int nold = NmaxGhost;
|
|
|
|
NmaxGhost += DELTA;
|
|
|
|
|
2021-11-03 00:57:24 +01:00
|
|
|
atom->border_map = (int*) reallocate(atom->border_map, ALIGNMENT, NmaxGhost * sizeof(int), nold * sizeof(int));
|
2020-08-19 09:00:35 +02:00
|
|
|
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));
|
|
|
|
}
|