Cleanup. Port MultiGrid to 3D-seq

This commit is contained in:
Jan Eitzinger 2024-03-05 10:16:03 +01:00
parent 5a872d0533
commit 1cb82b1bfa
19 changed files with 649 additions and 427 deletions

View File

@ -1,61 +0,0 @@
/*
* 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.
*/
#ifdef __linux__
#ifdef _OPENMP
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX_NUM_THREADS 128
#define gettid() syscall(SYS_gettid)
static int getProcessorID(cpu_set_t* cpu_set)
{
int processorId;
for (processorId = 0; processorId < MAX_NUM_THREADS; processorId++) {
if (CPU_ISSET(processorId, cpu_set)) {
break;
}
}
return processorId;
}
int affinity_getProcessorId()
{
cpu_set_t cpu_set;
CPU_ZERO(&cpu_set);
sched_getaffinity(gettid(), sizeof(cpu_set_t), &cpu_set);
return getProcessorID(&cpu_set);
}
void affinity_pinThread(int processorId)
{
cpu_set_t cpuset;
pthread_t thread;
thread = pthread_self();
CPU_ZERO(&cpuset);
CPU_SET(processorId, &cpuset);
pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
}
void affinity_pinProcess(int processorId)
{
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(processorId, &cpuset);
sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
}
#endif /*_OPENMP*/
#endif /*__linux__*/

View File

@ -1,14 +0,0 @@
/*
* 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.
*/
#ifndef AFFINITY_H
#define AFFINITY_H
extern int affinity_getProcessorId();
extern void affinity_pinProcess(int);
extern void affinity_pinThread(int);
#endif /*AFFINITY_H*/

View File

@ -24,7 +24,6 @@ void initParameter(Parameter* param)
param->re = 100.0; param->re = 100.0;
param->gamma = 0.9; param->gamma = 0.9;
param->tau = 0.5; param->tau = 0.5;
param->rho = 0.99;
param->levels = 5; param->levels = 5;
} }
@ -81,7 +80,6 @@ void readParameter(Parameter* param, const char* filename)
PARSE_REAL(u_init); PARSE_REAL(u_init);
PARSE_REAL(v_init); PARSE_REAL(v_init);
PARSE_REAL(p_init); PARSE_REAL(p_init);
PARSE_REAL(rho);
} }
} }
@ -112,6 +110,5 @@ void printParameter(Parameter* param)
printf("\tepsilon (stopping tolerance) : %f\n", param->eps); printf("\tepsilon (stopping tolerance) : %f\n", param->eps);
printf("\tgamma (stopping tolerance) : %f\n", param->gamma); printf("\tgamma (stopping tolerance) : %f\n", param->gamma);
printf("\tomega (SOR relaxation): %f\n", param->omg); printf("\tomega (SOR relaxation): %f\n", param->omg);
printf("\trho (SOR relaxation): %f\n", param->rho);
printf("\tMultiGrid levels : %d\n", param->levels); printf("\tMultiGrid levels : %d\n", param->levels);
} }

View File

@ -9,6 +9,6 @@
extern void initProgress(double); extern void initProgress(double);
extern void printProgress(double); extern void printProgress(double);
extern void stopProgress(); extern void stopProgress(void);
#endif #endif

View File

