Compare commits

..

No commits in common. "7ce84e277315b0e888d7f0b2e020fc00a68c7a4a" and "5a872d05331041c19020e4c7ffda45f333ca9dd0" have entirely different histories.

44 changed files with 1646 additions and 1885 deletions

View File

@ -0,0 +1,61 @@
/*
* 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

@ -0,0 +1,14 @@
/*
* 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

@ -20,7 +20,6 @@ int main(int argc, char** argv)
Parameter p; Parameter p;
Discretization d; Discretization d;
Solver s; Solver s;
initParameter(&p); initParameter(&p);
if (argc != 2) { if (argc != 2) {
@ -32,7 +31,6 @@ int main(int argc, char** argv)
printParameter(&p); printParameter(&p);
initDiscretization(&d, &p); initDiscretization(&d, &p);
initSolver(&s, &d, &p); initSolver(&s, &d, &p);
#ifndef VERBOSE #ifndef VERBOSE
initProgress(d.te); initProgress(d.te);
#endif #endif

View File

@ -9,20 +9,22 @@
#include <string.h> #include <string.h>
#include "parameter.h" #include "parameter.h"
#include "util.h"
#define MAXLINE 4096 #define MAXLINE 4096
void initParameter(Parameter* param) void initParameter(Parameter* param)
{ {
param->xlength = 1.0; param->xlength = 1.0;
param->ylength = 1.0; param->ylength = 1.0;
param->imax = 128; param->imax = 100;
param->jmax = 128; param->jmax = 100;
param->itermax = 1000; param->itermax = 1000;
param->eps = 0.0001; param->eps = 0.0001;
param->omg = 1.7; param->omg = 1.7;
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;
} }
@ -79,6 +81,7 @@ 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);
} }
} }
@ -109,5 +112,6 @@ 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(void); extern void stopProgress();
#endif #endif

View File

@ -8,15 +8,16 @@
#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 (s->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)] #define OLD(i, j) old[(j) * (imax + 2) + (i)]
#define OLD(i, j) old[(j) * (imax + 2) + (i)]
static void restrictMG(Solver* s, int level, int imax, int jmax) static void restrictMG(Solver* s, int level, int imax, int jmax)
{ {
@ -50,7 +51,6 @@ 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,52 +106,12 @@ static double smooth(Solver* s, double* p, double* rhs, int level, int imax, int
return res; return res;
} }
static double multiGrid(Solver* s, double* p, double* rhs, int level, int imax, int jmax)
{
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);
}
return res;
}
// pre-smoothing TODO: Make smoothing steps configurable?
for (int i = 0; i < 5; i++) {
smooth(s, p, rhs, level, imax, jmax);
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
}
// restrict
restrictMG(s, level, imax, jmax);
// 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);
// 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 < 5; i++) {
res = 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) void initSolver(Solver* s, Discretization* d, Parameter* p)
{ {
s->eps = p->eps; s->eps = p->eps;
s->omega = p->omg; s->omega = p->omg;
s->itermax = p->itermax; s->itermax = p->itermax;
s->rho = p->rho;
s->levels = p->levels; s->levels = p->levels;
s->grid = &d->grid; s->grid = &d->grid;
@ -176,6 +136,52 @@ 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) 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,6 +4,7 @@
* 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,10 +18,9 @@ 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 = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c)) SRC = $(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)
@ -48,8 +47,6 @@ 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,8 +47,6 @@ 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,18 +2,16 @@ CC = clang
GCC = cc GCC = cc
LINKER = $(CC) LINKER = $(CC)
ifeq ($(strip $(ENABLE_OPENMP)),true) ifeq ($(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 DEFINES = -D_GNU_SOURCE# -DDEBUG
INCLUDES = INCLUDES =

View File

@ -1,41 +0,0 @@
/*
* 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,7 +9,6 @@
#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"
@ -18,8 +17,7 @@
#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( static void createBulkArrays(Solver* s, double* pg, double* ug, double* vg, double* wg)
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;
@ -69,7 +67,6 @@ 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);
@ -80,53 +77,51 @@ int main(int argc, char** argv)
readParameter(&p, argv[1]); readParameter(&p, argv[1]);
printParameter(&p); printParameter(&p);
initDiscretization(&d, &p); initSolver(&s, &p);
initSolver(&s, &d, &p);
#ifndef VERBOSE #ifndef VERBOSE
initProgress(d.te); initProgress(s.te);
#endif #endif
double tau = d.tau; double tau = s.tau;
double te = d.te; double te = s.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(&d); if (tau > 0.0) computeTimestep(&s);
setBoundaryConditions(&d); setBoundaryConditions(&s);
setSpecialBoundaryCondition(&d); setSpecialBoundaryCondition(&s);
computeFG(&d); computeFG(&s);
computeRHS(&d); computeRHS(&s);
if (nt % 100 == 0) normalizePressure(&d); solve(&s);
solve(&s, d.p, d.rhs); adaptUV(&s);
adaptUV(&d); t += s.dt;
t += d.dt;
nt++;
#ifdef VERBOSE #ifdef VERBOSE
printf("TIME %f , TIMESTEP %f\n", t, solver.dt); printf("TIME %f , TIMESTEP %f\n", t, s.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)(d.grid.imax * d.grid.jmax * d.grid.kmax) * sizeof(double); size_t bytesize = (size_t)(s.grid.imax * s.grid.jmax * s.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(&d, pg, ug, vg, wg); createBulkArrays(&s, pg, ug, vg, wg);
VtkOptions opts = { .grid = d.grid }; VtkOptions opts = { .grid = s.grid };
vtkOpen(&opts, d.problem); vtkOpen(&opts, s.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,7 +26,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->levels = 5;
} }
void readParameter(Parameter* param, const char* filename) void readParameter(Parameter* param, const char* filename)
@ -66,7 +65,6 @@ 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);
@ -125,5 +123,4 @@ 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, levels; int itermax;
double eps, omg, rho; double eps, omg;
double re, tau, gamma; double re, tau, gamma;
double te, dt; double te, dt;
double gx, gy, gz; double gx, gy, gz;

View File

@ -1,250 +0,0 @@
/*
* 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

@ -1,99 +0,0 @@
/*
* 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

@ -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. * All rights reserved. This file is part of nusif-solver.
* 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.
@ -10,120 +10,131 @@
#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"
static void printConfig(Discretization* d) #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)
{ {
printf("Parameters for #%s#\n", d->problem); printf("Parameters for #%s#\n", s->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",
d->bcLeft, s->bcLeft,
d->bcRight, s->bcRight,
d->bcBottom, s->bcBottom,
d->bcTop, s->bcTop,
d->bcFront, s->bcFront,
d->bcBack); s->bcBack);
printf("\tReynolds number: %.2f\n", d->re); printf("\tReynolds number: %.2f\n", s->re);
printf("\tGx Gy: %.2f %.2f %.2f\n", d->gx, d->gy, d->gz); printf("\tGx Gy: %.2f %.2f %.2f\n", s->gx, s->gy, s->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",
d->grid.xlength, s->grid.xlength,
d->grid.ylength, s->grid.ylength,
d->grid.zlength); s->grid.zlength);
printf("\tCells (x, y, z): %d, %d, %d\n", d->grid.imax, d->grid.jmax, d->grid.kmax); 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", d->grid.dx, d->grid.dy, d->grid.dz); printf("\tCell size (dx, dy, dz): %f, %f, %f\n", s->grid.dx, s->grid.dy, s->grid.dz);
printf("Timestep parameters:\n"); printf("Timestep parameters:\n");
printf("\tDefault stepsize: %.2f, Final time %.2f\n", d->dt, d->te); printf("\tDefault stepsize: %.2f, Final time %.2f\n", s->dt, s->te);
printf("\tdt bound: %.6f\n", d->dtBound); printf("\tdt bound: %.6f\n", s->dtBound);
printf("\tTau factor: %.2f\n", d->tau); printf("\tTau factor: %.2f\n", s->tau);
printf("Iterative parameters:\n"); printf("Iterative parameters:\n");
printf("\tepsilon (stopping tolerance) : %f\n", d->eps); printf("\tMax iterations: %d\n", s->itermax);
printf("\tgamma factor: %f\n", d->gamma); printf("\tepsilon (stopping tolerance) : %f\n", s->eps);
printf("\tomega (SOR relaxation): %f\n", d->omega); printf("\tgamma factor: %f\n", s->gamma);
printf("\tomega (SOR relaxation): %f\n", s->omega);
} }
void initDiscretization(Discretization* d, Parameter* p) void initSolver(Solver* s, Parameter* params)
{ {
d->problem = p->name; s->problem = params->name;
d->bcLeft = p->bcLeft; s->bcLeft = params->bcLeft;
d->bcRight = p->bcRight; s->bcRight = params->bcRight;
d->bcBottom = p->bcBottom; s->bcBottom = params->bcBottom;
d->bcTop = p->bcTop; s->bcTop = params->bcTop;
d->bcFront = p->bcFront; s->bcFront = params->bcFront;
d->bcBack = p->bcBack; s->bcBack = params->bcBack;
d->grid.imax = p->imax; s->grid.imax = params->imax;
d->grid.jmax = p->jmax; s->grid.jmax = params->jmax;
d->grid.kmax = p->kmax; s->grid.kmax = params->kmax;
d->grid.xlength = p->xlength; s->grid.xlength = params->xlength;
d->grid.ylength = p->ylength; s->grid.ylength = params->ylength;
d->grid.zlength = p->zlength; s->grid.zlength = params->zlength;
d->grid.dx = p->xlength / p->imax; s->grid.dx = params->xlength / params->imax;
d->grid.dy = p->ylength / p->jmax; s->grid.dy = params->ylength / params->jmax;
d->grid.dz = p->zlength / p->kmax; s->grid.dz = params->zlength / params->kmax;
d->eps = p->eps; s->eps = params->eps;
d->omega = p->omg; s->omega = params->omg;
d->re = p->re; s->itermax = params->itermax;
d->gx = p->gx; s->re = params->re;
d->gy = p->gy; s->gx = params->gx;
d->gz = p->gz; s->gy = params->gy;
d->dt = p->dt; s->gz = params->gz;
d->te = p->te; s->dt = params->dt;
d->tau = p->tau; s->te = params->te;
d->gamma = p->gamma; s->tau = params->tau;
s->gamma = params->gamma;
int imax = d->grid.imax; int imax = s->grid.imax;
int jmax = d->grid.jmax; int jmax = s->grid.jmax;
int kmax = d->grid.kmax; int kmax = s->grid.kmax;
size_t bytesize = (imax + 2) * (jmax + 2) * (kmax + 2) * sizeof(double); size_t bytesize = (imax + 2) * (jmax + 2) * (kmax + 2) * sizeof(double);
d->u = allocate(64, bytesize); s->u = allocate(64, bytesize);
d->v = allocate(64, bytesize); s->v = allocate(64, bytesize);
d->w = allocate(64, bytesize); s->w = allocate(64, bytesize);
d->p = allocate(64, bytesize); s->p = allocate(64, bytesize);
d->rhs = allocate(64, bytesize); s->rhs = allocate(64, bytesize);
d->f = allocate(64, bytesize); s->f = allocate(64, bytesize);
d->g = allocate(64, bytesize); s->g = allocate(64, bytesize);
d->h = allocate(64, bytesize); s->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++) {
d->u[i] = p->u_init; s->u[i] = params->u_init;
d->v[i] = p->v_init; s->v[i] = params->v_init;
d->w[i] = p->w_init; s->w[i] = params->w_init;
d->p[i] = p->p_init; s->p[i] = params->p_init;
d->rhs[i] = 0.0; s->rhs[i] = 0.0;
d->f[i] = 0.0; s->f[i] = 0.0;
d->g[i] = 0.0; s->g[i] = 0.0;
d->h[i] = 0.0; s->h[i] = 0.0;
} }
double dx = d->grid.dx; double dx = s->grid.dx;
double dy = d->grid.dy; double dy = s->grid.dy;
double dz = d->grid.dz; double dz = s->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);
d->dtBound = 0.5 * d->re * 1.0 / invSqrSum; s->dtBound = 0.5 * s->re * 1.0 / invSqrSum;
#ifdef VERBOSE #ifdef VERBOSE
printConfig(s); printConfig(s);
#endif /* VERBOSE */ #endif /* VERBOSE */
} }
void computeRHS(Discretization* d) void computeRHS(Solver* s)
{ {
int imax = d->grid.imax; int imax = s->grid.imax;
int jmax = d->grid.jmax; int jmax = s->grid.jmax;
int kmax = d->grid.kmax; int kmax = s->grid.kmax;
double idx = 1.0 / d->grid.dx; double idx = 1.0 / s->grid.dx;
double idy = 1.0 / d->grid.dy; double idy = 1.0 / s->grid.dy;
double idz = 1.0 / d->grid.dz; double idz = 1.0 / s->grid.dz;
double idt = 1.0 / d->dt; double idt = 1.0 / s->dt;
double* rhs = d->rhs; double* rhs = s->rhs;
double* f = d->f; double* f = s->f;
double* g = d->g; double* g = s->g;
double* h = d->h; double* h = s->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++) {
@ -137,9 +148,94 @@ void computeRHS(Discretization* d)
} }
} }
static double maxElement(Discretization* d, double* m) void solve(Solver* s)
{ {
int size = (d->grid.imax + 2) * (d->grid.jmax + 2) * (d->grid.kmax + 2); 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);
double maxval = DBL_MIN; double maxval = DBL_MIN;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@ -149,10 +245,10 @@ static double maxElement(Discretization* d, double* m)
return maxval; return maxval;
} }
void normalizePressure(Discretization* d) void normalizePressure(Solver* s)
{ {
int size = (d->grid.imax + 2) * (d->grid.jmax + 2) * (d->grid.kmax + 2); int size = (s->grid.imax + 2) * (s->grid.jmax + 2) * (s->grid.kmax + 2);
double* p = d->p; double* p = s->p;
double avgP = 0.0; double avgP = 0.0;
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {
@ -165,16 +261,16 @@ void normalizePressure(Discretization* d)
} }
} }
void computeTimestep(Discretization* d) void computeTimestep(Solver* s)
{ {
double dt = d->dtBound; double dt = s->dtBound;
double dx = d->grid.dx; double dx = s->grid.dx;
double dy = d->grid.dy; double dy = s->grid.dy;
double dz = d->grid.dz; double dz = s->grid.dz;
double umax = maxElement(d, d->u); double umax = maxElement(s, s->u);
double vmax = maxElement(d, d->v); double vmax = maxElement(s, s->v);
double wmax = maxElement(d, d->w); double wmax = maxElement(s, s->w);
if (umax > 0) { if (umax > 0) {
dt = (dt > dx / umax) ? dx / umax : dt; dt = (dt > dx / umax) ? dx / umax : dt;
@ -186,20 +282,20 @@ void computeTimestep(Discretization* d)
dt = (dt > dz / wmax) ? dz / wmax : dt; dt = (dt > dz / wmax) ? dz / wmax : dt;
} }
d->dt = dt * d->tau; s->dt = dt * s->tau;
} }
void setBoundaryConditions(Discretization* d) void setBoundaryConditions(Solver* s)
{ {
int imax = d->grid.imax; int imax = s->grid.imax;
int jmax = d->grid.jmax; int jmax = s->grid.jmax;
int kmax = d->grid.kmax; int kmax = s->grid.kmax;
double* u = d->u; double* u = s->u;
double* v = d->v; double* v = s->v;
double* w = d->w; double* w = s->w;
switch (d->bcTop) { switch (s->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++) {
@ -231,7 +327,7 @@ void setBoundaryConditions(Discretization* d)
break; break;
} }
switch (d->bcBottom) { switch (s->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++) {
@ -263,7 +359,7 @@ void setBoundaryConditions(Discretization* d)
break; break;
} }
switch (d->bcLeft) { switch (s->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++) {
@ -295,7 +391,7 @@ void setBoundaryConditions(Discretization* d)
break; break;
} }
switch (d->bcRight) { switch (s->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++) {
@ -327,7 +423,7 @@ void setBoundaryConditions(Discretization* d)
break; break;
} }
switch (d->bcFront) { switch (s->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++) {
@ -359,7 +455,7 @@ void setBoundaryConditions(Discretization* d)
break; break;
} }
switch (d->bcBack) { switch (s->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++) {
@ -392,23 +488,23 @@ void setBoundaryConditions(Discretization* d)
} }
} }
void setSpecialBoundaryCondition(Discretization* d) void setSpecialBoundaryCondition(Solver* s)
{ {
int imax = d->grid.imax; int imax = s->grid.imax;
int jmax = d->grid.jmax; int jmax = s->grid.jmax;
int kmax = d->grid.kmax; int kmax = s->grid.kmax;
double mDy = d->grid.dy; double mDy = s->grid.dy;
double* u = d->u; double* u = s->u;
if (strcmp(d->problem, "dcavity") == 0) { if (strcmp(s->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(d->problem, "canal") == 0) { } else if (strcmp(s->problem, "canal") == 0) {
double ylength = d->grid.ylength; double ylength = s->grid.ylength;
double y; double y;
for (int k = 1; k < kmax + 1; k++) { for (int k = 1; k < kmax + 1; k++) {
@ -420,29 +516,29 @@ void setSpecialBoundaryCondition(Discretization* d)
} }
} }
void computeFG(Discretization* d) void computeFG(Solver* s)
{ {
int imax = d->grid.imax; int imax = s->grid.imax;
int jmax = d->grid.jmax; int jmax = s->grid.jmax;
int kmax = d->grid.kmax; int kmax = s->grid.kmax;
double* u = d->u; double* u = s->u;
double* v = d->v; double* v = s->v;
double* w = d->w; double* w = s->w;
double* f = d->f; double* f = s->f;
double* g = d->g; double* g = s->g;
double* h = d->h; double* h = s->h;
double gx = d->gx; double gx = s->gx;
double gy = d->gy; double gy = s->gy;
double gz = d->gz; double gz = s->gz;
double dt = d->dt; double dt = s->dt;
double gamma = d->gamma; double gamma = s->gamma;
double inverseRe = 1.0 / d->re; double inverseRe = 1.0 / s->re;
double inverseDx = 1.0 / d->grid.dx; double inverseDx = 1.0 / s->grid.dx;
double inverseDy = 1.0 / d->grid.dy; double inverseDy = 1.0 / s->grid.dy;
double inverseDz = 1.0 / d->grid.dz; double inverseDz = 1.0 / s->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;
@ -609,23 +705,23 @@ void computeFG(Discretization* d)
} }
} }
void adaptUV(Discretization* d) void adaptUV(Solver* s)
{ {
int imax = d->grid.imax; int imax = s->grid.imax;
int jmax = d->grid.jmax; int jmax = s->grid.jmax;
int kmax = d->grid.kmax; int kmax = s->grid.kmax;
double* p = d->p; double* p = s->p;
double* u = d->u; double* u = s->u;
double* v = d->v; double* v = s->v;
double* w = d->w; double* w = s->w;
double* f = d->f; double* f = s->f;
double* g = d->g; double* g = s->g;
double* h = d->h; double* h = s->h;
double factorX = d->dt / d->grid.dx; double factorX = s->dt / s->grid.dx;
double factorY = d->dt / d->grid.dy; double factorY = s->dt / s->grid.dy;
double factorZ = d->dt / d->grid.dz; double factorZ = s->dt / s->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

@ -6,21 +6,38 @@
*/ */
#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, rho; double eps, omega;
double re, tau, gamma;
double gx, gy, gz;
/* time stepping */
int itermax; int itermax;
int levels; double dt, te;
double **r, **e; double dtBound;
char* problem;
int bcLeft, bcRight, bcBottom, bcTop, bcFront, bcBack;
} Solver; } Solver;
extern void initSolver(Solver*, Discretization*, Parameter*); extern void initSolver(Solver*, Parameter*);
extern void solve(Solver*, double*, double*); 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*);
#endif #endif

