Compare commits
21 Commits
5a872d0533
...
main
Author | SHA1 | Date | |
---|---|---|---|
3408d39545
|
|||
9db5dd9cb4
|
|||
3b13ac202a
|
|||
34f65b8fc3
|
|||
b2f9d41582
|
|||
002275e633 | |||
84319b78be | |||
91b08c5ad3 | |||
dae6a8b4cf | |||
85543dee11 | |||
d6aa1cf113 | |||
1a1420890b | |||
be3db085af | |||
d81313f293 | |||
8091c714e2 | |||
b9bf4d7b63 | |||
a834a57bf8 | |||
7ce84e2773 | |||
6f3d9e73ef | |||
ed929ab752 | |||
1cb82b1bfa |
@@ -22,6 +22,7 @@ SRC = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c)
|
||||
ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s, $(SRC))
|
||||
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
|
||||
OBJ += $(BUILD_DIR)/comm-$(COMM_TYPE).o
|
||||
OBJ += $(BUILD_DIR)/solver-$(SOLVER).o
|
||||
SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h)
|
||||
CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(OPTIONS) $(INCLUDES)
|
||||
|
||||
@@ -38,9 +39,20 @@ $(BUILD_DIR)/%.s: %.c
|
||||
$(info ===> GENERATE ASM $@)
|
||||
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONY: clean distclean tags info asm format
|
||||
.PHONY: clean distclean vis vis_clean tags info asm format
|
||||
|
||||
clean:
|
||||
vis:
|
||||
$(info ===> GENERATE VISUALIZATION)
|
||||
@gnuplot -e "filename='pressure.dat'" ./surface.plot
|
||||
@gnuplot -e "filename='velocity.dat'" ./vector.plot
|
||||
@gnuplot -e "filename='residual.dat'" ./residual.plot
|
||||
|
||||
vis_clean:
|
||||
$(info ===> CLEAN VISUALIZATION)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
|
||||
clean: vis_clean
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -f tags
|
||||
|
@@ -7,10 +7,10 @@
|
||||
|
||||
name canal # name of flow setup
|
||||
|
||||
bcN 1 # flags for boundary conditions
|
||||
bcE 3 # 1 = no-slip 3 = outflow
|
||||
bcS 1 # 2 = free-slip 4 = periodic
|
||||
bcW 3 #
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
@@ -27,15 +27,22 @@ p_init 0.0 # initial value for pressure
|
||||
xlength 30.0 # domain size in x-direction
|
||||
ylength 4.0 # domain size in y-direction
|
||||
imax 200 # number of interior cells in x-direction
|
||||
jmax 50 # number of interior cells in y-direction
|
||||
jmax 40 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 100.0 # final time
|
||||
te 60.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 2 # Multigrid levels
|
||||
presmooth 5 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
|
@@ -1,7 +1,11 @@
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= CLANG
|
||||
# Supported: GCC, CLANG, ICX
|
||||
TAG ?= ICX
|
||||
# Supported: true, false
|
||||
ENABLE_MPI ?= true
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: rb, mg
|
||||
SOLVER ?= mg
|
||||
# Supported: v1, v2, v3
|
||||
COMM_TYPE ?= v3
|
||||
|
||||
#Feature options
|
||||
|
@@ -26,21 +26,28 @@ p_init 0.0 # initial value for pressure
|
||||
|
||||
xlength 1.0 # domain size in x-direction
|
||||
ylength 1.0 # domain size in y-direction
|
||||
imax 80 # number of interior cells in x-direction
|
||||
jmax 80 # number of interior cells in y-direction
|
||||
imax 128 # number of interior cells in x-direction
|
||||
jmax 128 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 2 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 1000 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
omg 1.9 # relaxation parameter for SOR iteration
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
#===============================================================================
|
||||
|
@@ -1,8 +1,8 @@
|
||||
ifeq ($(ENABLE_MPI),true)
|
||||
CC = mpiicc
|
||||
CC = mpiicx
|
||||
DEFINES = -D_MPI
|
||||
else
|
||||
CC = icc
|
||||
CC = icx
|
||||
endif
|
||||
|
||||
GCC = gcc
|
||||
@@ -13,7 +13,7 @@ OPENMP = -qopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP)
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP) -Wno-unused-command-line-argument
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES += -D_GNU_SOURCE# -DDEBUG
|
||||
INCLUDES =
|
9
BasicSolver/2D-mpi/residual.plot
Normal file
9
BasicSolver/2D-mpi/residual.plot
Normal file
@@ -0,0 +1,9 @@
|
||||
set terminal png size 1800,768 enhanced font ,12
|
||||
set output 'residual.png'
|
||||
set datafile separator whitespace
|
||||
set xlabel "Timestep"
|
||||
set ylabel "Residual"
|
||||
|
||||
set logscale y 2
|
||||
|
||||
plot 'residual.dat' using 1:2 title "Residual"
|
@@ -4,23 +4,12 @@
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "comm.h"
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int position)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
static void gatherArray(
|
||||
Comm* c, int cnt, int* rcvCounts, int* displs, double* src, double* dst)
|
||||
{
|
||||
@@ -41,6 +30,52 @@ static void gatherArray(
|
||||
MPI_COMM_WORLD);
|
||||
}
|
||||
#endif // defined _MPI
|
||||
void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
double* pg,
|
||||
double* u,
|
||||
double* v,
|
||||
double* p,
|
||||
int imax,
|
||||
int jmax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int *rcvCounts, *displs;
|
||||
int cnt = c->jmaxLocal * (imax + 2);
|
||||
|
||||
if (c->rank == 0) {
|
||||
rcvCounts = (int*)malloc(c->size * sizeof(int));
|
||||
displs = (int*)malloc(c->size * sizeof(int));
|
||||
for (int i = 0; i < c->size; ++i) {
|
||||
rcvCounts[i] = 0;
|
||||
displs[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (c->rank == 0 && c->size == 1) {
|
||||
cnt = (c->jmaxLocal + 2) * (imax + 2);
|
||||
} else if (c->rank == 0 || c->rank == (c->size - 1)) {
|
||||
cnt = (c->jmaxLocal + 1) * (imax + 2);
|
||||
}
|
||||
|
||||
MPI_Gather(&cnt, 1, MPI_INTEGER, rcvCounts, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);
|
||||
|
||||
if (c->rank == 0) {
|
||||
displs[0] = 0;
|
||||
int cursor = rcvCounts[0];
|
||||
|
||||
for (int i = 1; i < c->size; i++) {
|
||||
displs[i] = cursor;
|
||||
cursor += rcvCounts[i];
|
||||
}
|
||||
}
|
||||
|
||||
gatherArray(c, cnt, rcvCounts, displs, p, pg);
|
||||
gatherArray(c, cnt, rcvCounts, displs, u, ug);
|
||||
gatherArray(c, cnt, rcvCounts, displs, v, vg);
|
||||
#endif
|
||||
}
|
||||
|
||||
// exported subroutines
|
||||
int commIsBoundary(Comm* c, int direction)
|
||||
@@ -67,6 +102,7 @@ int commIsBoundary(Comm* c, int direction)
|
||||
|
||||
void commExchange(Comm* c, double* grid)
|
||||
{
|
||||
// printf("Rank : %d In exchange \n", c->rank);
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[4] = { MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
@@ -126,56 +162,65 @@ void commShift(Comm* c, double* f, double* g)
|
||||
#endif
|
||||
}
|
||||
|
||||
void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
double* pg,
|
||||
double* u,
|
||||
double* v,
|
||||
double* p,
|
||||
int jmax,
|
||||
int imax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int *rcvCounts, *displs;
|
||||
int cnt = c->jmaxLocal * (imax + 2);
|
||||
|
||||
if (c->rank == 0) {
|
||||
rcvCounts = (int*)malloc(c->size * sizeof(int));
|
||||
displs = (int*)malloc(c->size * sizeof(int));
|
||||
}
|
||||
|
||||
if (c->rank == 0 && c->size == 1) {
|
||||
cnt = (c->jmaxLocal + 2) * (imax + 2);
|
||||
} else if (c->rank == 0 || c->rank == (c->size - 1)) {
|
||||
cnt = (c->jmaxLocal + 1) * (imax + 2);
|
||||
}
|
||||
|
||||
MPI_Gather(&cnt, 1, MPI_INTEGER, rcvCounts, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);
|
||||
|
||||
if (c->rank == 0) {
|
||||
displs[0] = 0;
|
||||
int cursor = rcvCounts[0];
|
||||
|
||||
for (int i = 1; i < c->size; i++) {
|
||||
displs[i] = cursor;
|
||||
cursor += rcvCounts[i];
|
||||
}
|
||||
}
|
||||
|
||||
gatherArray(c, cnt, rcvCounts, displs, p, pg);
|
||||
gatherArray(c, cnt, rcvCounts, displs, u, ug);
|
||||
gatherArray(c, cnt, rcvCounts, displs, v, vg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commPartition(Comm* c, int jmax, int imax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = sizeOfRank(c->rank, c->size, jmax);
|
||||
c->jmaxLocal = sizeOfRank(c->coords[JDIM], c->size, jmax);
|
||||
|
||||
c->neighbours[BOTTOM] = c->rank == 0 ? -1 : c->rank - 1;
|
||||
c->neighbours[TOP] = c->rank == (c->size - 1) ? -1 : c->rank + 1;
|
||||
c->neighbours[LEFT] = -1;
|
||||
c->neighbours[RIGHT] = -1;
|
||||
|
||||
c->coords[IDIM] = 0;
|
||||
c->coords[JDIM] = c->rank;
|
||||
|
||||
c->dims[IDIM] = 1;
|
||||
c->dims[JDIM] = c->size;
|
||||
#else
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = jmax;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
|
||||
#if defined _MPI
|
||||
newcomm->comm = MPI_COMM_NULL;
|
||||
int result = MPI_Comm_dup(MPI_COMM_WORLD, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
|
||||
newcomm->neighbours[BOTTOM] = newcomm->rank == 0 ? -1 : newcomm->rank - 1;
|
||||
newcomm->neighbours[TOP] = newcomm->rank == (newcomm->size - 1) ? -1
|
||||
: newcomm->rank + 1;
|
||||
newcomm->neighbours[LEFT] = -1;
|
||||
newcomm->neighbours[RIGHT] = -1;
|
||||
|
||||
newcomm->coords[IDIM] = 0;
|
||||
newcomm->coords[JDIM] = newcomm->rank;
|
||||
|
||||
newcomm->dims[IDIM] = 1;
|
||||
newcomm->dims[JDIM] = newcomm->size;
|
||||
|
||||
#endif
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
@@ -4,18 +4,18 @@
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "comm.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "comm.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int position)
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
@@ -79,8 +79,8 @@ static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax
|
||||
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
|
||||
int coords[NDIMS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
int starts[NDIMS] = { sum(newSizesJ, coords[JDIM]),
|
||||
sum(newSizesI, coords[IDIM]) };
|
||||
int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
|
||||
sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
|
||||
printf(
|
||||
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
|
||||
"Starts(i,j): %d %d\n",
|
||||
@@ -252,8 +252,8 @@ void commPartition(Comm* c, int jmax, int imax)
|
||||
MPI_Cart_shift(c->comm, JDIM, 1, &c->neighbours[BOTTOM], &c->neighbours[TOP]);
|
||||
MPI_Cart_get(c->comm, NDIMS, c->dims, periods, c->coords);
|
||||
|
||||
int imaxLocal = sizeOfRank(c->rank, dims[IDIM], imax);
|
||||
int jmaxLocal = sizeOfRank(c->rank, dims[JDIM], jmax);
|
||||
int imaxLocal = sizeOfRank(c->coords[IDIM], dims[IDIM], imax);
|
||||
int jmaxLocal = sizeOfRank(c->coords[JDIM], dims[JDIM], jmax);
|
||||
|
||||
c->imaxLocal = imaxLocal;
|
||||
c->jmaxLocal = jmaxLocal;
|
||||
@@ -285,3 +285,58 @@ void commPartition(Comm* c, int jmax, int imax)
|
||||
c->jmaxLocal = jmax;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
#if defined _MPI
|
||||
newcomm->comm = MPI_COMM_NULL;
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
memcpy(&newcomm->neighbours, &oldcomm->neighbours, sizeof(oldcomm->neighbours));
|
||||
memcpy(&newcomm->coords, &oldcomm->coords, sizeof(oldcomm->coords));
|
||||
memcpy(&newcomm->dims, &oldcomm->dims, sizeof(oldcomm->dims));
|
||||
|
||||
newcomm->imaxLocal = imaxLocal/2;
|
||||
newcomm->jmaxLocal = jmaxLocal/2;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
MPI_Type_commit(&jBufferType);
|
||||
|
||||
MPI_Datatype iBufferType;
|
||||
MPI_Type_vector(jmaxLocal, 1, imaxLocal + 2, MPI_DOUBLE, &iBufferType);
|
||||
MPI_Type_commit(&iBufferType);
|
||||
|
||||
newcomm->bufferTypes[LEFT] = iBufferType;
|
||||
newcomm->bufferTypes[RIGHT] = iBufferType;
|
||||
newcomm->bufferTypes[BOTTOM] = jBufferType;
|
||||
newcomm->bufferTypes[TOP] = jBufferType;
|
||||
|
||||
newcomm->sdispls[LEFT] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[RIGHT] = (imaxLocal + 2) + imaxLocal;
|
||||
newcomm->sdispls[BOTTOM] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[TOP] = jmaxLocal * (imaxLocal + 2) + 1;
|
||||
|
||||
newcomm->rdispls[LEFT] = (imaxLocal + 2);
|
||||
newcomm->rdispls[RIGHT] = (imaxLocal + 2) + (imaxLocal + 1);
|
||||
newcomm->rdispls[BOTTOM] = 1;
|
||||
newcomm->rdispls[TOP] = (jmaxLocal + 1) * (imaxLocal + 2) + 1;
|
||||
#else
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
@@ -11,11 +11,11 @@
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int position)
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
@@ -79,8 +79,8 @@ static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax
|
||||
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
|
||||
int coords[NDIMS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
int starts[NDIMS] = { sum(newSizesJ, coords[JDIM]),
|
||||
sum(newSizesI, coords[IDIM]) };
|
||||
int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
|
||||
sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
|
||||
printf(
|
||||
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
|
||||
"Starts(i,j): %d %d\n",
|
||||
@@ -139,7 +139,6 @@ void commExchange(Comm* c, double* grid)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int counts[NDIRS] = { 1, 1, 1, 1 };
|
||||
|
||||
MPI_Neighbor_alltoallw(grid,
|
||||
counts,
|
||||
c->sdispls,
|
||||
@@ -233,8 +232,8 @@ void commPartition(Comm* c, int jmax, int imax)
|
||||
MPI_Cart_shift(c->comm, JDIM, 1, &c->neighbours[BOTTOM], &c->neighbours[TOP]);
|
||||
MPI_Cart_get(c->comm, NDIMS, c->dims, periods, c->coords);
|
||||
|
||||
int imaxLocal = sizeOfRank(c->rank, dims[IDIM], imax);
|
||||
int jmaxLocal = sizeOfRank(c->rank, dims[JDIM], jmax);
|
||||
int imaxLocal = sizeOfRank(c->coords[IDIM], dims[IDIM], imax);
|
||||
int jmaxLocal = sizeOfRank(c->coords[JDIM], dims[JDIM], jmax);
|
||||
|
||||
c->imaxLocal = imaxLocal;
|
||||
c->jmaxLocal = jmaxLocal;
|
||||
@@ -267,3 +266,55 @@ void commPartition(Comm* c, int jmax, int imax)
|
||||
c->jmaxLocal = jmax;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
#if defined _MPI
|
||||
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
MPI_Type_commit(&jBufferType);
|
||||
|
||||
MPI_Datatype iBufferType;
|
||||
MPI_Type_vector(jmaxLocal, 1, imaxLocal + 2, MPI_DOUBLE, &iBufferType);
|
||||
MPI_Type_commit(&iBufferType);
|
||||
|
||||
newcomm->bufferTypes[LEFT] = iBufferType;
|
||||
newcomm->bufferTypes[RIGHT] = iBufferType;
|
||||
newcomm->bufferTypes[BOTTOM] = jBufferType;
|
||||
newcomm->bufferTypes[TOP] = jBufferType;
|
||||
|
||||
newcomm->sdispls[LEFT] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[RIGHT] = (imaxLocal + 2) + imaxLocal;
|
||||
newcomm->sdispls[BOTTOM] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[TOP] = jmaxLocal * (imaxLocal + 2) + 1;
|
||||
|
||||
newcomm->rdispls[LEFT] = (imaxLocal + 2);
|
||||
newcomm->rdispls[RIGHT] = (imaxLocal + 2) + (imaxLocal + 1);
|
||||
newcomm->rdispls[BOTTOM] = 1;
|
||||
newcomm->rdispls[TOP] = (jmaxLocal + 1) * (imaxLocal + 2) + 1;
|
||||
#else
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
@@ -41,6 +41,8 @@ extern void commExchange(Comm*, double*);
|
||||
extern void commShift(Comm* c, double* f, double* g);
|
||||
extern void commReduction(double* v, int op);
|
||||
extern int commIsBoundary(Comm* c, int direction);
|
||||
extern void commUpdateDatatypes(Comm*, Comm*, int, int);
|
||||
extern void commFreeCommunicator(Comm*);
|
||||
extern void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
|
@@ -273,17 +273,18 @@ void setSpecialBoundaryCondition(Discretization* s)
|
||||
if (commIsBoundary(&s->comm, LEFT)) {
|
||||
double ylength = s->grid.ylength;
|
||||
double dy = s->grid.dy;
|
||||
int rest = s->grid.jmax % s->comm.size;
|
||||
int yc = s->comm.rank * (s->grid.jmax / s->comm.size) +
|
||||
int rest = s->grid.jmax % s->comm.dims[JDIM];
|
||||
int yc = s->comm.rank * (s->grid.jmax / s->comm.dims[JDIM]) +
|
||||
MIN(rest, s->comm.rank);
|
||||
double ys = dy * (yc + 0.5);
|
||||
double y;
|
||||
|
||||
/* printf("RANK %d yc: %d ys: %f\n", solver->rank, yc, ys); */
|
||||
// printf("RANK %d yc: %d ys: %f\n", s->comm.rank, yc, ys);
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
y = ys + dy * (j - 0.5);
|
||||
U(0, j) = y * (ylength - y) * 4.0 / (ylength * ylength);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,24 @@
|
||||
#include "solver.h"
|
||||
#include "timing.h"
|
||||
|
||||
static FILE* initResidualWriter()
|
||||
{
|
||||
FILE* fp;
|
||||
fp = fopen("residual.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return fp;
|
||||
}
|
||||
|
||||
static void writeResidual(FILE* fp, double ts, double res)
|
||||
{
|
||||
fprintf(fp, "%f, %f\n", ts, res);
|
||||
}
|
||||
|
||||
static void writeResults(Discretization* s)
|
||||
{
|
||||
#ifdef _MPI
|
||||
@@ -26,7 +44,9 @@ static void writeResults(Discretization* s)
|
||||
double* pg = allocate(64, bytesize);
|
||||
|
||||
commCollectResult(&s->comm, ug, vg, pg, s->u, s->v, s->p, s->grid.imax, s->grid.jmax);
|
||||
writeResult(s, ug, vg, pg);
|
||||
if (commIsMaster(&s->comm)) {
|
||||
writeResult(s, ug, vg, pg);
|
||||
}
|
||||
|
||||
free(ug);
|
||||
free(vg);
|
||||
@@ -47,6 +67,9 @@ int main(int argc, char** argv)
|
||||
commInit(&d.comm, argc, argv);
|
||||
initParameter(&p);
|
||||
|
||||
FILE* fp;
|
||||
if (commIsMaster(&d.comm)) fp = initResidualWriter();
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <configFile>\n", argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
@@ -77,16 +100,21 @@ int main(int argc, char** argv)
|
||||
double tau = d.tau;
|
||||
double te = d.te;
|
||||
double t = 0.0;
|
||||
double res = 0.0;
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
while (t <= te) {
|
||||
|
||||
if (tau > 0.0) computeTimestep(&d);
|
||||
setBoundaryConditions(&d);
|
||||
setSpecialBoundaryCondition(&d);
|
||||
computeFG(&d);
|
||||
computeRHS(&d);
|
||||
solve(&s, d.p, d.rhs);
|
||||
res = solve(&s, d.p, d.rhs);
|
||||
adaptUV(&d);
|
||||
|
||||
if (commIsMaster(&d.comm)) writeResidual(fp, t, res);
|
||||
|
||||
t += d.dt;
|
||||
|
||||
#ifdef VERBOSE
|
||||
@@ -104,7 +132,7 @@ int main(int argc, char** argv)
|
||||
if (commIsMaster(s.comm)) {
|
||||
printf("Solution took %.2fs\n", timeStop - timeStart);
|
||||
}
|
||||
|
||||
if (commIsMaster(&d.comm)) fclose(fp);
|
||||
writeResults(&d);
|
||||
commFinalize(s.comm);
|
||||
return EXIT_SUCCESS;
|
||||
|
@@ -14,13 +14,16 @@
|
||||
|
||||
void initParameter(Parameter* param)
|
||||
{
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.8;
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.8;
|
||||
param->levels = 5;
|
||||
param->presmooth = 5;
|
||||
param->postsmooth = 5;
|
||||
}
|
||||
|
||||
void readParameter(Parameter* param, const char* filename)
|
||||
@@ -72,6 +75,9 @@ void readParameter(Parameter* param, const char* filename)
|
||||
PARSE_INT(bcRight);
|
||||
PARSE_INT(bcBottom);
|
||||
PARSE_INT(bcTop);
|
||||
PARSE_INT(levels);
|
||||
PARSE_INT(presmooth);
|
||||
PARSE_INT(postsmooth);
|
||||
PARSE_REAL(u_init);
|
||||
PARSE_REAL(v_init);
|
||||
PARSE_REAL(p_init);
|
||||
|
@@ -18,6 +18,7 @@ typedef struct {
|
||||
char* name;
|
||||
int bcLeft, bcRight, bcBottom, bcTop;
|
||||
double u_init, v_init, p_init;
|
||||
int levels, presmooth, postsmooth;
|
||||
} Parameter;
|
||||
|
||||
void initParameter(Parameter*);
|
||||
|
@@ -8,7 +8,7 @@
|
||||
#include <mpi.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "progress.h"
|
||||
|
||||
static double _end;
|
||||
|
@@ -10,5 +10,4 @@
|
||||
extern void initProgress(double);
|
||||
extern void printProgress(double);
|
||||
extern void stopProgress();
|
||||
|
||||
#endif
|
||||
|
303
BasicSolver/2D-mpi/src/solver-mg.c
Normal file
303
BasicSolver/2D-mpi/src/solver-mg.c
Normal file
@@ -0,0 +1,303 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
#define FINEST_LEVEL 0
|
||||
#define COARSEST_LEVEL (s->levels - 1)
|
||||
// #define S(i, j) s[(j) * (imaxLocal + 2) + (i)]
|
||||
#define E(i, j) e[(j) * (imaxLocal + 2) + (i)]
|
||||
#define R(i, j) r[(j) * (imaxLocal + 2) + (i)]
|
||||
#define OLD(i, j) old[(j) * (imaxLocal + 2) + (i)]
|
||||
|
||||
static void restrictMG(Solver* s, int level, Comm* comm)
|
||||
{
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
|
||||
double* r = s->r[level + 1];
|
||||
double* old = s->r[level];
|
||||
|
||||
#ifdef _MPI
|
||||
commExchange(comm, old);
|
||||
#endif
|
||||
|
||||
for (int j = 1; j < (jmaxLocal / 2) + 1; j++) {
|
||||
for (int i = 1; i < (imaxLocal / 2) + 1; i++) {
|
||||
R(i, j) = (OLD(2 * i - 1, 2 * j - 1) + OLD(2 * i, 2 * j - 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1) + OLD(2 * i - 1, 2 * j) * 2 +
|
||||
OLD(2 * i, 2 * j) * 4 + OLD(2 * i + 1, 2 * j) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1) + OLD(2 * i, 2 * j + 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1)) /
|
||||
16.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void prolongate(Solver* s, int level, Comm* comm)
|
||||
{
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
|
||||
double* old = s->r[level + 1];
|
||||
double* e = s->r[level];
|
||||
|
||||
for (int j = 2; j < jmaxLocal + 1; j += 2) {
|
||||
for (int i = 2; i < imaxLocal + 1; i += 2) {
|
||||
E(i, j) = OLD(i / 2, j / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void correct(Solver* s, double* p, int level, Comm* comm)
|
||||
{
|
||||
double* e = s->e[level];
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; ++j) {
|
||||
for (int i = 1; i < imaxLocal + 1; ++i) {
|
||||
P(i, j) += E(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void setBoundaryCondition(Solver* s, double* p, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
#ifdef _MPI
|
||||
if (commIsBoundary(s->comm, BOTTOM)) { // set bottom bc
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, 0) = P(i, 1);
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, TOP)) { // set top bc
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, jmaxLocal + 1) = P(i, jmaxLocal);
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, LEFT)) { // set left bc
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(0, j) = P(1, j);
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, RIGHT)) { // set right bc
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(imaxLocal + 1, j) = P(imaxLocal, j);
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, 0) = P(i, 1);
|
||||
P(i, jmaxLocal + 1) = P(i, jmaxLocal);
|
||||
}
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(0, j) = P(1, j);
|
||||
P(imaxLocal + 1, j) = P(imaxLocal, j);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void smooth(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double* r = s->r[level];
|
||||
|
||||
double res = 1.0;
|
||||
int pass, jsw, isw;
|
||||
|
||||
jsw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
isw = jsw;
|
||||
|
||||
#ifdef _MPI
|
||||
commExchange(comm, p);
|
||||
#endif
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = isw; i < imaxLocal + 1; i += 2) {
|
||||
|
||||
P(i, j) -= factor *
|
||||
(RHS(i, j) -
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
}
|
||||
|
||||
static double calculateResidual(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double* r = s->r[level];
|
||||
double res = 1.0;
|
||||
int pass, jsw, isw;
|
||||
|
||||
jsw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
isw = jsw;
|
||||
|
||||
#ifdef _MPI
|
||||
commExchange(comm, p);
|
||||
#endif
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = isw; i < imaxLocal + 1; i += 2) {
|
||||
|
||||
R(i, j) = RHS(i, j) -
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
|
||||
|
||||
res += (R(i, j) * R(i, j));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
|
||||
#ifdef _MPI
|
||||
commReduction(&res, SUM);
|
||||
#endif
|
||||
|
||||
res = res / (double)(imax * jmax);
|
||||
#ifdef DEBUG
|
||||
if (commIsMaster(s->comm)) {
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
}
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
|
||||
static double multiGrid(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
// coarsest level
|
||||
if (level == COARSEST_LEVEL) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
smooth(s, p, rhs, level, comm);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// pre-smoothing
|
||||
for (int i = 0; i < s->presmooth; i++) {
|
||||
smooth(s, p, rhs, level, comm);
|
||||
if (level == FINEST_LEVEL)
|
||||
setBoundaryCondition(s, p, comm->imaxLocal, comm->jmaxLocal);
|
||||
}
|
||||
|
||||
// calculate residuals
|
||||
res = calculateResidual(s, p, rhs, level, comm);
|
||||
|
||||
// restrict
|
||||
restrictMG(s, level, comm);
|
||||
|
||||
// Create a new comm object withupdated imaxLocal and jmaxLocal
|
||||
// along with their updated bufferTypes, sdispls, rdispls
|
||||
Comm newcomm;
|
||||
commUpdateDatatypes(s->comm, &newcomm, comm->imaxLocal, comm->jmaxLocal);
|
||||
|
||||
// MGSolver on residual and error.
|
||||
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm);
|
||||
|
||||
commFreeCommunicator(&newcomm);
|
||||
|
||||
// prolongate
|
||||
prolongate(s, level, comm);
|
||||
|
||||
// correct p on finer level using residual
|
||||
correct(s, p, level, comm);
|
||||
|
||||
if (level == FINEST_LEVEL)
|
||||
setBoundaryCondition(s, p, comm->imaxLocal, comm->jmaxLocal);
|
||||
|
||||
// post-smoothing
|
||||
for (int i = 0; i < s->postsmooth; i++) {
|
||||
smooth(s, p, rhs, level, comm);
|
||||
if (level == FINEST_LEVEL)
|
||||
setBoundaryCondition(s, p, comm->imaxLocal, comm->jmaxLocal);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
s->itermax = p->itermax;
|
||||
s->levels = p->levels;
|
||||
s->grid = &d->grid;
|
||||
s->comm = &d->comm;
|
||||
s->presmooth = p->presmooth;
|
||||
s->postsmooth = p->postsmooth;
|
||||
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int levels = s->levels;
|
||||
printf("Using Multigrid solver with %d levels\n", levels);
|
||||
|
||||
s->r = malloc(levels * sizeof(double*));
|
||||
s->e = malloc(levels * sizeof(double*));
|
||||
|
||||
size_t size = (imax + 2) * (jmax + 2) * sizeof(double);
|
||||
|
||||
for (int j = 0; j < levels; j++) {
|
||||
s->r[j] = allocate(64, size);
|
||||
s->e[j] = allocate(64, size);
|
||||
|
||||
for (int i = 0; i < (imax + 2) * (jmax + 2); i++) {
|
||||
s->r[j][i] = 0.0;
|
||||
s->e[j][i] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
double res = multiGrid(s, p, rhs, 0, s->comm);
|
||||
|
||||
#ifdef VERBOSE
|
||||
if (commIsMaster(s->comm)) {
|
||||
printf("Residuum: %.6f\n", res);
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
@@ -23,7 +23,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
s->comm = &d->comm;
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
@@ -101,4 +101,6 @@ void solve(Solver* s, double* p, double* rhs)
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
@@ -9,6 +9,7 @@
|
||||
#include "comm.h"
|
||||
#include "discretization.h"
|
||||
#include "grid.h"
|
||||
#include "mpi.h"
|
||||
#include "parameter.h"
|
||||
|
||||
typedef struct {
|
||||
@@ -17,10 +18,12 @@ typedef struct {
|
||||
/* parameters */
|
||||
double eps, omega;
|
||||
int itermax;
|
||||
int levels, presmooth, postsmooth;
|
||||
double **r, **e;
|
||||
/* communication */
|
||||
Comm* comm;
|
||||
} Solver;
|
||||
|
||||
void initSolver(Solver*, Discretization*, Parameter*);
|
||||
void solve(Solver*, double*, double*);
|
||||
double solve(Solver*, double*, double*);
|
||||
#endif
|
||||
|
@@ -38,9 +38,22 @@ $(BUILD_DIR)/%.s: %.c
|
||||
$(info ===> GENERATE ASM $@)
|
||||
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONY: clean distclean tags info asm format
|
||||
.PHONY: clean distclean vis vis_clean tags info asm format
|
||||
|
||||
clean:
|
||||
vis:
|
||||
$(info ===> GENERATE VISUALIZATION)
|
||||
@gnuplot -e "filename='pressure.dat'" ./surface.plot
|
||||
@gnuplot -e "filename='velocity.dat'" ./vector.plot
|
||||
@gnuplot -e "filename='residual.dat'" ./residual.plot
|
||||
|
||||
vis_clean:
|
||||
$(info ===> CLEAN VISUALIZATION)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
@rm -f ./vis_files/*.dat
|
||||
@rm -f ./vis_files/*.gif
|
||||
|
||||
clean: vis_clean
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -f tags
|
||||
|
@@ -36,6 +36,13 @@ te 100.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 5 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
|
@@ -1,12 +1,12 @@
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= CLANG
|
||||
# Supported: GCC, CLANG, ICX
|
||||
TAG ?= ICX
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: sor, mg
|
||||
SOLVER ?= sor
|
||||
# Supported: sor, rb, mg
|
||||
SOLVER ?= mg
|
||||
# Run in debug settings
|
||||
DEBUG ?= false
|
||||
|
||||
#Feature options
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
#OPTIONS += -DVERBOSE
|
||||
OPTIONS += -DVERBOSE
|
||||
#OPTIONS += -DDEBUG
|
||||
|
@@ -26,8 +26,8 @@ p_init 0.0 # initial value for pressure
|
||||
|
||||
xlength 1.0 # domain size in x-direction
|
||||
ylength 1.0 # domain size in y-direction
|
||||
imax 100 # number of interior cells in x-direction
|
||||
jmax 100 # number of interior cells in y-direction
|
||||
imax 128 # number of interior cells in x-direction
|
||||
jmax 128 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
@@ -36,6 +36,13 @@ te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 2 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Solver Data:
|
||||
# -----------------------
|
||||
|
||||
@@ -44,5 +51,4 @@ eps 0.001 # stopping tolerance for pressure iteration
|
||||
rho 0.5
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
levels 5 # Multigrid levels
|
||||
#===============================================================================
|
||||
|
@@ -7,7 +7,7 @@ OPENMP = -qopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP)
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP) -Wno-unused-command-line-argument
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES = -D_GNU_SOURCE
|
||||
INCLUDES =
|
9
BasicSolver/2D-seq/residual.plot
Normal file
9
BasicSolver/2D-seq/residual.plot
Normal file
@@ -0,0 +1,9 @@
|
||||
set terminal png size 1800,768 enhanced font ,12
|
||||
set output 'residual.png'
|
||||
set datafile separator whitespace
|
||||
set xlabel "Timestep"
|
||||
set ylabel "Residual"
|
||||
|
||||
set logscale y 2
|
||||
|
||||
plot 'residual.dat' using 1:2 title "Residual"
|
@@ -14,13 +14,36 @@
|
||||
#include "solver.h"
|
||||
#include "timing.h"
|
||||
|
||||
static FILE* initResidualWriter()
|
||||
{
|
||||
FILE* fp;
|
||||
fp = fopen("residual.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return fp;
|
||||
|
||||
}
|
||||
|
||||
static void writeResidual(FILE* fp, double ts, double res)
|
||||
{
|
||||
fprintf(fp, "%f, %f\n", ts, res);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
double timeStart, timeStop;
|
||||
Parameter p;
|
||||
Discretization d;
|
||||
Solver s;
|
||||
|
||||
initParameter(&p);
|
||||
FILE* fp;
|
||||
fp = initResidualWriter();
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <configFile>\n", argv[0]);
|
||||
@@ -31,6 +54,7 @@ int main(int argc, char** argv)
|
||||
printParameter(&p);
|
||||
initDiscretization(&d, &p);
|
||||
initSolver(&s, &d, &p);
|
||||
|
||||
#ifndef VERBOSE
|
||||
initProgress(d.te);
|
||||
#endif
|
||||
@@ -39,8 +63,9 @@ int main(int argc, char** argv)
|
||||
double te = d.te;
|
||||
double t = 0.0;
|
||||
int nt = 0;
|
||||
double res = 0.0;
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
timeStart = getTimeStamp();
|
||||
while (t <= te) {
|
||||
if (tau > 0.0) computeTimestep(&d);
|
||||
setBoundaryConditions(&d);
|
||||
@@ -48,17 +73,21 @@ int main(int argc, char** argv)
|
||||
computeFG(&d);
|
||||
computeRHS(&d);
|
||||
if (nt % 100 == 0) normalizePressure(&d);
|
||||
solve(&s, d.p, d.rhs);
|
||||
res = solve(&s, d.p, d.rhs);
|
||||
adaptUV(&d);
|
||||
|
||||
writeResidual(fp, t, res);
|
||||
|
||||
t += d.dt;
|
||||
nt++;
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
|
||||
printf("TIME %f , TIMESTEP %f\n", t, d.dt);
|
||||
#else
|
||||
printProgress(t);
|
||||
#endif
|
||||
}
|
||||
fclose(fp);
|
||||
timeStop = getTimeStamp();
|
||||
stopProgress();
|
||||
printf("Solution took %.2fs\n", timeStop - timeStart);
|
||||
|
@@ -9,23 +9,23 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "parameter.h"
|
||||
#include "util.h"
|
||||
#define MAXLINE 4096
|
||||
|
||||
void initParameter(Parameter* param)
|
||||
{
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->imax = 128;
|
||||
param->jmax = 128;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.7;
|
||||
param->re = 100.0;
|
||||
param->gamma = 0.9;
|
||||
param->tau = 0.5;
|
||||
param->rho = 0.99;
|
||||
param->levels = 5;
|
||||
param->presmooth = 5;
|
||||
param->postsmooth = 5;
|
||||
}
|
||||
|
||||
void readParameter(Parameter* param, const char* filename)
|
||||
@@ -81,7 +81,8 @@ void readParameter(Parameter* param, const char* filename)
|
||||
PARSE_REAL(u_init);
|
||||
PARSE_REAL(v_init);
|
||||
PARSE_REAL(p_init);
|
||||
PARSE_REAL(rho);
|
||||
PARSE_INT(presmooth);
|
||||
PARSE_INT(postsmooth);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +113,5 @@ void printParameter(Parameter* param)
|
||||
printf("\tepsilon (stopping tolerance) : %f\n", param->eps);
|
||||
printf("\tgamma (stopping tolerance) : %f\n", param->gamma);
|
||||
printf("\tomega (SOR relaxation): %f\n", param->omg);
|
||||
printf("\trho (SOR relaxation): %f\n", param->rho);
|
||||
printf("\tMultiGrid levels : %d\n", param->levels);
|
||||
}
|
||||
|
@@ -18,6 +18,7 @@ typedef struct {
|
||||
char* name;
|
||||
int bcLeft, bcRight, bcBottom, bcTop;
|
||||
double u_init, v_init, p_init;
|
||||
int presmooth, postsmooth;
|
||||
} Parameter;
|
||||
|
||||
void initParameter(Parameter*);
|
||||
|
@@ -48,4 +48,4 @@ void stopProgress()
|
||||
{
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
@@ -9,6 +9,6 @@
|
||||
|
||||
extern void initProgress(double);
|
||||
extern void printProgress(double);
|
||||
extern void stopProgress();
|
||||
extern void stopProgress(void);
|
||||
|
||||
#endif
|
||||
|
@@ -8,16 +8,15 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
#define FINEST_LEVEL 0
|
||||
#define S(i, j) s[(j) * (imax + 2) + (i)]
|
||||
#define E(i, j) e[(j) * (imax + 2) + (i)]
|
||||
#define R(i, j) r[(j) * (imax + 2) + (i)]
|
||||
#define OLD(i, j) old[(j) * (imax + 2) + (i)]
|
||||
#define FINEST_LEVEL 0
|
||||
#define COARSEST_LEVEL (s->levels - 1)
|
||||
#define S(i, j) s[(j) * (imax + 2) + (i)]
|
||||
#define E(i, j) e[(j) * (imax + 2) + (i)]
|
||||
#define R(i, j) r[(j) * (imax + 2) + (i)]
|
||||
#define OLD(i, j) old[(j) * (imax + 2) + (i)]
|
||||
|
||||
static void restrictMG(Solver* s, int level, int imax, int jmax)
|
||||
{
|
||||
@@ -51,6 +50,7 @@ static void prolongate(Solver* s, int level, int imax, int jmax)
|
||||
static void correct(Solver* s, double* p, int level, int imax, int jmax)
|
||||
{
|
||||
double* e = s->e[level];
|
||||
|
||||
for (int j = 1; j < jmax + 1; ++j) {
|
||||
for (int i = 1; i < imax + 1; ++i) {
|
||||
P(i, j) += E(i, j);
|
||||
@@ -71,7 +71,38 @@ static void setBoundaryCondition(double* p, int imax, int jmax)
|
||||
}
|
||||
}
|
||||
|
||||
static double smooth(Solver* s, double* p, double* rhs, int level, int imax, int jmax)
|
||||
static void smooth(Solver* s, double* p, double* rhs, int level, int imax, int jmax)
|
||||
{
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double* r = s->r[level];
|
||||
double res = 1.0;
|
||||
int pass, jsw, isw;
|
||||
|
||||
jsw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
isw = jsw;
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
P(i, j) -= factor * (RHS(i, j) -
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2));
|
||||
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static double calculateResidual(Solver* s, double* p, double* rhs, int level, int imax, int jmax)
|
||||
{
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
@@ -94,7 +125,6 @@ static double smooth(Solver* s, double* p, double* rhs, int level, int imax, int
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
|
||||
|
||||
P(i, j) -= (factor * R(i, j));
|
||||
res += (R(i, j) * R(i, j));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
@@ -106,14 +136,57 @@ static double smooth(Solver* s, double* p, double* rhs, int level, int imax, int
|
||||
return res;
|
||||
}
|
||||
|
||||
static double multiGrid(Solver* s, double* p, double* rhs, int level, int imax, int jmax)
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
// coarsest level
|
||||
if (level == COARSEST_LEVEL) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
smooth(s, p, rhs, level, imax, jmax);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// pre-smoothing
|
||||
for (int i = 0; i < s->presmooth; i++) {
|
||||
smooth(s, p, rhs, level, imax, jmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
|
||||
}
|
||||
|
||||
res = calculateResidual(s, p, rhs, level, imax, jmax);
|
||||
|
||||
// restrict
|
||||
restrictMG(s, level, imax, jmax);
|
||||
|
||||
// MGSolver on residual and error.
|
||||
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, imax / 2, jmax / 2);
|
||||
|
||||
// prolongate
|
||||
prolongate(s, level, imax, jmax);
|
||||
|
||||
// correct p on finer level using residual
|
||||
correct(s, p, level, imax, jmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
|
||||
|
||||
// post-smoothing
|
||||
for (int i = 0; i < s->postsmooth; i++) {
|
||||
smooth(s, p, rhs, level, imax, jmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
s->itermax = p->itermax;
|
||||
s->rho = p->rho;
|
||||
s->levels = p->levels;
|
||||
s->grid = &d->grid;
|
||||
s->presmooth = p->presmooth;
|
||||
s->postsmooth = p->postsmooth;
|
||||
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
@@ -136,57 +209,13 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
}
|
||||
}
|
||||
|
||||
double multiGrid(Solver* solver, double* p, double* rhs, int level, int imax, int jmax)
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
// coarsest level TODO: Use direct solver?
|
||||
if (level == (solver->levels - 1)) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
smooth(solver, p, rhs, level, imax, jmax);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// pre-smoothing TODO: Make smoothing steps configurable?
|
||||
for (int i = 0; i < 5; i++) {
|
||||
smooth(solver, p, rhs, level, imax, jmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
|
||||
}
|
||||
|
||||
// restrict
|
||||
restrictMG(solver, level, imax, jmax);
|
||||
|
||||
// MGSolver on residual and error.
|
||||
// TODO: What if there is a rest?
|
||||
multiGrid(solver,
|
||||
solver->e[level + 1],
|
||||
solver->r[level],
|
||||
level + 1,
|
||||
imax / 2,
|
||||
jmax / 2);
|
||||
|
||||
// prolongate
|
||||
prolongate(solver, level, imax, jmax);
|
||||
|
||||
// correct p on finer level using residual
|
||||
correct(solver, p, level, imax, jmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
|
||||
|
||||
// post-smoothing
|
||||
for (int i = 0; i < 5; i++) {
|
||||
res = smooth(solver, p, rhs, level, imax, jmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax);
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Residuum: %.6f\n", res);
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
78
BasicSolver/2D-seq/src/solver-rb.c
Normal file
78
BasicSolver/2D-seq/src/solver-rb.c
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->grid = &d->grid;
|
||||
s->itermax = p->itermax;
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
}
|
||||
|
||||
double solve(Solver* solver, double* p, double* rhs)
|
||||
{
|
||||
int imax = solver->grid->imax;
|
||||
int jmax = solver->grid->jmax;
|
||||
double eps = solver->eps;
|
||||
int itermax = solver->itermax;
|
||||
double dx2 = solver->grid->dx * solver->grid->dx;
|
||||
double dy2 = solver->grid->dy * solver->grid->dy;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
res = 0.0;
|
||||
jsw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
isw = jsw;
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
double r = RHS(i, j) -
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
|
||||
|
||||
P(i, j) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0) = P(i, 1);
|
||||
P(i, jmax + 1) = P(i, jmax);
|
||||
}
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j) = P(1, j);
|
||||
P(imax + 1, j) = P(imax, j);
|
||||
}
|
||||
|
||||
res = res / (double)(imax * jmax);
|
||||
#ifdef DEBUG
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
#endif
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
@@ -4,7 +4,6 @@
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "discretization.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
@@ -16,7 +15,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
s->omega = p->omg;
|
||||
}
|
||||
|
||||
void solveSOR(Solver* solver, double* p, double* rhs)
|
||||
double solve(Solver* solver, double* p, double* rhs)
|
||||
{
|
||||
int imax = solver->grid->imax;
|
||||
int jmax = solver->grid->jmax;
|
||||
@@ -66,64 +65,6 @@ void solveSOR(Solver* solver, double* p, double* rhs)
|
||||
#ifdef VERBOSE
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
#endif
|
||||
}
|
||||
|
||||
void solve(Solver* solver, double* p, double* rhs)
|
||||
{
|
||||
int imax = solver->grid->imax;
|
||||
int jmax = solver->grid->jmax;
|
||||
double eps = solver->eps;
|
||||
int itermax = solver->itermax;
|
||||
double dx2 = solver->grid->dx * solver->grid->dx;
|
||||
double dy2 = solver->grid->dy * solver->grid->dy;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
res = 0.0;
|
||||
jsw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
isw = jsw;
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
double r = RHS(i, j) -
|
||||
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
|
||||
(P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
|
||||
|
||||
P(i, j) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0) = P(i, 1);
|
||||
P(i, jmax + 1) = P(i, jmax);
|
||||
}
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j) = P(1, j);
|
||||
P(imax + 1, j) = P(imax, j);
|
||||
}
|
||||
|
||||
res = res / (double)(imax * jmax);
|
||||
#ifdef DEBUG
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
#endif
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@@ -18,9 +18,10 @@ typedef struct {
|
||||
int itermax;
|
||||
int levels;
|
||||
double **r, **e;
|
||||
int presmooth, postsmooth;
|
||||
} Solver;
|
||||
|
||||
extern void initSolver(Solver*, Discretization*, Parameter*);
|
||||
extern void solve(Solver*, double*, double*);
|
||||
extern double solve(Solver*, double*, double*);
|
||||
|
||||
#endif
|
||||
|
@@ -22,6 +22,7 @@ SRC = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c)
|
||||
ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s, $(SRC))
|
||||
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
|
||||
OBJ += $(BUILD_DIR)/vtkWriter-$(VTK_OUTPUT_FMT).o
|
||||
OBJ += $(BUILD_DIR)/solver-$(SOLVER).o
|
||||
SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h)
|
||||
ifeq ($(VTK_OUTPUT_FMT),mpi)
|
||||
DEFINES += -D_VTK_WRITER_MPI
|
||||
@@ -42,9 +43,19 @@ $(BUILD_DIR)/%.s: %.c
|
||||
$(info ===> GENERATE ASM $@)
|
||||
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONY: clean distclean tags info asm format
|
||||
.PHONY: clean distclean vis vis_clean tags info asm format
|
||||
|
||||
clean:
|
||||
vis:
|
||||
$(info ===> GENERATE VISUALIZATION)
|
||||
@gnuplot -e "filename='residual.dat'" ./residual.plot
|
||||
|
||||
vis_clean:
|
||||
$(info ===> CLEAN VISUALIZATION)
|
||||
@rm -f *.dat
|
||||
@rm -f *.vtk
|
||||
@rm -f *.png
|
||||
|
||||
clean: vis_clean
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -f tags
|
||||
|
@@ -32,21 +32,28 @@ xlength 30.0 # domain size in x-direction
|
||||
ylength 4.0 # domain size in y-direction
|
||||
zlength 4.0 # domain size in z-direction
|
||||
imax 200 # number of interior cells in x-direction
|
||||
jmax 50 # number of interior cells in y-direction
|
||||
kmax 50 # number of interior cells in z-direction
|
||||
jmax 40 # number of interior cells in y-direction
|
||||
kmax 40 # number of interior cells in z-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 100.0 # final time
|
||||
te 60.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 2 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 500 # maximal number of pressure iteration in one time step
|
||||
eps 0.0001 # stopping tolerance for pressure iteration
|
||||
omg 1.3 # relaxation parameter for SOR iteration
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
#===============================================================================
|
||||
|
@@ -1,7 +1,10 @@
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= CLANG
|
||||
# Supported: GCC, CLANG, ICX
|
||||
TAG ?= ICX
|
||||
# Supported: true, false
|
||||
ENABLE_MPI ?= true
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: rb, mg
|
||||
SOLVER ?= mg
|
||||
# Supported: seq, mpi
|
||||
VTK_OUTPUT_FMT ?= seq
|
||||
|
||||
|
@@ -18,7 +18,7 @@ gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
gz 0.0 #
|
||||
|
||||
re 1000.0 # Reynolds number
|
||||
re 1000.0 # Reynolds number
|
||||
|
||||
u_init 0.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
@@ -42,6 +42,13 @@ te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
|
@@ -1,8 +1,8 @@
|
||||
ifeq ($(ENABLE_MPI),true)
|
||||
CC = mpiicc
|
||||
CC = mpiicx
|
||||
DEFINES = -D_MPI
|
||||
else
|
||||
CC = icc
|
||||
CC = icx
|
||||
endif
|
||||
|
||||
GCC = gcc
|
||||
@@ -13,7 +13,7 @@ OPENMP = -qopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP)
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP) -Wno-unused-command-line-argument
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES += -D_GNU_SOURCE# -DDEBUG
|
||||
INCLUDES =
|
9
BasicSolver/3D-mpi/residual.plot
Normal file
9
BasicSolver/3D-mpi/residual.plot
Normal file
@@ -0,0 +1,9 @@
|
||||
set terminal png size 1800,768 enhanced font ,12
|
||||
set output 'residual.png'
|
||||
set datafile separator whitespace
|
||||
set xlabel "Timestep"
|
||||
set ylabel "Residual"
|
||||
|
||||
set logscale y 2
|
||||
|
||||
plot 'residual.dat' using 1:2 title "Residual"
|
@@ -167,11 +167,12 @@ static void assembleResult(Comm* c,
|
||||
MPI_Waitall(numRequests, requests, MPI_STATUSES_IGNORE);
|
||||
}
|
||||
|
||||
static int sum(int* sizes, int position)
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = 0; i < position; i++) {
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
@@ -336,7 +337,6 @@ void commCollectResult(Comm* c,
|
||||
int imaxLocal = c->imaxLocal;
|
||||
int jmaxLocal = c->jmaxLocal;
|
||||
int kmaxLocal = c->kmaxLocal;
|
||||
|
||||
#if defined(_MPI)
|
||||
int offset[c->size * NDIMS];
|
||||
int imaxLocalAll[c->size];
|
||||
@@ -351,9 +351,13 @@ void commCollectResult(Comm* c,
|
||||
for (int i = 0; i < c->size; i++) {
|
||||
int coords[NCORDS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
offset[i * NDIMS + IDIM] = sum(imaxLocalAll, coords[ICORD]);
|
||||
offset[i * NDIMS + JDIM] = sum(jmaxLocalAll, coords[JCORD]);
|
||||
offset[i * NDIMS + KDIM] = sum(kmaxLocalAll, coords[KCORD]);
|
||||
offset[i * NDIMS + IDIM] = sum(imaxLocalAll,
|
||||
i,
|
||||
c->dims[IDIM] * c->dims[JDIM],
|
||||
coords[ICORD]);
|
||||
offset[i * NDIMS + JDIM] = sum(jmaxLocalAll, i, c->dims[IDIM], coords[JCORD]);
|
||||
offset[i * NDIMS + KDIM] = sum(kmaxLocalAll, i, 1, coords[KCORD]);
|
||||
|
||||
printf("Rank: %d, Coords(k,j,i): %d %d %d, Size(k,j,i): %d %d %d, "
|
||||
"Offset(k,j,i): %d %d %d\n",
|
||||
i,
|
||||
@@ -562,9 +566,9 @@ void commPartition(Comm* c, int kmax, int jmax, int imax)
|
||||
MPI_Cart_shift(c->comm, KCORD, 1, &c->neighbours[FRONT], &c->neighbours[BACK]);
|
||||
MPI_Cart_get(c->comm, NCORDS, c->dims, periods, c->coords);
|
||||
|
||||
c->imaxLocal = sizeOfRank(c->rank, dims[ICORD], imax);
|
||||
c->jmaxLocal = sizeOfRank(c->rank, dims[JCORD], jmax);
|
||||
c->kmaxLocal = sizeOfRank(c->rank, dims[KCORD], kmax);
|
||||
c->imaxLocal = sizeOfRank(c->coords[KDIM], dims[ICORD], imax);
|
||||
c->jmaxLocal = sizeOfRank(c->coords[JDIM], dims[JCORD], jmax);
|
||||
c->kmaxLocal = sizeOfRank(c->coords[IDIM], dims[KCORD], kmax);
|
||||
|
||||
// setup buffer types for communication
|
||||
setupCommunication(c, LEFT, BULK);
|
||||
@@ -597,3 +601,47 @@ void commFinalize(Comm* c)
|
||||
MPI_Finalize();
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(
|
||||
Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal, int kmaxLocal)
|
||||
{
|
||||
#if defined _MPI
|
||||
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
newcomm->kmaxLocal = kmaxLocal / 2;
|
||||
|
||||
setupCommunication(newcomm, LEFT, BULK);
|
||||
setupCommunication(newcomm, LEFT, HALO);
|
||||
setupCommunication(newcomm, RIGHT, BULK);
|
||||
setupCommunication(newcomm, RIGHT, HALO);
|
||||
setupCommunication(newcomm, BOTTOM, BULK);
|
||||
setupCommunication(newcomm, BOTTOM, HALO);
|
||||
setupCommunication(newcomm, TOP, BULK);
|
||||
setupCommunication(newcomm, TOP, HALO);
|
||||
setupCommunication(newcomm, FRONT, BULK);
|
||||
setupCommunication(newcomm, FRONT, HALO);
|
||||
setupCommunication(newcomm, BACK, BULK);
|
||||
setupCommunication(newcomm, BACK, HALO);
|
||||
#else
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
newcomm->kmaxLocal = kmaxLocal;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
@@ -23,6 +23,7 @@ typedef enum dimension { KDIM = 0, JDIM, IDIM, NDIMS } Dimension;
|
||||
enum layer { HALO = 0, BULK };
|
||||
enum op { MAX = 0, SUM };
|
||||
|
||||
|
||||
typedef struct {
|
||||
int rank;
|
||||
int size;
|
||||
@@ -45,6 +46,9 @@ extern void commShift(Comm* c, double* f, double* g, double* h);
|
||||
extern void commReduction(double* v, int op);
|
||||
extern int commIsBoundary(Comm* c, Direction direction);
|
||||
extern void commGetOffsets(Comm* c, int offsets[], int kmax, int jmax, int imax);
|
||||
extern void commFreeCommunicator(Comm* comm);
|
||||
extern void commUpdateDatatypes(
|
||||
Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal, int kmaxLocal);
|
||||
extern void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
@@ -7,13 +7,11 @@
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "comm.h"
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
#define P(i, j, k) \
|
||||
@@ -33,7 +31,7 @@
|
||||
#define RHS(i, j, k) \
|
||||
rhs[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
|
||||
static void printConfig(Solver* s)
|
||||
static void printConfig(Discretization* s)
|
||||
{
|
||||
if (commIsMaster(&s->comm)) {
|
||||
printf("Parameters for #%s#\n", s->problem);
|
||||
@@ -72,7 +70,7 @@ static void printConfig(Solver* s)
|
||||
commPrintConfig(&s->comm);
|
||||
}
|
||||
|
||||
void initSolver(Solver* s, Parameter* params)
|
||||
void initDiscretization(Discretization* s, Parameter* params)
|
||||
{
|
||||
s->problem = params->name;
|
||||
s->bcLeft = params->bcLeft;
|
||||
@@ -142,226 +140,7 @@ void initSolver(Solver* s, Parameter* params)
|
||||
#endif /* VERBOSE */
|
||||
}
|
||||
|
||||
void computeRHS(Solver* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
|
||||
double idx = 1.0 / s->grid.dx;
|
||||
double idy = 1.0 / s->grid.dy;
|
||||
double idz = 1.0 / s->grid.dz;
|
||||
double idt = 1.0 / s->dt;
|
||||
|
||||
double* rhs = s->rhs;
|
||||
double* f = s->f;
|
||||
double* g = s->g;
|
||||
double* h = s->h;
|
||||
|
||||
commShift(&s->comm, f, g, h);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
RHS(i, j, k) = ((F(i, j, k) - F(i - 1, j, k)) * idx +
|
||||
(G(i, j, k) - G(i, j - 1, k)) * idy +
|
||||
(H(i, j, k) - H(i, j, k - 1)) * idz) *
|
||||
idt;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void solve(Solver* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid.dx * s->grid.dx;
|
||||
double dy2 = s->grid.dy * s->grid.dy;
|
||||
double dz2 = s->grid.dz * s->grid.dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double* p = s->p;
|
||||
double* rhs = s->rhs;
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, ksw, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
commExchange(&s->comm, p);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = isw; i < imaxLocal + 1; i += 2) {
|
||||
|
||||
double r =
|
||||
RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2);
|
||||
|
||||
P(i, j, k) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
|
||||
if (commIsBoundary(&s->comm, FRONT)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&s->comm, BACK)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, kmaxLocal + 1) = P(i, j, kmaxLocal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&s->comm, BOTTOM)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&s->comm, TOP)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, jmaxLocal + 1, k) = P(i, jmaxLocal, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&s->comm, LEFT)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&s->comm, RIGHT)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(imaxLocal + 1, j, k) = P(imaxLocal, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
commReduction(&res, SUM);
|
||||
res = res / (double)(imax * jmax * kmax);
|
||||
#ifdef DEBUG
|
||||
if (commIsMaster(&s->comm)) {
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
}
|
||||
#endif
|
||||
commExchange(&s->comm, p);
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
if (commIsMaster(&s->comm)) {
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static double maxElement(Solver* s, double* m)
|
||||
{
|
||||
int size = (s->comm.imaxLocal + 2) * (s->comm.jmaxLocal + 2) *
|
||||
(s->comm.kmaxLocal + 2);
|
||||
double maxval = DBL_MIN;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
maxval = MAX(maxval, fabs(m[i]));
|
||||
}
|
||||
commReduction(&maxval, MAX);
|
||||
return maxval;
|
||||
}
|
||||
|
||||
void normalizePressure(Solver* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
|
||||
double* p = s->p;
|
||||
double avgP = 0.0;
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
avgP += P(i, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
commReduction(&avgP, SUM);
|
||||
avgP /= (s->grid.imax * s->grid.jmax * s->grid.kmax);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, k) = P(i, j, k) - avgP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void computeTimestep(Solver* s)
|
||||
{
|
||||
double dt = s->dtBound;
|
||||
double dx = s->grid.dx;
|
||||
double dy = s->grid.dy;
|
||||
double dz = s->grid.dz;
|
||||
|
||||
double umax = maxElement(s, s->u);
|
||||
double vmax = maxElement(s, s->v);
|
||||
double wmax = maxElement(s, s->w);
|
||||
|
||||
if (umax > 0) {
|
||||
dt = (dt > dx / umax) ? dx / umax : dt;
|
||||
}
|
||||
if (vmax > 0) {
|
||||
dt = (dt > dy / vmax) ? dy / vmax : dt;
|
||||
}
|
||||
if (wmax > 0) {
|
||||
dt = (dt > dz / wmax) ? dz / wmax : dt;
|
||||
}
|
||||
|
||||
s->dt = dt * s->tau;
|
||||
}
|
||||
|
||||
void setBoundaryConditions(Solver* s)
|
||||
void setBoundaryConditions(Discretization* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
@@ -576,7 +355,37 @@ void setBoundaryConditions(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void setSpecialBoundaryCondition(Solver* s)
|
||||
void computeRHS(Discretization* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
|
||||
double idx = 1.0 / s->grid.dx;
|
||||
double idy = 1.0 / s->grid.dy;
|
||||
double idz = 1.0 / s->grid.dz;
|
||||
double idt = 1.0 / s->dt;
|
||||
|
||||
double* rhs = s->rhs;
|
||||
double* f = s->f;
|
||||
double* g = s->g;
|
||||
double* h = s->h;
|
||||
|
||||
commShift(&s->comm, f, g, h);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
RHS(i, j, k) = ((F(i, j, k) - F(i - 1, j, k)) * idx +
|
||||
(G(i, j, k) - G(i, j - 1, k)) * idy +
|
||||
(H(i, j, k) - H(i, j, k - 1)) * idz) *
|
||||
idt;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setSpecialBoundaryCondition(Discretization* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
@@ -603,7 +412,74 @@ void setSpecialBoundaryCondition(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void computeFG(Solver* s)
|
||||
static double maxElement(Discretization* s, double* m)
|
||||
{
|
||||
int size = (s->comm.imaxLocal + 2) * (s->comm.jmaxLocal + 2) *
|
||||
(s->comm.kmaxLocal + 2);
|
||||
double maxval = DBL_MIN;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
maxval = MAX(maxval, fabs(m[i]));
|
||||
}
|
||||
commReduction(&maxval, MAX);
|
||||
return maxval;
|
||||
}
|
||||
|
||||
void normalizePressure(Discretization* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
|
||||
double* p = s->p;
|
||||
double avgP = 0.0;
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
avgP += P(i, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
commReduction(&avgP, SUM);
|
||||
avgP /= (s->grid.imax * s->grid.jmax * s->grid.kmax);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, k) = P(i, j, k) - avgP;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void computeTimestep(Discretization* s)
|
||||
{
|
||||
double dt = s->dtBound;
|
||||
double dx = s->grid.dx;
|
||||
double dy = s->grid.dy;
|
||||
double dz = s->grid.dz;
|
||||
|
||||
double umax = maxElement(s, s->u);
|
||||
double vmax = maxElement(s, s->v);
|
||||
double wmax = maxElement(s, s->w);
|
||||
|
||||
if (umax > 0) {
|
||||
dt = (dt > dx / umax) ? dx / umax : dt;
|
||||
}
|
||||
if (vmax > 0) {
|
||||
dt = (dt > dy / vmax) ? dy / vmax : dt;
|
||||
}
|
||||
if (wmax > 0) {
|
||||
dt = (dt > dz / wmax) ? dz / wmax : dt;
|
||||
}
|
||||
|
||||
s->dt = dt * s->tau;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void computeFG(Discretization* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
@@ -823,7 +699,7 @@ void computeFG(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void adaptUV(Solver* s)
|
||||
void adaptUV(Discretization* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
@@ -850,4 +726,4 @@ void adaptUV(Solver* s)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
44
BasicSolver/3D-mpi/src/discretization.h
Normal file
44
BasicSolver/3D-mpi/src/discretization.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __DISCRETIZATION_H_
|
||||
#define __DISCRETIZATION_H_
|
||||
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
#include "comm.h"
|
||||
|
||||
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
|
||||
|
||||
typedef struct {
|
||||
/* geometry and grid information */
|
||||
Grid grid;
|
||||
/* arrays */
|
||||
double *p, *rhs;
|
||||
double *f, *g, *h;
|
||||
double *u, *v, *w;
|
||||
/* parameters */
|
||||
double eps, omega;
|
||||
double re, tau, gamma;
|
||||
double gx, gy, gz;
|
||||
/* time stepping */
|
||||
int itermax;
|
||||
double dt, te;
|
||||
double dtBound;
|
||||
char* problem;
|
||||
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
|
||||
Comm comm;
|
||||
} Discretization;
|
||||
|
||||
extern void initDiscretization(Discretization*, Parameter*);
|
||||
extern void computeRHS(Discretization*);
|
||||
extern void normalizePressure(Discretization*);
|
||||
extern void computeTimestep(Discretization*);
|
||||
extern void setBoundaryConditions(Discretization*);
|
||||
extern void setSpecialBoundaryCondition(Discretization*);
|
||||
extern void computeFG(Discretization*);
|
||||
extern void adaptUV(Discretization*);
|
||||
#endif
|
@@ -9,7 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "comm.h"
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "progress.h"
|
||||
#include "solver.h"
|
||||
@@ -21,9 +21,12 @@ int main(int argc, char** argv)
|
||||
double timeStart, timeStop;
|
||||
Parameter p;
|
||||
Solver s;
|
||||
Discretization d;
|
||||
|
||||
commInit(&s.comm, argc, argv);
|
||||
commInit(&d.comm, argc, argv);
|
||||
initParameter(&p);
|
||||
FILE* fp;
|
||||
if (commIsMaster(&d.comm)) fp = initResidualWriter();
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <configFile>\n", argv[0]);
|
||||
@@ -31,33 +34,44 @@ int main(int argc, char** argv)
|
||||
}
|
||||
|
||||
readParameter(&p, argv[1]);
|
||||
commPartition(&s.comm, p.kmax, p.jmax, p.imax);
|
||||
if (commIsMaster(&s.comm)) {
|
||||
commPartition(&d.comm, p.kmax, p.jmax, p.imax);
|
||||
|
||||
if (commIsMaster(&d.comm)) {
|
||||
printParameter(&p);
|
||||
}
|
||||
initSolver(&s, &p);
|
||||
|
||||
initDiscretization(&d, &p);
|
||||
initSolver(&s, &d, &p);
|
||||
|
||||
#ifndef VERBOSE
|
||||
initProgress(s.te);
|
||||
initProgress(d.te);
|
||||
#endif
|
||||
|
||||
double tau = s.tau;
|
||||
double te = s.te;
|
||||
double tau = d.tau;
|
||||
double te = d.te;
|
||||
double t = 0.0;
|
||||
int nt = 0;
|
||||
double res = 0.0;
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
while (t <= te) {
|
||||
if (tau > 0.0) computeTimestep(&s);
|
||||
setBoundaryConditions(&s);
|
||||
setSpecialBoundaryCondition(&s);
|
||||
computeFG(&s);
|
||||
computeRHS(&s);
|
||||
solve(&s);
|
||||
adaptUV(&s);
|
||||
t += s.dt;
|
||||
if (tau > 0.0) computeTimestep(&d);
|
||||
setBoundaryConditions(&d);
|
||||
setSpecialBoundaryCondition(&d);
|
||||
computeFG(&d);
|
||||
computeRHS(&d);
|
||||
if (nt % 100 == 0) normalizePressure(&d);
|
||||
res = solve(&s, d.p, d.rhs);
|
||||
adaptUV(&d);
|
||||
|
||||
if (commIsMaster(&d.comm)) writeResidual(fp, t, res);
|
||||
|
||||
t += d.dt;
|
||||
nt++;
|
||||
|
||||
#ifdef VERBOSE
|
||||
if (commIsMaster(&s.comm)) {
|
||||
printf("TIME %f , TIMESTEP %f\n", t, s.dt);
|
||||
if (commIsMaster(s.comm)) {
|
||||
printf("TIME %f , TIMESTEP %f\n", t, d.dt);
|
||||
}
|
||||
#else
|
||||
printProgress(t);
|
||||
@@ -67,7 +81,7 @@ int main(int argc, char** argv)
|
||||
#ifndef VERBOSE
|
||||
stopProgress();
|
||||
#endif
|
||||
if (commIsMaster(&s.comm)) {
|
||||
if (commIsMaster(s.comm)) {
|
||||
printf("Solution took %.2fs\n", timeStop - timeStart);
|
||||
}
|
||||
|
||||
@@ -75,14 +89,16 @@ int main(int argc, char** argv)
|
||||
#ifdef _VTK_WRITER_MPI
|
||||
VtkOptions opts = { .grid = s.grid, .comm = s.comm };
|
||||
vtkOpen(&opts, s.problem);
|
||||
vtkScalar(&opts, "pressure", s.p);
|
||||
vtkVector(&opts, "velocity", (VtkVector) { s.u, s.v, s.w });
|
||||
vtkScalar(&opts, "pressure", d.p);
|
||||
vtkVector(&opts, "velocity", (VtkVector) { d.u, d.v, d.w });
|
||||
vtkClose(&opts);
|
||||
#else
|
||||
if (commIsMaster(&d.comm)) fclose(fp);
|
||||
|
||||
double *pg, *ug, *vg, *wg;
|
||||
|
||||
if (commIsMaster(&s.comm)) {
|
||||
size_t bytesize = s.grid.imax * s.grid.jmax * s.grid.kmax * sizeof(double);
|
||||
if (commIsMaster(s.comm)) {
|
||||
size_t bytesize = s.grid->imax * s.grid->jmax * s.grid->kmax * sizeof(double);
|
||||
|
||||
pg = allocate(64, bytesize);
|
||||
ug = allocate(64, bytesize);
|
||||
@@ -90,34 +106,35 @@ int main(int argc, char** argv)
|
||||
wg = allocate(64, bytesize);
|
||||
}
|
||||
|
||||
commCollectResult(&s.comm,
|
||||
commCollectResult(s.comm,
|
||||
ug,
|
||||
vg,
|
||||
wg,
|
||||
pg,
|
||||
s.u,
|
||||
s.v,
|
||||
s.w,
|
||||
s.p,
|
||||
s.grid.kmax,
|
||||
s.grid.jmax,
|
||||
s.grid.imax);
|
||||
d.u,
|
||||
d.v,
|
||||
d.w,
|
||||
d.p,
|
||||
s.grid->kmax,
|
||||
s.grid->jmax,
|
||||
s.grid->imax);
|
||||
|
||||
if (commIsMaster(&s.comm)) {
|
||||
if (commIsMaster(s.comm)) {
|
||||
VtkOptions opts = { .grid = s.grid };
|
||||
vtkOpen(&opts, s.problem);
|
||||
vtkScalar(&opts, "pressure", pg);
|
||||
vtkVector(&opts, "velocity", (VtkVector) { ug, vg, wg });
|
||||
vtkClose(&opts);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
timeStop = getTimeStamp();
|
||||
|
||||
if (commIsMaster(&s.comm)) {
|
||||
if (commIsMaster(s.comm)) {
|
||||
printf("Result output took %.2fs\n", timeStop - timeStart);
|
||||
}
|
||||
|
||||
commFinalize(&s.comm);
|
||||
commFinalize(s.comm);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
@@ -14,18 +14,22 @@
|
||||
|
||||
void initParameter(Parameter* param)
|
||||
{
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->zlength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->kmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.7;
|
||||
param->re = 100.0;
|
||||
param->gamma = 0.9;
|
||||
param->tau = 0.5;
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->zlength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->kmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.7;
|
||||
param->re = 100.0;
|
||||
param->gamma = 0.9;
|
||||
param->tau = 0.5;
|
||||
param->levels = 5;
|
||||
param->presmooth = 5;
|
||||
param->postsmooth = 5;
|
||||
|
||||
}
|
||||
|
||||
void readParameter(Parameter* param, const char* filename)
|
||||
@@ -65,6 +69,9 @@ void readParameter(Parameter* param, const char* filename)
|
||||
PARSE_INT(jmax);
|
||||
PARSE_INT(kmax);
|
||||
PARSE_INT(itermax);
|
||||
PARSE_INT(levels);
|
||||
PARSE_INT(presmooth);
|
||||
PARSE_INT(postsmooth);
|
||||
PARSE_REAL(eps);
|
||||
PARSE_REAL(omg);
|
||||
PARSE_REAL(re);
|
||||
|
@@ -18,6 +18,7 @@ typedef struct {
|
||||
char* name;
|
||||
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
|
||||
double u_init, v_init, w_init, p_init;
|
||||
int levels, presmooth, postsmooth;
|
||||
} Parameter;
|
||||
|
||||
void initParameter(Parameter*);
|
||||
|
@@ -4,12 +4,12 @@
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "progress.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "progress.h"
|
||||
|
||||
static double _end;
|
||||
static int _current;
|
||||
|
||||
@@ -48,3 +48,18 @@ void stopProgress()
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
FILE* initResidualWriter()
|
||||
{
|
||||
FILE* fp;
|
||||
fp = fopen("residual.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return fp;
|
||||
}
|
||||
|
||||
void writeResidual(FILE* fp, double ts, double res) { fprintf(fp, "%f, %f\n", ts, res); }
|
@@ -4,11 +4,14 @@
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef __PROGRESS_H_
|
||||
#define __PROGRESS_H_
|
||||
|
||||
extern void initProgress(double);
|
||||
extern void printProgress(double);
|
||||
extern void stopProgress(void);
|
||||
|
||||
extern FILE* initResidualWriter(void);
|
||||
extern void writeResidual(FILE*, double, double);
|
||||
#endif
|
||||
|
405
BasicSolver/3D-mpi/src/solver-mg.c
Normal file
405
BasicSolver/3D-mpi/src/solver-mg.c
Normal file
@@ -0,0 +1,405 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
#define FINEST_LEVEL 0
|
||||
#define COARSEST_LEVEL (s->levels - 1)
|
||||
#define S(i, j, k) \
|
||||
s[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define E(i, j, k) \
|
||||
e[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define R(i, j, k) \
|
||||
r[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define OLD(i, j, k) \
|
||||
old[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
|
||||
static void restrictMG(Solver* s, int level, Comm* comm)
|
||||
{
|
||||
double* r = s->r[level + 1];
|
||||
double* old = s->r[level];
|
||||
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
int kmaxLocal = comm->kmaxLocal;
|
||||
|
||||
commExchange(comm, old);
|
||||
|
||||
for (int k = 1; k < comm->kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < comm->jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < comm->imaxLocal + 1; ++i) {
|
||||
R(i, j, k) = (OLD(2 * i - 1, 2 * j - 1, 2 * k) +
|
||||
OLD(2 * i, 2 * j - 1, 2 * k) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1, 2 * k) +
|
||||
OLD(2 * i - 1, 2 * j, 2 * k) * 2 +
|
||||
OLD(2 * i, 2 * j, 2 * k) * 8 +
|
||||
OLD(2 * i + 1, 2 * j, 2 * k) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1, 2 * k) +
|
||||
OLD(2 * i, 2 * j + 1, 2 * k) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1, 2 * k) +
|
||||
|
||||
OLD(2 * i - 1, 2 * j - 1, 2 * k - 1) +
|
||||
OLD(2 * i, 2 * j - 1, 2 * k - 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1, 2 * k - 1) +
|
||||
OLD(2 * i - 1, 2 * j, 2 * k - 1) * 2 +
|
||||
OLD(2 * i, 2 * j, 2 * k - 1) * 4 +
|
||||
OLD(2 * i + 1, 2 * j, 2 * k - 1) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1, 2 * k - 1) +
|
||||
OLD(2 * i, 2 * j + 1, 2 * k - 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1, 2 * k - 1) +
|
||||
|
||||
OLD(2 * i - 1, 2 * j - 1, 2 * k + 1) +
|
||||
OLD(2 * i, 2 * j - 1, 2 * k + 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1, 2 * k + 1) +
|
||||
OLD(2 * i - 1, 2 * j, 2 * k + 1) * 2 +
|
||||
OLD(2 * i, 2 * j, 2 * k + 1) * 4 +
|
||||
OLD(2 * i + 1, 2 * j, 2 * k + 1) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1, 2 * k + 1) +
|
||||
OLD(2 * i, 2 * j + 1, 2 * k + 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1, 2 * k + 1)) /
|
||||
64.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void prolongate(Solver* s, int level, Comm* comm)
|
||||
{
|
||||
double* old = s->r[level + 1];
|
||||
double* e = s->r[level];
|
||||
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
int kmaxLocal = comm->kmaxLocal;
|
||||
|
||||
for (int k = 2; k < kmaxLocal + 1; k += 2) {
|
||||
for (int j = 2; j < jmaxLocal + 1; j += 2) {
|
||||
for (int i = 2; i < imaxLocal + 1; i += 2) {
|
||||
E(i, j, k) = OLD(i / 2, j / 2, k / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void correct(Solver* s, double* p, int level, Comm* comm)
|
||||
{
|
||||
double* e = s->e[level];
|
||||
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
int kmaxLocal = comm->kmaxLocal;
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; ++k) {
|
||||
for (int j = 1; j < jmaxLocal + 1; ++j) {
|
||||
for (int i = 1; i < imaxLocal + 1; ++i) {
|
||||
P(i, j, k) += E(i, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void setBoundaryCondition(
|
||||
Solver* s, double* p, int imaxLocal, int jmaxLocal, int kmaxLocal)
|
||||
{
|
||||
#ifdef _MPI
|
||||
if (commIsBoundary(s->comm, FRONT)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, BACK)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, kmaxLocal + 1) = P(i, j, kmaxLocal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, BOTTOM)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, TOP)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, jmaxLocal + 1, k) = P(i, jmaxLocal, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, LEFT)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, RIGHT)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(imaxLocal + 1, j, k) = P(imaxLocal, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
P(i, j, kmaxLocal + 1) = P(i, j, kmaxLocal);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
P(i, jmaxLocal + 1, k) = P(i, jmaxLocal, k);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
P(imaxLocal + 1, j, k) = P(imaxLocal, j, k);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
static void smooth(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
int kmaxLocal = comm->kmaxLocal;
|
||||
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double* r = s->r[level];
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
int pass, ksw, jsw, isw;
|
||||
double res = 1.0;
|
||||
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
commExchange(comm, p);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = isw; i < imaxLocal + 1; i += 2) {
|
||||
|
||||
P(i, j, k) -=
|
||||
factor *
|
||||
(RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
}
|
||||
|
||||
static double calculateResidual(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
int kmaxLocal = comm->kmaxLocal;
|
||||
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double* r = s->r[level];
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
int pass, ksw, jsw, isw;
|
||||
double res = 1.0;
|
||||
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
commExchange(comm, p);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = isw; i < imaxLocal + 1; i += 2) {
|
||||
|
||||
R(i,
|
||||
j,
|
||||
k) = (RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) *
|
||||
idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2));
|
||||
|
||||
res += (R(i, j, k) * R(i, j, k));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
|
||||
commReduction(&res, SUM);
|
||||
|
||||
res = res / (double)(imaxLocal * jmaxLocal * kmaxLocal);
|
||||
#ifdef DEBUG
|
||||
if (commIsMaster(s->comm)) {
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
}
|
||||
#endif
|
||||
return res;
|
||||
}
|
||||
|
||||
static double multiGrid(Solver* s, double* p, double* rhs, int level, Comm* comm)
|
||||
{
|
||||
int imaxLocal = comm->imaxLocal;
|
||||
int jmaxLocal = comm->jmaxLocal;
|
||||
int kmaxLocal = comm->kmaxLocal;
|
||||
|
||||
double res = 0.0;
|
||||
|
||||
// coarsest level
|
||||
if (level == COARSEST_LEVEL) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
smooth(s, p, rhs, level, comm);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// pre-smoothing
|
||||
for (int i = 0; i < s->presmooth; i++) {
|
||||
smooth(s, p, rhs, level, comm);
|
||||
if (level == FINEST_LEVEL)
|
||||
setBoundaryCondition(s, p, imaxLocal, jmaxLocal, kmaxLocal);
|
||||
}
|
||||
|
||||
res = calculateResidual(s, p, rhs, level, comm);
|
||||
|
||||
// restrict
|
||||
restrictMG(s, level, comm);
|
||||
|
||||
// Create a new comm object withupdated imaxLocal and jmaxLocal
|
||||
// along with their updated bufferTypes, sdispls, rdispls
|
||||
Comm newcomm;
|
||||
commUpdateDatatypes(s->comm,
|
||||
&newcomm,
|
||||
imaxLocal,
|
||||
jmaxLocal,
|
||||
kmaxLocal);
|
||||
|
||||
// MGSolver on residual and error.
|
||||
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm);
|
||||
|
||||
commFreeCommunicator(&newcomm);
|
||||
|
||||
// prolongate
|
||||
prolongate(s, level, comm);
|
||||
|
||||
// correct p on finer level using residual
|
||||
correct(s, p, level, comm);
|
||||
if (level == FINEST_LEVEL)
|
||||
setBoundaryCondition(s, p, imaxLocal, jmaxLocal, kmaxLocal);
|
||||
|
||||
// post-smoothing
|
||||
for (int i = 0; i < s->postsmooth; i++) {
|
||||
smooth(s, p, rhs, level, comm);
|
||||
if (level == FINEST_LEVEL)
|
||||
setBoundaryCondition(s, p, imaxLocal, jmaxLocal, kmaxLocal);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
s->itermax = p->itermax;
|
||||
s->levels = p->levels;
|
||||
s->grid = &d->grid;
|
||||
s->presmooth = p->presmooth;
|
||||
s->postsmooth = p->postsmooth;
|
||||
s->comm = &d->comm;
|
||||
s->problem = p->name;
|
||||
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int kmax = s->grid->kmax;
|
||||
int levels = s->levels;
|
||||
if (commIsMaster(s->comm)) printf("Using Multigrid solver with %d levels\n", levels);
|
||||
|
||||
s->r = malloc(levels * sizeof(double*));
|
||||
s->e = malloc(levels * sizeof(double*));
|
||||
|
||||
size_t size = (imax + 2) * (jmax + 2) * (kmax + 2);
|
||||
|
||||
for (int j = 0; j < levels; j++) {
|
||||
s->r[j] = allocate(64, size * sizeof(double));
|
||||
s->e[j] = allocate(64, size * sizeof(double));
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
s->r[j][i] = 0.0;
|
||||
s->e[j][i] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
double res = multiGrid(s, p, rhs, 0, s->comm);
|
||||
|
||||
#ifdef VERBOSE
|
||||
if (commIsMaster(s->comm)) {
|
||||
printf("Residuum: %.6f\n", res);
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
175
BasicSolver/3D-mpi/src/solver-rb.c
Normal file
175
BasicSolver/3D-mpi/src/solver-rb.c
Normal file
@@ -0,0 +1,175 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "comm.h"
|
||||
#include "parameter.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
s->itermax = p->itermax;
|
||||
s->levels = p->levels;
|
||||
s->grid = &d->grid;
|
||||
s->presmooth = p->presmooth;
|
||||
s->postsmooth = p->postsmooth;
|
||||
s->comm = &d->comm;
|
||||
s->problem = p->name;
|
||||
}
|
||||
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imaxLocal = s->comm->imaxLocal;
|
||||
int jmaxLocal = s->comm->jmaxLocal;
|
||||
int kmaxLocal = s->comm->kmaxLocal;
|
||||
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int kmax = s->grid->kmax;
|
||||
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, ksw, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
commExchange(s->comm, p);
|
||||
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = isw; i < imaxLocal + 1; i += 2) {
|
||||
|
||||
double r =
|
||||
RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2);
|
||||
|
||||
P(i, j, k) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
#ifdef _MPI
|
||||
if (commIsBoundary(s->comm, FRONT)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, BACK)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, j, kmaxLocal + 1) = P(i, j, kmaxLocal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, BOTTOM)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, TOP)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
P(i, jmaxLocal + 1, k) = P(i, jmaxLocal, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, LEFT)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(s->comm, RIGHT)) {
|
||||
for (int k = 1; k < kmaxLocal + 1; k++) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
P(imaxLocal + 1, j, k) = P(imaxLocal, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
#else
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
P(i, j, kmax + 1) = P(i, j, kmax);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
P(i, jmax + 1, k) = P(i, jmax, k);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
P(imax + 1, j, k) = P(imax, j, k);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
commReduction(&res, SUM);
|
||||
res = res / (double)(imax * jmax * kmax);
|
||||
#ifdef DEBUG
|
||||
if (commIsMaster(&s->comm)) {
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
}
|
||||
#endif
|
||||
commExchange(s->comm, p);
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
if (commIsMaster(s->comm)) {
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
}
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
@@ -9,12 +9,11 @@
|
||||
#include "comm.h"
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
|
||||
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
|
||||
#include "discretization.h"
|
||||
|
||||
typedef struct {
|
||||
/* geometry and grid information */
|
||||
Grid grid;
|
||||
Grid* grid;
|
||||
/* arrays */
|
||||
double *p, *rhs;
|
||||
double *f, *g, *h;
|
||||
@@ -30,16 +29,11 @@ typedef struct {
|
||||
char* problem;
|
||||
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
|
||||
/* communication */
|
||||
Comm comm;
|
||||
double **r, **e;
|
||||
int levels, presmooth, postsmooth;
|
||||
Comm* comm;
|
||||
} Solver;
|
||||
|
||||
extern void initSolver(Solver*, Parameter*);
|
||||
extern void computeRHS(Solver*);
|
||||
extern void solve(Solver*);
|
||||
extern void normalizePressure(Solver*);
|
||||
extern void computeTimestep(Solver*);
|
||||
extern void setBoundaryConditions(Solver*);
|
||||
extern void setSpecialBoundaryCondition(Solver*);
|
||||
extern void computeFG(Solver*);
|
||||
extern void adaptUV(Solver*);
|
||||
extern double solve(Solver* , double* , double* );
|
||||
extern void initSolver(Solver*, Discretization*, Parameter*);
|
||||
#endif
|
||||
|
@@ -14,10 +14,10 @@
|
||||
|
||||
void testInit(Solver* s)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
int myrank = s->comm.rank;
|
||||
int imaxLocal = s->comm->imaxLocal;
|
||||
int jmaxLocal = s->comm->jmaxLocal;
|
||||
int kmaxLocal = s->comm->kmaxLocal;
|
||||
int myrank = s->comm->rank;
|
||||
double* p = s->p;
|
||||
double* f = s->f;
|
||||
double* g = s->g;
|
||||
@@ -76,11 +76,11 @@ static char* direction2String(Direction dir)
|
||||
|
||||
static void printPlane(Solver* s, double* a, int ymax, int xmax, Direction dir)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
int imaxLocal = s->comm->imaxLocal;
|
||||
int jmaxLocal = s->comm->jmaxLocal;
|
||||
int kmaxLocal = s->comm->kmaxLocal;
|
||||
char filename[50];
|
||||
snprintf(filename, 50, "halo-%s-r%d.txt", direction2String(dir), s->comm.rank);
|
||||
snprintf(filename, 50, "halo-%s-r%d.txt", direction2String(dir), s->comm->rank);
|
||||
FILE* fh = fopen(filename, "w");
|
||||
|
||||
for (int y = 0; y < ymax; y++) {
|
||||
@@ -116,9 +116,9 @@ static void printPlane(Solver* s, double* a, int ymax, int xmax, Direction dir)
|
||||
|
||||
void testPrintHalo(Solver* s, double* a)
|
||||
{
|
||||
int imaxLocal = s->comm.imaxLocal;
|
||||
int jmaxLocal = s->comm.jmaxLocal;
|
||||
int kmaxLocal = s->comm.kmaxLocal;
|
||||
int imaxLocal = s->comm->imaxLocal;
|
||||
int jmaxLocal = s->comm->jmaxLocal;
|
||||
int kmaxLocal = s->comm->kmaxLocal;
|
||||
|
||||
printPlane(s, a, kmaxLocal + 2, imaxLocal + 2, BOTTOM);
|
||||
printPlane(s, a, kmaxLocal + 2, imaxLocal + 2, TOP);
|
||||
|
@@ -19,4 +19,13 @@
|
||||
#define ABS(a) ((a) >= 0 ? (a) : -(a))
|
||||
#endif
|
||||
|
||||
#define P(i, j, k) p[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define F(i, j, k) f[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define G(i, j, k) g[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define H(i, j, k) h[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define U(i, j, k) u[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define V(i, j, k) v[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define W(i, j, k) w[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
#define RHS(i, j, k) rhs[(k) * (imaxLocal + 2) * (jmaxLocal + 2) + (j) * (imaxLocal + 2) + (i)]
|
||||
|
||||
#endif // __UTIL_H_
|
||||
|
@@ -46,18 +46,18 @@ static void writeHeader(VtkOptions* o)
|
||||
cursor += sprintf(cursor, "DATASET STRUCTURED_POINTS\n");
|
||||
cursor += sprintf(cursor,
|
||||
"DIMENSIONS %d %d %d\n",
|
||||
o->grid.imax,
|
||||
o->grid.jmax,
|
||||
o->grid.kmax);
|
||||
o->grid->imax,
|
||||
o->grid->jmax,
|
||||
o->grid->kmax);
|
||||
cursor += sprintf(cursor,
|
||||
"ORIGIN %f %f %f\n",
|
||||
o->grid.dx * 0.5,
|
||||
o->grid.dy * 0.5,
|
||||
o->grid.dz * 0.5);
|
||||
cursor += sprintf(cursor, "SPACING %f %f %f\n", o->grid.dx, o->grid.dy, o->grid.dz);
|
||||
o->grid->dx * 0.5,
|
||||
o->grid->dy * 0.5,
|
||||
o->grid->dz * 0.5);
|
||||
cursor += sprintf(cursor, "SPACING %f %f %f\n", o->grid->dx, o->grid->dy, o->grid->dz);
|
||||
cursor += sprintf(cursor,
|
||||
"POINT_DATA %d\n",
|
||||
o->grid.imax * o->grid.jmax * o->grid.kmax);
|
||||
o->grid->imax * o->grid->jmax * o->grid->kmax);
|
||||
|
||||
if (commIsMaster(&o->comm)) {
|
||||
MPI_File_write(o->fh, header, (int)strlen(header), MPI_CHAR, MPI_STATUS_IGNORE);
|
||||
@@ -67,7 +67,6 @@ static void writeHeader(VtkOptions* o)
|
||||
void vtkOpen(VtkOptions* o, char* problem)
|
||||
{
|
||||
char filename[50];
|
||||
|
||||
snprintf(filename, 50, "%s-p%d.vtk", problem, o->comm.size);
|
||||
MPI_File_open(o->comm.comm,
|
||||
filename,
|
||||
@@ -98,7 +97,7 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
|
||||
}
|
||||
|
||||
int offsets[NDIMS];
|
||||
commGetOffsets(&o->comm, offsets, o->grid.kmax, o->grid.jmax, o->grid.imax);
|
||||
commGetOffsets(&o->comm, offsets, o->grid->kmax, o->grid->jmax, o->grid->imax);
|
||||
|
||||
// set global view in file
|
||||
MPI_Offset disp;
|
||||
@@ -108,7 +107,7 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
|
||||
MPI_File_get_size(o->fh, &disp);
|
||||
|
||||
MPI_Type_create_subarray(NDIMS,
|
||||
(int[NDIMS]) { o->grid.kmax, o->grid.jmax, o->grid.imax },
|
||||
(int[NDIMS]) { o->grid->kmax, o->grid->jmax, o->grid->imax },
|
||||
(int[NDIMS]) { o->comm.kmaxLocal, o->comm.jmaxLocal, o->comm.imaxLocal },
|
||||
offsets,
|
||||
MPI_ORDER_C,
|
||||
@@ -177,7 +176,7 @@ void vtkVector(VtkOptions* o, char* name, VtkVector vec)
|
||||
}
|
||||
|
||||
int offsets[NDIMS];
|
||||
commGetOffsets(&o->comm, offsets, o->grid.kmax, o->grid.jmax, o->grid.imax);
|
||||
commGetOffsets(&o->comm, offsets, o->grid->kmax, o->grid->jmax, o->grid->imax);
|
||||
|
||||
// set global view in file
|
||||
MPI_Offset disp;
|
||||
@@ -190,7 +189,7 @@ void vtkVector(VtkOptions* o, char* name, VtkVector vec)
|
||||
MPI_Type_commit(&vectorType);
|
||||
|
||||
MPI_Type_create_subarray(NDIMS,
|
||||
(int[NDIMS]) { o->grid.kmax, o->grid.jmax, o->grid.imax },
|
||||
(int[NDIMS]) { o->grid->kmax, o->grid->jmax, o->grid->imax },
|
||||
(int[NDIMS]) { kmaxLocal, jmaxLocal, imaxLocal },
|
||||
offsets,
|
||||
MPI_ORDER_C,
|
||||
|
@@ -41,14 +41,14 @@ static void writeHeader(VtkOptions* o)
|
||||
}
|
||||
|
||||
fprintf(o->fh, "DATASET STRUCTURED_POINTS\n");
|
||||
fprintf(o->fh, "DIMENSIONS %d %d %d\n", o->grid.imax, o->grid.jmax, o->grid.kmax);
|
||||
fprintf(o->fh, "DIMENSIONS %d %d %d\n", o->grid->imax, o->grid->jmax, o->grid->kmax);
|
||||
fprintf(o->fh,
|
||||
"ORIGIN %f %f %f\n",
|
||||
o->grid.dx * 0.5,
|
||||
o->grid.dy * 0.5,
|
||||
o->grid.dz * 0.5);
|
||||
fprintf(o->fh, "SPACING %f %f %f\n", o->grid.dx, o->grid.dy, o->grid.dz);
|
||||
fprintf(o->fh, "POINT_DATA %d\n", o->grid.imax * o->grid.jmax * o->grid.kmax);
|
||||
o->grid->dx * 0.5,
|
||||
o->grid->dy * 0.5,
|
||||
o->grid->dz * 0.5);
|
||||
fprintf(o->fh, "SPACING %f %f %f\n", o->grid->dx, o->grid->dy, o->grid->dz);
|
||||
fprintf(o->fh, "POINT_DATA %d\n", o->grid->imax * o->grid->jmax * o->grid->kmax);
|
||||
}
|
||||
|
||||
void vtkOpen(VtkOptions* o, char* problem)
|
||||
@@ -64,9 +64,9 @@ void vtkOpen(VtkOptions* o, char* problem)
|
||||
|
||||
static void writeScalar(VtkOptions* o, double* s)
|
||||
{
|
||||
int imax = o->grid.imax;
|
||||
int jmax = o->grid.jmax;
|
||||
int kmax = o->grid.kmax;
|
||||
int imax = o->grid->imax;
|
||||
int jmax = o->grid->jmax;
|
||||
int kmax = o->grid->kmax;
|
||||
|
||||
for (int k = 0; k < kmax; k++) {
|
||||
for (int j = 0; j < jmax; j++) {
|
||||
@@ -105,9 +105,9 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
|
||||
|
||||
static void writeVector(VtkOptions* o, VtkVector vec)
|
||||
{
|
||||
int imax = o->grid.imax;
|
||||
int jmax = o->grid.jmax;
|
||||
int kmax = o->grid.kmax;
|
||||
int imax = o->grid->imax;
|
||||
int jmax = o->grid->jmax;
|
||||
int kmax = o->grid->kmax;
|
||||
|
||||
for (int k = 0; k < kmax; k++) {
|
||||
for (int j = 0; j < jmax; j++) {
|
||||
|
@@ -14,7 +14,7 @@
|
||||
typedef enum VtkFormat { ASCII = 0, BINARY } VtkFormat;
|
||||
|
||||
typedef struct VtkOptions {
|
||||
Grid grid;
|
||||
Grid* grid;
|
||||
#ifdef _VTK_WRITER_MPI
|
||||
MPI_File fh;
|
||||
#else
|
||||
|
@@ -18,9 +18,10 @@ include $(MAKE_DIR)/include_$(TAG).mk
|
||||
INCLUDES += -I$(SRC_DIR) -I$(BUILD_DIR)
|
||||
|
||||
VPATH = $(SRC_DIR)
|
||||
SRC = $(wildcard $(SRC_DIR)/*.c)
|
||||
SRC = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c))
|
||||
ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s, $(SRC))
|
||||
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
|
||||
OBJ += $(BUILD_DIR)/solver-$(SOLVER).o
|
||||
SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h)
|
||||
CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(OPTIONS) $(INCLUDES)
|
||||
|
||||
@@ -39,6 +40,18 @@ $(BUILD_DIR)/%.s: %.c
|
||||
|
||||
.PHONY: clean distclean tags info asm format
|
||||
|
||||
vis:
|
||||
$(info ===> GENERATE VISUALIZATION)
|
||||
@gnuplot -e "filename='residual.dat'" ./residual.plot
|
||||
|
||||
vis_clean:
|
||||
$(info ===> CLEAN VISUALIZATION)
|
||||
@rm -f *.dat
|
||||
@rm -f *.vtk
|
||||
@rm -f *.png
|
||||
|
||||
clean: vis_clean
|
||||
|
||||
clean:
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@@ -47,6 +60,8 @@ clean:
|
||||
distclean: clean
|
||||
$(info ===> DIST CLEAN)
|
||||
@rm -f $(TARGET)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
|
||||
info:
|
||||
$(info $(CFLAGS))
|
||||
|
@@ -38,10 +38,17 @@ kmax 50 # number of interior cells in z-direction
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 100.0 # final time
|
||||
te 60.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 5 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
|
@@ -1,12 +1,12 @@
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= CLANG
|
||||
# Supported: GCC, CLANG, ICX
|
||||
TAG ?= ICX
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: rb, mg
|
||||
SOLVER ?= mg
|
||||
# Run in debug settings
|
||||
DEBUG ?= false
|
||||
|
||||
#Feature options
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
OPTIONS += -DVERBOSE
|
||||
#OPTIONS += -DDEBUG
|
||||
#OPTIONS += -DBOUNDCHECK
|
||||
#OPTIONS += -DVERBOSE_AFFINITY
|
||||
#OPTIONS += -DVERBOSE_DATASIZE
|
||||
#OPTIONS += -DVERBOSE_TIMER
|
||||
|
@@ -38,15 +38,23 @@ kmax 128 # number of interior cells in z-direction
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 2.0 # final time
|
||||
te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 1000 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
rho 0.5
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
#===============================================================================
|
||||
|
@@ -2,16 +2,18 @@ CC = clang
|
||||
GCC = cc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(ENABLE_OPENMP),true)
|
||||
ifeq ($(strip $(ENABLE_OPENMP)),true)
|
||||
OPENMP = -fopenmp
|
||||
#OPENMP = -Xpreprocessor -fopenmp #required on Macos with homebrew libomp
|
||||
LIBS = # -lomp
|
||||
endif
|
||||
ifeq ($(strip $(DEBUG)),true)
|
||||
CFLAGS = -O0 -g -std=c17
|
||||
else
|
||||
CFLAGS = -O3 -std=c17 $(OPENMP)
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
# CFLAGS = -O3 -std=c17 $(OPENMP)
|
||||
CFLAGS = -Ofast -std=c17 #-Weverything
|
||||
#CFLAGS = -Ofast -fnt-store=aggressive -std=c99 $(OPENMP) #AMD CLANG
|
||||
LFLAGS = $(OPENMP) -lm
|
||||
DEFINES = -D_GNU_SOURCE# -DDEBUG
|
||||
DEFINES = -D_GNU_SOURCE
|
||||
INCLUDES =
|
||||
|
@@ -1,4 +1,4 @@
|
||||
CC = icc
|
||||
CC = icx
|
||||
GCC = gcc
|
||||
LINKER = $(CC)
|
||||
|
||||
@@ -7,7 +7,7 @@ OPENMP = -qopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP)
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP) -Wno-unused-command-line-argument
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES = -D_GNU_SOURCE
|
||||
INCLUDES =
|
9
BasicSolver/3D-seq/residual.plot
Normal file
9
BasicSolver/3D-seq/residual.plot
Normal file
@@ -0,0 +1,9 @@
|
||||
set terminal png size 1800,768 enhanced font ,12
|
||||
set output 'residual.png'
|
||||
set datafile separator whitespace
|
||||
set xlabel "Timestep"
|
||||
set ylabel "Residual"
|
||||
|
||||
set logscale y 2
|
||||
|
||||
plot 'residual.dat' using 1:2 title "Residual"
|
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
@@ -10,131 +10,120 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
#define P(i, j, k) p[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define F(i, j, k) f[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define G(i, j, k) g[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define H(i, j, k) h[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define U(i, j, k) u[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define V(i, j, k) v[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define W(i, j, k) w[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define RHS(i, j, k) rhs[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
|
||||
static void printConfig(Solver* s)
|
||||
static void printConfig(Discretization* d)
|
||||
{
|
||||
printf("Parameters for #%s#\n", s->problem);
|
||||
printf("Parameters for #%s#\n", d->problem);
|
||||
printf("BC Left:%d Right:%d Bottom:%d Top:%d Front:%d Back:%d\n",
|
||||
s->bcLeft,
|
||||
s->bcRight,
|
||||
s->bcBottom,
|
||||
s->bcTop,
|
||||
s->bcFront,
|
||||
s->bcBack);
|
||||
printf("\tReynolds number: %.2f\n", s->re);
|
||||
printf("\tGx Gy: %.2f %.2f %.2f\n", s->gx, s->gy, s->gz);
|
||||
d->bcLeft,
|
||||
d->bcRight,
|
||||
d->bcBottom,
|
||||
d->bcTop,
|
||||
d->bcFront,
|
||||
d->bcBack);
|
||||
printf("\tReynolds number: %.2f\n", d->re);
|
||||
printf("\tGx Gy: %.2f %.2f %.2f\n", d->gx, d->gy, d->gz);
|
||||
printf("Geometry data:\n");
|
||||
printf("\tDomain box size (x, y, z): %.2f, %.2f, %.2f\n",
|
||||
s->grid.xlength,
|
||||
s->grid.ylength,
|
||||
s->grid.zlength);
|
||||
printf("\tCells (x, y, z): %d, %d, %d\n", s->grid.imax, s->grid.jmax, s->grid.kmax);
|
||||
printf("\tCell size (dx, dy, dz): %f, %f, %f\n", s->grid.dx, s->grid.dy, s->grid.dz);
|
||||
d->grid.xlength,
|
||||
d->grid.ylength,
|
||||
d->grid.zlength);
|
||||
printf("\tCells (x, y, z): %d, %d, %d\n", d->grid.imax, d->grid.jmax, d->grid.kmax);
|
||||
printf("\tCell size (dx, dy, dz): %f, %f, %f\n", d->grid.dx, d->grid.dy, d->grid.dz);
|
||||
printf("Timestep parameters:\n");
|
||||
printf("\tDefault stepsize: %.2f, Final time %.2f\n", s->dt, s->te);
|
||||
printf("\tdt bound: %.6f\n", s->dtBound);
|
||||
printf("\tTau factor: %.2f\n", s->tau);
|
||||
printf("\tDefault stepsize: %.2f, Final time %.2f\n", d->dt, d->te);
|
||||
printf("\tdt bound: %.6f\n", d->dtBound);
|
||||
printf("\tTau factor: %.2f\n", d->tau);
|
||||
printf("Iterative parameters:\n");
|
||||
printf("\tMax iterations: %d\n", s->itermax);
|
||||
printf("\tepsilon (stopping tolerance) : %f\n", s->eps);
|
||||
printf("\tgamma factor: %f\n", s->gamma);
|
||||
printf("\tomega (SOR relaxation): %f\n", s->omega);
|
||||
printf("\tepsilon (stopping tolerance) : %f\n", d->eps);
|
||||
printf("\tgamma factor: %f\n", d->gamma);
|
||||
printf("\tomega (SOR relaxation): %f\n", d->omega);
|
||||
}
|
||||
|
||||
void initSolver(Solver* s, Parameter* params)
|
||||
void initDiscretization(Discretization* d, Parameter* p)
|
||||
{
|
||||
s->problem = params->name;
|
||||
s->bcLeft = params->bcLeft;
|
||||
s->bcRight = params->bcRight;
|
||||
s->bcBottom = params->bcBottom;
|
||||
s->bcTop = params->bcTop;
|
||||
s->bcFront = params->bcFront;
|
||||
s->bcBack = params->bcBack;
|
||||
d->problem = p->name;
|
||||
d->bcLeft = p->bcLeft;
|
||||
d->bcRight = p->bcRight;
|
||||
d->bcBottom = p->bcBottom;
|
||||
d->bcTop = p->bcTop;
|
||||
d->bcFront = p->bcFront;
|
||||
d->bcBack = p->bcBack;
|
||||
|
||||
s->grid.imax = params->imax;
|
||||
s->grid.jmax = params->jmax;
|
||||
s->grid.kmax = params->kmax;
|
||||
s->grid.xlength = params->xlength;
|
||||
s->grid.ylength = params->ylength;
|
||||
s->grid.zlength = params->zlength;
|
||||
s->grid.dx = params->xlength / params->imax;
|
||||
s->grid.dy = params->ylength / params->jmax;
|
||||
s->grid.dz = params->zlength / params->kmax;
|
||||
d->grid.imax = p->imax;
|
||||
d->grid.jmax = p->jmax;
|
||||
d->grid.kmax = p->kmax;
|
||||
d->grid.xlength = p->xlength;
|
||||
d->grid.ylength = p->ylength;
|
||||
d->grid.zlength = p->zlength;
|
||||
d->grid.dx = p->xlength / p->imax;
|
||||
d->grid.dy = p->ylength / p->jmax;
|
||||
d->grid.dz = p->zlength / p->kmax;
|
||||
|
||||
s->eps = params->eps;
|
||||
s->omega = params->omg;
|
||||
s->itermax = params->itermax;
|
||||
s->re = params->re;
|
||||
s->gx = params->gx;
|
||||
s->gy = params->gy;
|
||||
s->gz = params->gz;
|
||||
s->dt = params->dt;
|
||||
s->te = params->te;
|
||||
s->tau = params->tau;
|
||||
s->gamma = params->gamma;
|
||||
d->eps = p->eps;
|
||||
d->omega = p->omg;
|
||||
d->re = p->re;
|
||||
d->gx = p->gx;
|
||||
d->gy = p->gy;
|
||||
d->gz = p->gz;
|
||||
d->dt = p->dt;
|
||||
d->te = p->te;
|
||||
d->tau = p->tau;
|
||||
d->gamma = p->gamma;
|
||||
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
int kmax = d->grid.kmax;
|
||||
size_t bytesize = (imax + 2) * (jmax + 2) * (kmax + 2) * sizeof(double);
|
||||
s->u = allocate(64, bytesize);
|
||||
s->v = allocate(64, bytesize);
|
||||
s->w = allocate(64, bytesize);
|
||||
s->p = allocate(64, bytesize);
|
||||
s->rhs = allocate(64, bytesize);
|
||||
s->f = allocate(64, bytesize);
|
||||
s->g = allocate(64, bytesize);
|
||||
s->h = allocate(64, bytesize);
|
||||
d->u = allocate(64, bytesize);
|
||||
d->v = allocate(64, bytesize);
|
||||
d->w = allocate(64, bytesize);
|
||||
d->p = allocate(64, bytesize);
|
||||
d->rhs = allocate(64, bytesize);
|
||||
d->f = allocate(64, bytesize);
|
||||
d->g = allocate(64, bytesize);
|
||||
d->h = allocate(64, bytesize);
|
||||
|
||||
for (int i = 0; i < (imax + 2) * (jmax + 2) * (kmax + 2); i++) {
|
||||
s->u[i] = params->u_init;
|
||||
s->v[i] = params->v_init;
|
||||
s->w[i] = params->w_init;
|
||||
s->p[i] = params->p_init;
|
||||
s->rhs[i] = 0.0;
|
||||
s->f[i] = 0.0;
|
||||
s->g[i] = 0.0;
|
||||
s->h[i] = 0.0;
|
||||
d->u[i] = p->u_init;
|
||||
d->v[i] = p->v_init;
|
||||
d->w[i] = p->w_init;
|
||||
d->p[i] = p->p_init;
|
||||
d->rhs[i] = 0.0;
|
||||
d->f[i] = 0.0;
|
||||
d->g[i] = 0.0;
|
||||
d->h[i] = 0.0;
|
||||
}
|
||||
|
||||
double dx = s->grid.dx;
|
||||
double dy = s->grid.dy;
|
||||
double dz = s->grid.dz;
|
||||
double dx = d->grid.dx;
|
||||
double dy = d->grid.dy;
|
||||
double dz = d->grid.dz;
|
||||
|
||||
double invSqrSum = 1.0 / (dx * dx) + 1.0 / (dy * dy) + 1.0 / (dz * dz);
|
||||
s->dtBound = 0.5 * s->re * 1.0 / invSqrSum;
|
||||
d->dtBound = 0.5 * d->re * 1.0 / invSqrSum;
|
||||
|
||||
#ifdef VERBOSE
|
||||
printConfig(s);
|
||||
printConfig(d);
|
||||
#endif /* VERBOSE */
|
||||
}
|
||||
|
||||
void computeRHS(Solver* s)
|
||||
void computeRHS(Discretization* d)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
double idx = 1.0 / s->grid.dx;
|
||||
double idy = 1.0 / s->grid.dy;
|
||||
double idz = 1.0 / s->grid.dz;
|
||||
double idt = 1.0 / s->dt;
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
int kmax = d->grid.kmax;
|
||||
double idx = 1.0 / d->grid.dx;
|
||||
double idy = 1.0 / d->grid.dy;
|
||||
double idz = 1.0 / d->grid.dz;
|
||||
double idt = 1.0 / d->dt;
|
||||
|
||||
double* rhs = s->rhs;
|
||||
double* f = s->f;
|
||||
double* g = s->g;
|
||||
double* h = s->h;
|
||||
double* rhs = d->rhs;
|
||||
double* f = d->f;
|
||||
double* g = d->g;
|
||||
double* h = d->h;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
@@ -148,94 +137,9 @@ void computeRHS(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void solve(Solver* s)
|
||||
static double maxElement(Discretization* d, double* m)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid.dx * s->grid.dx;
|
||||
double dy2 = s->grid.dy * s->grid.dy;
|
||||
double dz2 = s->grid.dz * s->grid.dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double* p = s->p;
|
||||
double* rhs = s->rhs;
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, ksw, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
res = 0.0;
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
double r =
|
||||
RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2);
|
||||
|
||||
P(i, j, k) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
P(i, j, kmax + 1) = P(i, j, kmax);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
P(i, jmax + 1, k) = P(i, jmax, k);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
P(imax + 1, j, k) = P(imax, j, k);
|
||||
}
|
||||
}
|
||||
|
||||
res = res / (double)(imax * jmax * kmax);
|
||||
#ifdef DEBUG
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
#endif
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
#endif
|
||||
}
|
||||
|
||||
static double maxElement(Solver* s, double* m)
|
||||
{
|
||||
int size = (s->grid.imax + 2) * (s->grid.jmax + 2) * (s->grid.kmax + 2);
|
||||
int size = (d->grid.imax + 2) * (d->grid.jmax + 2) * (d->grid.kmax + 2);
|
||||
double maxval = DBL_MIN;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
@@ -245,10 +149,10 @@ static double maxElement(Solver* s, double* m)
|
||||
return maxval;
|
||||
}
|
||||
|
||||
void normalizePressure(Solver* s)
|
||||
void normalizePressure(Discretization* d)
|
||||
{
|
||||
int size = (s->grid.imax + 2) * (s->grid.jmax + 2) * (s->grid.kmax + 2);
|
||||
double* p = s->p;
|
||||
int size = (d->grid.imax + 2) * (d->grid.jmax + 2) * (d->grid.kmax + 2);
|
||||
double* p = d->p;
|
||||
double avgP = 0.0;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
@@ -261,16 +165,16 @@ void normalizePressure(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void computeTimestep(Solver* s)
|
||||
void computeTimestep(Discretization* d)
|
||||
{
|
||||
double dt = s->dtBound;
|
||||
double dx = s->grid.dx;
|
||||
double dy = s->grid.dy;
|
||||
double dz = s->grid.dz;
|
||||
double dt = d->dtBound;
|
||||
double dx = d->grid.dx;
|
||||
double dy = d->grid.dy;
|
||||
double dz = d->grid.dz;
|
||||
|
||||
double umax = maxElement(s, s->u);
|
||||
double vmax = maxElement(s, s->v);
|
||||
double wmax = maxElement(s, s->w);
|
||||
double umax = maxElement(d, d->u);
|
||||
double vmax = maxElement(d, d->v);
|
||||
double wmax = maxElement(d, d->w);
|
||||
|
||||
if (umax > 0) {
|
||||
dt = (dt > dx / umax) ? dx / umax : dt;
|
||||
@@ -282,20 +186,20 @@ void computeTimestep(Solver* s)
|
||||
dt = (dt > dz / wmax) ? dz / wmax : dt;
|
||||
}
|
||||
|
||||
s->dt = dt * s->tau;
|
||||
d->dt = dt * d->tau;
|
||||
}
|
||||
|
||||
void setBoundaryConditions(Solver* s)
|
||||
void setBoundaryConditions(Discretization* d)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
int kmax = d->grid.kmax;
|
||||
|
||||
double* u = s->u;
|
||||
double* v = s->v;
|
||||
double* w = s->w;
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
double* w = d->w;
|
||||
|
||||
switch (s->bcTop) {
|
||||
switch (d->bcTop) {
|
||||
case NOSLIP:
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
@@ -327,7 +231,7 @@ void setBoundaryConditions(Solver* s)
|
||||
break;
|
||||
}
|
||||
|
||||
switch (s->bcBottom) {
|
||||
switch (d->bcBottom) {
|
||||
case NOSLIP:
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
@@ -359,7 +263,7 @@ void setBoundaryConditions(Solver* s)
|
||||
break;
|
||||
}
|
||||
|
||||
switch (s->bcLeft) {
|
||||
switch (d->bcLeft) {
|
||||
case NOSLIP:
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
@@ -391,7 +295,7 @@ void setBoundaryConditions(Solver* s)
|
||||
break;
|
||||
}
|
||||
|
||||
switch (s->bcRight) {
|
||||
switch (d->bcRight) {
|
||||
case NOSLIP:
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
@@ -423,7 +327,7 @@ void setBoundaryConditions(Solver* s)
|
||||
break;
|
||||
}
|
||||
|
||||
switch (s->bcFront) {
|
||||
switch (d->bcFront) {
|
||||
case NOSLIP:
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
@@ -455,7 +359,7 @@ void setBoundaryConditions(Solver* s)
|
||||
break;
|
||||
}
|
||||
|
||||
switch (s->bcBack) {
|
||||
switch (d->bcBack) {
|
||||
case NOSLIP:
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
@@ -488,23 +392,23 @@ void setBoundaryConditions(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void setSpecialBoundaryCondition(Solver* s)
|
||||
void setSpecialBoundaryCondition(Discretization* d)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
int kmax = d->grid.kmax;
|
||||
|
||||
double mDy = s->grid.dy;
|
||||
double* u = s->u;
|
||||
double mDy = d->grid.dy;
|
||||
double* u = d->u;
|
||||
|
||||
if (strcmp(s->problem, "dcavity") == 0) {
|
||||
if (strcmp(d->problem, "dcavity") == 0) {
|
||||
for (int k = 1; k < kmax; k++) {
|
||||
for (int i = 1; i < imax; i++) {
|
||||
U(i, jmax + 1, k) = 2.0 - U(i, jmax, k);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(s->problem, "canal") == 0) {
|
||||
double ylength = s->grid.ylength;
|
||||
} else if (strcmp(d->problem, "canal") == 0) {
|
||||
double ylength = d->grid.ylength;
|
||||
double y;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
@@ -516,29 +420,29 @@ void setSpecialBoundaryCondition(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void computeFG(Solver* s)
|
||||
void computeFG(Discretization* d)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
int kmax = d->grid.kmax;
|
||||
|
||||
double* u = s->u;
|
||||
double* v = s->v;
|
||||
double* w = s->w;
|
||||
double* f = s->f;
|
||||
double* g = s->g;
|
||||
double* h = s->h;
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
double* w = d->w;
|
||||
double* f = d->f;
|
||||
double* g = d->g;
|
||||
double* h = d->h;
|
||||
|
||||
double gx = s->gx;
|
||||
double gy = s->gy;
|
||||
double gz = s->gz;
|
||||
double dt = s->dt;
|
||||
double gx = d->gx;
|
||||
double gy = d->gy;
|
||||
double gz = d->gz;
|
||||
double dt = d->dt;
|
||||
|
||||
double gamma = s->gamma;
|
||||
double inverseRe = 1.0 / s->re;
|
||||
double inverseDx = 1.0 / s->grid.dx;
|
||||
double inverseDy = 1.0 / s->grid.dy;
|
||||
double inverseDz = 1.0 / s->grid.dz;
|
||||
double gamma = d->gamma;
|
||||
double inverseRe = 1.0 / d->re;
|
||||
double inverseDx = 1.0 / d->grid.dx;
|
||||
double inverseDy = 1.0 / d->grid.dy;
|
||||
double inverseDz = 1.0 / d->grid.dz;
|
||||
double du2dx, dv2dy, dw2dz;
|
||||
double duvdx, duwdx, duvdy, dvwdy, duwdz, dvwdz;
|
||||
double du2dx2, du2dy2, du2dz2;
|
||||
@@ -705,23 +609,23 @@ void computeFG(Solver* s)
|
||||
}
|
||||
}
|
||||
|
||||
void adaptUV(Solver* s)
|
||||
void adaptUV(Discretization* d)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
int kmax = s->grid.kmax;
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
int kmax = d->grid.kmax;
|
||||
|
||||
double* p = s->p;
|
||||
double* u = s->u;
|
||||
double* v = s->v;
|
||||
double* w = s->w;
|
||||
double* f = s->f;
|
||||
double* g = s->g;
|
||||
double* h = s->h;
|
||||
double* p = d->p;
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
double* w = d->w;
|
||||
double* f = d->f;
|
||||
double* g = d->g;
|
||||
double* h = d->h;
|
||||
|
||||
double factorX = s->dt / s->grid.dx;
|
||||
double factorY = s->dt / s->grid.dy;
|
||||
double factorZ = s->dt / s->grid.dz;
|
||||
double factorX = d->dt / d->grid.dx;
|
||||
double factorY = d->dt / d->grid.dy;
|
||||
double factorZ = d->dt / d->grid.dz;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
41
BasicSolver/3D-seq/src/discretization.h
Normal file
41
BasicSolver/3D-seq/src/discretization.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __DISCRETIZATION_H_
|
||||
#define __DISCRETIZATION_H_
|
||||
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
|
||||
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
|
||||
|
||||
typedef struct {
|
||||
/* geometry and grid information */
|
||||
Grid grid;
|
||||
/* arrays */
|
||||
double *p, *rhs;
|
||||
double *f, *g, *h;
|
||||
double *u, *v, *w;
|
||||
/* parameters */
|
||||
double eps, omega;
|
||||
double re, tau, gamma;
|
||||
double gx, gy, gz;
|
||||
/* time stepping */
|
||||
double dt, te;
|
||||
double dtBound;
|
||||
char* problem;
|
||||
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
|
||||
} Discretization;
|
||||
|
||||
extern void initDiscretization(Discretization*, Parameter*);
|
||||
extern void computeRHS(Discretization*);
|
||||
extern void normalizePressure(Discretization*);
|
||||
extern void computeTimestep(Discretization*);
|
||||
extern void setBoundaryConditions(Discretization*);
|
||||
extern void setSpecialBoundaryCondition(Discretization*);
|
||||
extern void computeFG(Discretization*);
|
||||
extern void adaptUV(Discretization*);
|
||||
#endif
|
@@ -9,6 +9,7 @@
|
||||
#include <unistd.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "progress.h"
|
||||
#include "solver.h"
|
||||
@@ -17,7 +18,27 @@
|
||||
|
||||
#define G(v, i, j, k) v[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
|
||||
static void createBulkArrays(Solver* s, double* pg, double* ug, double* vg, double* wg)
|
||||
static FILE* initResidualWriter()
|
||||
{
|
||||
FILE* fp;
|
||||
fp = fopen("residual.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return fp;
|
||||
|
||||
}
|
||||
|
||||
static void writeResidual(FILE* fp, double ts, double res)
|
||||
{
|
||||
fprintf(fp, "%f, %f\n", ts, res);
|
||||
}
|
||||
|
||||
static void createBulkArrays(
|
||||
Discretization* s, double* pg, double* ug, double* vg, double* wg)
|
||||
{
|
||||
int imax = s->grid.imax;
|
||||
int jmax = s->grid.jmax;
|
||||
@@ -67,9 +88,13 @@ int main(int argc, char** argv)
|
||||
{
|
||||
double timeStart, timeStop;
|
||||
Parameter p;
|
||||
Discretization d;
|
||||
Solver s;
|
||||
initParameter(&p);
|
||||
|
||||
FILE* fp;
|
||||
fp = initResidualWriter();
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <configFile>\n", argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
@@ -77,51 +102,58 @@ int main(int argc, char** argv)
|
||||
|
||||
readParameter(&p, argv[1]);
|
||||
printParameter(&p);
|
||||
initSolver(&s, &p);
|
||||
initDiscretization(&d, &p);
|
||||
initSolver(&s, &d, &p);
|
||||
#ifndef VERBOSE
|
||||
initProgress(s.te);
|
||||
initProgress(d.te);
|
||||
#endif
|
||||
|
||||
double tau = s.tau;
|
||||
double te = s.te;
|
||||
double tau = d.tau;
|
||||
double te = d.te;
|
||||
double t = 0.0;
|
||||
int nt = 0;
|
||||
double res = 0.0;
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
while (t <= te) {
|
||||
if (tau > 0.0) computeTimestep(&s);
|
||||
setBoundaryConditions(&s);
|
||||
setSpecialBoundaryCondition(&s);
|
||||
computeFG(&s);
|
||||
computeRHS(&s);
|
||||
solve(&s);
|
||||
adaptUV(&s);
|
||||
t += s.dt;
|
||||
if (tau > 0.0) computeTimestep(&d);
|
||||
setBoundaryConditions(&d);
|
||||
setSpecialBoundaryCondition(&d);
|
||||
computeFG(&d);
|
||||
computeRHS(&d);
|
||||
if (nt % 100 == 0) normalizePressure(&d);
|
||||
res = solve(&s, d.p, d.rhs);
|
||||
adaptUV(&d);
|
||||
|
||||
writeResidual(fp, t, res);
|
||||
|
||||
t += d.dt;
|
||||
nt++;
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("TIME %f , TIMESTEP %f\n", t, s.dt);
|
||||
printf("TIME %f , TIMESTEP %f\n", t, d.dt);
|
||||
#else
|
||||
printProgress(t);
|
||||
#endif
|
||||
}
|
||||
timeStop = getTimeStamp();
|
||||
#ifndef VERBOSE
|
||||
stopProgress();
|
||||
#endif
|
||||
printf("Solution took %.2fs\n", timeStop - timeStart);
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
double *pg, *ug, *vg, *wg;
|
||||
|
||||
size_t bytesize = (size_t)(s.grid.imax * s.grid.jmax * s.grid.kmax) * sizeof(double);
|
||||
size_t bytesize = (size_t)(d.grid.imax * d.grid.jmax * d.grid.kmax) * sizeof(double);
|
||||
|
||||
pg = allocate(64, bytesize);
|
||||
ug = allocate(64, bytesize);
|
||||
vg = allocate(64, bytesize);
|
||||
wg = allocate(64, bytesize);
|
||||
|
||||
createBulkArrays(&s, pg, ug, vg, wg);
|
||||
VtkOptions opts = { .grid = s.grid };
|
||||
vtkOpen(&opts, s.problem);
|
||||
|
||||
fclose(fp);
|
||||
createBulkArrays(&d, pg, ug, vg, wg);
|
||||
VtkOptions opts = { .grid = d.grid };
|
||||
vtkOpen(&opts, d.problem);
|
||||
vtkScalar(&opts, "pressure", pg);
|
||||
vtkVector(&opts, "velocity", (VtkVector) { ug, vg, wg });
|
||||
vtkClose(&opts);
|
||||
|
@@ -14,18 +14,21 @@
|
||||
|
||||
void initParameter(Parameter* param)
|
||||
{
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->zlength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->kmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.7;
|
||||
param->re = 100.0;
|
||||
param->gamma = 0.9;
|
||||
param->tau = 0.5;
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->zlength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->kmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.7;
|
||||
param->re = 100.0;
|
||||
param->gamma = 0.9;
|
||||
param->tau = 0.5;
|
||||
param->levels = 5;
|
||||
param->presmooth = 5;
|
||||
param->postsmooth = 5;
|
||||
}
|
||||
|
||||
void readParameter(Parameter* param, const char* filename)
|
||||
@@ -65,6 +68,7 @@ void readParameter(Parameter* param, const char* filename)
|
||||
PARSE_INT(jmax);
|
||||
PARSE_INT(kmax);
|
||||
PARSE_INT(itermax);
|
||||
PARSE_INT(levels);
|
||||
PARSE_REAL(eps);
|
||||
PARSE_REAL(omg);
|
||||
PARSE_REAL(re);
|
||||
@@ -123,4 +127,5 @@ void printParameter(Parameter* param)
|
||||
printf("\tepsilon (stopping tolerance) : %f\n", param->eps);
|
||||
printf("\tgamma (stopping tolerance) : %f\n", param->gamma);
|
||||
printf("\tomega (SOR relaxation): %f\n", param->omg);
|
||||
printf("\tMultiGrid levels : %d\n", param->levels);
|
||||
}
|
||||
|
@@ -10,14 +10,15 @@
|
||||
typedef struct {
|
||||
int imax, jmax, kmax;
|
||||
double xlength, ylength, zlength;
|
||||
int itermax;
|
||||
double eps, omg;
|
||||
int itermax, levels;
|
||||
double eps, omg, rho;
|
||||
double re, tau, gamma;
|
||||
double te, dt;
|
||||
double gx, gy, gz;
|
||||
char* name;
|
||||
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
|
||||
double u_init, v_init, w_init, p_init;
|
||||
int presmooth, postsmooth;
|
||||
} Parameter;
|
||||
|
||||
void initParameter(Parameter*);
|
||||
|
@@ -48,4 +48,4 @@ void stopProgress()
|
||||
{
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
@@ -10,5 +10,4 @@
|
||||
extern void initProgress(double);
|
||||
extern void printProgress(double);
|
||||
extern void stopProgress(void);
|
||||
|
||||
#endif
|
||||
|
304
BasicSolver/3D-seq/src/solver-mg.c
Normal file
304
BasicSolver/3D-seq/src/solver-mg.c
Normal file
@@ -0,0 +1,304 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
#define FINEST_LEVEL 0
|
||||
#define COARSEST_LEVEL (s->levels - 1)
|
||||
#define S(i, j, k) s[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define E(i, j, k) e[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define R(i, j, k) r[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define OLD(i, j, k) old[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
|
||||
static void restrictMG(Solver* s, int level, int imax, int jmax, int kmax)
|
||||
{
|
||||
double* r = s->r[level + 1];
|
||||
double* old = s->r[level];
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; ++i) {
|
||||
R(i, j, k) = (OLD(2 * i - 1, 2 * j - 1, 2 * k) +
|
||||
OLD(2 * i, 2 * j - 1, 2 * k) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1, 2 * k) +
|
||||
OLD(2 * i - 1, 2 * j, 2 * k) * 2 +
|
||||
OLD(2 * i, 2 * j, 2 * k) * 8 +
|
||||
OLD(2 * i + 1, 2 * j, 2 * k) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1, 2 * k) +
|
||||
OLD(2 * i, 2 * j + 1, 2 * k) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1, 2 * k) +
|
||||
|
||||
OLD(2 * i - 1, 2 * j - 1, 2 * k - 1) +
|
||||
OLD(2 * i, 2 * j - 1, 2 * k - 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1, 2 * k - 1) +
|
||||
OLD(2 * i - 1, 2 * j, 2 * k - 1) * 2 +
|
||||
OLD(2 * i, 2 * j, 2 * k - 1) * 4 +
|
||||
OLD(2 * i + 1, 2 * j, 2 * k - 1) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1, 2 * k - 1) +
|
||||
OLD(2 * i, 2 * j + 1, 2 * k - 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1, 2 * k - 1) +
|
||||
|
||||
OLD(2 * i - 1, 2 * j - 1, 2 * k + 1) +
|
||||
OLD(2 * i, 2 * j - 1, 2 * k + 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j - 1, 2 * k + 1) +
|
||||
OLD(2 * i - 1, 2 * j, 2 * k + 1) * 2 +
|
||||
OLD(2 * i, 2 * j, 2 * k + 1) * 4 +
|
||||
OLD(2 * i + 1, 2 * j, 2 * k + 1) * 2 +
|
||||
OLD(2 * i - 1, 2 * j + 1, 2 * k + 1) +
|
||||
OLD(2 * i, 2 * j + 1, 2 * k + 1) * 2 +
|
||||
OLD(2 * i + 1, 2 * j + 1, 2 * k + 1)) /
|
||||
64.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void prolongate(Solver* s, int level, int imax, int jmax, int kmax)
|
||||
{
|
||||
double* old = s->r[level + 1];
|
||||
double* e = s->r[level];
|
||||
|
||||
for (int k = 2; k < kmax + 1; k += 2) {
|
||||
for (int j = 2; j < jmax + 1; j += 2) {
|
||||
for (int i = 2; i < imax + 1; i += 2) {
|
||||
E(i, j, k) = OLD(i / 2, j / 2, k / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void correct(Solver* s, double* p, int level, int imax, int jmax, int kmax)
|
||||
{
|
||||
double* e = s->e[level];
|
||||
|
||||
for (int k = 1; k < kmax + 1; ++k) {
|
||||
for (int j = 1; j < jmax + 1; ++j) {
|
||||
for (int i = 1; i < imax + 1; ++i) {
|
||||
P(i, j, k) += E(i, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void setBoundaryCondition(double* p, int imax, int jmax, int kmax)
|
||||
{
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
P(i, j, kmax + 1) = P(i, j, kmax);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
P(i, jmax + 1, k) = P(i, jmax, k);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
P(imax + 1, j, k) = P(imax, j, k);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void smooth(
|
||||
Solver* s, double* p, double* rhs, int level, int imax, int jmax, int kmax)
|
||||
{
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double* r = s->r[level];
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
int pass, ksw, jsw, isw;
|
||||
double res = 1.0;
|
||||
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
P(i, j, k) -=
|
||||
factor *
|
||||
(RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
}
|
||||
|
||||
static double calculateResidual(
|
||||
Solver* s, double* p, double* rhs, int level, int imax, int jmax, int kmax)
|
||||
{
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double* r = s->r[level];
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
int pass, ksw, jsw, isw;
|
||||
double res = 1.0;
|
||||
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
R(i,
|
||||
j,
|
||||
k) = (RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) *
|
||||
idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2));
|
||||
|
||||
res += (R(i, j, k) * R(i, j, k));
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
|
||||
res = res / (double)(imax * jmax * kmax);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
static double multiGrid(
|
||||
Solver* s, double* p, double* rhs, int level, int imax, int jmax, int kmax)
|
||||
{
|
||||
double res = 0.0;
|
||||
|
||||
// coarsest level
|
||||
if (level == COARSEST_LEVEL) {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
smooth(s, p, rhs, level, imax, jmax, kmax);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// pre-smoothing
|
||||
for (int i = 0; i < s->presmooth; i++) {
|
||||
smooth(s, p, rhs, level, imax, jmax, kmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax, kmax);
|
||||
}
|
||||
|
||||
res = calculateResidual(s, p, rhs, level, imax, jmax, kmax);
|
||||
|
||||
// restrict
|
||||
restrictMG(s, level, imax, jmax, kmax);
|
||||
|
||||
// MGSolver on residual and error.
|
||||
multiGrid(s,
|
||||
s->e[level + 1],
|
||||
s->r[level + 1],
|
||||
level + 1,
|
||||
imax / 2,
|
||||
jmax / 2,
|
||||
kmax / 2);
|
||||
|
||||
// prolongate
|
||||
prolongate(s, level, imax, jmax, kmax);
|
||||
|
||||
// correct p on finer level using residual
|
||||
correct(s, p, level, imax, jmax, kmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax, kmax);
|
||||
|
||||
// post-smoothing
|
||||
for (int i = 0; i < s->postsmooth; i++) {
|
||||
smooth(s, p, rhs, level, imax, jmax, kmax);
|
||||
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax, kmax);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
s->itermax = p->itermax;
|
||||
s->levels = p->levels;
|
||||
s->grid = &d->grid;
|
||||
s->presmooth = p->presmooth;
|
||||
s->postsmooth = p->postsmooth;
|
||||
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int kmax = s->grid->kmax;
|
||||
int levels = s->levels;
|
||||
printf("Using Multigrid solver with %d levels\n", levels);
|
||||
|
||||
s->r = malloc(levels * sizeof(double*));
|
||||
s->e = malloc(levels * sizeof(double*));
|
||||
|
||||
size_t size = (imax + 2) * (jmax + 2) * (kmax + 2);
|
||||
|
||||
for (int j = 0; j < levels; j++) {
|
||||
s->r[j] = allocate(64, size * sizeof(double));
|
||||
s->e[j] = allocate(64, size * sizeof(double));
|
||||
|
||||
for (size_t i = 0; i < size; i++) {
|
||||
s->r[j][i] = 0.0;
|
||||
s->e[j][i] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax, s->grid->kmax);
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Residuum: %.6f\n", res);
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
101
BasicSolver/3D-seq/src/solver-rb.c
Normal file
101
BasicSolver/3D-seq/src/solver-rb.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->grid = &d->grid;
|
||||
s->itermax = p->itermax;
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
}
|
||||
|
||||
double solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int kmax = s->grid->kmax;
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, ksw, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
res = 0.0;
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
double r =
|
||||
RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2);
|
||||
|
||||
P(i, j, k) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
P(i, j, kmax + 1) = P(i, j, kmax);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
P(i, jmax + 1, k) = P(i, jmax, k);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
P(imax + 1, j, k) = P(imax, j, k);
|
||||
}
|
||||
}
|
||||
|
||||
res = res / (double)(imax * jmax * kmax);
|
||||
#ifdef DEBUG
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
#endif
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
#endif
|
||||
|
||||
return res;
|
||||
}
|
99
BasicSolver/3D-seq/src/solver-sor.c
Normal file
99
BasicSolver/3D-seq/src/solver-sor.c
Normal file
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "solver.h"
|
||||
#include "util.h"
|
||||
|
||||
void initSolver(Solver* s, Discretization* d, Parameter* p)
|
||||
{
|
||||
s->grid = &d->grid;
|
||||
s->itermax = p->itermax;
|
||||
s->eps = p->eps;
|
||||
s->omega = p->omg;
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
int kmax = s->grid->kmax;
|
||||
double eps = s->eps;
|
||||
int itermax = s->itermax;
|
||||
double dx2 = s->grid->dx * s->grid->dx;
|
||||
double dy2 = s->grid->dy * s->grid->dy;
|
||||
double dz2 = s->grid->dz * s->grid->dz;
|
||||
double idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double idz2 = 1.0 / dz2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2 * dz2) /
|
||||
(dy2 * dz2 + dx2 * dz2 + dx2 * dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
double res = 1.0;
|
||||
int pass, ksw, jsw, isw;
|
||||
|
||||
while ((res >= epssq) && (it < itermax)) {
|
||||
res = 0.0;
|
||||
ksw = 1;
|
||||
|
||||
for (pass = 0; pass < 2; pass++) {
|
||||
jsw = ksw;
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
isw = jsw;
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = isw; i < imax + 1; i += 2) {
|
||||
|
||||
double r =
|
||||
RHS(i, j, k) -
|
||||
((P(i + 1, j, k) - 2.0 * P(i, j, k) + P(i - 1, j, k)) * idx2 +
|
||||
(P(i, j + 1, k) - 2.0 * P(i, j, k) + P(i, j - 1, k)) *
|
||||
idy2 +
|
||||
(P(i, j, k + 1) - 2.0 * P(i, j, k) + P(i, j, k - 1)) *
|
||||
idz2);
|
||||
|
||||
P(i, j, k) -= (factor * r);
|
||||
res += (r * r);
|
||||
}
|
||||
isw = 3 - isw;
|
||||
}
|
||||
jsw = 3 - jsw;
|
||||
}
|
||||
ksw = 3 - ksw;
|
||||
}
|
||||
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, j, 0) = P(i, j, 1);
|
||||
P(i, j, kmax + 1) = P(i, j, kmax);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int i = 1; i < imax + 1; i++) {
|
||||
P(i, 0, k) = P(i, 1, k);
|
||||
P(i, jmax + 1, k) = P(i, jmax, k);
|
||||
}
|
||||
}
|
||||
|
||||
for (int k = 1; k < kmax + 1; k++) {
|
||||
for (int j = 1; j < jmax + 1; j++) {
|
||||
P(0, j, k) = P(1, j, k);
|
||||
P(imax + 1, j, k) = P(imax, j, k);
|
||||
}
|
||||
}
|
||||
|
||||
res = res / (double)(imax * jmax * kmax);
|
||||
#ifdef DEBUG
|
||||
printf("%d Residuum: %e\n", it, res);
|
||||
#endif
|
||||
it++;
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
|
||||
#endif
|
||||
}
|
@@ -6,38 +6,22 @@
|
||||
*/
|
||||
#ifndef __SOLVER_H_
|
||||
#define __SOLVER_H_
|
||||
|
||||
#include "discretization.h"
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
|
||||
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
|
||||
|
||||
typedef struct {
|
||||
/* geometry and grid information */
|
||||
Grid grid;
|
||||
/* arrays */
|
||||
double *p, *rhs;
|
||||
double *f, *g, *h;
|
||||
double *u, *v, *w;
|
||||
Grid* grid;
|
||||
/* parameters */
|
||||
double eps, omega;
|
||||
double re, tau, gamma;
|
||||
double gx, gy, gz;
|
||||
/* time stepping */
|
||||
double eps, omega, rho;
|
||||
int itermax;
|
||||
double dt, te;
|
||||
double dtBound;
|
||||
char* problem;
|
||||
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
|
||||
int levels;
|
||||
double **r, **e;
|
||||
int presmooth, postsmooth;
|
||||
} Solver;
|
||||
|
||||
extern void initSolver(Solver*, Parameter*);
|
||||
extern void computeRHS(Solver*);
|
||||
extern void solve(Solver*);
|
||||
extern void normalizePressure(Solver*);
|
||||
extern void computeTimestep(Solver*);
|
||||
extern void setBoundaryConditions(Solver*);
|
||||
extern void setSpecialBoundaryCondition(Solver*);
|
||||
extern void computeFG(Solver*);
|
||||
extern void adaptUV(Solver*);
|
||||
extern void initSolver(Solver*, Discretization*, Parameter*);
|
||||
extern double solve(Solver*, double*, double*);
|
||||
|
||||
#endif
|
||||
|
@@ -20,3 +20,5 @@ double getTimeResolution(void)
|
||||
clock_getres(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
|
||||
}
|
||||
|
||||
|
||||
|
@@ -19,4 +19,13 @@
|
||||
#define ABS(a) ((a) >= 0 ? (a) : -(a))
|
||||
#endif
|
||||
|
||||
#define P(i, j, k) p[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define F(i, j, k) f[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define G(i, j, k) g[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define H(i, j, k) h[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define U(i, j, k) u[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define V(i, j, k) v[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define W(i, j, k) w[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
#define RHS(i, j, k) rhs[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)]
|
||||
|
||||
#endif // __UTIL_H_
|
||||
|
@@ -70,6 +70,7 @@ void vtkScalar(VtkOptions* o, char* name, double* s)
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fprintf(o->fh, "SCALARS %s float\n", name);
|
||||
fprintf(o->fh, "LOOKUP_TABLE default\n");
|
||||
|
||||
for (int k = 0; k < kmax; k++) {
|
||||
for (int j = 0; j < jmax; j++) {
|
||||
|
88
EnhancedSolver/2D-mpi/Makefile
Normal file
88
EnhancedSolver/2D-mpi/Makefile
Normal file
@@ -0,0 +1,88 @@
|
||||
#=======================================================================================
|
||||
# Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
# All rights reserved.
|
||||
# Use of this source code is governed by a MIT-style
|
||||
# license that can be found in the LICENSE file.
|
||||
#=======================================================================================
|
||||
|
||||
#CONFIGURE BUILD SYSTEM
|
||||
TARGET = exe-$(TAG)
|
||||
BUILD_DIR = ./$(TAG)
|
||||
SRC_DIR = ./src
|
||||
MAKE_DIR = ./
|
||||
Q ?= @
|
||||
|
||||
#DO NOT EDIT BELOW
|
||||
include $(MAKE_DIR)/config.mk
|
||||
include $(MAKE_DIR)/include_$(TAG).mk
|
||||
INCLUDES += -I$(SRC_DIR) -I$(BUILD_DIR)
|
||||
|
||||
VPATH = $(SRC_DIR)
|
||||
SRC = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c))
|
||||
ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s, $(SRC))
|
||||
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
|
||||
OBJ += $(BUILD_DIR)/comm-$(COMM_TYPE).o
|
||||
OBJ += $(BUILD_DIR)/solver-$(SOLVER).o
|
||||
SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h)
|
||||
CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(OPTIONS) $(INCLUDES)
|
||||
|
||||
${TARGET}: $(BUILD_DIR) $(OBJ)
|
||||
$(info ===> LINKING $(TARGET))
|
||||
$(Q)${LINKER} ${LFLAGS} -o $(TARGET) $(OBJ) $(LIBS)
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c $(MAKE_DIR)/include_$(TAG).mk $(MAKE_DIR)/config.mk
|
||||
$(info ===> COMPILE $@)
|
||||
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
$(Q)$(GCC) $(CPPFLAGS) -MT $(@:.d=.o) -MM $< > $(BUILD_DIR)/$*.d
|
||||
|
||||
$(BUILD_DIR)/%.s: %.c
|
||||
$(info ===> GENERATE ASM $@)
|
||||
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONY: clean distclean vis_clean vis tags info asm format
|
||||
|
||||
vis:
|
||||
$(info ===> GENERATE VISUALIZATION)
|
||||
@gnuplot -e "filename='pressure.dat'" ./surface.plot
|
||||
@gnuplot -e "filename='velocity.dat'" ./vector.plot
|
||||
@gnuplot -e "filename='residual.dat'" ./residual.plot
|
||||
|
||||
vis_clean:
|
||||
$(info ===> CLEAN VISUALIZATION)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
@rm -f ./vis_files/*.dat
|
||||
@rm -f ./vis_files/*.gif
|
||||
|
||||
clean: vis_clean
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -f tags
|
||||
|
||||
distclean: clean
|
||||
$(info ===> DIST CLEAN)
|
||||
@rm -f $(TARGET)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
|
||||
info:
|
||||
$(info $(CFLAGS))
|
||||
$(Q)$(CC) $(VERSION)
|
||||
|
||||
asm: $(BUILD_DIR) $(ASM)
|
||||
|
||||
tags:
|
||||
$(info ===> GENERATE TAGS)
|
||||
$(Q)ctags -R
|
||||
|
||||
format:
|
||||
@for src in $(SOURCES) ; do \
|
||||
echo "Formatting $$src" ; \
|
||||
clang-format -i $$src ; \
|
||||
done
|
||||
@echo "Done"
|
||||
|
||||
$(BUILD_DIR):
|
||||
@mkdir $(BUILD_DIR)
|
||||
|
||||
-include $(OBJ:.o=.d)
|
48
EnhancedSolver/2D-mpi/README.md
Normal file
48
EnhancedSolver/2D-mpi/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# C source skeleton
|
||||
|
||||
## Build
|
||||
|
||||
1. Configure the toolchain and additional options in `config.mk`:
|
||||
```
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= GCC
|
||||
ENABLE_OPENMP ?= false
|
||||
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
#OPTIONS += -DVERBOSE_AFFINITY
|
||||
#OPTIONS += -DVERBOSE_DATASIZE
|
||||
#OPTIONS += -DVERBOSE_TIMER
|
||||
```
|
||||
|
||||
The verbosity options enable detailed output about affinity settings, allocation sizes and timer resolution.
|
||||
|
||||
|
||||
2. Build with:
|
||||
```
|
||||
make
|
||||
```
|
||||
|
||||
You can build multiple toolchains in the same directory, but notice that the Makefile is only acting on the one currently set.
|
||||
Intermediate build results are located in the `<TOOLCHAIN>` directory.
|
||||
|
||||
To output the executed commands use:
|
||||
```
|
||||
make Q=
|
||||
```
|
||||
|
||||
3. Clean up with:
|
||||
```
|
||||
make clean
|
||||
```
|
||||
to clean intermediate build results.
|
||||
|
||||
```
|
||||
make distclean
|
||||
```
|
||||
to clean intermediate build results and binary.
|
||||
|
||||
4. (Optional) Generate assembler:
|
||||
```
|
||||
make asm
|
||||
```
|
||||
The assembler files will also be located in the `<TOOLCHAIN>` directory.
|
79
EnhancedSolver/2D-mpi/backstep.par
Normal file
79
EnhancedSolver/2D-mpi/backstep.par
Normal file
@@ -0,0 +1,79 @@
|
||||
#==============================================================================
|
||||
# Laminar Canal Flow
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name backstep # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 36000.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 1.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 7.0 # domain size in x-direction
|
||||
ylength 1.5 # domain size in y-direction
|
||||
imax 200 # number of interior cells in x-direction
|
||||
jmax 40 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 60.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 500 # maximal number of pressure iteration in one time step
|
||||
eps 0.0001 # stopping tolerance for pressure iteration
|
||||
rho 0.52
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 55 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 500
|
||||
startTime 0 #if you want to see particles trapped in recirculation zone, startTime should be set to 0
|
||||
injectTimePeriod 1.0
|
||||
writeTimePeriod 0.5
|
||||
|
||||
x1 0.0
|
||||
y1 0.5
|
||||
x2 0.0
|
||||
y2 1.5
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 1
|
||||
xCenter 0.0
|
||||
yCenter 0.0
|
||||
xRectLength 2.0
|
||||
yRectLength 1.0
|
||||
circleRadius 1.0
|
||||
|
||||
#===============================================================================
|
78
EnhancedSolver/2D-mpi/canal.par
Normal file
78
EnhancedSolver/2D-mpi/canal.par
Normal file
@@ -0,0 +1,78 @@
|
||||
#==============================================================================
|
||||
# Laminar Canal Flow
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name canal # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 100.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 0.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 30.0 # domain size in x-direction
|
||||
ylength 4.0 # domain size in y-direction
|
||||
imax 200 # number of interior cells in x-direction
|
||||
jmax 40 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 60.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 5 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 500 # maximal number of pressure iteration in one time step
|
||||
eps 0.00001 # stopping tolerance for pressure iteration
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 60
|
||||
startTime 10.0
|
||||
injectTimePeriod 4.0
|
||||
writeTimePeriod 1.0
|
||||
|
||||
x1 1.0
|
||||
y1 0.0
|
||||
x2 1.0
|
||||
y2 4.0
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 0
|
||||
xCenter 10.0
|
||||
yCenter 2
|
||||
xRectLength 6.0
|
||||
yRectLength 1.0
|
||||
circleRadius 1.0
|
||||
|
||||
#===============================================================================
|
17
EnhancedSolver/2D-mpi/config.mk
Normal file
17
EnhancedSolver/2D-mpi/config.mk
Normal file
@@ -0,0 +1,17 @@
|
||||
# Supported: GCC, CLANG, ICX
|
||||
TAG ?= ICX
|
||||
# Supported: true, false
|
||||
ENABLE_MPI ?= true
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: rb, mg
|
||||
SOLVER ?= mg
|
||||
# Run in debug settings ?= mg
|
||||
COMM_TYPE ?= v3
|
||||
|
||||
#Feature options
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
OPTIONS += -DVERBOSE
|
||||
# OPTIONS += -DTEST
|
||||
#OPTIONS += -DVERBOSE_AFFINITY
|
||||
#OPTIONS += -DVERBOSE_DATASIZE
|
||||
#OPTIONS += -DVERBOSE_TIMER
|
79
EnhancedSolver/2D-mpi/dcavity.par
Normal file
79
EnhancedSolver/2D-mpi/dcavity.par
Normal file
@@ -0,0 +1,79 @@
|
||||
#==============================================================================
|
||||
# Driven Cavity
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name dcavity # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 1 # 2 = free-slip 4 = periodic
|
||||
bcRight 1 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 10.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 0.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 1.0 # domain size in x-direction
|
||||
ylength 1.0 # domain size in y-direction
|
||||
imax 128 # number of interior cells in x-direction
|
||||
jmax 128 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 1000 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
rho 0.5
|
||||
omg 1.7 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 20 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 200
|
||||
startTime 2.0
|
||||
injectTimePeriod 0.5
|
||||
writeTimePeriod 0.2
|
||||
|
||||
x1 0.1
|
||||
y1 0.9
|
||||
x2 0.9
|
||||
y2 0.9
|
||||
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 0
|
||||
xCenter 0.5
|
||||
yCenter 0.5
|
||||
xRectLength 0.5
|
||||
yRectLength 0.5
|
||||
circleRadius 0.5
|
||||
#===============================================================================
|
21
EnhancedSolver/2D-mpi/include_CLANG.mk
Normal file
21
EnhancedSolver/2D-mpi/include_CLANG.mk
Normal file
@@ -0,0 +1,21 @@
|
||||
ifeq ($(ENABLE_MPI),true)
|
||||
CC = mpicc
|
||||
DEFINES = -D_MPI
|
||||
else
|
||||
CC = cc
|
||||
endif
|
||||
|
||||
GCC = cc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(ENABLE_OPENMP),true)
|
||||
OPENMP = -fopenmp
|
||||
#OPENMP = -Xpreprocessor -fopenmp #required on Macos with homebrew libomp
|
||||
LIBS = # -lomp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -Ofast -std=c17
|
||||
LFLAGS = $(OPENMP) -lm
|
||||
DEFINES += -D_GNU_SOURCE# -DDEBUG
|
||||
INCLUDES = -I/opt/homebrew/include
|
20
EnhancedSolver/2D-mpi/include_GCC.mk
Normal file
20
EnhancedSolver/2D-mpi/include_GCC.mk
Normal file
@@ -0,0 +1,20 @@
|
||||
ifeq ($(ENABLE_MPI),true)
|
||||
CC = mpicc
|
||||
DEFINES = -D_MPI
|
||||
else
|
||||
CC = gcc
|
||||
endif
|
||||
|
||||
GCC = gcc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(ENABLE_OPENMP),true)
|
||||
OPENMP = -fopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -Ofast -ffreestanding -std=c99 $(OPENMP)
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES += -D_GNU_SOURCE
|
||||
INCLUDES =
|
||||
LIBS =
|
20
EnhancedSolver/2D-mpi/include_ICX.mk
Normal file
20
EnhancedSolver/2D-mpi/include_ICX.mk
Normal file
@@ -0,0 +1,20 @@
|
||||
ifeq ($(ENABLE_MPI),true)
|
||||
CC = mpiicx
|
||||
DEFINES = -D_MPI
|
||||
else
|
||||
CC = icx
|
||||
endif
|
||||
|
||||
GCC = gcc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(ENABLE_OPENMP),true)
|
||||
OPENMP = -qopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP) -Wno-unused-command-line-argument
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES += -D_GNU_SOURCE# -DDEBUG
|
||||
INCLUDES =
|
||||
LIBS =
|
79
EnhancedSolver/2D-mpi/karman.par
Normal file
79
EnhancedSolver/2D-mpi/karman.par
Normal file
@@ -0,0 +1,79 @@
|
||||
#==============================================================================
|
||||
# Laminar Canal Flow
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name karman # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 5050.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 0.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 30.0 # domain size in x-direction
|
||||
ylength 8.0 # domain size in y-direction
|
||||
imax 400 # number of interior cells in x-direction
|
||||
jmax 200 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 150.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 200 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
rho 0.52
|
||||
omg 1.75 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
|
||||
# Multigrid data:
|
||||
# ---------
|
||||
|
||||
levels 3 # Multigrid levels
|
||||
presmooth 15 # Pre-smoothning iterations
|
||||
postsmooth 5 # Post-smoothning iterations
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 200
|
||||
startTime 50
|
||||
injectTimePeriod 1.0
|
||||
writeTimePeriod 0.5
|
||||
|
||||
x1 0.0
|
||||
y1 3.8
|
||||
x2 0.0
|
||||
y2 4.1
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 2
|
||||
xCenter 5.0
|
||||
yCenter 4.0
|
||||
xRectLength 2.0
|
||||
yRectLength 1.0
|
||||
circleRadius 1.0
|
||||
|
||||
#===============================================================================
|
9
EnhancedSolver/2D-mpi/residual.plot
Normal file
9
EnhancedSolver/2D-mpi/residual.plot
Normal file
@@ -0,0 +1,9 @@
|
||||
set terminal png size 1800,768 enhanced font ,12
|
||||
set output 'residual.png'
|
||||
set datafile separator whitespace
|
||||
set xlabel "Timestep"
|
||||
set ylabel "Residual"
|
||||
|
||||
set logscale y 2
|
||||
|
||||
plot 'residual.dat' using 1:2 title "Residual"
|
38
EnhancedSolver/2D-mpi/src/allocate.c
Normal file
38
EnhancedSolver/2D-mpi/src/allocate.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "allocate.h"
|
||||
|
||||
void* allocate(size_t alignment, size_t bytesize)
|
||||
{
|
||||
int errorCode;
|
||||
void* ptr;
|
||||
|
||||
errorCode = posix_memalign(&ptr, alignment, bytesize);
|
||||
|
||||
if (errorCode) {
|
||||
if (errorCode == EINVAL) {
|
||||
fprintf(stderr, "Error: Alignment parameter is not a power of two\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (errorCode == ENOMEM) {
|
||||
fprintf(stderr, "Error: Insufficient memory to fulfill the request\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (ptr == NULL) {
|
||||
fprintf(stderr, "Error: posix_memalign failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
@@ -4,11 +4,10 @@
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef AFFINITY_H
|
||||
#define AFFINITY_H
|
||||
#ifndef __ALLOCATE_H_
|
||||
#define __ALLOCATE_H_
|
||||
#include <stdlib.h>
|
||||
|
||||
extern int affinity_getProcessorId();
|
||||
extern void affinity_pinProcess(int);
|
||||
extern void affinity_pinThread(int);
|
||||
extern void* allocate(size_t alignment, size_t bytesize);
|
||||
|
||||
#endif /*AFFINITY_H*/
|
||||
#endif
|
222
EnhancedSolver/2D-mpi/src/comm-v1.c
Normal file
222
EnhancedSolver/2D-mpi/src/comm-v1.c
Normal file
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "comm.h"
|
||||
|
||||
#ifdef _MPI
|
||||
|
||||
static void gatherArray(
|
||||
Comm* c, int cnt, int* rcvCounts, int* displs, double* src, double* dst)
|
||||
{
|
||||
double* sendbuffer = src + (c->imaxLocal + 2);
|
||||
|
||||
if (c->rank == 0) {
|
||||
sendbuffer = src;
|
||||
}
|
||||
|
||||
MPI_Gatherv(sendbuffer,
|
||||
cnt,
|
||||
MPI_DOUBLE,
|
||||
dst,
|
||||
rcvCounts,
|
||||
displs,
|
||||
MPI_DOUBLE,
|
||||
0,
|
||||
MPI_COMM_WORLD);
|
||||
}
|
||||
#endif // defined _MPI
|
||||
|
||||
// exported subroutines
|
||||
int commIsBoundary(Comm* c, int direction)
|
||||
{
|
||||
#ifdef _MPI
|
||||
switch (direction) {
|
||||
case L:
|
||||
return 1;
|
||||
break;
|
||||
case R:
|
||||
return 1;
|
||||
break;
|
||||
case B:
|
||||
return c->rank == 0;
|
||||
break;
|
||||
case T:
|
||||
return c->rank == (c->size - 1);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void commExchange(Comm* c, double* grid)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[4] = { MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL };
|
||||
|
||||
/* exchange ghost cells with top neighbor */
|
||||
if (c->rank + 1 < c->size) {
|
||||
int top = c->rank + 1;
|
||||
double* src = grid + (c->jmaxLocal) * (c->imaxLocal + 2) + 1;
|
||||
double* dst = grid + (c->jmaxLocal + 1) * (c->imaxLocal + 2) + 1;
|
||||
|
||||
MPI_Isend(src, c->imaxLocal, MPI_DOUBLE, top, 1, MPI_COMM_WORLD, &requests[0]);
|
||||
MPI_Irecv(dst, c->imaxLocal, MPI_DOUBLE, top, 2, MPI_COMM_WORLD, &requests[1]);
|
||||
}
|
||||
|
||||
/* exchange ghost cells with bottom neighbor */
|
||||
if (c->rank > 0) {
|
||||
int bottom = c->rank - 1;
|
||||
double* src = grid + (c->imaxLocal + 2) + 1;
|
||||
double* dst = grid + 1;
|
||||
|
||||
MPI_Isend(src, c->imaxLocal, MPI_DOUBLE, bottom, 2, MPI_COMM_WORLD, &requests[2]);
|
||||
MPI_Irecv(dst, c->imaxLocal, MPI_DOUBLE, bottom, 1, MPI_COMM_WORLD, &requests[3]);
|
||||
}
|
||||
|
||||
MPI_Waitall(4, requests, MPI_STATUSES_IGNORE);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commShift(Comm* c, double* f, double* g)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[2] = { MPI_REQUEST_NULL, MPI_REQUEST_NULL };
|
||||
|
||||
/* shift G */
|
||||
/* receive ghost cells from bottom neighbor */
|
||||
if (c->rank > 0) {
|
||||
int bottom = c->rank - 1;
|
||||
MPI_Irecv(g + 1,
|
||||
c->imaxLocal,
|
||||
MPI_DOUBLE,
|
||||
bottom,
|
||||
0,
|
||||
MPI_COMM_WORLD,
|
||||
&requests[0]);
|
||||
}
|
||||
|
||||
if (c->rank + 1 < c->size) {
|
||||
int top = c->rank + 1;
|
||||
double* buf = g + (c->jmaxLocal) * (c->imaxLocal + 2) + 1;
|
||||
/* send ghost cells to top neighbor */
|
||||
MPI_Isend(buf, c->imaxLocal, MPI_DOUBLE, top, 0, MPI_COMM_WORLD, &requests[1]);
|
||||
}
|
||||
|
||||
MPI_Waitall(2, requests, MPI_STATUSES_IGNORE);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
double* pg,
|
||||
double* u,
|
||||
double* v,
|
||||
double* p,
|
||||
int imax,
|
||||
int jmax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int *rcvCounts, *displs;
|
||||
int cnt = c->jmaxLocal * (imax + 2);
|
||||
|
||||
if (c->rank == 0) {
|
||||
rcvCounts = (int*)malloc(c->size * sizeof(int));
|
||||
displs = (int*)malloc(c->size * sizeof(int));
|
||||
}
|
||||
|
||||
if (c->rank == 0 && c->size == 1) {
|
||||
cnt = (c->jmaxLocal + 2) * (imax + 2);
|
||||
} else if (c->rank == 0 || c->rank == (c->size - 1)) {
|
||||
cnt = (c->jmaxLocal + 1) * (imax + 2);
|
||||
}
|
||||
|
||||
MPI_Gather(&cnt, 1, MPI_INTEGER, rcvCounts, 1, MPI_INTEGER, 0, MPI_COMM_WORLD);
|
||||
|
||||
if (c->rank == 0) {
|
||||
displs[0] = 0;
|
||||
int cursor = rcvCounts[0];
|
||||
|
||||
for (int i = 1; i < c->size; i++) {
|
||||
displs[i] = cursor;
|
||||
cursor += rcvCounts[i];
|
||||
}
|
||||
}
|
||||
|
||||
gatherArray(c, cnt, rcvCounts, displs, p, pg);
|
||||
gatherArray(c, cnt, rcvCounts, displs, u, ug);
|
||||
gatherArray(c, cnt, rcvCounts, displs, v, vg);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commPartition(Comm* c, int jmax, int imax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = sizeOfRank(c->coords[JDIM], c->size, jmax);
|
||||
|
||||
c->neighbours[BOTTOM] = c->rank == 0 ? -1 : c->rank - 1;
|
||||
c->neighbours[TOP] = c->rank == (c->size - 1) ? -1 : c->rank + 1;
|
||||
c->neighbours[LEFT] = -1;
|
||||
c->neighbours[RIGHT] = -1;
|
||||
|
||||
c->coords[IDIM] = 0;
|
||||
c->coords[JDIM] = c->rank;
|
||||
|
||||
c->dims[IDIM] = 1;
|
||||
c->dims[JDIM] = c->size;
|
||||
#else
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = jmax;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
|
||||
#if defined _MPI
|
||||
newcomm->comm = MPI_COMM_NULL;
|
||||
int result = MPI_Comm_dup(MPI_COMM_WORLD, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
|
||||
newcomm->neighbours[BOTTOM] = newcomm->rank == 0 ? -1 : newcomm->rank - 1;
|
||||
newcomm->neighbours[TOP] = newcomm->rank == (newcomm->size - 1) ? -1 : newcomm->rank + 1;
|
||||
newcomm->neighbours[LEFT] = -1;
|
||||
newcomm->neighbours[RIGHT] = -1;
|
||||
|
||||
newcomm->coords[IDIM] = 0;
|
||||
newcomm->coords[JDIM] = newcomm->rank;
|
||||
|
||||
newcomm->dims[IDIM] = 1;
|
||||
newcomm->dims[JDIM] = newcomm->size;
|
||||
|
||||
|
||||
#endif
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
342
EnhancedSolver/2D-mpi/src/comm-v2.c
Normal file
342
EnhancedSolver/2D-mpi/src/comm-v2.c
Normal file
@@ -0,0 +1,342 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include "comm.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax)
|
||||
{
|
||||
MPI_Request* requests;
|
||||
int numRequests = 1;
|
||||
|
||||
if (c->rank == 0) {
|
||||
numRequests = c->size + 1;
|
||||
} else {
|
||||
numRequests = 1;
|
||||
}
|
||||
|
||||
requests = (MPI_Request*)malloc(numRequests * sizeof(MPI_Request));
|
||||
|
||||
/* all ranks send their bulk array, including the external boundary layer */
|
||||
MPI_Datatype bulkType;
|
||||
int oldSizes[NDIMS] = { c->jmaxLocal + 2, c->imaxLocal + 2 };
|
||||
int newSizes[NDIMS] = { c->jmaxLocal, c->imaxLocal };
|
||||
int starts[NDIMS] = { 1, 1 };
|
||||
|
||||
if (commIsBoundary(c, L)) {
|
||||
newSizes[CIDIM] += 1;
|
||||
starts[CIDIM] = 0;
|
||||
}
|
||||
if (commIsBoundary(c, R)) {
|
||||
newSizes[CIDIM] += 1;
|
||||
}
|
||||
if (commIsBoundary(c, B)) {
|
||||
newSizes[CJDIM] += 1;
|
||||
starts[CJDIM] = 0;
|
||||
}
|
||||
if (commIsBoundary(c, T)) {
|
||||
newSizes[CJDIM] += 1;
|
||||
}
|
||||
|
||||
MPI_Type_create_subarray(NDIMS,
|
||||
oldSizes,
|
||||
newSizes,
|
||||
starts,
|
||||
MPI_ORDER_C,
|
||||
MPI_DOUBLE,
|
||||
&bulkType);
|
||||
MPI_Type_commit(&bulkType);
|
||||
MPI_Isend(src, 1, bulkType, 0, 0, c->comm, &requests[0]);
|
||||
|
||||
int newSizesI[c->size];
|
||||
int newSizesJ[c->size];
|
||||
MPI_Gather(&newSizes[CIDIM], 1, MPI_INT, newSizesI, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
||||
MPI_Gather(&newSizes[CJDIM], 1, MPI_INT, newSizesJ, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
||||
|
||||
/* rank 0 assembles the subdomains */
|
||||
if (c->rank == 0) {
|
||||
for (int i = 0; i < c->size; i++) {
|
||||
MPI_Datatype domainType;
|
||||
int oldSizes[NDIMS] = { jmax + 2, imax + 2 };
|
||||
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
|
||||
int coords[NDIMS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
|
||||
sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
|
||||
printf(
|
||||
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
|
||||
"Starts(i,j): %d %d\n",
|
||||
i,
|
||||
coords[IDIM],
|
||||
coords[JDIM],
|
||||
oldSizes[CIDIM],
|
||||
oldSizes[CJDIM],
|
||||
newSizes[CIDIM],
|
||||
newSizes[CJDIM],
|
||||
starts[CIDIM],
|
||||
starts[CJDIM]);
|
||||
|
||||
MPI_Type_create_subarray(NDIMS,
|
||||
oldSizes,
|
||||
newSizes,
|
||||
starts,
|
||||
MPI_ORDER_C,
|
||||
MPI_DOUBLE,
|
||||
&domainType);
|
||||
MPI_Type_commit(&domainType);
|
||||
|
||||
MPI_Irecv(dst, 1, domainType, i, 0, c->comm, &requests[i + 1]);
|
||||
MPI_Type_free(&domainType);
|
||||
}
|
||||
}
|
||||
|
||||
MPI_Waitall(numRequests, requests, MPI_STATUSES_IGNORE);
|
||||
}
|
||||
#endif // defined _MPI
|
||||
|
||||
// exported subroutines
|
||||
int commIsBoundary(Comm* c, int direction)
|
||||
{
|
||||
#ifdef _MPI
|
||||
switch (direction) {
|
||||
case L:
|
||||
return c->coords[IDIM] == 0;
|
||||
break;
|
||||
case R:
|
||||
return c->coords[IDIM] == (c->dims[IDIM] - 1);
|
||||
break;
|
||||
case B:
|
||||
return c->coords[JDIM] == 0;
|
||||
break;
|
||||
case T:
|
||||
return c->coords[JDIM] == (c->dims[JDIM] - 1);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void commExchange(Comm* c, double* grid)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[8];
|
||||
for (int i = 0; i < 8; i++)
|
||||
requests[i] = MPI_REQUEST_NULL;
|
||||
|
||||
for (int i = 0; i < NDIRS; i++) {
|
||||
double* sbuf = grid + c->sdispls[i];
|
||||
double* rbuf = grid + c->rdispls[i];
|
||||
|
||||
int tag = 0;
|
||||
if (c->neighbours[i] != MPI_PROC_NULL) {
|
||||
// printf("DEBUG: Rank %d - SendRecv with %d\n", c->rank, c->neighbours[i]);
|
||||
tag = c->neighbours[i];
|
||||
}
|
||||
MPI_Irecv(rbuf,
|
||||
1,
|
||||
c->bufferTypes[i],
|
||||
c->neighbours[i],
|
||||
tag,
|
||||
c->comm,
|
||||
&requests[i * 2]);
|
||||
MPI_Isend(sbuf,
|
||||
1,
|
||||
c->bufferTypes[i],
|
||||
c->neighbours[i],
|
||||
c->rank,
|
||||
c->comm,
|
||||
&requests[i * 2 + 1]);
|
||||
}
|
||||
|
||||
MPI_Waitall(8, requests, MPI_STATUSES_IGNORE);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commShift(Comm* c, double* f, double* g)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[4] = { MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL };
|
||||
|
||||
/* shift G */
|
||||
/* receive ghost cells from bottom neighbor */
|
||||
double* buf = g + 1;
|
||||
MPI_Irecv(buf,
|
||||
1,
|
||||
c->bufferTypes[B],
|
||||
c->neighbours[B],
|
||||
0,
|
||||
c->comm,
|
||||
&requests[0]);
|
||||
|
||||
/* send ghost cells to top neighbor */
|
||||
buf = g + (c->jmaxLocal) * (c->imaxLocal + 2) + 1;
|
||||
MPI_Isend(buf, 1, c->bufferTypes[T], c->neighbours[T], 0, c->comm, &requests[1]);
|
||||
|
||||
/* shift F */
|
||||
/* receive ghost cells from left neighbor */
|
||||
buf = f + (c->imaxLocal + 2);
|
||||
MPI_Irecv(buf,
|
||||
1,
|
||||
c->bufferTypes[L],
|
||||
c->neighbours[L],
|
||||
1,
|
||||
c->comm,
|
||||
&requests[2]);
|
||||
|
||||
/* send ghost cells to right neighbor */
|
||||
buf = f + (c->imaxLocal + 2) + (c->imaxLocal);
|
||||
MPI_Isend(buf,
|
||||
1,
|
||||
c->bufferTypes[R],
|
||||
c->neighbours[R],
|
||||
1,
|
||||
c->comm,
|
||||
&requests[3]);
|
||||
|
||||
MPI_Waitall(4, requests, MPI_STATUSES_IGNORE);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
double* pg,
|
||||
double* u,
|
||||
double* v,
|
||||
double* p,
|
||||
int imax,
|
||||
int jmax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
/* collect P */
|
||||
assembleResult(c, p, pg, imax, jmax);
|
||||
|
||||
/* collect U */
|
||||
assembleResult(c, u, ug, imax, jmax);
|
||||
|
||||
/* collect V */
|
||||
assembleResult(c, v, vg, imax, jmax);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commPartition(Comm* c, int jmax, int imax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int dims[NDIMS] = { 0, 0 };
|
||||
int periods[NDIMS] = { 0, 0 };
|
||||
MPI_Dims_create(c->size, NDIMS, dims);
|
||||
MPI_Cart_create(MPI_COMM_WORLD, NDIMS, dims, periods, 0, &c->comm);
|
||||
MPI_Cart_shift(c->comm, IDIM, 1, &c->neighbours[L], &c->neighbours[R]);
|
||||
MPI_Cart_shift(c->comm, JDIM, 1, &c->neighbours[B], &c->neighbours[T]);
|
||||
MPI_Cart_get(c->comm, NDIMS, c->dims, periods, c->coords);
|
||||
|
||||
int imaxLocal = sizeOfRank(c->coords[IDIM], dims[IDIM], imax);
|
||||
int jmaxLocal = sizeOfRank(c->coords[JDIM], dims[JDIM], jmax);
|
||||
|
||||
c->imaxLocal = imaxLocal;
|
||||
c->jmaxLocal = jmaxLocal;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
MPI_Type_commit(&jBufferType);
|
||||
|
||||
MPI_Datatype iBufferType;
|
||||
MPI_Type_vector(jmaxLocal, 1, imaxLocal + 2, MPI_DOUBLE, &iBufferType);
|
||||
MPI_Type_commit(&iBufferType);
|
||||
|
||||
c->bufferTypes[L] = iBufferType;
|
||||
c->bufferTypes[R] = iBufferType;
|
||||
c->bufferTypes[B] = jBufferType;
|
||||
c->bufferTypes[T] = jBufferType;
|
||||
|
||||
c->sdispls[L] = (imaxLocal + 2) + 1;
|
||||
c->sdispls[R] = (imaxLocal + 2) + imaxLocal;
|
||||
c->sdispls[B] = (imaxLocal + 2) + 1;
|
||||
c->sdispls[T] = jmaxLocal * (imaxLocal + 2) + 1;
|
||||
|
||||
c->rdispls[L] = (imaxLocal + 2);
|
||||
c->rdispls[R] = (imaxLocal + 2) + (imaxLocal + 1);
|
||||
c->rdispls[B] = 1;
|
||||
c->rdispls[T] = (jmaxLocal + 1) * (imaxLocal + 2) + 1;
|
||||
#else
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = jmax;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
#if defined _MPI
|
||||
newcomm->comm = MPI_COMM_NULL;
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
memcpy(&newcomm->neighbours, &oldcomm->neighbours, sizeof(oldcomm->neighbours));
|
||||
memcpy(&newcomm->coords, &oldcomm->coords, sizeof(oldcomm->coords));
|
||||
memcpy(&newcomm->dims, &oldcomm->dims, sizeof(oldcomm->dims));
|
||||
|
||||
newcomm->imaxLocal = imaxLocal/2;
|
||||
newcomm->jmaxLocal = jmaxLocal/2;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
MPI_Type_commit(&jBufferType);
|
||||
|
||||
MPI_Datatype iBufferType;
|
||||
MPI_Type_vector(jmaxLocal, 1, imaxLocal + 2, MPI_DOUBLE, &iBufferType);
|
||||
MPI_Type_commit(&iBufferType);
|
||||
|
||||
newcomm->bufferTypes[L] = iBufferType;
|
||||
newcomm->bufferTypes[R] = iBufferType;
|
||||
newcomm->bufferTypes[B] = jBufferType;
|
||||
newcomm->bufferTypes[T] = jBufferType;
|
||||
|
||||
newcomm->sdispls[L] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[R] = (imaxLocal + 2) + imaxLocal;
|
||||
newcomm->sdispls[B] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[T] = jmaxLocal * (imaxLocal + 2) + 1;
|
||||
|
||||
newcomm->rdispls[L] = (imaxLocal + 2);
|
||||
newcomm->rdispls[R] = (imaxLocal + 2) + (imaxLocal + 1);
|
||||
newcomm->rdispls[B] = 1;
|
||||
newcomm->rdispls[T] = (jmaxLocal + 1) * (imaxLocal + 2) + 1;
|
||||
#else
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
320
EnhancedSolver/2D-mpi/src/comm-v3.c
Normal file
320
EnhancedSolver/2D-mpi/src/comm-v3.c
Normal file
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "comm.h"
|
||||
|
||||
#ifdef _MPI
|
||||
// subroutines local to this module
|
||||
static int sum(int* sizes, int init, int offset, int coord)
|
||||
{
|
||||
int sum = 0;
|
||||
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax)
|
||||
{
|
||||
MPI_Request* requests;
|
||||
int numRequests = 1;
|
||||
|
||||
if (c->rank == 0) {
|
||||
numRequests = c->size + 1;
|
||||
} else {
|
||||
numRequests = 1;
|
||||
}
|
||||
|
||||
requests = (MPI_Request*)malloc(numRequests * sizeof(MPI_Request));
|
||||
|
||||
/* all ranks send their bulk array, including the external boundary layer */
|
||||
MPI_Datatype bulkType;
|
||||
int oldSizes[NDIMS] = { c->jmaxLocal + 2, c->imaxLocal + 2 };
|
||||
int newSizes[NDIMS] = { c->jmaxLocal, c->imaxLocal };
|
||||
int starts[NDIMS] = { 1, 1 };
|
||||
|
||||
if (commIsBoundary(c, L)) {
|
||||
newSizes[CIDIM] += 1;
|
||||
starts[CIDIM] = 0;
|
||||
}
|
||||
if (commIsBoundary(c, R)) {
|
||||
newSizes[CIDIM] += 1;
|
||||
}
|
||||
if (commIsBoundary(c, B)) {
|
||||
newSizes[CJDIM] += 1;
|
||||
starts[CJDIM] = 0;
|
||||
}
|
||||
if (commIsBoundary(c, T)) {
|
||||
newSizes[CJDIM] += 1;
|
||||
}
|
||||
|
||||
MPI_Type_create_subarray(NDIMS,
|
||||
oldSizes,
|
||||
newSizes,
|
||||
starts,
|
||||
MPI_ORDER_C,
|
||||
MPI_DOUBLE,
|
||||
&bulkType);
|
||||
MPI_Type_commit(&bulkType);
|
||||
MPI_Isend(src, 1, bulkType, 0, 0, c->comm, &requests[0]);
|
||||
|
||||
int newSizesI[c->size];
|
||||
int newSizesJ[c->size];
|
||||
MPI_Gather(&newSizes[CIDIM], 1, MPI_INT, newSizesI, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
||||
MPI_Gather(&newSizes[CJDIM], 1, MPI_INT, newSizesJ, 1, MPI_INT, 0, MPI_COMM_WORLD);
|
||||
|
||||
/* rank 0 assembles the subdomains */
|
||||
if (c->rank == 0) {
|
||||
for (int i = 0; i < c->size; i++) {
|
||||
MPI_Datatype domainType;
|
||||
int oldSizes[NDIMS] = { jmax + 2, imax + 2 };
|
||||
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
|
||||
int coords[NDIMS];
|
||||
MPI_Cart_coords(c->comm, i, NDIMS, coords);
|
||||
int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
|
||||
sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
|
||||
printf(
|
||||
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
|
||||
"Starts(i,j): %d %d\n",
|
||||
i,
|
||||
coords[IDIM],
|
||||
coords[JDIM],
|
||||
oldSizes[CIDIM],
|
||||
oldSizes[CJDIM],
|
||||
newSizes[CIDIM],
|
||||
newSizes[CJDIM],
|
||||
starts[CIDIM],
|
||||
starts[CJDIM]);
|
||||
|
||||
MPI_Type_create_subarray(NDIMS,
|
||||
oldSizes,
|
||||
newSizes,
|
||||
starts,
|
||||
MPI_ORDER_C,
|
||||
MPI_DOUBLE,
|
||||
&domainType);
|
||||
MPI_Type_commit(&domainType);
|
||||
|
||||
MPI_Irecv(dst, 1, domainType, i, 0, c->comm, &requests[i + 1]);
|
||||
MPI_Type_free(&domainType);
|
||||
}
|
||||
}
|
||||
|
||||
MPI_Waitall(numRequests, requests, MPI_STATUSES_IGNORE);
|
||||
}
|
||||
#endif // defined _MPI
|
||||
|
||||
// exported subroutines
|
||||
int commIsBoundary(Comm* c, int direction)
|
||||
{
|
||||
#ifdef _MPI
|
||||
switch (direction) {
|
||||
case L:
|
||||
return c->coords[IDIM] == 0;
|
||||
break;
|
||||
case R:
|
||||
return c->coords[IDIM] == (c->dims[IDIM] - 1);
|
||||
break;
|
||||
case B:
|
||||
return c->coords[JDIM] == 0;
|
||||
break;
|
||||
case T:
|
||||
return c->coords[JDIM] == (c->dims[JDIM] - 1);
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
void commExchange(Comm* c, double* grid)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int counts[NDIRS] = { 1, 1, 1, 1 };
|
||||
MPI_Neighbor_alltoallw(grid,
|
||||
counts,
|
||||
c->sdispls,
|
||||
c->bufferTypes,
|
||||
grid,
|
||||
counts,
|
||||
c->rdispls,
|
||||
c->bufferTypes,
|
||||
c->comm);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commShift(Comm* c, double* f, double* g)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Request requests[4] = { MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL,
|
||||
MPI_REQUEST_NULL };
|
||||
|
||||
/* shift G */
|
||||
/* receive ghost cells from bottom neighbor */
|
||||
double* buf = g + 1;
|
||||
MPI_Irecv(buf,
|
||||
1,
|
||||
c->bufferTypes[B],
|
||||
c->neighbours[B],
|
||||
0,
|
||||
c->comm,
|
||||
&requests[0]);
|
||||
|
||||
/* send ghost cells to top neighbor */
|
||||
buf = g + (c->jmaxLocal) * (c->imaxLocal + 2) + 1;
|
||||
MPI_Isend(buf, 1, c->bufferTypes[T], c->neighbours[T], 0, c->comm, &requests[1]);
|
||||
|
||||
/* shift F */
|
||||
/* receive ghost cells from left neighbor */
|
||||
buf = f + (c->imaxLocal + 2);
|
||||
MPI_Irecv(buf,
|
||||
1,
|
||||
c->bufferTypes[L],
|
||||
c->neighbours[L],
|
||||
1,
|
||||
c->comm,
|
||||
&requests[2]);
|
||||
|
||||
/* send ghost cells to right neighbor */
|
||||
buf = f + (c->imaxLocal + 2) + (c->imaxLocal);
|
||||
MPI_Isend(buf,
|
||||
1,
|
||||
c->bufferTypes[R],
|
||||
c->neighbours[R],
|
||||
1,
|
||||
c->comm,
|
||||
&requests[3]);
|
||||
|
||||
MPI_Waitall(4, requests, MPI_STATUSES_IGNORE);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
double* pg,
|
||||
double* u,
|
||||
double* v,
|
||||
double* p,
|
||||
int imax,
|
||||
int jmax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
/* collect P */
|
||||
assembleResult(c, p, pg, imax, jmax);
|
||||
|
||||
/* collect U */
|
||||
assembleResult(c, u, ug, imax, jmax);
|
||||
|
||||
/* collect V */
|
||||
assembleResult(c, v, vg, imax, jmax);
|
||||
#endif
|
||||
}
|
||||
|
||||
void commPartition(Comm* c, int jmax, int imax)
|
||||
{
|
||||
#ifdef _MPI
|
||||
int dims[NDIMS] = { 0, 0 };
|
||||
int periods[NDIMS] = { 0, 0 };
|
||||
MPI_Dims_create(c->size, NDIMS, dims);
|
||||
MPI_Cart_create(MPI_COMM_WORLD, NDIMS, dims, periods, 0, &c->comm);
|
||||
MPI_Cart_shift(c->comm, IDIM, 1, &c->neighbours[L], &c->neighbours[R]);
|
||||
MPI_Cart_shift(c->comm, JDIM, 1, &c->neighbours[B], &c->neighbours[T]);
|
||||
MPI_Cart_get(c->comm, NDIMS, c->dims, periods, c->coords);
|
||||
|
||||
int imaxLocal = sizeOfRank(c->coords[IDIM], dims[IDIM], imax);
|
||||
int jmaxLocal = sizeOfRank(c->coords[JDIM], dims[JDIM], jmax);
|
||||
|
||||
c->imaxLocal = imaxLocal;
|
||||
c->jmaxLocal = jmaxLocal;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
MPI_Type_commit(&jBufferType);
|
||||
|
||||
MPI_Datatype iBufferType;
|
||||
MPI_Type_vector(jmaxLocal, 1, imaxLocal + 2, MPI_DOUBLE, &iBufferType);
|
||||
MPI_Type_commit(&iBufferType);
|
||||
|
||||
c->bufferTypes[L] = iBufferType;
|
||||
c->bufferTypes[R] = iBufferType;
|
||||
c->bufferTypes[B] = jBufferType;
|
||||
c->bufferTypes[T] = jBufferType;
|
||||
|
||||
size_t dblsize = sizeof(double);
|
||||
c->sdispls[L] = ((imaxLocal + 2) + 1) * dblsize;
|
||||
c->sdispls[R] = ((imaxLocal + 2) + imaxLocal) * dblsize;
|
||||
c->sdispls[B] = ((imaxLocal + 2) + 1) * dblsize;
|
||||
c->sdispls[T] = (jmaxLocal * (imaxLocal + 2) + 1) * dblsize;
|
||||
|
||||
c->rdispls[L] = (imaxLocal + 2) * dblsize;
|
||||
c->rdispls[R] = ((imaxLocal + 2) + (imaxLocal + 1)) * dblsize;
|
||||
c->rdispls[B] = 1 * dblsize;
|
||||
c->rdispls[T] = ((jmaxLocal + 1) * (imaxLocal + 2) + 1) * dblsize;
|
||||
#else
|
||||
c->imaxLocal = imax;
|
||||
c->jmaxLocal = jmax;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
|
||||
{
|
||||
#if defined _MPI
|
||||
|
||||
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
|
||||
|
||||
if (result == MPI_ERR_COMM) {
|
||||
printf("\nNull communicator. Duplication failed !!\n");
|
||||
}
|
||||
|
||||
newcomm->rank = oldcomm->rank;
|
||||
newcomm->size = oldcomm->size;
|
||||
|
||||
|
||||
newcomm->imaxLocal = imaxLocal / 2;
|
||||
newcomm->jmaxLocal = jmaxLocal / 2;
|
||||
|
||||
MPI_Datatype jBufferType;
|
||||
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
|
||||
MPI_Type_commit(&jBufferType);
|
||||
|
||||
MPI_Datatype iBufferType;
|
||||
MPI_Type_vector(jmaxLocal, 1, imaxLocal + 2, MPI_DOUBLE, &iBufferType);
|
||||
MPI_Type_commit(&iBufferType);
|
||||
|
||||
newcomm->bufferTypes[L] = iBufferType;
|
||||
newcomm->bufferTypes[R] = iBufferType;
|
||||
newcomm->bufferTypes[B] = jBufferType;
|
||||
newcomm->bufferTypes[T] = jBufferType;
|
||||
|
||||
newcomm->sdispls[L] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[R] = (imaxLocal + 2) + imaxLocal;
|
||||
newcomm->sdispls[B] = (imaxLocal + 2) + 1;
|
||||
newcomm->sdispls[T] = jmaxLocal * (imaxLocal + 2) + 1;
|
||||
|
||||
newcomm->rdispls[L] = (imaxLocal + 2);
|
||||
newcomm->rdispls[R] = (imaxLocal + 2) + (imaxLocal + 1);
|
||||
newcomm->rdispls[B] = 1;
|
||||
newcomm->rdispls[T] = (jmaxLocal + 1) * (imaxLocal + 2) + 1;
|
||||
#else
|
||||
newcomm->imaxLocal = imaxLocal;
|
||||
newcomm->jmaxLocal = jmaxLocal;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commFreeCommunicator(Comm* comm)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Comm_free(&comm->comm);
|
||||
#endif
|
||||
}
|
129
EnhancedSolver/2D-mpi/src/comm.c
Normal file
129
EnhancedSolver/2D-mpi/src/comm.c
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "comm.h"
|
||||
|
||||
// subroutines local to this module
|
||||
int sizeOfRank(int rank, int size, int N)
|
||||
{
|
||||
return N / size + ((N % size > rank) ? 1 : 0);
|
||||
}
|
||||
|
||||
void commReduction(double* v, int op)
|
||||
{
|
||||
#ifdef _MPI
|
||||
if (op == MAX) {
|
||||
MPI_Allreduce(MPI_IN_PLACE, v, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
|
||||
} else if (op == SUM) {
|
||||
MPI_Allreduce(MPI_IN_PLACE, v, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void commPrintConfig(Comm* c)
|
||||
{
|
||||
#ifdef _MPI
|
||||
fflush(stdout);
|
||||
MPI_Barrier(MPI_COMM_WORLD);
|
||||
if (commIsMaster(c)) {
|
||||
printf("Communication setup:\n");
|
||||
}
|
||||
|
||||
for (int i = 0; i < c->size; i++) {
|
||||
if (i == c->rank) {
|
||||
printf("\tRank %d of %d\n", c->rank, c->size);
|
||||
printf("\tNeighbours (bottom, top, left, right): %d %d, %d, %d\n",
|
||||
c->neighbours[B],
|
||||
c->neighbours[T],
|
||||
c->neighbours[L],
|
||||
c->neighbours[R]);
|
||||
printf("\tIs boundary:\n");
|
||||
printf("\t\tLEFT: %d\n", commIsBoundary(c, L));
|
||||
printf("\t\tRIGHT: %d\n", commIsBoundary(c, R));
|
||||
printf("\t\tBOTTOM: %d\n", commIsBoundary(c, B));
|
||||
printf("\t\tTOP: %d\n", commIsBoundary(c, T));
|
||||
printf("\tCoordinates (i,j) %d %d\n", c->coords[IDIM], c->coords[JDIM]);
|
||||
printf("\tDims (i,j) %d %d\n", c->dims[IDIM], c->dims[JDIM]);
|
||||
printf("\tLocal domain size (i,j) %dx%d\n", c->imaxLocal, c->jmaxLocal);
|
||||
fflush(stdout);
|
||||
}
|
||||
MPI_Barrier(MPI_COMM_WORLD);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void commInit(Comm* c, int argc, char** argv)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Init(&argc, &argv);
|
||||
MPI_Comm_rank(MPI_COMM_WORLD, &(c->rank));
|
||||
MPI_Comm_size(MPI_COMM_WORLD, &(c->size));
|
||||
#else
|
||||
c->rank = 0;
|
||||
c->size = 1;
|
||||
#endif
|
||||
}
|
||||
|
||||
void commTestInit(Comm* c, double* p, double* f, double* g)
|
||||
{
|
||||
int imax = c->imaxLocal;
|
||||
int jmax = c->jmaxLocal;
|
||||
int rank = c->rank;
|
||||
|
||||
for (int j = 0; j < jmax + 2; j++) {
|
||||
for (int i = 0; i < imax + 2; i++) {
|
||||
p[j * (imax + 2) + i] = rank;
|
||||
f[j * (imax + 2) + i] = rank;
|
||||
g[j * (imax + 2) + i] = rank;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void testWriteFile(char* filename, double* grid, int imax, int jmax)
|
||||
{
|
||||
FILE* fp = fopen(filename, "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int j = 0; j < jmax + 2; j++) {
|
||||
for (int i = 0; i < imax + 2; i++) {
|
||||
fprintf(fp, "%.2f ", grid[j * (imax + 2) + i]);
|
||||
}
|
||||
fprintf(fp, "\n");
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void commTestWrite(Comm* c, double* p, double* f, double* g)
|
||||
{
|
||||
int imax = c->imaxLocal;
|
||||
int jmax = c->jmaxLocal;
|
||||
int rank = c->rank;
|
||||
|
||||
char filename[30];
|
||||
snprintf(filename, 30, "ptest-%d.dat", rank);
|
||||
testWriteFile(filename, p, imax, jmax);
|
||||
|
||||
snprintf(filename, 30, "ftest-%d.dat", rank);
|
||||
testWriteFile(filename, f, imax, jmax);
|
||||
|
||||
snprintf(filename, 30, "gtest-%d.dat", rank);
|
||||
testWriteFile(filename, g, imax, jmax);
|
||||
}
|
||||
|
||||
void commFinalize(Comm* c)
|
||||
{
|
||||
#ifdef _MPI
|
||||
MPI_Finalize();
|
||||
#endif
|
||||
}
|
57
EnhancedSolver/2D-mpi/src/comm.h
Normal file
57
EnhancedSolver/2D-mpi/src/comm.h
Normal file
@@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __COMM_H_
|
||||
#define __COMM_H_
|
||||
#if defined(_MPI)
|
||||
#include <mpi.h>
|
||||
#endif
|
||||
|
||||
enum direction { L = 0, R, B, T, NDIRS }; // L = Left, R = Right, B = Bottom, T =Top
|
||||
enum dimension { IDIM = 0, JDIM, NDIMS };
|
||||
enum cdimension { CJDIM = 0, CIDIM };
|
||||
enum layer { HALO = 0, BULK };
|
||||
enum op { MAX = 0, SUM };
|
||||
|
||||
typedef struct {
|
||||
int rank;
|
||||
int size;
|
||||
#if defined(_MPI)
|
||||
MPI_Comm comm;
|
||||
MPI_Datatype bufferTypes[NDIRS];
|
||||
MPI_Aint sdispls[NDIRS];
|
||||
MPI_Aint rdispls[NDIRS];
|
||||
#endif
|
||||
int neighbours[NDIRS];
|
||||
int coords[NDIMS], dims[NDIMS];
|
||||
int imaxLocal, jmaxLocal;
|
||||
} Comm;
|
||||
|
||||
extern int sizeOfRank(int rank, int size, int N);
|
||||
extern void commInit(Comm* c, int argc, char** argv);
|
||||
extern void commTestInit(Comm* c, double* p, double* f, double* g);
|
||||
extern void commTestWrite(Comm* c, double* p, double* f, double* g);
|
||||
extern void commFinalize(Comm* c);
|
||||
extern void commPartition(Comm* c, int jmax, int imax);
|
||||
extern void commPrintConfig(Comm*);
|
||||
extern void commExchange(Comm*, double*);
|
||||
extern void commShift(Comm* c, double* f, double* g);
|
||||
extern void commReduction(double* v, int op);
|
||||
extern int commIsBoundary(Comm* c, int direction);
|
||||
extern void commUpdateDatatypes(Comm*, Comm*, int, int);
|
||||
extern void commFreeCommunicator(Comm*);
|
||||
extern void commCollectResult(Comm* c,
|
||||
double* ug,
|
||||
double* vg,
|
||||
double* pg,
|
||||
double* u,
|
||||
double* v,
|
||||
double* p,
|
||||
int jmax,
|
||||
int imax);
|
||||
|
||||
static inline int commIsMaster(Comm* c) { return c->rank == 0; }
|
||||
#endif // __COMM_H_
|
724
EnhancedSolver/2D-mpi/src/discretization.c
Normal file
724
EnhancedSolver/2D-mpi/src/discretization.c
Normal file
@@ -0,0 +1,724 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "allocate.h"
|
||||
#include "comm.h"
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "util.h"
|
||||
|
||||
static double distance(double i, double j, double iCenter, double jCenter)
|
||||
{
|
||||
return sqrt(pow(iCenter - i, 2) + pow(jCenter - j, 2) * 1.0);
|
||||
}
|
||||
|
||||
double sumOffset(double* sizes, int init, int offset, int coord)
|
||||
{
|
||||
double sum = 0;
|
||||
|
||||
for (int i = init - offset; coord > 0; i -= offset, --coord) {
|
||||
sum += sizes[i];
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
void print(Discretization* d, double* grid)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
|
||||
for (int i = 0; i < d->comm.size; i++) {
|
||||
if (i == d->comm.rank) {
|
||||
sleep(1 * d->comm.rank);
|
||||
printf("### RANK %d LVL "
|
||||
"###################################################### #\n ",
|
||||
d->comm.rank);
|
||||
for (int j = 0; j < d->comm.jmaxLocal + 2; j++) {
|
||||
printf("%02d: ", j);
|
||||
for (int i = 0; i < d->comm.imaxLocal + 2; i++) {
|
||||
printf("%2.2f ", grid[j * (imaxLocal + 2) + i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void printConfig(Discretization* d)
|
||||
{
|
||||
if (commIsMaster(&d->comm)) {
|
||||
printf("Parameters for #%s#\n", d->problem);
|
||||
printf("BC Left:%d Right:%d Bottom:%d Top:%d\n",
|
||||
d->bcLeft,
|
||||
d->bcRight,
|
||||
d->bcBottom,
|
||||
d->bcTop);
|
||||
printf("\tReynolds number: %.2f\n", d->re);
|
||||
printf("\tGx Gy: %.2f %.2f\n", d->gx, d->gy);
|
||||
printf("Geometry data:\n");
|
||||
printf("\tDomain box size (x, y): %.2f, %.2f\n",
|
||||
d->grid.xlength,
|
||||
d->grid.ylength);
|
||||
printf("\tCells (x, y): %d, %d\n", d->grid.imax, d->grid.jmax);
|
||||
printf("\tCell size (dx, dy): %f, %f\n", d->grid.dx, d->grid.dy);
|
||||
printf("Timestep parameters:\n");
|
||||
printf("\tDefault stepsize: %.2f, Final time %.2f\n", d->dt, d->te);
|
||||
printf("\tdt bound: %.6f\n", d->dtBound);
|
||||
printf("\tTau factor: %.2f\n", d->tau);
|
||||
printf("Iterative s parameters:\n");
|
||||
printf("\tgamma factor: %f\n", d->gamma);
|
||||
}
|
||||
commPrintConfig(&d->comm);
|
||||
}
|
||||
|
||||
void initDiscretiztion(Discretization* d, Parameter* params)
|
||||
{
|
||||
d->problem = params->name;
|
||||
d->bcLeft = params->bcLeft;
|
||||
d->bcRight = params->bcRight;
|
||||
d->bcBottom = params->bcBottom;
|
||||
d->bcTop = params->bcTop;
|
||||
d->grid.imax = params->imax;
|
||||
d->grid.jmax = params->jmax;
|
||||
d->grid.xlength = params->xlength;
|
||||
d->grid.ylength = params->ylength;
|
||||
d->grid.dx = params->xlength / params->imax;
|
||||
d->grid.dy = params->ylength / params->jmax;
|
||||
d->re = params->re;
|
||||
d->gx = params->gx;
|
||||
d->gy = params->gy;
|
||||
d->dt = params->dt;
|
||||
d->te = params->te;
|
||||
d->tau = params->tau;
|
||||
d->gamma = params->gamma;
|
||||
|
||||
/* allocate arrays */
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
size_t size = (imaxLocal + 2) * (jmaxLocal + 2);
|
||||
|
||||
d->u = allocate(64, size * sizeof(double));
|
||||
d->v = allocate(64, size * sizeof(double));
|
||||
d->p = allocate(64, size * sizeof(double));
|
||||
d->rhs = allocate(64, size * sizeof(double));
|
||||
d->f = allocate(64, size * sizeof(double));
|
||||
d->g = allocate(64, size * sizeof(double));
|
||||
d->grid.s = allocate(64, size * sizeof(double));
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
d->u[i] = params->u_init;
|
||||
d->v[i] = params->v_init;
|
||||
d->p[i] = params->p_init;
|
||||
d->rhs[i] = 0.0;
|
||||
d->f[i] = 0.0;
|
||||
d->g[i] = 0.0;
|
||||
d->grid.s[i] = FLUID;
|
||||
}
|
||||
|
||||
double dx = d->grid.dx;
|
||||
double dy = d->grid.dy;
|
||||
|
||||
double invSqrSum = 1.0 / (dx * dx) + 1.0 / (dy * dy);
|
||||
d->dtBound = 0.5 * d->re * 1.0 / invSqrSum;
|
||||
|
||||
d->xLocal = d->comm.imaxLocal * d->grid.dx;
|
||||
d->yLocal = d->comm.jmaxLocal * d->grid.dy;
|
||||
|
||||
double xLocal[d->comm.size];
|
||||
double yLocal[d->comm.size];
|
||||
|
||||
#ifdef _MPI
|
||||
MPI_Allgather(&d->xLocal, 1, MPI_DOUBLE, xLocal, 1, MPI_DOUBLE, d->comm.comm);
|
||||
MPI_Allgather(&d->yLocal, 1, MPI_DOUBLE, yLocal, 1, MPI_DOUBLE, d->comm.comm);
|
||||
|
||||
d->xOffset = sumOffset(xLocal,
|
||||
d->comm.rank,
|
||||
d->comm.dims[JDIM],
|
||||
d->comm.coords[IDIM]);
|
||||
d->yOffset = sumOffset(yLocal, d->comm.rank, 1, d->comm.coords[JDIM]);
|
||||
d->xOffsetEnd = d->xOffset + d->xLocal;
|
||||
d->yOffsetEnd = d->yOffset + d->yLocal;
|
||||
#else
|
||||
|
||||
d->xOffset = 0;
|
||||
d->yOffset = 0;
|
||||
d->xOffsetEnd = d->xOffset + d->xLocal;
|
||||
d->yOffsetEnd = d->yOffset + d->yLocal;
|
||||
#endif
|
||||
|
||||
printf("Rank : %d, xOffset : %.2f, yOffset : %.2f, xOffsetEnd : %.2f, yOffsetEnd : "
|
||||
"%.2f\n",
|
||||
d->comm.rank,
|
||||
d->xOffset,
|
||||
d->yOffset,
|
||||
d->xOffsetEnd,
|
||||
d->yOffsetEnd);
|
||||
|
||||
double* s = d->grid.s;
|
||||
int iOffset = 0, jOffset = 0;
|
||||
|
||||
double xCenter = 0, yCenter = 0, radius = 0;
|
||||
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
|
||||
|
||||
switch (params->shape) {
|
||||
case NOSHAPE:
|
||||
break;
|
||||
case RECT:
|
||||
x1 = params->xCenter - params->xRectLength / 2;
|
||||
x2 = params->xCenter + params->xRectLength / 2;
|
||||
y1 = params->yCenter - params->yRectLength / 2;
|
||||
y2 = params->yCenter + params->yRectLength / 2;
|
||||
|
||||
iOffset = d->xOffset / dx;
|
||||
jOffset = d->yOffset / dy;
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; ++j) {
|
||||
for (int i = 1; i < imaxLocal + 1; ++i) {
|
||||
if ((x1 <= ((i + iOffset) * dx)) && (((i + iOffset) * dx) <= x2) &&
|
||||
(y1 <= ((j + jOffset) * dy)) && (((j + jOffset) * dy) <= y2)) {
|
||||
S(i, j) = OBSTACLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
case CIRCLE:
|
||||
xCenter = params->xCenter;
|
||||
yCenter = params->yCenter;
|
||||
radius = params->circleRadius;
|
||||
|
||||
iOffset = d->xOffset / dx;
|
||||
jOffset = d->yOffset / dy;
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
if (distance(((i + iOffset) * dx),
|
||||
((j + jOffset) * dy),
|
||||
xCenter,
|
||||
yCenter) <= radius) {
|
||||
S(i, j) = OBSTACLE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef _MPI
|
||||
commExchange(&d->comm, s);
|
||||
#endif
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
if (S(i, j - 1) == FLUID && S(i, j + 1) == OBSTACLE && S(i, j) == OBSTACLE)
|
||||
S(i, j) = BOTTOM; // BOTTOM
|
||||
if (S(i - 1, j) == FLUID && S(i + 1, j) == OBSTACLE && S(i, j) == OBSTACLE)
|
||||
S(i, j) = LEFT; // LEFT
|
||||
if (S(i + 1, j) == FLUID && S(i - 1, j) == OBSTACLE && S(i, j) == OBSTACLE)
|
||||
S(i, j) = RIGHT; // RIGHT
|
||||
if (S(i, j + 1) == FLUID && S(i, j - 1) == OBSTACLE && S(i, j) == OBSTACLE)
|
||||
S(i, j) = TOP; // TOP
|
||||
if (S(i - 1, j - 1) == FLUID && S(i, j - 1) == FLUID &&
|
||||
S(i - 1, j) == FLUID && S(i + 1, j + 1) == OBSTACLE &&
|
||||
(S(i, j) == OBSTACLE || S(i, j) == LEFT || S(i, j) == BOTTOM))
|
||||
S(i, j) = BOTTOMLEFT; // BOTTOMLEFT
|
||||
if (S(i + 1, j - 1) == FLUID && S(i, j - 1) == FLUID &&
|
||||
S(i + 1, j) == FLUID && S(i - 1, j + 1) == OBSTACLE &&
|
||||
(S(i, j) == OBSTACLE || S(i, j) == RIGHT || S(i, j) == BOTTOM))
|
||||
S(i, j) = BOTTOMRIGHT; // BOTTOMRIGHT
|
||||
if (S(i - 1, j + 1) == FLUID && S(i - 1, j) == FLUID &&
|
||||
S(i, j + 1) == FLUID && S(i + 1, j - 1) == OBSTACLE &&
|
||||
(S(i, j) == OBSTACLE || S(i, j) == LEFT || S(i, j) == TOP))
|
||||
S(i, j) = TOPLEFT; // TOPLEFT
|
||||
if (S(i + 1, j + 1) == FLUID && S(i + 1, j) == FLUID &&
|
||||
S(i, j + 1) == FLUID && S(i - 1, j - 1) == OBSTACLE &&
|
||||
(S(i, j) == OBSTACLE || S(i, j) == RIGHT || S(i, j) == TOP))
|
||||
S(i, j) = TOPRIGHT; // TOPRIGHT
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef VERBOSE
|
||||
printConfig(d);
|
||||
#endif
|
||||
}
|
||||
|
||||
void computeRHS(Discretization* d)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
double idx = 1.0 / d->grid.dx;
|
||||
double idy = 1.0 / d->grid.dy;
|
||||
double idt = 1.0 / d->dt;
|
||||
double* rhs = d->rhs;
|
||||
double* f = d->f;
|
||||
double* g = d->g;
|
||||
|
||||
commShift(&d->comm, f, g);
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
RHS(i, j) = ((F(i, j) - F(i - 1, j)) * idx + (G(i, j) - G(i, j - 1)) * idy) *
|
||||
idt;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static double maxElement(Discretization* d, double* m)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
int size = (imaxLocal + 2) * (jmaxLocal + 2);
|
||||
double maxval = DBL_MIN;
|
||||
|
||||
for (int i = 0; i < size; i++) {
|
||||
maxval = MAX(maxval, fabs(m[i]));
|
||||
}
|
||||
|
||||
commReduction(&maxval, MAX);
|
||||
return maxval;
|
||||
}
|
||||
|
||||
void computeTimestep(Discretization* d)
|
||||
{
|
||||
double dt = d->dtBound;
|
||||
double dx = d->grid.dx;
|
||||
double dy = d->grid.dy;
|
||||
double umax = maxElement(d, d->u);
|
||||
double vmax = maxElement(d, d->v);
|
||||
|
||||
if (umax > 0) {
|
||||
dt = (dt > dx / umax) ? dx / umax : dt;
|
||||
}
|
||||
if (vmax > 0) {
|
||||
dt = (dt > dy / vmax) ? dy / vmax : dt;
|
||||
}
|
||||
|
||||
d->dt = dt * d->tau;
|
||||
}
|
||||
|
||||
void setBoundaryConditions(Discretization* d)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
|
||||
if (commIsBoundary(&d->comm, T)) {
|
||||
switch (d->bcTop) {
|
||||
case NOSLIP:
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
V(i, jmaxLocal) = 0.0;
|
||||
U(i, jmaxLocal + 1) = -U(i, jmaxLocal);
|
||||
}
|
||||
break;
|
||||
case SLIP:
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
V(i, jmaxLocal) = 0.0;
|
||||
U(i, jmaxLocal + 1) = U(i, jmaxLocal);
|
||||
}
|
||||
break;
|
||||
case OUTFLOW:
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
U(i, jmaxLocal + 1) = U(i, jmaxLocal);
|
||||
V(i, jmaxLocal) = V(i, jmaxLocal - 1);
|
||||
}
|
||||
break;
|
||||
case PERIODIC:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&d->comm, B)) {
|
||||
switch (d->bcBottom) {
|
||||
case NOSLIP:
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
V(i, 0) = 0.0;
|
||||
U(i, 0) = -U(i, 1);
|
||||
}
|
||||
break;
|
||||
case SLIP:
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
V(i, 0) = 0.0;
|
||||
U(i, 0) = U(i, 1);
|
||||
}
|
||||
break;
|
||||
case OUTFLOW:
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
U(i, 0) = U(i, 1);
|
||||
V(i, 0) = V(i, 1);
|
||||
}
|
||||
break;
|
||||
case PERIODIC:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&d->comm, R)) {
|
||||
switch (d->bcRight) {
|
||||
case NOSLIP:
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(imaxLocal, j) = 0.0;
|
||||
V(imaxLocal + 1, j) = -V(imaxLocal, j);
|
||||
}
|
||||
break;
|
||||
case SLIP:
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(imaxLocal, j) = 0.0;
|
||||
V(imaxLocal + 1, j) = V(imaxLocal, j);
|
||||
}
|
||||
break;
|
||||
case OUTFLOW:
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(imaxLocal, j) = U(imaxLocal - 1, j);
|
||||
V(imaxLocal + 1, j) = V(imaxLocal, j);
|
||||
}
|
||||
break;
|
||||
case PERIODIC:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&d->comm, L)) {
|
||||
switch (d->bcLeft) {
|
||||
case NOSLIP:
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(0, j) = 0.0;
|
||||
V(0, j) = -V(1, j);
|
||||
}
|
||||
break;
|
||||
case SLIP:
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(0, j) = 0.0;
|
||||
V(0, j) = V(1, j);
|
||||
}
|
||||
break;
|
||||
case OUTFLOW:
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(0, j) = U(1, j);
|
||||
V(0, j) = V(1, j);
|
||||
}
|
||||
break;
|
||||
case PERIODIC:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setSpecialBoundaryCondition(Discretization* d)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
double* u = d->u;
|
||||
double* s = d->grid.s;
|
||||
|
||||
if (strcmp(d->problem, "dcavity") == 0) {
|
||||
if (commIsBoundary(&d->comm, T)) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
U(i, jmaxLocal + 1) = 2.0 - U(i, jmaxLocal);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(d->problem, "canal") == 0) {
|
||||
if (commIsBoundary(&d->comm, L)) {
|
||||
|
||||
double ylength = d->grid.ylength;
|
||||
double dy = d->grid.dy;
|
||||
int rest = d->grid.jmax % d->comm.dims[JDIM];
|
||||
int yc = d->comm.rank * (d->grid.jmax / d->comm.dims[JDIM]) +
|
||||
MIN(rest, d->comm.rank);
|
||||
double ys = dy * (yc + 0.5);
|
||||
double y;
|
||||
|
||||
// printf("RANK %d yc: %d ys: %f\n",d->comm.rank, yc, ys);
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
y = ys + dy * (j - 0.5);
|
||||
U(0, j) = y * (ylength - y) * 4.0 / (ylength * ylength);
|
||||
}
|
||||
}
|
||||
} else if (strcmp(d->problem, "backstep") == 0) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
if (S(0, j) == FLUID) U(0, j) = 1.0;
|
||||
}
|
||||
} else if (strcmp(d->problem, "karman") == 0) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
U(0, j) = 1.0;
|
||||
}
|
||||
}
|
||||
/* print(solver, solver->u); */
|
||||
}
|
||||
|
||||
void setObjectBoundaryCondition(Discretization* d)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
double* s = d->grid.s;
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
switch ((int)S(i, j)) {
|
||||
case TOP:
|
||||
U(i, j) = -U(i, j + 1);
|
||||
U(i - 1, j) = -U(i - 1, j + 1);
|
||||
V(i, j) = 0.0;
|
||||
break;
|
||||
case BOTTOM:
|
||||
U(i, j) = -U(i, j - 1);
|
||||
U(i - 1, j) = -U(i - 1, j - 1);
|
||||
V(i, j) = 0.0;
|
||||
break;
|
||||
case LEFT:
|
||||
U(i - 1, j) = 0.0;
|
||||
V(i, j) = -V(i - 1, j);
|
||||
V(i, j - 1) = -V(i - 1, j - 1);
|
||||
break;
|
||||
case RIGHT:
|
||||
U(i, j) = 0.0;
|
||||
V(i, j) = -V(i + 1, j);
|
||||
V(i, j - 1) = -V(i + 1, j - 1);
|
||||
break;
|
||||
case TOPLEFT:
|
||||
U(i, j) = -U(i, j + 1);
|
||||
U(i - 1, j) = 0.0;
|
||||
V(i, j) = 0.0;
|
||||
V(i, j - 1) = -V(i - 1, j - 1);
|
||||
break;
|
||||
case TOPRIGHT:
|
||||
U(i, j) = 0.0;
|
||||
U(i - 1, j) = -U(i - 1, j + 1);
|
||||
V(i, j) = 0.0;
|
||||
V(i, j - 1) = -V(i + 1, j - 1);
|
||||
break;
|
||||
case BOTTOMLEFT:
|
||||
U(i, j) = -U(i, j - 1);
|
||||
U(i - 1, j) = 0.0;
|
||||
V(i, j) = -V(i - 1, j);
|
||||
V(i, j - 1) = 0.0;
|
||||
break;
|
||||
case BOTTOMRIGHT:
|
||||
U(i, j) = 0.0;
|
||||
U(i - 1, j) = -U(i - 1, j - 1);
|
||||
V(i, j) = -V(i, j + 1);
|
||||
V(i, j - 1) = 0.0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void computeFG(Discretization* d)
|
||||
{
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
double* f = d->f;
|
||||
double* g = d->g;
|
||||
double* s = d->grid.s;
|
||||
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
|
||||
double gx = d->gx;
|
||||
double gy = d->gy;
|
||||
double gamma = d->gamma;
|
||||
double dt = d->dt;
|
||||
double inverseRe = 1.0 / d->re;
|
||||
double inverseDx = 1.0 / d->grid.dx;
|
||||
double inverseDy = 1.0 / d->grid.dy;
|
||||
double du2dx, dv2dy, duvdx, duvdy;
|
||||
double du2dx2, du2dy2, dv2dx2, dv2dy2;
|
||||
|
||||
commExchange(&d->comm, u);
|
||||
commExchange(&d->comm, v);
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
if (S(i, j) == FLUID) {
|
||||
du2dx = inverseDx * 0.25 *
|
||||
((U(i, j) + U(i + 1, j)) * (U(i, j) + U(i + 1, j)) -
|
||||
(U(i, j) + U(i - 1, j)) * (U(i, j) + U(i - 1, j))) +
|
||||
gamma * inverseDx * 0.25 *
|
||||
(fabs(U(i, j) + U(i + 1, j)) * (U(i, j) - U(i + 1, j)) +
|
||||
fabs(U(i, j) + U(i - 1, j)) * (U(i, j) - U(i - 1, j)));
|
||||
|
||||
duvdy = inverseDy * 0.25 *
|
||||
((V(i, j) + V(i + 1, j)) * (U(i, j) + U(i, j + 1)) -
|
||||
(V(i, j - 1) + V(i + 1, j - 1)) *
|
||||
(U(i, j) + U(i, j - 1))) +
|
||||
gamma * inverseDy * 0.25 *
|
||||
(fabs(V(i, j) + V(i + 1, j)) * (U(i, j) - U(i, j + 1)) +
|
||||
fabs(V(i, j - 1) + V(i + 1, j - 1)) *
|
||||
(U(i, j) - U(i, j - 1)));
|
||||
|
||||
du2dx2 = inverseDx * inverseDx *
|
||||
(U(i + 1, j) - 2.0 * U(i, j) + U(i - 1, j));
|
||||
du2dy2 = inverseDy * inverseDy *
|
||||
(U(i, j + 1) - 2.0 * U(i, j) + U(i, j - 1));
|
||||
F(i, j) = U(i, j) +
|
||||
dt * (inverseRe * (du2dx2 + du2dy2) - du2dx - duvdy + gx);
|
||||
|
||||
duvdx = inverseDx * 0.25 *
|
||||
((U(i, j) + U(i, j + 1)) * (V(i, j) + V(i + 1, j)) -
|
||||
(U(i - 1, j) + U(i - 1, j + 1)) *
|
||||
(V(i, j) + V(i - 1, j))) +
|
||||
gamma * inverseDx * 0.25 *
|
||||
(fabs(U(i, j) + U(i, j + 1)) * (V(i, j) - V(i + 1, j)) +
|
||||
fabs(U(i - 1, j) + U(i - 1, j + 1)) *
|
||||
(V(i, j) - V(i - 1, j)));
|
||||
|
||||
dv2dy = inverseDy * 0.25 *
|
||||
((V(i, j) + V(i, j + 1)) * (V(i, j) + V(i, j + 1)) -
|
||||
(V(i, j) + V(i, j - 1)) * (V(i, j) + V(i, j - 1))) +
|
||||
gamma * inverseDy * 0.25 *
|
||||
(fabs(V(i, j) + V(i, j + 1)) * (V(i, j) - V(i, j + 1)) +
|
||||
fabs(V(i, j) + V(i, j - 1)) * (V(i, j) - V(i, j - 1)));
|
||||
|
||||
dv2dx2 = inverseDx * inverseDx *
|
||||
(V(i + 1, j) - 2.0 * V(i, j) + V(i - 1, j));
|
||||
dv2dy2 = inverseDy * inverseDy *
|
||||
(V(i, j + 1) - 2.0 * V(i, j) + V(i, j - 1));
|
||||
G(i, j) = V(i, j) +
|
||||
dt * (inverseRe * (dv2dx2 + dv2dy2) - duvdx - dv2dy + gy);
|
||||
} else {
|
||||
switch ((int)S(i, j)) {
|
||||
case TOP:
|
||||
G(i, j) = V(i, j);
|
||||
break;
|
||||
case BOTTOM:
|
||||
G(i, j - 1) = V(i, j - 1);
|
||||
break;
|
||||
case LEFT:
|
||||
F(i - 1, j) = U(i - 1, j);
|
||||
break;
|
||||
case RIGHT:
|
||||
F(i, j) = U(i, j);
|
||||
break;
|
||||
case TOPLEFT:
|
||||
F(i - 1, j) = U(i - 1, j);
|
||||
G(i, j) = V(i, j);
|
||||
break;
|
||||
case TOPRIGHT:
|
||||
F(i, j) = U(i, j);
|
||||
G(i, j) = V(i, j);
|
||||
break;
|
||||
case BOTTOMLEFT:
|
||||
F(i - 1, j) = U(i - 1, j);
|
||||
G(i, j - 1) = V(i, j - 1);
|
||||
break;
|
||||
case BOTTOMRIGHT:
|
||||
F(i, j) = U(i, j);
|
||||
G(i, j - 1) = V(i, j - 1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------- boundary of F --------------------------- */
|
||||
if (commIsBoundary(&d->comm, L)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
F(0, j) = U(0, j);
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&d->comm, R)) {
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
F(imaxLocal, j) = U(imaxLocal, j);
|
||||
}
|
||||
}
|
||||
|
||||
/* ----------------------------- boundary of G --------------------------- */
|
||||
if (commIsBoundary(&d->comm, B)) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
G(i, 0) = V(i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (commIsBoundary(&d->comm, T)) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
G(i, jmaxLocal) = V(i, jmaxLocal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void adaptUV(Discretization* d)
|
||||
{
|
||||
int imaxLocal = d->comm.imaxLocal;
|
||||
int jmaxLocal = d->comm.jmaxLocal;
|
||||
|
||||
double* p = d->p;
|
||||
double* u = d->u;
|
||||
double* v = d->v;
|
||||
double* f = d->f;
|
||||
double* g = d->g;
|
||||
|
||||
double factorX = d->dt / d->grid.dx;
|
||||
double factorY = d->dt / d->grid.dy;
|
||||
|
||||
for (int j = 1; j < jmaxLocal + 1; j++) {
|
||||
for (int i = 1; i < imaxLocal + 1; i++) {
|
||||
U(i, j) = F(i, j) - (P(i + 1, j) - P(i, j)) * factorX;
|
||||
V(i, j) = G(i, j) - (P(i, j + 1) - P(i, j)) * factorY;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void writeResult(Discretization* d, double* u, double* v, double* p)
|
||||
{
|
||||
int imax = d->grid.imax;
|
||||
int jmax = d->grid.jmax;
|
||||
double dx = d->grid.dx;
|
||||
double dy = d->grid.dy;
|
||||
double x = 0.0, y = 0.0;
|
||||
|
||||
FILE* fp;
|
||||
fp = fopen("pressure.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int j = 1; j <= jmax; j++) {
|
||||
y = (double)(j - 0.5) * dy;
|
||||
for (int i = 1; i <= imax; i++) {
|
||||
x = (double)(i - 0.5) * dx;
|
||||
fprintf(fp, "%.2f %.2f %f\n", x, y, p[j * (imax + 2) + i]);
|
||||
}
|
||||
fprintf(fp, "\n");
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
|
||||
fp = fopen("velocity.dat", "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int j = 1; j <= jmax; j++) {
|
||||
y = dy * (j - 0.5);
|
||||
for (int i = 1; i <= imax; i++) {
|
||||
x = dx * (i - 0.5);
|
||||
double velU = (u[j * (imax + 2) + i] + u[j * (imax + 2) + (i - 1)]) / 2.0;
|
||||
double velV = (v[j * (imax + 2) + i] + v[(j - 1) * (imax + 2) + i]) / 2.0;
|
||||
double len = sqrt((velU * velU) + (velV * velV));
|
||||
fprintf(fp, "%.2f %.2f %f %f %f\n", x, y, velU, velV, len);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
66
EnhancedSolver/2D-mpi/src/discretization.h
Normal file
66
EnhancedSolver/2D-mpi/src/discretization.h
Normal file
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __DISCRETIZATION_H_
|
||||
#define __DISCRETIZATION_H_
|
||||
#include "comm.h"
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
#include<unistd.h>
|
||||
|
||||
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
|
||||
|
||||
enum OBJECTBOUNDARY {
|
||||
FLUID = 0,
|
||||
TOP,
|
||||
BOTTOM,
|
||||
LEFT,
|
||||
RIGHT,
|
||||
TOPLEFT,
|
||||
BOTTOMLEFT,
|
||||
TOPRIGHT,
|
||||
BOTTOMRIGHT,
|
||||
OBSTACLE
|
||||
};
|
||||
|
||||
enum SHAPE { NOSHAPE = 0, RECT, CIRCLE };
|
||||
|
||||
typedef struct {
|
||||
/* geometry and grid information */
|
||||
Grid grid;
|
||||
/* arrays */
|
||||
double *p, *rhs;
|
||||
double *f, *g;
|
||||
double *u, *v;
|
||||
/* parameters */
|
||||
double re, tau, gamma;
|
||||
double gx, gy;
|
||||
/* time stepping */
|
||||
double dt, te;
|
||||
double dtBound;
|
||||
char* problem;
|
||||
|
||||
double xLocal, yLocal, xOffset, yOffset, xOffsetEnd, yOffsetEnd;
|
||||
|
||||
|
||||
int bcLeft, bcRight, bcBottom, bcTop;
|
||||
/* communication */
|
||||
Comm comm;
|
||||
} Discretization;
|
||||
|
||||
extern void initDiscretiztion(Discretization*, Parameter*);
|
||||
extern void computeRHS(Discretization*);
|
||||
extern void normalizePressure(Discretization*);
|
||||
extern void computeTimestep(Discretization*);
|
||||
extern void setBoundaryConditions(Discretization*);
|
||||
extern void setSpecialBoundaryCondition(Discretization*);
|
||||
extern void setObjectBoundaryCondition(Discretization*);
|
||||
extern void computeFG(Discretization*);
|
||||
extern void adaptUV(Discretization*);
|
||||
extern void writeResult(Discretization* s, double* u, double* v, double* p);
|
||||
extern double sumOffset(double* , int , int , int );
|
||||
extern void print(Discretization* , double* );
|
||||
#endif
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user