@ -8,12 +8,11 @@
#include <stdlib.h> #include <stdlib.h>
#include "allocate.h" #include "allocate.h"
#include "discretization.h"
#include "parameter.h"
#include "solver.h" #include "solver.h"
#include "util.h" #include "util.h"
#define FINEST_LEVEL 0 #define FINEST_LEVEL 0
#define COARSEST_LEVEL (solver->levels - 1)
#define S(i, j) s[(j) * (imax + 2) + (i)] #define S(i, j) s[(j) * (imax + 2) + (i)]
#define E(i, j) e[(j) * (imax + 2) + (i)] #define E(i, j) e[(j) * (imax + 2) + (i)]
#define R(i, j) r[(j) * (imax + 2) + (i)] #define R(i, j) r[(j) * (imax + 2) + (i)]
@ -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) static void correct(Solver* s, double* p, int level, int imax, int jmax)
{ {
double* e = s->e[level]; double* e = s->e[level];
for (int j = 1; j < jmax + 1; ++j) { for (int j = 1; j < jmax + 1; ++j) {
for (int i = 1; i < imax + 1; ++i) { for (int i = 1; i < imax + 1; ++i) {
P(i, j) += E(i, j); P(i, j) += E(i, j);
@ -106,37 +106,8 @@ static double smooth(Solver* s, double* p, double* rhs, int level, int imax, int
return res; return res;
} }
void initSolver(Solver* s, Discretization* d, Parameter* p) static double multiGrid(
{ Solver* solver, double* p, double* rhs, int level, int imax, int jmax)
s->eps = p->eps;
s->omega = p->omg;
s->itermax = p->itermax;
s->rho = p->rho;
s->levels = p->levels;
s->grid = &d->grid;
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 multiGrid(Solver* solver, double* p, double* rhs, int level, int imax, int jmax)
{ {
double res = 0.0; double res = 0.0;
@ -161,7 +132,7 @@ double multiGrid(Solver* solver, double* p, double* rhs, int level, int imax, in
// TODO: What if there is a rest? // TODO: What if there is a rest?
multiGrid(solver, multiGrid(solver,
solver->e[level + 1], solver->e[level + 1],
solver->r[level], solver->r[level + 1],
level + 1, level + 1,
imax / 2, imax / 2,
jmax / 2); jmax / 2);
@ -182,6 +153,35 @@ double multiGrid(Solver* solver, double* p, double* rhs, int level, int imax, in
return res; 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;
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;
}
}
}
void solve(Solver* s, double* p, double* rhs) void solve(Solver* s, double* p, double* rhs)
{ {
double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax); double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax);

View File

@ -4,7 +4,6 @@
* Use of this source code is governed by a MIT style * Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include "discretization.h"
#include "solver.h" #include "solver.h"
#include "util.h" #include "util.h"

View File

@ -18,9 +18,10 @@ include $(MAKE_DIR)/include_$(TAG).mk
INCLUDES += -I$(SRC_DIR) -I$(BUILD_DIR) INCLUDES += -I$(SRC_DIR) -I$(BUILD_DIR)
VPATH = $(SRC_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)) ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s, $(SRC))
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC)) OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
OBJ += $(BUILD_DIR)/solver-$(SOLVER).o
SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h) SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h)
CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(OPTIONS) $(INCLUDES) CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(OPTIONS) $(INCLUDES)
@ -47,6 +48,8 @@ clean:
distclean: clean distclean: clean
$(info ===> DIST CLEAN) $(info ===> DIST CLEAN)
@rm -f $(TARGET) @rm -f $(TARGET)
@rm -f *.dat
@rm -f *.png
info: info:
$(info $(CFLAGS)) $(info $(CFLAGS))

View File

@ -1,12 +1,12 @@
# Supported: GCC, CLANG, ICC # Supported: GCC, CLANG, ICC
TAG ?= CLANG TAG ?= CLANG
ENABLE_OPENMP ?= false ENABLE_OPENMP ?= false
# Supported: sor, mg
SOLVER ?= mg
# Run in debug settings
DEBUG ?= false
#Feature options #Feature options
OPTIONS += -DARRAY_ALIGNMENT=64 OPTIONS += -DARRAY_ALIGNMENT=64
OPTIONS += -DVERBOSE #OPTIONS += -DVERBOSE
#OPTIONS += -DDEBUG #OPTIONS += -DDEBUG
#OPTIONS += -DBOUNDCHECK
#OPTIONS += -DVERBOSE_AFFINITY
#OPTIONS += -DVERBOSE_DATASIZE
#OPTIONS += -DVERBOSE_TIMER

View File

@ -47,6 +47,8 @@ tau 0.5 # safety factor for time stepsize control (<0 constant delt)
itermax 1000 # maximal number of pressure iteration in one time step itermax 1000 # maximal number of pressure iteration in one time step
eps 0.001 # stopping tolerance for pressure iteration eps 0.001 # stopping tolerance for pressure iteration
rho 0.5
omg 1.7 # relaxation parameter for SOR iteration omg 1.7 # relaxation parameter for SOR iteration
gamma 0.9 # upwind differencing factor gamma gamma 0.9 # upwind differencing factor gamma
levels 5 # Multigrid levels
#=============================================================================== #===============================================================================

View File

@ -2,16 +2,18 @@ CC = clang
GCC = cc GCC = cc
LINKER = $(CC) LINKER = $(CC)
ifeq ($(ENABLE_OPENMP),true) ifeq ($(strip $(ENABLE_OPENMP)),true)
OPENMP = -fopenmp OPENMP = -fopenmp
#OPENMP = -Xpreprocessor -fopenmp #required on Macos with homebrew libomp #OPENMP = -Xpreprocessor -fopenmp #required on Macos with homebrew libomp
LIBS = # -lomp LIBS = # -lomp
endif endif
ifeq ($(strip $(DEBUG)),true)
CFLAGS = -O0 -g -std=c17
else
CFLAGS = -O3 -std=c17 $(OPENMP)
endif
VERSION = --version 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 LFLAGS = $(OPENMP) -lm
DEFINES = -D_GNU_SOURCE# -DDEBUG DEFINES = -D_GNU_SOURCE
INCLUDES = INCLUDES =

View File

@ -10,131 +10,120 @@
#include <string.h> #include <string.h>
#include "allocate.h" #include "allocate.h"
#include "discretization.h"
#include "parameter.h" #include "parameter.h"
#include "solver.h"
#include "util.h" #include "util.h"
#define P(i, j, k) p[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)] static void printConfig(Discretization* d)
#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)
{ {
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", printf("BC Left:%d Right:%d Bottom:%d Top:%d Front:%d Back:%d\n",
s->bcLeft, d->bcLeft,
s->bcRight, d->bcRight,
s->bcBottom, d->bcBottom,
s->bcTop, d->bcTop,
s->bcFront, d->bcFront,
s->bcBack); d->bcBack);
printf("\tReynolds number: %.2f\n", s->re); printf("\tReynolds number: %.2f\n", d->re);
printf("\tGx Gy: %.2f %.2f %.2f\n", s->gx, s->gy, s->gz); printf("\tGx Gy: %.2f %.2f %.2f\n", d->gx, d->gy, d->gz);
printf("Geometry data:\n"); printf("Geometry data:\n");
printf("\tDomain box size (x, y, z): %.2f, %.2f, %.2f\n", printf("\tDomain box size (x, y, z): %.2f, %.2f, %.2f\n",
s->grid.xlength, d->grid.xlength,
s->grid.ylength, d->grid.ylength,
s->grid.zlength); d->grid.zlength);
printf("\tCells (x, y, z): %d, %d, %d\n", s->grid.imax, s->grid.jmax, s->grid.kmax); 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", s->grid.dx, s->grid.dy, s->grid.dz); printf("\tCell size (dx, dy, dz): %f, %f, %f\n", d->grid.dx, d->grid.dy, d->grid.dz);
printf("Timestep parameters:\n"); printf("Timestep parameters:\n");
printf("\tDefault stepsize: %.2f, Final time %.2f\n", s->dt, s->te); printf("\tDefault stepsize: %.2f, Final time %.2f\n", d->dt, d->te);
printf("\tdt bound: %.6f\n", s->dtBound); printf("\tdt bound: %.6f\n", d->dtBound);
printf("\tTau factor: %.2f\n", s->tau); printf("\tTau factor: %.2f\n", d->tau);
printf("Iterative parameters:\n"); printf("Iterative parameters:\n");
printf("\tMax iterations: %d\n", s->itermax); printf("\tepsilon (stopping tolerance) : %f\n", d->eps);
printf("\tepsilon (stopping tolerance) : %f\n", s->eps); printf("\tgamma factor: %f\n", d->gamma);
printf("\tgamma factor: %f\n", s->gamma); printf("\tomega (SOR relaxation): %f\n", d->omega);
printf("\tomega (SOR relaxation): %f\n", s->omega);
} }
void initSolver(Solver* s, Parameter* params) void initDiscretization(Discretization* d, Parameter* p)
{ {
s->problem = params->name; d->problem = p->name;
s->bcLeft = params->bcLeft; d->bcLeft = p->bcLeft;
s->bcRight = params->bcRight; d->bcRight = p->bcRight;
s->bcBottom = params->bcBottom; d->bcBottom = p->bcBottom;
s->bcTop = params->bcTop; d->bcTop = p->bcTop;
s->bcFront = params->bcFront; d->bcFront = p->bcFront;
s->bcBack = params->bcBack; d->bcBack = p->bcBack;
s->grid.imax = params->imax; d->grid.imax = p->imax;
s->grid.jmax = params->jmax; d->grid.jmax = p->jmax;
s->grid.kmax = params->kmax; d->grid.kmax = p->kmax;
s->grid.xlength = params->xlength; d->grid.xlength = p->xlength;
s->grid.ylength = params->ylength; d->grid.ylength = p->ylength;
s->grid.zlength = params->zlength; d->grid.zlength = p->zlength;
s->grid.dx = params->xlength / params->imax; d->grid.dx = p->xlength / p->imax;
s->grid.dy = params->ylength / params->jmax; d->grid.dy = p->ylength / p->jmax;
s->grid.dz = params->zlength / params->kmax; d->grid.dz = p->zlength / p->kmax;
s->eps = params->eps; d->eps = p->eps;
s->omega = params->omg; d->omega = p->omg;
s->itermax = params->itermax; d->re = p->re;
s->re = params->re; d->gx = p->gx;
s->gx = params->gx; d->gy = p->gy;
s->gy = params->gy; d->gz = p->gz;
s->gz = params->gz; d->dt = p->dt;
s->dt = params->dt; d->te = p->te;
s->te = params->te; d->tau = p->tau;
s->tau = params->tau; d->gamma = p->gamma;
s->gamma = params->gamma;
int imax = s->grid.imax; int imax = d->grid.imax;
int jmax = s->grid.jmax; int jmax = d->grid.jmax;
int kmax = s->grid.kmax; int kmax = d->grid.kmax;
size_t bytesize = (imax + 2) * (jmax + 2) * (kmax + 2) * sizeof(double); size_t bytesize = (imax + 2) * (jmax + 2) * (kmax + 2) * sizeof(double);
s->u = allocate(64, bytesize); d->u = allocate(64, bytesize);
s->v = allocate(64, bytesize); d->v = allocate(64, bytesize);
s->w = allocate(64, bytesize); d->w = allocate(64, bytesize);
s->p = allocate(64, bytesize); d->p = allocate(64, bytesize);
s->rhs = allocate(64, bytesize); d->rhs = allocate(64, bytesize);
s->f = allocate(64, bytesize); d->f = allocate(64, bytesize);
s->g = allocate(64, bytesize); d->g = allocate(64, bytesize);
s->h = allocate(64, bytesize); d->h = allocate(64, bytesize);
for (int i = 0; i < (imax + 2) * (jmax + 2) * (kmax + 2); i++) { for (int i = 0; i < (imax + 2) * (jmax + 2) * (kmax + 2); i++) {
s->u[i] = params->u_init; d->u[i] = p->u_init;
s->v[i] = params->v_init; d->v[i] = p->v_init;
s->w[i] = params->w_init; d->w[i] = p->w_init;
s->p[i] = params->p_init; d->p[i] = p->p_init;
s->rhs[i] = 0.0; d->rhs[i] = 0.0;
s->f[i] = 0.0; d->f[i] = 0.0;
s->g[i] = 0.0; d->g[i] = 0.0;
s->h[i] = 0.0; d->h[i] = 0.0;
} }
double dx = s->grid.dx; double dx = d->grid.dx;
double dy = s->grid.dy; double dy = d->grid.dy;
double dz = s->grid.dz; double dz = d->grid.dz;
double invSqrSum = 1.0 / (dx * dx) + 1.0 / (dy * dy) + 1.0 / (dz * 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 #ifdef VERBOSE
printConfig(s); printConfig(s);
#endif /* VERBOSE */ #endif /* VERBOSE */
} }
void computeRHS(Solver* s) void computeRHS(Discretization* d)
{ {
int imax = s->grid.imax; int imax = d->grid.imax;
int jmax = s->grid.jmax; int jmax = d->grid.jmax;
int kmax = s->grid.kmax; int kmax = d->grid.kmax;
double idx = 1.0 / s->grid.dx; double idx = 1.0 / d->grid.dx;
double idy = 1.0 / s->grid.dy; double idy = 1.0 / d->grid.dy;
double idz = 1.0 / s->grid.dz; double idz = 1.0 / d->grid.dz;
double idt = 1.0 / s->dt; double idt = 1.0 / d->dt;
double* rhs = s->rhs; double* rhs = d->rhs;
double* f = s->f; double* f = d->f;
double* g = s->g; double* g = d->g;
double* h = s->h; double* h = d->h;
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
for (int j = 1; j < jmax + 1; j++) { 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 size = (d->grid.imax + 2) * (d->grid.jmax + 2) * (d->grid.kmax + 2);
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);
double maxval = DBL_MIN; double maxval = DBL_MIN;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@ -245,10 +149,10 @@ static double maxElement(Solver* s, double* m)
return maxval; return maxval;
} }
void normalizePressure(Solver* s) void normalizePressure(Discretization* d)
{ {
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* p = s->p; double* p = d->p;
double avgP = 0.0; double avgP = 0.0;
for (int i = 0; i < size; i++) { 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 dt = d->dtBound;
double dx = s->grid.dx; double dx = d->grid.dx;
double dy = s->grid.dy; double dy = d->grid.dy;
double dz = s->grid.dz; double dz = d->grid.dz;
double umax = maxElement(s, s->u); double umax = maxElement(d, d->u);
double vmax = maxElement(s, s->v); double vmax = maxElement(d, d->v);
double wmax = maxElement(s, s->w); double wmax = maxElement(d, d->w);
if (umax > 0) { if (umax > 0) {
dt = (dt > dx / umax) ? dx / umax : dt; dt = (dt > dx / umax) ? dx / umax : dt;
@ -282,20 +186,20 @@ void computeTimestep(Solver* s)
dt = (dt > dz / wmax) ? dz / wmax : dt; 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 imax = d->grid.imax;
int jmax = s->grid.jmax; int jmax = d->grid.jmax;
int kmax = s->grid.kmax; int kmax = d->grid.kmax;
double* u = s->u; double* u = d->u;
double* v = s->v; double* v = d->v;
double* w = s->w; double* w = d->w;
switch (s->bcTop) { switch (d->bcTop) {
case NOSLIP: case NOSLIP:
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
for (int i = 1; i < imax + 1; i++) { for (int i = 1; i < imax + 1; i++) {
@ -327,7 +231,7 @@ void setBoundaryConditions(Solver* s)
break; break;
} }
switch (s->bcBottom) { switch (d->bcBottom) {
case NOSLIP: case NOSLIP:
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
for (int i = 1; i < imax + 1; i++) { for (int i = 1; i < imax + 1; i++) {
@ -359,7 +263,7 @@ void setBoundaryConditions(Solver* s)
break; break;
} }
switch (s->bcLeft) { switch (d->bcLeft) {
case NOSLIP: case NOSLIP:
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
for (int j = 1; j < jmax + 1; j++) { for (int j = 1; j < jmax + 1; j++) {
@ -391,7 +295,7 @@ void setBoundaryConditions(Solver* s)
break; break;
} }
switch (s->bcRight) { switch (d->bcRight) {
case NOSLIP: case NOSLIP:
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
for (int j = 1; j < jmax + 1; j++) { for (int j = 1; j < jmax + 1; j++) {
@ -423,7 +327,7 @@ void setBoundaryConditions(Solver* s)
break; break;
} }
switch (s->bcFront) { switch (d->bcFront) {
case NOSLIP: case NOSLIP:
for (int j = 1; j < jmax + 1; j++) { for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) { for (int i = 1; i < imax + 1; i++) {
@ -455,7 +359,7 @@ void setBoundaryConditions(Solver* s)
break; break;
} }
switch (s->bcBack) { switch (d->bcBack) {
case NOSLIP: case NOSLIP:
for (int j = 1; j < jmax + 1; j++) { for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) { 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 imax = d->grid.imax;
int jmax = s->grid.jmax; int jmax = d->grid.jmax;
int kmax = s->grid.kmax; int kmax = d->grid.kmax;
double mDy = s->grid.dy; double mDy = d->grid.dy;
double* u = s->u; 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 k = 1; k < kmax; k++) {
for (int i = 1; i < imax; i++) { for (int i = 1; i < imax; i++) {
U(i, jmax + 1, k) = 2.0 - U(i, jmax, k); U(i, jmax + 1, k) = 2.0 - U(i, jmax, k);
} }
} }
} else if (strcmp(s->problem, "canal") == 0) { } else if (strcmp(d->problem, "canal") == 0) {
double ylength = s->grid.ylength; double ylength = d->grid.ylength;
double y; double y;
for (int k = 1; k < kmax + 1; k++) { 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 imax = d->grid.imax;
int jmax = s->grid.jmax; int jmax = d->grid.jmax;
int kmax = s->grid.kmax; int kmax = d->grid.kmax;
double* u = s->u; double* u = d->u;
double* v = s->v; double* v = d->v;
double* w = s->w; double* w = d->w;
double* f = s->f; double* f = d->f;
double* g = s->g; double* g = d->g;
double* h = s->h; double* h = d->h;
double gx = s->gx; double gx = d->gx;
double gy = s->gy; double gy = d->gy;
double gz = s->gz; double gz = d->gz;
double dt = s->dt; double dt = d->dt;
double gamma = s->gamma; double gamma = d->gamma;
double inverseRe = 1.0 / s->re; double inverseRe = 1.0 / d->re;
double inverseDx = 1.0 / s->grid.dx; double inverseDx = 1.0 / d->grid.dx;
double inverseDy = 1.0 / s->grid.dy; double inverseDy = 1.0 / d->grid.dy;
double inverseDz = 1.0 / s->grid.dz; double inverseDz = 1.0 / d->grid.dz;
double du2dx, dv2dy, dw2dz; double du2dx, dv2dy, dw2dz;
double duvdx, duwdx, duvdy, dvwdy, duwdz, dvwdz; double duvdx, duwdx, duvdy, dvwdy, duwdz, dvwdz;
double du2dx2, du2dy2, du2dz2; 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 imax = d->grid.imax;
int jmax = s->grid.jmax; int jmax = d->grid.jmax;
int kmax = s->grid.kmax; int kmax = d->grid.kmax;
double* p = s->p; double* p = d->p;
double* u = s->u; double* u = d->u;
double* v = s->v; double* v = d->v;
double* w = s->w; double* w = d->w;
double* f = s->f; double* f = d->f;
double* g = s->g; double* g = d->g;
double* h = s->h; double* h = d->h;
double factorX = s->dt / s->grid.dx; double factorX = d->dt / d->grid.dx;
double factorY = s->dt / s->grid.dy; double factorY = d->dt / d->grid.dy;
double factorZ = s->dt / s->grid.dz; double factorZ = d->dt / d->grid.dz;
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
for (int j = 1; j < jmax + 1; j++) { for (int j = 1; j < jmax + 1; j++) {

View 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

View File

@ -9,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
#include "allocate.h" #include "allocate.h"
#include "discretization.h"
#include "parameter.h" #include "parameter.h"
#include "progress.h" #include "progress.h"
#include "solver.h" #include "solver.h"
@ -17,7 +18,8 @@
#define G(v, i, j, k) v[(k) * (imax + 2) * (jmax + 2) + (j) * (imax + 2) + (i)] #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 void createBulkArrays(
Discretization* s, double* pg, double* ug, double* vg, double* wg)
{ {
int imax = s->grid.imax; int imax = s->grid.imax;
int jmax = s->grid.jmax; int jmax = s->grid.jmax;
@ -67,6 +69,7 @@ int main(int argc, char** argv)
{ {
double timeStart, timeStop; double timeStart, timeStop;
Parameter p; Parameter p;
Discretization d;
Solver s; Solver s;
initParameter(&p); initParameter(&p);
@ -77,51 +80,53 @@ int main(int argc, char** argv)
readParameter(&p, argv[1]); readParameter(&p, argv[1]);
printParameter(&p); printParameter(&p);
initSolver(&s, &p); initDiscretization(&d, &p);
initSolver(&s, &d, &p);
#ifndef VERBOSE #ifndef VERBOSE
initProgress(s.te); initProgress(d.te);
#endif #endif
double tau = s.tau; double tau = d.tau;
double te = s.te; double te = d.te;
double t = 0.0; double t = 0.0;
int nt = 0;
timeStart = getTimeStamp(); timeStart = getTimeStamp();
while (t <= te) { while (t <= te) {
if (tau > 0.0) computeTimestep(&s); if (tau > 0.0) computeTimestep(&d);
setBoundaryConditions(&s); setBoundaryConditions(&d);
setSpecialBoundaryCondition(&s); setSpecialBoundaryCondition(&d);
computeFG(&s); computeFG(&d);
computeRHS(&s); computeRHS(&d);
solve(&s); if (nt % 100 == 0) normalizePressure(&d);
adaptUV(&s); solve(&s, d.p, d.rhs);
t += s.dt; adaptUV(&d);
t += d.dt;
nt++;
#ifdef VERBOSE #ifdef VERBOSE
printf("TIME %f , TIMESTEP %f\n", t, s.dt); printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
#else #else
printProgress(t); printProgress(t);
#endif #endif
} }
timeStop = getTimeStamp(); timeStop = getTimeStamp();
#ifndef VERBOSE
stopProgress(); stopProgress();
#endif
printf("Solution took %.2fs\n", timeStop - timeStart); printf("Solution took %.2fs\n", timeStop - timeStart);
timeStart = getTimeStamp(); timeStart = getTimeStamp();
double *pg, *ug, *vg, *wg; 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); pg = allocate(64, bytesize);
ug = allocate(64, bytesize); ug = allocate(64, bytesize);
vg = allocate(64, bytesize); vg = allocate(64, bytesize);
wg = allocate(64, bytesize); wg = allocate(64, bytesize);
createBulkArrays(&s, pg, ug, vg, wg); createBulkArrays(&d, pg, ug, vg, wg);
VtkOptions opts = { .grid = s.grid }; VtkOptions opts = { .grid = d.grid };
vtkOpen(&opts, s.problem); vtkOpen(&opts, d.problem);
vtkScalar(&opts, "pressure", pg); vtkScalar(&opts, "pressure", pg);
vtkVector(&opts, "velocity", (VtkVector) { ug, vg, wg }); vtkVector(&opts, "velocity", (VtkVector) { ug, vg, wg });
vtkClose(&opts); vtkClose(&opts);

View File

@ -26,6 +26,7 @@ void initParameter(Parameter* param)
param->re = 100.0; param->re = 100.0;
param->gamma = 0.9; param->gamma = 0.9;
param->tau = 0.5; param->tau = 0.5;
param->levels = 5;
} }
void readParameter(Parameter* param, const char* filename) void readParameter(Parameter* param, const char* filename)
@ -65,6 +66,7 @@ void readParameter(Parameter* param, const char* filename)
PARSE_INT(jmax); PARSE_INT(jmax);
PARSE_INT(kmax); PARSE_INT(kmax);
PARSE_INT(itermax); PARSE_INT(itermax);
PARSE_INT(levels);
PARSE_REAL(eps); PARSE_REAL(eps);
PARSE_REAL(omg); PARSE_REAL(omg);
PARSE_REAL(re); PARSE_REAL(re);
@ -123,4 +125,5 @@ void printParameter(Parameter* param)
printf("\tepsilon (stopping tolerance) : %f\n", param->eps); printf("\tepsilon (stopping tolerance) : %f\n", param->eps);
printf("\tgamma (stopping tolerance) : %f\n", param->gamma); printf("\tgamma (stopping tolerance) : %f\n", param->gamma);
printf("\tomega (SOR relaxation): %f\n", param->omg); printf("\tomega (SOR relaxation): %f\n", param->omg);
printf("\tMultiGrid levels : %d\n", param->levels);
} }

View File

@ -10,8 +10,8 @@
typedef struct { typedef struct {
int imax, jmax, kmax; int imax, jmax, kmax;
double xlength, ylength, zlength; double xlength, ylength, zlength;
int itermax; int itermax, levels;
double eps, omg; double eps, omg, rho;
double re, tau, gamma; double re, tau, gamma;
double te, dt; double te, dt;
double gx, gy, gz; double gx, gy, gz;

View File

@ -0,0 +1,250 @@
/*
* 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 double 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) {
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);
P(i, j, k) -= (factor * R(i, j, k));
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 TODO: Use direct solver?
if (level == COARSEST_LEVEL) {
for (int i = 0; i < 5; i++) {
smooth(s, p, rhs, level, imax, jmax, kmax);
}
return res;
}
// pre-smoothing TODO: Make smoothing steps configurable?
for (int i = 0; i < 5; i++) {
smooth(s, p, rhs, level, imax, jmax, kmax);
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax, kmax);
}
// restrict
restrictMG(s, level, imax, jmax, kmax);
// MGSolver on residual and error.
// TODO: What if there is a rest?
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 < 5; i++) {
res = 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;
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;
}
}
}
void 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
}

View 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
}

View File

@ -6,38 +6,21 @@
*/ */
#ifndef __SOLVER_H_ #ifndef __SOLVER_H_
#define __SOLVER_H_ #define __SOLVER_H_
#include "discretization.h"
#include "grid.h" #include "grid.h"
#include "parameter.h" #include "parameter.h"
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
typedef struct { typedef struct {
/* geometry and grid information */ /* geometry and grid information */
Grid grid; Grid* grid;
/* arrays */
double *p, *rhs;
double *f, *g, *h;
double *u, *v, *w;
/* parameters */ /* parameters */
double eps, omega; double eps, omega, rho;
double re, tau, gamma;
double gx, gy, gz;
/* time stepping */
int itermax; int itermax;
double dt, te; int levels;
double dtBound; double **r, **e;
char* problem;
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
} Solver; } Solver;
extern void initSolver(Solver*, Parameter*); extern void initSolver(Solver*, Discretization*, Parameter*);
extern void computeRHS(Solver*); extern void solve(Solver*, double*, double*);
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*);
#endif #endif

View File

@ -19,4 +19,13 @@
#define ABS(a) ((a) >= 0 ? (a) : -(a)) #define ABS(a) ((a) >= 0 ? (a) : -(a))
#endif #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_ #endif // __UTIL_H_