View File

@ -19,13 +19,4 @@
#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_

View File

@ -18,10 +18,9 @@ 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 = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c)) SRC = $(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)
@ -50,8 +49,6 @@ distclean: clean
@rm -f $(TARGET) @rm -f $(TARGET)
@rm -f *.dat @rm -f *.dat
@rm -f *.png @rm -f *.png
@rm -f ./vis_files/*.dat
@rm -f ./vis_files/*.gif
info: info:
$(info $(CFLAGS)) $(info $(CFLAGS))

View File

@ -26,13 +26,13 @@ p_init 1.0 # initial value for pressure
xlength 30.0 # domain size in x-direction xlength 30.0 # domain size in x-direction
ylength 4.0 # domain size in y-direction ylength 4.0 # domain size in y-direction
imax 256 # number of interior cells in x-direction imax 200 # number of interior cells in x-direction
jmax 64 # number of interior cells in y-direction jmax 50 # number of interior cells in y-direction
# Time Data: # Time Data:
# --------- # ---------
te 80.0 # final time te 60.0 # final time
dt 0.02 # time stepsize dt 0.02 # time stepsize
tau 0.5 # safety factor for time stepsize control (<0 constant delt) tau 0.5 # safety factor for time stepsize control (<0 constant delt)
@ -50,9 +50,9 @@ levels 5 # Multigrid levels
# ----------------------- # -----------------------
numberOfParticles 60 numberOfParticles 60
startTime 5.0 startTime 100
injectTimePeriod 4.0 injectTimePeriod 2.0
writeTimePeriod 1.0 writeTimePeriod 0.5
x1 1.0 x1 1.0
y1 0.0 y1 0.0

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

@ -26,8 +26,8 @@ p_init 0.0 # initial value for pressure
xlength 1.0 # domain size in x-direction xlength 1.0 # domain size in x-direction
ylength 1.0 # domain size in y-direction ylength 1.0 # domain size in y-direction
imax 128 # number of interior cells in x-direction imax 100 # number of interior cells in x-direction
jmax 128 # number of interior cells in y-direction jmax 100 # number of interior cells in y-direction
# Time Data: # Time Data:
# --------- # ---------
@ -50,7 +50,7 @@ levels 5 # Multigrid levels
# ----------------------- # -----------------------
numberOfParticles 200 numberOfParticles 200
startTime 2.0 startTime 100
injectTimePeriod 0.5 injectTimePeriod 0.5
writeTimePeriod 0.2 writeTimePeriod 0.2

View File

@ -2,18 +2,16 @@ CC = clang
GCC = cc GCC = cc
LINKER = $(CC) LINKER = $(CC)
ifeq ($(strip $(ENABLE_OPENMP)),true) ifeq ($(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
#CFLAGS = -Ofast -fnt-store=aggressive -std=c99 $(OPENMP) #AMD CLANG
LFLAGS = $(OPENMP) -lm LFLAGS = $(OPENMP) -lm
DEFINES = -D_GNU_SOURCE DEFINES = -D_GNU_SOURCE# -DDEBUG
INCLUDES = INCLUDES =

View File

@ -0,0 +1,61 @@
/*
* 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

@ -0,0 +1,14 @@
/*
* 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

@ -5,13 +5,10 @@
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include <errno.h> #include <errno.h>
#include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "allocate.h" void* allocate(int alignment, size_t bytesize)
void* allocate(size_t alignment, size_t bytesize)
{ {
int errorCode; int errorCode;
void* ptr; void* ptr;

View File

@ -8,6 +8,6 @@
#define __ALLOCATE_H_ #define __ALLOCATE_H_
#include <stdlib.h> #include <stdlib.h>
extern void* allocate(size_t alignment, size_t bytesize); extern void* allocate(int alignment, size_t bytesize);
#endif #endif

View File

@ -1,649 +0,0 @@
/*
* 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 "discretization.h"
#include "grid.h"
#include "parameter.h"
#include "util.h"
#define S(i, j) s[(j) * (imax + 2) + (i)]
static double distance(double i, double j, double iCenter, double jCenter)
{
return sqrt(pow(iCenter - i, 2) + pow(jCenter - j, 2) * 1.0);
}
void print(Discretization* d, double* grid)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
for (int j = 0; j < jmax + 2; j++) {
printf("%02d: ", j);
for (int i = 0; i < imax + 2; i++) {
printf("%3.2f ", grid[j * (imax + 2) + i]);
}
printf("\n");
}
fflush(stdout);
}
void printGrid(Discretization* d, int* grid)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
for (int j = 0; j < jmax + 2; j++) {
printf("%02d: ", j);
for (int i = 0; i < imax + 2; i++) {
printf("%2d ", grid[j * (imax + 2) + i]);
}
printf("\n");
}
fflush(stdout);
}
static void printConfig(Discretization* d)
{
printf("Parameters for #%s#\n", d->problem);
printf("Boundary conditions 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("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 d parameters:\n");
printf("\tgamma factor: %f\n", d->gamma);
}
void initDiscretization(Discretization* d, Parameter* p)
{
d->problem = p->name;
d->bcLeft = p->bcLeft;
d->bcRight = p->bcRight;
d->bcBottom = p->bcBottom;
d->bcTop = p->bcTop;
d->grid.imax = p->imax;
d->grid.jmax = p->jmax;
d->grid.xlength = p->xlength;
d->grid.ylength = p->ylength;
d->grid.dx = p->xlength / p->imax;
d->grid.dy = p->ylength / p->jmax;
d->re = p->re;
d->gx = p->gx;
d->gy = p->gy;
d->dt = p->dt;
d->te = p->te;
d->tau = p->tau;
d->gamma = p->gamma;
int imax = d->grid.imax;
int jmax = d->grid.jmax;
size_t size = (imax + 2) * (jmax + 2) * sizeof(double);
d->u = allocate(64, size);
d->v = allocate(64, size);
d->grid.s = allocate(64, size);
d->p = allocate(64, size);
d->rhs = allocate(64, size);
d->f = allocate(64, size);
d->g = allocate(64, size);
for (int i = 0; i < (imax + 2) * (jmax + 2); i++) {
d->u[i] = p->u_init;
d->v[i] = p->v_init;
d->p[i] = p->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;
double xCenter = 0, yCenter = 0, radius = 0;
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
int* s = d->grid.s;
switch (p->shape) {
case NOSHAPE:
break;
case RECT:
x1 = p->xCenter - p->xRectLength / 2;
x2 = p->xCenter + p->xRectLength / 2;
y1 = p->yCenter - p->yRectLength / 2;
y2 = p->yCenter + p->yRectLength / 2;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if ((x1 <= (i * dx)) && ((i * dx) <= x2) && (y1 <= (j * dy)) &&
((j * dy) <= y2)) {
S(i, j) = OBSTACLE;
}
}
}
break;
case CIRCLE:
xCenter = p->xCenter;
yCenter = p->yCenter;
radius = p->circleRadius;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if (distance((i * dx), (j * dy), xCenter, yCenter) <= radius) {
S(i, j) = OBSTACLE;
}
}
}
break;
}
if (p->shape != NOSHAPE) {
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if (S(i, j - 1) == FLUID && S(i, j + 1) == OBSTACLE &&
S(i, j) == OBSTACLE)
S(i, j) = BOTTOM; // TOP
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; // BOTTOM
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; // TOPLEFT
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; // TOPRIGHT
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; // BOTTOMLEFT
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; // BOTTOMRIGHT
}
}
}
#ifdef VERBOSE
printConfig(solver);
#endif
}
static double maxElement(Discretization* d, double* m)
{
int size = (d->grid.imax + 2) * (d->grid.jmax + 2);
double maxval = DBL_MIN;
for (int i = 0; i < size; i++) {
maxval = MAX(maxval, fabs(m[i]));
}
return maxval;
}
void computeRHS(Discretization* d)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
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;
int* s = d->grid.s;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
RHS(i, j) = idt *
((F(i, j) - F(i - 1, j)) * idx + (G(i, j) - G(i, j - 1)) * idy);
}
}
}
void normalizePressure(Discretization* d)
{
int size = (d->grid.imax + 2) * (d->grid.jmax + 2);
double* p = d->p;
double avgP = 0.0;
for (int i = 0; i < size; i++) {
avgP += p[i];
}
avgP /= size;
for (int i = 0; i < size; i++) {
p[i] = p[i] - avgP;
}
}
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 imax = d->grid.imax;
int jmax = d->grid.jmax;
double* u = d->u;
double* v = d->v;
// Left boundary
switch (d->bcLeft) {
case NOSLIP:
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = 0.0;
V(0, j) = -V(1, j);
}
break;
case SLIP:
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = 0.0;
V(0, j) = V(1, j);
}
break;
case OUTFLOW:
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = U(1, j);
V(0, j) = V(1, j);
}
break;
case PERIODIC:
break;
}
// Right boundary
switch (d->bcRight) {
case NOSLIP:
for (int j = 1; j < jmax + 1; j++) {
U(imax, j) = 0.0;
V(imax + 1, j) = -V(imax, j);
}
break;
case SLIP:
for (int j = 1; j < jmax + 1; j++) {
U(imax, j) = 0.0;
V(imax + 1, j) = V(imax, j);
}
break;
case OUTFLOW:
for (int j = 1; j < jmax + 1; j++) {
U(imax, j) = U(imax - 1, j);
V(imax + 1, j) = V(imax, j);
}
break;
case PERIODIC:
break;
}
// Bottom boundary
switch (d->bcBottom) {
case NOSLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, 0) = 0.0;
U(i, 0) = -U(i, 1);
}
break;
case SLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, 0) = 0.0;
U(i, 0) = U(i, 1);
}
break;
case OUTFLOW:
for (int i = 1; i < imax + 1; i++) {
U(i, 0) = U(i, 1);
V(i, 0) = V(i, 1);
}
break;
case PERIODIC:
break;
}
// Top boundary
switch (d->bcTop) {
case NOSLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, jmax) = 0.0;
U(i, jmax + 1) = -U(i, jmax);
}
break;
case SLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, jmax) = 0.0;
U(i, jmax + 1) = U(i, jmax);
}
break;
case OUTFLOW:
for (int i = 1; i < imax + 1; i++) {
U(i, jmax + 1) = U(i, jmax);
V(i, jmax) = V(i, jmax - 1);
}
break;
case PERIODIC:
break;
}
}
void setSpecialBoundaryCondition(Discretization* d)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
double mDy = d->grid.dy;
double* u = d->u;
int* s = d->grid.s;
if (strcmp(d->problem, "dcavity") == 0) {
for (int i = 1; i < imax; i++) {
U(i, jmax + 1) = 2.0 - U(i, jmax);
}
} else if (strcmp(d->problem, "canal") == 0) {
double ylength = d->grid.ylength;
double y;
for (int j = 1; j < jmax + 1; j++) {
y = mDy * (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 < jmax + 1; j++) {
if (S(0, j) == FLUID) U(0, j) = 1.0;
}
} else if (strcmp(d->problem, "karman") == 0) {
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = 1.0;
}
}
}
void setObjectBoundaryCondition(Discretization* d)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
double* u = d->u;
double* v = d->v;
int* s = d->grid.s;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
switch (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;
int* s = d->grid.s;
int imax = d->grid.imax;
int jmax = d->grid.jmax;
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;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 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 (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 --------------------------- */
for (int j = 1; j < jmax + 1; j++) {
F(0, j) = U(0, j);
F(imax, j) = U(imax, j);
}
/* ---------------------- boundary of G --------------------------- */
for (int i = 1; i < imax + 1; i++) {
G(i, 0) = V(i, 0);
G(i, jmax) = V(i, jmax);
}
}
void adaptUV(Discretization* d)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
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 < jmax + 1; j++) {
for (int i = 1; i < imax + 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)
{
int imax = d->grid.imax;
int jmax = d->grid.jmax;
double dx = d->grid.dx;
double dy = d->grid.dy;
double* p = d->p;
double* u = d->u;
double* v = d->v;
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 + 1; j++) {
y = (double)(j - 0.5) * dy;
for (int i = 1; i < imax + 1; i++) {
x = (double)(i - 0.5) * dx;
fprintf(fp, "%.2f %.2f %f\n", x, y, P(i, j));
}
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 + 1; j++) {
y = dy * (j - 0.5);
for (int i = 1; i < imax + 1; i++) {
x = dx * (i - 0.5);
double velU = (U(i, j) + U(i - 1, j)) / 2.0;
double velV = (V(i, j) + V(i, j - 1)) / 2.0;
double len = sqrt((velU * velU) + (velV * velV));
fprintf(fp, "%.2f %.2f %f %f %f\n", x, y, velU, velV, len);
}
}
fclose(fp);
}

View File

@ -1,44 +0,0 @@
/*
* 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;
double *u, *v;
/* parameters */
double rho;
double re, tau, gamma;
double gx, gy;
/* time stepping */
double dt, te;
double dtBound;
char* problem;
int bcLeft, bcRight, bcBottom, bcTop;
} 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 setObjectBoundaryCondition(Discretization*);
extern void computeFG(Discretization*);
extern void adaptUV(Discretization*);
extern void writeResult(Discretization*);
extern void print(Discretization*, double*);
extern void printGrid(Discretization*, int*);
#endif

View File

@ -7,32 +7,10 @@
#ifndef __GRID_H_ #ifndef __GRID_H_
#define __GRID_H_ #define __GRID_H_
#define S(i, j) s[(j) * (imax + 2) + (i)]
enum OBJECTBOUNDARY {
FLUID = 0,
TOP,
BOTTOM,
LEFT,
RIGHT,
TOPLEFT,
BOTTOMLEFT,
TOPRIGHT,
BOTTOMRIGHT,
OBSTACLE
};
enum SHAPE { NOSHAPE = 0, RECT, CIRCLE };
typedef struct { typedef struct {
double dx, dy; double dx, dy;
int imax, jmax; int imax, jmax;
double xlength, ylength; double xlength, ylength;
int* s;
} Grid; } Grid;
static inline int gridIsFluid(Grid* g, int i, int j)
{
return g->s[j * (g->imax + 2) + i] == FLUID;
}
#endif // __GRID_H_ #endif // __GRID_H_

View File

@ -8,7 +8,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include "discretization.h"
#include "parameter.h" #include "parameter.h"
#include "particletracing.h" #include "particletracing.h"
#include "progress.h" #include "progress.h"
@ -17,51 +16,52 @@
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
double timeStart, timeStop; double S, E;
Parameter p; Parameter params;
Discretization d; Solver solver;
Solver s;
ParticleTracer particletracer; ParticleTracer particletracer;
initParameter(&params);
initParameter(&p);
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <configFile>\n", argv[0]); printf("Usage: %s <configFile>\n", argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
readParameter(&p, argv[1]); readParameter(&params, argv[1]);
printParameter(&p); printParameter(&params);
initDiscretization(&d, &p); initSolver(&solver, &params);
initSolver(&s, &d, &p); printf("initsolver done\n");
initParticleTracer(&particletracer, &d.grid, &p);
initParticleTracer(&particletracer, &solver.grid, &params);
printParticleTracerParameters(&particletracer); printParticleTracerParameters(&particletracer);
#ifndef VERBOSE #ifndef VERBOSE
initProgress(d.te); initProgress(solver.te);
#endif #endif
double tau = d.tau; double tau = solver.tau;
double te = d.te; double te = solver.te;
double t = 0.0; double t = 0.0;
int nt = 0; int nt = 0;
timeStart = getTimeStamp(); S = getTimeStamp();
while (t <= te) { while (t <= te) {
if (tau > 0.0) computeTimestep(&d); if (tau > 0.0) computeTimestep(&solver);
setBoundaryConditions(&d); setBoundaryConditions(&solver);
setSpecialBoundaryCondition(&d); setSpecialBoundaryCondition(&solver);
setObjectBoundaryCondition(&d); setObjectBoundaryCondition(&solver);
computeFG(&solver);
computeRHS(&solver);
if (nt % 100 == 0) normalizePressure(&solver);
multiGrid(&solver);
computeFG(&d); adaptUV(&solver);
computeRHS(&d);
if (nt % 100 == 0) normalizePressure(&d);
solve(&s, d.p, d.rhs);
adaptUV(&d);
trace(&particletracer, d.u, d.v, d.dt, t);
t += d.dt; /* Added function for particle tracing. Will inject and advance particles as per
* timePeriod */
trace(&particletracer, solver.u, solver.v, solver.s, t);
t += solver.dt;
nt++; nt++;
#ifdef VERBOSE #ifdef VERBOSE
@ -70,12 +70,14 @@ int main(int argc, char** argv)
printProgress(t); printProgress(t);
#endif #endif
} }
printf("Total particles : %d\n", particletracer.totalParticles);
timeStop = getTimeStamp(); E = getTimeStamp();
stopProgress(); stopProgress();
freeParticles(&particletracer); freeParticles(&particletracer);
printf("Solution took %.2fs\n", timeStop - timeStart);
writeResult(&d); printf("Solution took %.2fs\n", E - S);
writeResult(&solver);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }

View File

@ -8,13 +8,14 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "grid.h"
#include "vtkWriter.h" #include "vtkWriter.h"
#define U(i, j) u[(j) * (imax + 2) + (i)] #define U(i, j) u[(j) * (imax + 2) + (i)]
#define V(i, j) v[(j) * (imax + 2) + (i)] #define V(i, j) v[(j) * (imax + 2) + (i)]
#define S(i, j) s[(j) * (imax + 2) + (i)] #define S(i, j) s[(j) * (imax + 2) + (i)]
static int ts = 0;
void printParticles(ParticleTracer* p) void printParticles(ParticleTracer* p)
{ {
for (int i = 0; i < p->totalParticles; ++i) { for (int i = 0; i < p->totalParticles; ++i) {
@ -24,28 +25,29 @@ void printParticles(ParticleTracer* p)
p->particlePool[i].flag); p->particlePool[i].flag);
} }
} }
void injectParticles(ParticleTracer* p)
static void injectParticles(ParticleTracer* p)
{ {
if (p->totalParticles + p->numParticlesInLine > p->numAllocatedParticles) { for (int i = 0; i < p->numberOfParticles; ++i) {
return;
}
for (int i = 0; i < p->numParticlesInLine; ++i) {
p->particlePool[p->pointer].x = p->linSpaceLine[i].x; p->particlePool[p->pointer].x = p->linSpaceLine[i].x;
p->particlePool[p->pointer].y = p->linSpaceLine[i].y; p->particlePool[p->pointer].y = p->linSpaceLine[i].y;
p->particlePool[p->pointer].flag = true; p->particlePool[p->pointer].flag = true;
p->pointer++; ++(p->pointer);
p->totalParticles++; ++(p->totalParticles);
} }
} }
static void advanceParticles( void advanceParticles(ParticleTracer* p,
ParticleTracer* p, double* restrict u, double* restrict v, double dt) double* restrict u,
double* restrict v,
int* restrict s,
double time)
{ {
int imax = p->grid->imax; int imax = p->grid->imax;
int jmax = p->grid->jmax; int jmax = p->grid->jmax;
double dx = p->grid->dx;
double dy = p->grid->dy; double dx = p->grid->dx;
double dy = p->grid->dy;
double xlength = p->grid->xlength; double xlength = p->grid->xlength;
double ylength = p->grid->ylength; double ylength = p->grid->ylength;
@ -62,14 +64,14 @@ static void advanceParticles(
double x2 = (double)iCoord * dx; double x2 = (double)iCoord * dx;
double y2 = ((double)jCoord - 0.5) * dy; double y2 = ((double)jCoord - 0.5) * dy;
double intU = (1.0 / (dx * dy)) * double u_n = (1.0 / (dx * dy)) *
((x2 - x) * (y2 - y) * U(iCoord - 1, jCoord - 1) + ((x2 - x) * (y2 - y) * U(iCoord - 1, jCoord - 1) +
(x - x1) * (y2 - y) * U(iCoord, jCoord - 1) + (x - x1) * (y2 - y) * U(iCoord, jCoord - 1) +
(x2 - x) * (y - y1) * U(iCoord - 1, jCoord) + (x2 - x) * (y - y1) * U(iCoord - 1, jCoord) +
(x - x1) * (y - y1) * U(iCoord, jCoord)); (x - x1) * (y - y1) * U(iCoord, jCoord));
double newX = x + dt * intU; double new_x = x + p->dt * u_n;
p->particlePool[i].x = newX; p->particlePool[i].x = new_x;
iCoord = (int)((x + 0.5 * dx) / dx) + 1; iCoord = (int)((x + 0.5 * dx) / dx) + 1;
jCoord = (int)(y / dy) + 1; jCoord = (int)(y / dy) + 1;
@ -79,58 +81,50 @@ static void advanceParticles(
x2 = ((double)iCoord - 0.5) * dx; x2 = ((double)iCoord - 0.5) * dx;
y2 = (double)jCoord * dy; y2 = (double)jCoord * dy;
double intV = (1.0 / (dx * dy)) * double v_n = (1.0 / (dx * dy)) *
((x2 - x) * (y2 - y) * V(iCoord - 1, jCoord - 1) + ((x2 - x) * (y2 - y) * V(iCoord - 1, jCoord - 1) +
(x - x1) * (y2 - y) * V(iCoord, jCoord - 1) + (x - x1) * (y2 - y) * V(iCoord, jCoord - 1) +
(x2 - x) * (y - y1) * V(iCoord - 1, jCoord) + (x2 - x) * (y - y1) * V(iCoord - 1, jCoord) +
(x - x1) * (y - y1) * V(iCoord, jCoord)); (x - x1) * (y - y1) * V(iCoord, jCoord));
double newY = y + dt * intV; double new_y = y + p->dt * v_n;
p->particlePool[i].y = newY; p->particlePool[i].y = new_y;
if (((newX < 0.0) || (newX > xlength) || (newY < 0.0) || (newY > ylength))) { // printf("\tOld X : %.2f, New X : %.2f, iCoord : %d\n\tOld Y : %.2f, New Y :
// %.2f, jCoord : %d\n\n", x, new_x, iCoord, y, new_y, jCoord);
// printf("\tU(iCoord - 1, jCoord - 1) : %.2f, U(iCoord, jCoord - 1) : %.2f,
// U(iCoord - 1, jCoord) : %.2f, U(iCoord, jCoord) : %.2f\n", U(iCoord - 1,
// jCoord - 1), U(iCoord, jCoord - 1), U(iCoord - 1, jCoord), U(iCoord,
// jCoord)); printf("\tV(iCoord - 1, jCoord - 1) : %.2f, V(iCoord, jCoord - 1)
// : %.2f, V(iCoord - 1, jCoord) : %.2f, V(iCoord, jCoord) : %.2f\n\n",
// V(iCoord - 1, jCoord - 1), V(iCoord, jCoord - 1), V(iCoord - 1, jCoord),
// V(iCoord, jCoord)); printf("\t U N : %.2f, V N : %.2f\n\n", u_n, v_n);
// printf("\t j-1 * (imax + 2) + i-1 = %d with element from U : %.2f", (jCoord
// - 1) * (200 + 2) + (iCoord - 1), u[(jCoord - 1) * (imax + 2) + (iCoord -
// 1)]); printf("\nimax : %d, jmax : %d\n", imax, jmax);
if (((new_x < 0.0) || (new_x > xlength) || (new_y < 0.0) ||
(new_y > ylength))) {
p->particlePool[i].flag = false; p->particlePool[i].flag = false;
p->removedParticles++;
} }
int i_new = new_x / dx, j_new = new_y / dy;
int newI = newX / dx, newJ = newY / dy; if (S(i_new, j_new) != NONE) {
if (!gridIsFluid(p->grid, newI, newJ)) {
p->particlePool[i].flag = false; p->particlePool[i].flag = false;
p->removedParticles++;
printf("Forbidden movement of particle into obstacle!\n");
} }
} }
} }
} }
static void compress(ParticleTracer* p) void freeParticles(ParticleTracer* p)
{ {
Particle* memPool = p->particlePool; if (p->particlePool != NULL) {
Particle tempPool[p->totalParticles]; free(p->particlePool);
int totalParticles = 0; free(p->linSpaceLine);
printf("Performing compression ...");
for (int i = 0; i < p->totalParticles; i++) {
if (memPool[i].flag == 1) {
tempPool[totalParticles].x = memPool[i].x;
tempPool[totalParticles].y = memPool[i].y;
tempPool[totalParticles].flag = memPool[i].flag;
totalParticles++;
}
} }
printf(" remove %d particles\n", p->totalParticles - totalParticles);
p->totalParticles = totalParticles;
p->removedParticles = 0;
p->pointer = totalParticles + 1;
memcpy(p->particlePool, tempPool, totalParticles * sizeof(Particle));
} }
void writeParticles(ParticleTracer* p) void writeParticles(ParticleTracer* p)
{ {
static int ts = 0;
VtkOptions opts = { .particletracer = p }; VtkOptions opts = { .particletracer = p };
char filename[50]; char filename[50];
@ -160,43 +154,44 @@ void writeParticles(ParticleTracer* p)
++ts; ++ts;
} }
void initParticleTracer(ParticleTracer* pt, Grid* g, Parameter* p) void initParticleTracer(ParticleTracer* p, Grid* grid, Parameter* params)
{ {
pt->numParticlesInLine = p->numberOfParticles; p->numberOfParticles = params->numberOfParticles;
pt->startTime = p->startTime; p->startTime = params->startTime;
pt->injectTimePeriod = p->injectTimePeriod; p->injectTimePeriod = params->injectTimePeriod;
pt->writeTimePeriod = p->writeTimePeriod; p->writeTimePeriod = params->writeTimePeriod;
pt->grid = g;
pt->x1 = p->x1; p->dt = params->dt;
pt->y1 = p->y1; p->grid = grid;
pt->x2 = p->x2;
pt->y2 = p->y2;
pt->lastInjectTime = p->startTime; p->x1 = params->x1;
pt->lastWriteTime = p->startTime; p->y1 = params->y1;
p->x2 = params->x2;
p->y2 = params->y2;
pt->pointer = 0; p->lastInjectTime = params->startTime;
pt->removedParticles = 0; p->lastUpdateTime = params->startTime;
pt->totalParticles = 0; p->lastWriteTime = params->startTime;
if (p->te > p->startTime) { p->pointer = 0;
pt->numAllocatedParticles = ((p->te - p->startTime) / p->injectTimePeriod) * p->totalParticles = 0;
p->numberOfParticles;
pt->numAllocatedParticles += (2 * p->numberOfParticles);
pt->particlePool = malloc(sizeof(Particle) * pt->numAllocatedParticles); if (params->te > params->startTime) {
pt->linSpaceLine = malloc(sizeof(Particle) * pt->numParticlesInLine); p->estimatedNumParticles = ((params->te - params->startTime) + 2) *
params->numberOfParticles;
for (int i = 0; i < pt->numParticlesInLine; ++i) { p->particlePool = malloc(sizeof(Particle) * p->estimatedNumParticles);
double spacing = (double)i / (double)(pt->numParticlesInLine - 1); p->linSpaceLine = malloc(sizeof(Particle) * p->numberOfParticles);
pt->linSpaceLine[i].x = spacing * pt->x1 + (1.0 - spacing) * pt->x2;
pt->linSpaceLine[i].y = spacing * pt->y1 + (1.0 - spacing) * pt->y2; for (int i = 0; i < p->numberOfParticles; ++i) {
pt->linSpaceLine[i].flag = true; double spacing = (double)i / (double)(p->numberOfParticles - 1);
p->linSpaceLine[i].x = spacing * p->x1 + (1.0 - spacing) * p->x2;
p->linSpaceLine[i].y = spacing * p->y1 + (1.0 - spacing) * p->y2;
p->linSpaceLine[i].flag = true;
} }
} else { } else {
pt->particlePool = NULL; p->particlePool = NULL;
pt->linSpaceLine = NULL; p->linSpaceLine = NULL;
} }
} }
@ -204,7 +199,7 @@ void printParticleTracerParameters(ParticleTracer* p)
{ {
printf("Particle Tracing data:\n"); printf("Particle Tracing data:\n");
printf("\tNumber of particles : %d being injected for every period of %.2f\n", printf("\tNumber of particles : %d being injected for every period of %.2f\n",
p->numParticlesInLine, p->numberOfParticles,
p->injectTimePeriod); p->injectTimePeriod);
printf("\tstartTime : %.2f\n", p->startTime); printf("\tstartTime : %.2f\n", p->startTime);
printf("\t(Line along which the particles are to be injected) \n\tx1 : %.2f, y1 : " printf("\t(Line along which the particles are to be injected) \n\tx1 : %.2f, y1 : "
@ -214,33 +209,43 @@ void printParticleTracerParameters(ParticleTracer* p)
p->x2, p->x2,
p->y2); p->y2);
printf("\tPointer : %d, TotalParticles : %d\n", p->pointer, p->totalParticles); printf("\tPointer : %d, TotalParticles : %d\n", p->pointer, p->totalParticles);
printf("\tdt : %.2f, dx : %.2f, dy : %.2f\n", p->dt, p->grid->dx, p->grid->dy);
} }
void trace(ParticleTracer* p, double* u, double* v, double dt, double time) void trace(ParticleTracer* p, double* u, double* v, int* s, double time)
{ {
if (time >= p->startTime) { if (time >= p->startTime) {
// printParticles(particletracer);
if ((time - p->lastInjectTime) >= p->injectTimePeriod) { if ((time - p->lastInjectTime) >= p->injectTimePeriod) {
injectParticles(p); injectParticles(p);
p->lastInjectTime = time; p->lastInjectTime = time;
} }
if ((time - p->lastWriteTime) >= p->writeTimePeriod) { if ((time - p->lastWriteTime) >= p->writeTimePeriod) {
writeParticles(p); writeParticles(p);
p->lastWriteTime = time; p->lastWriteTime = time;
} }
advanceParticles(p, u, v, s, time);
compress(p);
p->lastUpdateTime = time;
}
}
advanceParticles(p, u, v, dt); void compress(ParticleTracer* p)
{
Particle* memPool = p->particlePool;
Particle tempPool[p->totalParticles];
int totalParticles = 0;
if (p->removedParticles > (p->totalParticles * 0.2)) { for (int i = 0; i < p->totalParticles; ++i) {
compress(p); if (memPool[i].flag == 1) {
tempPool[totalParticles].x = memPool[i].x;
tempPool[totalParticles].y = memPool[i].y;
tempPool[totalParticles].flag = memPool[i].flag;
++totalParticles;
} }
} }
}
void freeParticles(ParticleTracer* p) p->totalParticles = totalParticles;
{ p->pointer = totalParticles + 1;
if (p->particlePool != NULL) { memcpy(p->particlePool, tempPool, totalParticles * sizeof(Particle));
free(p->particlePool);
free(p->linSpaceLine);
}
} }

View File

@ -19,12 +19,13 @@ typedef struct {
} Particle; } Particle;
typedef struct { typedef struct {
int numParticlesInLine, removedParticles, totalParticles; int numberOfParticles, totalParticles;
double startTime, injectTimePeriod, writeTimePeriod; double startTime, injectTimePeriod, writeTimePeriod;
double lastInjectTime, lastUpdateTime, lastWriteTime; double lastInjectTime, lastUpdateTime, lastWriteTime;
int numAllocatedParticles; int estimatedNumParticles;
double dt;
Particle* linSpaceLine; Particle* linSpaceLine;
Particle* particlePool; Particle* particlePool;
@ -34,8 +35,12 @@ typedef struct {
} ParticleTracer; } ParticleTracer;
extern void initParticleTracer(ParticleTracer*, Grid*, Parameter*); extern void initParticleTracer(ParticleTracer*, Grid*, Parameter*);
extern void injectParticles(ParticleTracer*);
extern void advanceParticles(ParticleTracer*, double*, double*, int*, double);
extern void freeParticles(ParticleTracer*); extern void freeParticles(ParticleTracer*);
extern void writeParticles(ParticleTracer*); extern void writeParticles(ParticleTracer*);
extern void printParticleTracerParameters(ParticleTracer*); extern void printParticleTracerParameters(ParticleTracer*);
extern void trace(ParticleTracer*, double*, double*, double, double); extern void printParticles(ParticleTracer*);
extern void trace(ParticleTracer*, double*, double*, int*, double);
extern void compress(ParticleTracer*);
#endif #endif

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(void); extern void stopProgress();
#endif #endif

View File

@ -1,186 +0,0 @@
/*
* 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) * (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)
{
double* r = s->r[level + 1];
double* old = s->r[level];
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 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, int imax, int jmax)
{
double* old = s->r[level + 1];
double* e = s->r[level];
for (int j = 2; j < jmax + 1; j += 2) {
for (int i = 2; i < imax + 1; i += 2) {
E(i, j) = OLD(i / 2, j / 2);
}
}
}
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);
}
}
}
static void setBoundaryCondition(double* p, int imax, int jmax)
{
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);
}
}
static double 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) {
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);
P(i, j) -= (factor * R(i, j));
res += (R(i, j) * R(i, j));
}
isw = 3 - isw;
}
jsw = 3 - jsw;
}
res = res / (double)(imax * jmax);
return res;
}
static double multiGrid(Solver* s, double* p, double* rhs, int level, int imax, int jmax)
{
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);
}
return res;
}
// pre-smoothing TODO: Make smoothing steps configurable?
for (int i = 0; i < 5; i++) {
smooth(s, p, rhs, level, imax, jmax);
if (level == FINEST_LEVEL) setBoundaryCondition(p, imax, jmax);
}
// restrict
restrictMG(s, level, imax, jmax);
// 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);
// 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 < 5; i++) {
res = 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->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)
{
double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax);
#ifdef VERBOSE
printf("Residuum: %.6f\n", res);
#endif
}

View File

@ -1,128 +0,0 @@
/*
* 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 solveSOR(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;
while ((res >= epssq) && (it < itermax)) {
res = 0.0;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
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);
}
}
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
}
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
}

View File

@ -0,0 +1,900 @@
/*
* 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 "parameter.h"
#include "solver.h"
#include "util.h"
#define P(i, j) p[(j) * (imax + 2) + (i)]
#define F(i, j) f[(j) * (imax + 2) + (i)]
#define G(i, j) g[(j) * (imax + 2) + (i)]
#define U(i, j) u[(j) * (imax + 2) + (i)]
#define V(i, j) v[(j) * (imax + 2) + (i)]
#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 oldR(i, j) oldr[(j) * (imax + 2) + (i)]
#define oldE(i, j) olde[(j) * (imax + 2) + (i)]
#define RHS(i, j) rhs[(j) * (imax + 2) + (i)]
static double distance(double i, double j, double iCenter, double jCenter)
{
return sqrt(pow(iCenter - i, 2) + pow(jCenter - j, 2) * 1.0);
}
void print(Solver* solver, double* grid)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
for (int j = 0; j < jmax + 2; j++) {
printf("%02d: ", j);
for (int i = 0; i < imax + 2; i++) {
printf("%3.2f ", grid[j * (imax + 2) + i]);
}
printf("\n");
}
fflush(stdout);
}
void printGrid(Solver* solver, int* grid)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
for (int j = 0; j < jmax + 2; j++) {
printf("%02d: ", j);
for (int i = 0; i < imax + 2; i++) {
printf("%2d ", grid[j * (imax + 2) + i]);
}
printf("\n");
}
fflush(stdout);
}
static void printConfig(Solver* solver)
{
printf("Parameters for #%s#\n", solver->problem);
printf("Boundary conditions Left:%d Right:%d Bottom:%d Top:%d\n",
solver->bcLeft,
solver->bcRight,
solver->bcBottom,
solver->bcTop);
printf("\tReynolds number: %.2f\n", solver->re);
printf("\tGx Gy: %.2f %.2f\n", solver->gx, solver->gy);
printf("Geometry data:\n");
printf("\tDomain box size (x, y): %.2f, %.2f\n",
solver->grid.xlength,
solver->grid.ylength);
printf("\tCells (x, y): %d, %d\n", solver->grid.imax, solver->grid.jmax);
printf("Timestep parameters:\n");
printf("\tDefault stepsize: %.2f, Final time %.2f\n", solver->dt, solver->te);
printf("\tdt bound: %.6f\n", solver->dtBound);
printf("\tTau factor: %.2f\n", solver->tau);
printf("Iterative solver parameters:\n");
printf("\tMax iterations: %d\n", solver->itermax);
printf("\tepsilon (stopping tolerance) : %f\n", solver->eps);
printf("\tgamma factor: %f\n", solver->gamma);
printf("\tomega (SOR relaxation): %f\n", solver->omega);
}
void initSolver(Solver* solver, Parameter* params)
{
solver->problem = params->name;
solver->bcLeft = params->bcLeft;
solver->bcRight = params->bcRight;
solver->bcBottom = params->bcBottom;
solver->bcTop = params->bcTop;
solver->grid.imax = params->imax;
solver->grid.jmax = params->jmax;
solver->grid.xlength = params->xlength;
solver->grid.ylength = params->ylength;
solver->grid.dx = params->xlength / params->imax;
solver->grid.dy = params->ylength / params->jmax;
solver->eps = params->eps;
solver->omega = params->omg;
solver->itermax = params->itermax;
solver->re = params->re;
solver->gx = params->gx;
solver->gy = params->gy;
solver->dt = params->dt;
solver->te = params->te;
solver->tau = params->tau;
solver->gamma = params->gamma;
solver->rho = params->rho;
solver->levels = params->levels;
solver->currentlevel = 0;
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
int levels = solver->levels;
size_t size_level = levels * (imax + 2) * (jmax + 2) * sizeof(double);
size_t size = (imax + 2) * (jmax + 2) * sizeof(double);
solver->u = allocate(64, size);
solver->v = allocate(64, size);
solver->s = allocate(64, size);
solver->p = allocate(64, size);
solver->rhs = allocate(64, size);
solver->f = allocate(64, size);
solver->g = allocate(64, size);
solver->r = malloc(levels * sizeof(double*));
solver->e = malloc(levels * sizeof(double*));
for (int j = 0; j < levels; ++j) {
solver->r[j] = allocate(64, size);
solver->e[j] = allocate(64, size);
}
for (int i = 0; i < (imax + 2) * (jmax + 2); i++) {
solver->u[i] = params->u_init;
solver->v[i] = params->v_init;
solver->p[i] = params->p_init;
solver->rhs[i] = 0.0;
solver->f[i] = 0.0;
solver->g[i] = 0.0;
solver->s[i] = NONE;
for (int j = 0; j < levels; ++j) {
solver->r[j][i] = 0.0;
solver->e[j][i] = 0.0;
}
}
double dx = solver->grid.dx;
double dy = solver->grid.dy;
double invSqrSum = 1.0 / (dx * dx) + 1.0 / (dy * dy);
solver->dtBound = 0.5 * solver->re * 1.0 / invSqrSum;
double xCenter = 0, yCenter = 0, radius = 0;
double x1 = 0, x2 = 0, y1 = 0, y2 = 0;
int* s = solver->s;
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;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if ((x1 <= (i * dx)) && ((i * dx) <= x2) && (y1 <= (j * dy)) &&
((j * dy) <= y2)) {
S(i, j) = LOCAL;
}
}
}
break;
case CIRCLE:
xCenter = params->xCenter;
yCenter = params->yCenter;
radius = params->circleRadius;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if (distance((i * dx), (j * dy), xCenter, yCenter) <= radius) {
S(i, j) = LOCAL;
}
}
}
break;
default:
break;
}
if (params->shape != NOSHAPE) {
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if (S(i, j - 1) == NONE && S(i, j + 1) == LOCAL && S(i, j) == LOCAL)
S(i, j) = BOTTOM; // TOP
if (S(i - 1, j) == NONE && S(i + 1, j) == LOCAL && S(i, j) == LOCAL)
S(i, j) = LEFT; // LEFT
if (S(i + 1, j) == NONE && S(i - 1, j) == LOCAL && S(i, j) == LOCAL)
S(i, j) = RIGHT; // RIGHT
if (S(i, j + 1) == NONE && S(i, j - 1) == LOCAL && S(i, j) == LOCAL)
S(i, j) = TOP; // BOTTOM
if (S(i - 1, j - 1) == NONE && S(i, j - 1) == NONE &&
S(i - 1, j) == NONE && S(i + 1, j + 1) == LOCAL &&
(S(i, j) == LOCAL || S(i, j) == LEFT || S(i, j) == BOTTOM))
S(i, j) = BOTTOMLEFT; // TOPLEFT
if (S(i + 1, j - 1) == NONE && S(i, j - 1) == NONE &&
S(i + 1, j) == NONE && S(i - 1, j + 1) == LOCAL &&
(S(i, j) == LOCAL || S(i, j) == RIGHT || S(i, j) == BOTTOM))
S(i, j) = BOTTOMRIGHT; // TOPRIGHT
if (S(i - 1, j + 1) == NONE && S(i - 1, j) == NONE &&
S(i, j + 1) == NONE && S(i + 1, j - 1) == LOCAL &&
(S(i, j) == LOCAL || S(i, j) == LEFT || S(i, j) == TOP))
S(i, j) = TOPLEFT; // BOTTOMLEFT
if (S(i + 1, j + 1) == NONE && S(i + 1, j) == NONE &&
S(i, j + 1) == NONE && S(i - 1, j - 1) == LOCAL &&
(S(i, j) == LOCAL || S(i, j) == RIGHT || S(i, j) == TOP))
S(i, j) = TOPRIGHT; // BOTTOMRIGHT
}
}
}
#ifdef VERBOSE
printConfig(solver);
#endif
}
static double maxElement(Solver* solver, double* m)
{
int size = (solver->grid.imax + 2) * (solver->grid.jmax + 2);
double maxval = DBL_MIN;
for (int i = 0; i < size; i++) {
maxval = MAX(maxval, fabs(m[i]));
}
return maxval;
}
void computeRHS(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double idx = 1.0 / solver->grid.dx;
double idy = 1.0 / solver->grid.dy;
double idt = 1.0 / solver->dt;
double* rhs = solver->rhs;
double* f = solver->f;
double* g = solver->g;
int* s = solver->s;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
RHS(i, j) = idt *
((F(i, j) - F(i - 1, j)) * idx + (G(i, j) - G(i, j - 1)) * idy);
}
}
}
void normalizePressure(Solver* solver)
{
int size = (solver->grid.imax + 2) * (solver->grid.jmax + 2);
double* p = solver->p;
double avgP = 0.0;
for (int i = 0; i < size; i++) {
avgP += p[i];
}
avgP /= size;
for (int i = 0; i < size; i++) {
p[i] = p[i] - avgP;
}
}
void computeTimestep(Solver* solver)
{
double dt = solver->dtBound;
double dx = solver->grid.dx;
double dy = solver->grid.dy;
double umax = maxElement(solver, solver->u);
double vmax = maxElement(solver, solver->v);
if (umax > 0) {
dt = (dt > dx / umax) ? dx / umax : dt;
}
if (vmax > 0) {
dt = (dt > dy / vmax) ? dy / vmax : dt;
}
solver->dt = dt * solver->tau;
}
void setBoundaryConditions(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double* u = solver->u;
double* v = solver->v;
// Left boundary
switch (solver->bcLeft) {
case NOSLIP:
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = 0.0;
V(0, j) = -V(1, j);
}
break;
case SLIP:
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = 0.0;
V(0, j) = V(1, j);
}
break;
case OUTFLOW:
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = U(1, j);
V(0, j) = V(1, j);
}
break;
case PERIODIC:
break;
}
// Right boundary
switch (solver->bcRight) {
case NOSLIP:
for (int j = 1; j < jmax + 1; j++) {
U(imax, j) = 0.0;
V(imax + 1, j) = -V(imax, j);
}
break;
case SLIP:
for (int j = 1; j < jmax + 1; j++) {
U(imax, j) = 0.0;
V(imax + 1, j) = V(imax, j);
}
break;
case OUTFLOW:
for (int j = 1; j < jmax + 1; j++) {
U(imax, j) = U(imax - 1, j);
V(imax + 1, j) = V(imax, j);
}
break;
case PERIODIC:
break;
}
// Bottom boundary
switch (solver->bcBottom) {
case NOSLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, 0) = 0.0;
U(i, 0) = -U(i, 1);
}
break;
case SLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, 0) = 0.0;
U(i, 0) = U(i, 1);
}
break;
case OUTFLOW:
for (int i = 1; i < imax + 1; i++) {
U(i, 0) = U(i, 1);
V(i, 0) = V(i, 1);
}
break;
case PERIODIC:
break;
}
// Top boundary
switch (solver->bcTop) {
case NOSLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, jmax) = 0.0;
U(i, jmax + 1) = -U(i, jmax);
}
break;
case SLIP:
for (int i = 1; i < imax + 1; i++) {
V(i, jmax) = 0.0;
U(i, jmax + 1) = U(i, jmax);
}
break;
case OUTFLOW:
for (int i = 1; i < imax + 1; i++) {
U(i, jmax + 1) = U(i, jmax);
V(i, jmax) = V(i, jmax - 1);
}
break;
case PERIODIC:
break;
}
}
void setSpecialBoundaryCondition(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double mDy = solver->grid.dy;
double* u = solver->u;
int* s = solver->s;
if (strcmp(solver->problem, "dcavity") == 0) {
for (int i = 1; i < imax; i++) {
U(i, jmax + 1) = 2.0 - U(i, jmax);
}
} else if (strcmp(solver->problem, "canal") == 0) {
double ylength = solver->grid.ylength;
double y;
for (int j = 1; j < jmax + 1; j++) {
y = mDy * (j - 0.5);
U(0, j) = y * (ylength - y) * 4.0 / (ylength * ylength);
}
} else if (strcmp(solver->problem, "backstep") == 0) {
for (int j = 1; j < jmax + 1; j++) {
if (S(0, j) == NONE) U(0, j) = 1.0;
}
} else if (strcmp(solver->problem, "karman") == 0) {
for (int j = 1; j < jmax + 1; j++) {
U(0, j) = 1.0;
}
}
}
void setObjectBoundaryCondition(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double* u = solver->u;
double* v = solver->v;
int* s = solver->s;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
switch (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(Solver* solver)
{
double* u = solver->u;
double* v = solver->v;
double* f = solver->f;
double* g = solver->g;
int* s = solver->s;
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double gx = solver->gx;
double gy = solver->gy;
double gamma = solver->gamma;
double dt = solver->dt;
double inverseRe = 1.0 / solver->re;
double inverseDx = 1.0 / solver->grid.dx;
double inverseDy = 1.0 / solver->grid.dy;
double du2dx, dv2dy, duvdx, duvdy;
double du2dx2, du2dy2, dv2dx2, dv2dy2;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; i++) {
if (S(i, j) == NONE) {
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 (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 --------------------------- */
for (int j = 1; j < jmax + 1; j++) {
F(0, j) = U(0, j);
F(imax, j) = U(imax, j);
}
/* ---------------------- boundary of G --------------------------- */
for (int i = 1; i < imax + 1; i++) {
G(i, 0) = V(i, 0);
G(i, jmax) = V(i, jmax);
}
}
void adaptUV(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double* p = solver->p;
double* u = solver->u;
double* v = solver->v;
int* s = solver->s;
double* f = solver->f;
double* g = solver->g;
double factorX = solver->dt / solver->grid.dx;
double factorY = solver->dt / solver->grid.dy;
double val = 0;
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 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;
}
}
}
double smoothRB(Solver* solver)
{
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* p = solver->p;
double* r = solver->r[solver->currentlevel];
double* rhs = solver->rhs;
double epssq = eps * eps;
int it = 0;
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) {
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);
P(i, j) -= (factor * R(i, j));
res += (R(i, j) * R(i, j));
}
isw = 3 - isw;
}
jsw = 3 - jsw;
}
res = res / (double)(imax * jmax);
return res;
}
void multiGrid(Solver* solver)
{
double res = 0.0;
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
if (solver->currentlevel == (solver->levels - 1)) {
for (int i = 0; i < 5; i++) {
smoothRB(solver);
}
return;
}
for (int i = 0; i < 5; i++) {
smoothRB(solver);
if (solver->currentlevel == 0) {
double* p = solver->p;
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);
}
}
}
Solver coarseSolver = copySolver(solver);
// restrict
restrictMG(solver);
coarseSolver.p = solver->e[coarseSolver.currentlevel];
coarseSolver.rhs = solver->r[coarseSolver.currentlevel];
coarseSolver.grid.imax /= 2;
coarseSolver.grid.jmax /= 2;
// MGSolver on residual and error.
multiGrid(&coarseSolver);
// prolongate
prolongate(solver);
// correct p on finest level using residual
correct(solver);
if (solver->currentlevel == 0) {
double* p = solver->p;
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);
}
}
for (int i = 0; i < 5; i++) {
res = smoothRB(solver);
if (solver->currentlevel == 0) {
double* p = solver->p;
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);
}
}
}
#ifdef VERBOSE
if (solver->currentlevel == 0) {
printf("Residuum: %.6f\n", res);
}
#endif
}
void restrictMG(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double* r = solver->r[solver->currentlevel + 1];
double* oldr = solver->r[solver->currentlevel];
for (int j = 1; j < jmax + 1; j++) {
for (int i = 1; i < imax + 1; ++i) {
R(i, j) = (oldR(2 * i - 1, 2 * j - 1) + oldR(2 * i, 2 * j - 1) * 2 +
oldR(2 * i + 1, 2 * j - 1) + oldR(2 * i - 1, 2 * j) * 2 +
oldR(2 * i, 2 * j) * 4 + oldR(2 * i + 1, 2 * j) * 2 +
oldR(2 * i - 1, 2 * j + 1) + oldR(2 * i, 2 * j + 1) * 2 +
oldR(2 * i + 1, 2 * j + 1)) /
16.0;
}
}
}
void prolongate(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double* olde = solver->r[solver->currentlevel + 1];
double* e = solver->r[solver->currentlevel];
for (int j = 2; j < jmax + 1; j += 2) {
for (int i = 2; i < imax + 1; i += 2) {
E(i, j) = oldE(i / 2, j / 2);
}
}
}
void correct(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double* p = solver->p;
double* e = solver->e[solver->currentlevel];
for (int j = 1; j < jmax + 1; ++j) {
for (int i = 1; i < imax + 1; ++i) {
P(i, j) += E(i, j);
}
}
}
Solver copySolver(Solver* solver)
{
Solver newSolver;
newSolver.problem = solver->problem;
newSolver.bcLeft = solver->bcLeft;
newSolver.bcRight = solver->bcRight;
newSolver.bcBottom = solver->bcBottom;
newSolver.bcTop = solver->bcTop;
newSolver.grid.imax = solver->grid.imax;
newSolver.grid.jmax = solver->grid.jmax;
newSolver.grid.xlength = solver->grid.xlength;
newSolver.grid.ylength = solver->grid.ylength;
newSolver.grid.dx = solver->grid.xlength / solver->grid.imax;
newSolver.grid.dy = solver->grid.ylength / solver->grid.jmax;
newSolver.eps = solver->eps;
newSolver.omega = solver->omega;
newSolver.itermax = solver->itermax;
newSolver.re = solver->re;
newSolver.gx = solver->gx;
newSolver.gy = solver->gy;
newSolver.dt = solver->dt;
newSolver.te = solver->te;
newSolver.tau = solver->tau;
newSolver.gamma = solver->gamma;
newSolver.rho = solver->rho;
newSolver.levels = solver->levels;
newSolver.currentlevel = solver->currentlevel + 1;
newSolver.r = solver->r;
newSolver.e = solver->e;
return newSolver;
}
void writeResult(Solver* solver)
{
int imax = solver->grid.imax;
int jmax = solver->grid.jmax;
double dx = solver->grid.dx;
double dy = solver->grid.dy;
double* p = solver->p;
double* u = solver->u;
double* v = solver->v;
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 + 1; j++) {
y = (double)(j - 0.5) * dy;
for (int i = 1; i < imax + 1; i++) {
x = (double)(i - 0.5) * dx;
fprintf(fp, "%.2f %.2f %f\n", x, y, P(i, j));
}
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 + 1; j++) {
y = dy * (j - 0.5);
for (int i = 1; i < imax + 1; i++) {
x = dx * (i - 0.5);
double vel_u = (U(i, j) + U(i - 1, j)) / 2.0;
double vel_v = (V(i, j) + V(i, j - 1)) / 2.0;
double len = sqrt((vel_u * vel_u) + (vel_v * vel_v));
fprintf(fp, "%.2f %.2f %f %f %f\n", x, y, vel_u, vel_v, len);
}
}
fclose(fp);
}

View File

@ -6,21 +6,62 @@
*/ */
#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 OBJECTBOUNDARY {
NONE = 0,
TOP,
BOTTOM,
LEFT,
RIGHT,
TOPLEFT,
BOTTOMLEFT,
TOPRIGHT,
BOTTOMRIGHT,
LOCAL
};
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
/// @brief
enum SHAPE { NOSHAPE = 0, RECT, CIRCLE };
typedef struct { typedef struct {
/* geometry and grid information */ /* geometry and grid information */
Grid* grid; Grid grid;
/* arrays */
double *p, *rhs, **r, **e;
double *f, *g;
double *u, *v;
int* s;
/* parameters */ /* parameters */
double eps, omega, rho; double eps, omega, rho;
int itermax; double re, tau, gamma;
int levels; double gx, gy;
double **r, **e; /* time stepping */
int itermax, levels, currentlevel;
double dt, te;
double dtBound;
char* problem;
int bcLeft, bcRight, bcBottom, bcTop;
} Solver; } Solver;
extern void initSolver(Solver*, Discretization*, Parameter*); extern void initSolver(Solver*, Parameter*);
extern void solve(Solver*, double*, double*); extern void computeRHS(Solver*);
extern double smoothRB(Solver*);
extern void restrictMG(Solver*);
extern void prolongate(Solver*);
extern void correct(Solver*);
extern Solver copySolver(Solver*);
extern void multiGrid(Solver*);
extern void normalizePressure(Solver*);
extern void computeTimestep(Solver*);
extern void setBoundaryConditions(Solver*);
extern void setSpecialBoundaryCondition(Solver*);
extern void setObjectBoundaryCondition(Solver*);
extern void computeFG(Solver*);
extern void adaptUV(Solver*);
extern void writeResult(Solver*);
extern void print(Solver*, double*);
extern void printGrid(Solver*, int*);
#endif #endif

View File

@ -7,16 +7,18 @@
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
double getTimeStamp(void) double getTimeStamp()
{ {
struct timespec ts; struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts); clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9; return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
} }
double getTimeResolution(void) double getTimeResolution()
{ {
struct timespec ts; struct timespec ts;
clock_getres(CLOCK_MONOTONIC, &ts); clock_getres(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9; return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
} }
double getTimeStamp_() { return getTimeStamp(); }

View File

@ -7,7 +7,8 @@
#ifndef __TIMING_H_ #ifndef __TIMING_H_
#define __TIMING_H_ #define __TIMING_H_
extern double getTimeStamp(void); extern double getTimeStamp();
extern double getTimeResolution(void); extern double getTimeResolution();
extern double getTimeStamp_();
#endif // __TIMING_H_ #endif // __TIMING_H_

View File

@ -7,7 +7,8 @@
#ifndef __UTIL_H_ #ifndef __UTIL_H_
#define __UTIL_H_ #define __UTIL_H_
#define HLINE \ #define HLINE \
"----------------------------------------------------------------------------\n" "------------------------------------------------------------------------" \
"----\n"
#ifndef MIN #ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y)) #define MIN(x, y) ((x) < (y) ? (x) : (y))
@ -19,11 +20,4 @@
#define ABS(a) ((a) >= 0 ? (a) : -(a)) #define ABS(a) ((a) >= 0 ? (a) : -(a))
#endif #endif
#define P(i, j) p[(j) * (imax + 2) + (i)]
#define F(i, j) f[(j) * (imax + 2) + (i)]
#define G(i, j) g[(j) * (imax + 2) + (i)]
#define U(i, j) u[(j) * (imax + 2) + (i)]
#define V(i, j) v[(j) * (imax + 2) + (i)]
#define RHS(i, j) rhs[(j) * (imax + 2) + (i)]
#endif // __UTIL_H_ #endif // __UTIL_H_

View File

@ -1,10 +0,0 @@
unset border; unset tics; unset key;
set term gif animate delay 30
set output "trace.gif"
set xrange [0:30]
set yrange [0:4]
do for [ts=0:120] {
plot "particles_".ts.".dat" with points pointtype 7
}
unset output