Merging the new branch
This commit is contained in:
76
EnhancedSolver/2D-seq/Makefile
Normal file
76
EnhancedSolver/2D-seq/Makefile
Normal file
@@ -0,0 +1,76 @@
|
||||
#=======================================================================================
|
||||
# Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
# All rights reserved.
|
||||
# Use of this source code is governed by a MIT-style
|
||||
# license that can be found in the LICENSE file.
|
||||
#=======================================================================================
|
||||
|
||||
#CONFIGURE BUILD SYSTEM
|
||||
TARGET = exe-$(TAG)
|
||||
BUILD_DIR = ./$(TAG)
|
||||
SRC_DIR = ./src
|
||||
MAKE_DIR = ./
|
||||
Q ?= @
|
||||
|
||||
#DO NOT EDIT BELOW
|
||||
include $(MAKE_DIR)/config.mk
|
||||
include $(MAKE_DIR)/include_$(TAG).mk
|
||||
INCLUDES += -I$(SRC_DIR) -I$(BUILD_DIR)
|
||||
|
||||
VPATH = $(SRC_DIR)
|
||||
SRC = $(filter-out $(wildcard $(SRC_DIR)/*-*.c),$(wildcard $(SRC_DIR)/*.c))
|
||||
ASM = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.s, $(SRC))
|
||||
OBJ = $(patsubst $(SRC_DIR)/%.c, $(BUILD_DIR)/%.o, $(SRC))
|
||||
OBJ += $(BUILD_DIR)/solver-$(SOLVER).o
|
||||
SOURCES = $(SRC) $(wildcard $(SRC_DIR)/*.h)
|
||||
CPPFLAGS := $(CPPFLAGS) $(DEFINES) $(OPTIONS) $(INCLUDES)
|
||||
|
||||
${TARGET}: $(BUILD_DIR) $(OBJ)
|
||||
$(info ===> LINKING $(TARGET))
|
||||
$(Q)${LINKER} ${LFLAGS} -o $(TARGET) $(OBJ) $(LIBS)
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c $(MAKE_DIR)/include_$(TAG).mk $(MAKE_DIR)/config.mk
|
||||
$(info ===> COMPILE $@)
|
||||
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
$(Q)$(GCC) $(CPPFLAGS) -MT $(@:.d=.o) -MM $< > $(BUILD_DIR)/$*.d
|
||||
|
||||
$(BUILD_DIR)/%.s: %.c
|
||||
$(info ===> GENERATE ASM $@)
|
||||
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
|
||||
|
||||
.PHONY: clean distclean tags info asm format
|
||||
|
||||
clean:
|
||||
$(info ===> CLEAN)
|
||||
@rm -rf $(BUILD_DIR)
|
||||
@rm -f tags
|
||||
|
||||
distclean: clean
|
||||
$(info ===> DIST CLEAN)
|
||||
@rm -f $(TARGET)
|
||||
@rm -f *.dat
|
||||
@rm -f *.png
|
||||
@rm -f ./vis_files/*.dat
|
||||
@rm -f ./vis_files/*.gif
|
||||
|
||||
info:
|
||||
$(info $(CFLAGS))
|
||||
$(Q)$(CC) $(VERSION)
|
||||
|
||||
asm: $(BUILD_DIR) $(ASM)
|
||||
|
||||
tags:
|
||||
$(info ===> GENERATE TAGS)
|
||||
$(Q)ctags -R
|
||||
|
||||
format:
|
||||
@for src in $(SOURCES) ; do \
|
||||
echo "Formatting $$src" ; \
|
||||
clang-format -i $$src ; \
|
||||
done
|
||||
@echo "Done"
|
||||
|
||||
$(BUILD_DIR):
|
||||
@mkdir $(BUILD_DIR)
|
||||
|
||||
-include $(OBJ:.o=.d)
|
78
EnhancedSolver/2D-seq/README.md
Normal file
78
EnhancedSolver/2D-seq/README.md
Normal file
@@ -0,0 +1,78 @@
|
||||
# C source skeleton
|
||||
|
||||
## Build
|
||||
|
||||
1. Configure the toolchain and additional options in `config.mk`:
|
||||
```
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= GCC
|
||||
ENABLE_OPENMP ?= false
|
||||
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
#OPTIONS += -DVERBOSE
|
||||
#OPTIONS += -DVERBOSE_AFFINITY
|
||||
#OPTIONS += -DVERBOSE_DATASIZE
|
||||
#OPTIONS += -DVERBOSE_TIMER
|
||||
```
|
||||
|
||||
The verbosity options enable detailed output about solver, affinity settings, allocation sizes and timer resolution.
|
||||
For debugging you may want to set the VERBOSE option:
|
||||
```
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= GCC
|
||||
ENABLE_OPENMP ?= false
|
||||
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
OPTIONS += -DVERBOSE
|
||||
#OPTIONS += -DVERBOSE_AFFINITY
|
||||
#OPTIONS += -DVERBOSE_DATASIZE
|
||||
#OPTIONS += -DVERBOSE_TIMER
|
||||
`
|
||||
|
||||
2. Build with:
|
||||
```
|
||||
make
|
||||
```
|
||||
|
||||
You can build multiple toolchains in the same directory, but notice that the Makefile is only acting on the one currently set.
|
||||
Intermediate build results are located in the `<TOOLCHAIN>` directory.
|
||||
|
||||
To output the executed commands use:
|
||||
```
|
||||
make Q=
|
||||
```
|
||||
|
||||
3. Clean up with:
|
||||
```
|
||||
make clean
|
||||
```
|
||||
to clean intermediate build results.
|
||||
|
||||
```
|
||||
make distclean
|
||||
```
|
||||
to clean intermediate build results and binary.
|
||||
|
||||
4. (Optional) Generate assembler:
|
||||
```
|
||||
make asm
|
||||
```
|
||||
The assembler files will also be located in the `<TOOLCHAIN>` directory.
|
||||
|
||||
## Usage
|
||||
|
||||
You have to provide a parameter file describing the problem you want to solve:
|
||||
```
|
||||
./exe-CLANG dcavity.par
|
||||
```
|
||||
|
||||
Examples are given in in dcavity (a lid driven cavity test case) and canal (simulating a empty canal).
|
||||
|
||||
You can plot the resulting velocity and pressure fields using gnuplot:
|
||||
```
|
||||
gnuplot vector.plot
|
||||
```
|
||||
and for the pressure:
|
||||
```
|
||||
gnuplot surface.plot
|
||||
```
|
72
EnhancedSolver/2D-seq/backstep.par
Normal file
72
EnhancedSolver/2D-seq/backstep.par
Normal file
@@ -0,0 +1,72 @@
|
||||
#==============================================================================
|
||||
# Laminar Canal Flow
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name backstep # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 36500.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 1.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 7.0 # domain size in x-direction
|
||||
ylength 1.5 # domain size in y-direction
|
||||
imax 210 # number of interior cells in x-direction
|
||||
jmax 45 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 60.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 500 # maximal number of pressure iteration in one time step
|
||||
eps 0.0001 # stopping tolerance for pressure iteration
|
||||
rho 0.52
|
||||
omg 1.8 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 200
|
||||
startTime 100
|
||||
injectTimePeriod 1.0
|
||||
writeTimePeriod 0.5
|
||||
|
||||
x1 0.0
|
||||
y1 0.5
|
||||
x2 0.0
|
||||
y2 1.5
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 1
|
||||
xCenter 0.0
|
||||
yCenter 0.0
|
||||
xRectLength 2.0
|
||||
yRectLength 1.0
|
||||
circleRadius 1.0
|
||||
|
||||
#===============================================================================
|
73
EnhancedSolver/2D-seq/canal.par
Normal file
73
EnhancedSolver/2D-seq/canal.par
Normal file
@@ -0,0 +1,73 @@
|
||||
#==============================================================================
|
||||
# Laminar Canal Flow
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name canal # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 100.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 1.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 30.0 # domain size in x-direction
|
||||
ylength 4.0 # domain size in y-direction
|
||||
imax 256 # number of interior cells in x-direction
|
||||
jmax 64 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 80.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 500 # maximal number of pressure iteration in one time step
|
||||
eps 0.0001 # stopping tolerance for pressure iteration
|
||||
rho 0.52
|
||||
omg 1.8 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
levels 5 # Multigrid levels
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 60
|
||||
startTime 5.0
|
||||
injectTimePeriod 4.0
|
||||
writeTimePeriod 1.0
|
||||
|
||||
x1 1.0
|
||||
y1 0.0
|
||||
x2 1.0
|
||||
y2 4.0
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 0
|
||||
xCenter 10.0
|
||||
yCenter 2
|
||||
xRectLength 6.0
|
||||
yRectLength 1.0
|
||||
circleRadius 1.0
|
||||
|
||||
#===============================================================================
|
12
EnhancedSolver/2D-seq/config.mk
Normal file
12
EnhancedSolver/2D-seq/config.mk
Normal file
@@ -0,0 +1,12 @@
|
||||
# Supported: GCC, CLANG, ICC
|
||||
TAG ?= CLANG
|
||||
ENABLE_OPENMP ?= false
|
||||
# Supported: sor, rb, mg
|
||||
SOLVER ?= mg
|
||||
# Run in debug settings
|
||||
DEBUG ?= false
|
||||
|
||||
#Feature options
|
||||
OPTIONS += -DARRAY_ALIGNMENT=64
|
||||
#OPTIONS += -DVERBOSE
|
||||
#OPTIONS += -DDEBUG
|
73
EnhancedSolver/2D-seq/dcavity.par
Normal file
73
EnhancedSolver/2D-seq/dcavity.par
Normal file
@@ -0,0 +1,73 @@
|
||||
#==============================================================================
|
||||
# Driven Cavity
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name dcavity # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 1 # 2 = free-slip 4 = periodic
|
||||
bcRight 1 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 10.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 0.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 1.0 # domain size in x-direction
|
||||
ylength 1.0 # domain size in y-direction
|
||||
imax 128 # number of interior cells in x-direction
|
||||
jmax 128 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 10.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 1000 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
rho 0.5
|
||||
omg 1.8 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
levels 5 # Multigrid levels
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 200
|
||||
startTime 2.0
|
||||
injectTimePeriod 0.5
|
||||
writeTimePeriod 0.2
|
||||
|
||||
x1 0.1
|
||||
y1 0.9
|
||||
x2 0.9
|
||||
y2 0.9
|
||||
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 0
|
||||
xCenter 0.5
|
||||
yCenter 0.5
|
||||
xRectLength 0.5
|
||||
yRectLength 0.5
|
||||
circleRadius 0.5
|
||||
#===============================================================================
|
19
EnhancedSolver/2D-seq/include_CLANG.mk
Normal file
19
EnhancedSolver/2D-seq/include_CLANG.mk
Normal file
@@ -0,0 +1,19 @@
|
||||
CC = clang
|
||||
GCC = cc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(strip $(ENABLE_OPENMP)),true)
|
||||
OPENMP = -fopenmp
|
||||
#OPENMP = -Xpreprocessor -fopenmp #required on Macos with homebrew libomp
|
||||
LIBS = # -lomp
|
||||
endif
|
||||
ifeq ($(strip $(DEBUG)),true)
|
||||
CFLAGS = -O0 -g -std=c17
|
||||
else
|
||||
CFLAGS = -O3 -std=c17 $(OPENMP)
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
LFLAGS = $(OPENMP) -lm
|
||||
DEFINES = -D_GNU_SOURCE
|
||||
INCLUDES =
|
14
EnhancedSolver/2D-seq/include_GCC.mk
Normal file
14
EnhancedSolver/2D-seq/include_GCC.mk
Normal file
@@ -0,0 +1,14 @@
|
||||
CC = gcc
|
||||
GCC = gcc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(ENABLE_OPENMP),true)
|
||||
OPENMP = -fopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -Ofast -ffreestanding -std=c99 $(OPENMP)
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES = -D_GNU_SOURCE
|
||||
INCLUDES =
|
||||
LIBS =
|
14
EnhancedSolver/2D-seq/include_ICC.mk
Normal file
14
EnhancedSolver/2D-seq/include_ICC.mk
Normal file
@@ -0,0 +1,14 @@
|
||||
CC = icx
|
||||
GCC = gcc
|
||||
LINKER = $(CC)
|
||||
|
||||
ifeq ($(ENABLE_OPENMP),true)
|
||||
OPENMP = -qopenmp
|
||||
endif
|
||||
|
||||
VERSION = --version
|
||||
CFLAGS = -O3 -xHost -qopt-zmm-usage=high -std=c99 $(OPENMP)
|
||||
LFLAGS = $(OPENMP)
|
||||
DEFINES = -D_GNU_SOURCE
|
||||
INCLUDES =
|
||||
LIBS =
|
73
EnhancedSolver/2D-seq/karman.par
Normal file
73
EnhancedSolver/2D-seq/karman.par
Normal file
@@ -0,0 +1,73 @@
|
||||
#==============================================================================
|
||||
# Laminar Canal Flow
|
||||
#==============================================================================
|
||||
|
||||
# Problem specific Data:
|
||||
# ---------------------
|
||||
|
||||
name karman # name of flow setup
|
||||
|
||||
bcTop 1 # flags for boundary conditions
|
||||
bcBottom 1 # 1 = no-slip 3 = outflow
|
||||
bcLeft 3 # 2 = free-slip 4 = periodic
|
||||
bcRight 3 #
|
||||
|
||||
gx 0.0 # Body forces (e.g. gravity)
|
||||
gy 0.0 #
|
||||
|
||||
re 5050.0 # Reynolds number
|
||||
|
||||
u_init 1.0 # initial value for velocity in x-direction
|
||||
v_init 0.0 # initial value for velocity in y-direction
|
||||
p_init 0.0 # initial value for pressure
|
||||
|
||||
# Geometry Data:
|
||||
# -------------
|
||||
|
||||
xlength 30.0 # domain size in x-direction
|
||||
ylength 8.0 # domain size in y-direction
|
||||
imax 400 # number of interior cells in x-direction
|
||||
jmax 200 # number of interior cells in y-direction
|
||||
|
||||
# Time Data:
|
||||
# ---------
|
||||
|
||||
te 150.0 # final time
|
||||
dt 0.02 # time stepsize
|
||||
tau 0.5 # safety factor for time stepsize control (<0 constant delt)
|
||||
|
||||
# Pressure Iteration Data:
|
||||
# -----------------------
|
||||
|
||||
itermax 200 # maximal number of pressure iteration in one time step
|
||||
eps 0.001 # stopping tolerance for pressure iteration
|
||||
rho 0.52
|
||||
omg 1.75 # relaxation parameter for SOR iteration
|
||||
gamma 0.9 # upwind differencing factor gamma
|
||||
levels 5 # Multigrid levels
|
||||
|
||||
# Particle Tracing Data:
|
||||
# -----------------------
|
||||
|
||||
numberOfParticles 200
|
||||
startTime 201
|
||||
injectTimePeriod 1.0
|
||||
writeTimePeriod 0.5
|
||||
|
||||
x1 0.0
|
||||
y1 3.8
|
||||
x2 0.0
|
||||
y2 4.1
|
||||
|
||||
# Obstacle Geometry Data:
|
||||
# -----------------------
|
||||
# Shape 0 disable, 1 Rectangle/Square, 2 Circle
|
||||
|
||||
shape 2
|
||||
xCenter 5.0
|
||||
yCenter 4.0
|
||||
xRectLength 2.0
|
||||
yRectLength 1.0
|
||||
circleRadius 1.0
|
||||
|
||||
#===============================================================================
|
38
EnhancedSolver/2D-seq/src/allocate.c
Normal file
38
EnhancedSolver/2D-seq/src/allocate.c
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "allocate.h"
|
||||
|
||||
void* allocate(size_t alignment, size_t bytesize)
|
||||
{
|
||||
int errorCode;
|
||||
void* ptr;
|
||||
|
||||
errorCode = posix_memalign(&ptr, alignment, bytesize);
|
||||
|
||||
if (errorCode) {
|
||||
if (errorCode == EINVAL) {
|
||||
fprintf(stderr, "Error: Alignment parameter is not a power of two\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (errorCode == ENOMEM) {
|
||||
fprintf(stderr, "Error: Insufficient memory to fulfill the request\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
if (ptr == NULL) {
|
||||
fprintf(stderr, "Error: posix_memalign failed!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
13
EnhancedSolver/2D-seq/src/allocate.h
Normal file
13
EnhancedSolver/2D-seq/src/allocate.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* 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 __ALLOCATE_H_
|
||||
#define __ALLOCATE_H_
|
||||
#include <stdlib.h>
|
||||
|
||||
extern void* allocate(size_t alignment, size_t bytesize);
|
||||
|
||||
#endif
|
649
EnhancedSolver/2D-seq/src/discretization.c
Normal file
649
EnhancedSolver/2D-seq/src/discretization.c
Normal file
@@ -0,0 +1,649 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
44
EnhancedSolver/2D-seq/src/discretization.h
Normal file
44
EnhancedSolver/2D-seq/src/discretization.h
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __DISCRETIZATION_H_
|
||||
#define __DISCRETIZATION_H_
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
|
||||
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
|
38
EnhancedSolver/2D-seq/src/grid.h
Normal file
38
EnhancedSolver/2D-seq/src/grid.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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 __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 {
|
||||
double dx, dy;
|
||||
int imax, jmax;
|
||||
double xlength, ylength;
|
||||
int* s;
|
||||
} Grid;
|
||||
|
||||
static inline int gridIsFluid(Grid* g, int i, int j)
|
||||
{
|
||||
return g->s[j * (g->imax + 2) + i] == FLUID;
|
||||
}
|
||||
#endif // __GRID_H_
|
54
EnhancedSolver/2D-seq/src/likwid-marker.h
Normal file
54
EnhancedSolver/2D-seq/src/likwid-marker.h
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* =======================================================================================
|
||||
*
|
||||
* Author: Jan Eitzinger (je), jan.eitzinger@fau.de
|
||||
* Copyright (c) 2020 RRZE, University Erlangen-Nuremberg
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* =======================================================================================
|
||||
*/
|
||||
#ifndef LIKWID_MARKERS_H
|
||||
#define LIKWID_MARKERS_H
|
||||
|
||||
#ifdef LIKWID_PERFMON
|
||||
#include <likwid.h>
|
||||
#define LIKWID_MARKER_INIT likwid_markerInit()
|
||||
#define LIKWID_MARKER_THREADINIT likwid_markerThreadInit()
|
||||
#define LIKWID_MARKER_SWITCH likwid_markerNextGroup()
|
||||
#define LIKWID_MARKER_REGISTER(regionTag) likwid_markerRegisterRegion(regionTag)
|
||||
#define LIKWID_MARKER_START(regionTag) likwid_markerStartRegion(regionTag)
|
||||
#define LIKWID_MARKER_STOP(regionTag) likwid_markerStopRegion(regionTag)
|
||||
#define LIKWID_MARKER_CLOSE likwid_markerClose()
|
||||
#define LIKWID_MARKER_RESET(regionTag) likwid_markerResetRegion(regionTag)
|
||||
#define LIKWID_MARKER_GET(regionTag, nevents, events, time, count) \
|
||||
likwid_markerGetRegion(regionTag, nevents, events, time, count)
|
||||
#else /* LIKWID_PERFMON */
|
||||
#define LIKWID_MARKER_INIT
|
||||
#define LIKWID_MARKER_THREADINIT
|
||||
#define LIKWID_MARKER_SWITCH
|
||||
#define LIKWID_MARKER_REGISTER(regionTag)
|
||||
#define LIKWID_MARKER_START(regionTag)
|
||||
#define LIKWID_MARKER_STOP(regionTag)
|
||||
#define LIKWID_MARKER_CLOSE
|
||||
#define LIKWID_MARKER_GET(regionTag, nevents, events, time, count)
|
||||
#define LIKWID_MARKER_RESET(regionTag)
|
||||
#endif /* LIKWID_PERFMON */
|
||||
|
||||
#endif /*LIKWID_MARKERS_H*/
|
81
EnhancedSolver/2D-seq/src/main.c
Normal file
81
EnhancedSolver/2D-seq/src/main.c
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "discretization.h"
|
||||
#include "parameter.h"
|
||||
#include "particletracing.h"
|
||||
#include "progress.h"
|
||||
#include "solver.h"
|
||||
#include "timing.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
double timeStart, timeStop;
|
||||
Parameter p;
|
||||
Discretization d;
|
||||
Solver s;
|
||||
ParticleTracer particletracer;
|
||||
|
||||
initParameter(&p);
|
||||
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s <configFile>\n", argv[0]);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
readParameter(&p, argv[1]);
|
||||
printParameter(&p);
|
||||
initDiscretization(&d, &p);
|
||||
initSolver(&s, &d, &p);
|
||||
initParticleTracer(&particletracer, &d.grid, &p);
|
||||
printParticleTracerParameters(&particletracer);
|
||||
|
||||
#ifndef VERBOSE
|
||||
initProgress(d.te);
|
||||
#endif
|
||||
|
||||
double tau = d.tau;
|
||||
double te = d.te;
|
||||
double t = 0.0;
|
||||
int nt = 0;
|
||||
|
||||
timeStart = getTimeStamp();
|
||||
|
||||
while (t <= te) {
|
||||
if (tau > 0.0) computeTimestep(&d);
|
||||
setBoundaryConditions(&d);
|
||||
setSpecialBoundaryCondition(&d);
|
||||
setObjectBoundaryCondition(&d);
|
||||
|
||||
computeFG(&d);
|
||||
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;
|
||||
nt++;
|
||||
|
||||
#ifdef VERBOSE
|
||||
printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
|
||||
#else
|
||||
printProgress(t);
|
||||
#endif
|
||||
}
|
||||
|
||||
timeStop = getTimeStamp();
|
||||
|
||||
stopProgress();
|
||||
freeParticles(&particletracer);
|
||||
printf("Solution took %.2fs\n", timeStop - timeStart);
|
||||
writeResult(&d);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
147
EnhancedSolver/2D-seq/src/parameter.c
Normal file
147
EnhancedSolver/2D-seq/src/parameter.c
Normal file
@@ -0,0 +1,147 @@
|
||||
/*
|
||||
* 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 <string.h>
|
||||
|
||||
#include "parameter.h"
|
||||
#include "util.h"
|
||||
#define MAXLINE 4096
|
||||
|
||||
void initParameter(Parameter* param)
|
||||
{
|
||||
param->xlength = 1.0;
|
||||
param->ylength = 1.0;
|
||||
param->imax = 100;
|
||||
param->jmax = 100;
|
||||
param->itermax = 1000;
|
||||
param->eps = 0.0001;
|
||||
param->omg = 1.7;
|
||||
param->re = 100.0;
|
||||
param->gamma = 0.9;
|
||||
param->tau = 0.5;
|
||||
param->rho = 0.99;
|
||||
param->levels = 5;
|
||||
}
|
||||
|
||||
void readParameter(Parameter* param, const char* filename)
|
||||
{
|
||||
FILE* fp = fopen(filename, "r");
|
||||
char line[MAXLINE];
|
||||
int i;
|
||||
|
||||
if (!fp) {
|
||||
fprintf(stderr, "Could not open parameter file: %s\n", filename);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
while (!feof(fp)) {
|
||||
line[0] = '\0';
|
||||
fgets(line, MAXLINE, fp);
|
||||
for (i = 0; line[i] != '\0' && line[i] != '#'; i++)
|
||||
;
|
||||
line[i] = '\0';
|
||||
|
||||
char* tok = strtok(line, " ");
|
||||
char* val = strtok(NULL, " ");
|
||||
|
||||
#define PARSE_PARAM(p, f) \
|
||||
if (strncmp(tok, #p, sizeof(#p) / sizeof(#p[0]) - 1) == 0) { \
|
||||
param->p = f(val); \
|
||||
}
|
||||
#define PARSE_STRING(p) PARSE_PARAM(p, strdup)
|
||||
#define PARSE_INT(p) PARSE_PARAM(p, atoi)
|
||||
#define PARSE_REAL(p) PARSE_PARAM(p, atof)
|
||||
|
||||
if (tok != NULL && val != NULL) {
|
||||
PARSE_REAL(xlength);
|
||||
PARSE_REAL(ylength);
|
||||
PARSE_INT(imax);
|
||||
PARSE_INT(jmax);
|
||||
PARSE_INT(itermax);
|
||||
PARSE_INT(levels);
|
||||
PARSE_REAL(eps);
|
||||
PARSE_REAL(omg);
|
||||
PARSE_REAL(re);
|
||||
PARSE_REAL(tau);
|
||||
PARSE_REAL(gamma);
|
||||
PARSE_REAL(dt);
|
||||
PARSE_REAL(te);
|
||||
PARSE_REAL(gx);
|
||||
PARSE_REAL(gy);
|
||||
PARSE_STRING(name);
|
||||
PARSE_INT(bcLeft);
|
||||
PARSE_INT(bcRight);
|
||||
PARSE_INT(bcBottom);
|
||||
PARSE_INT(bcTop);
|
||||
PARSE_REAL(u_init);
|
||||
PARSE_REAL(v_init);
|
||||
PARSE_REAL(p_init);
|
||||
PARSE_REAL(rho);
|
||||
|
||||
/* Added new particle tracing parameters */
|
||||
PARSE_INT(numberOfParticles);
|
||||
PARSE_REAL(startTime);
|
||||
PARSE_REAL(injectTimePeriod);
|
||||
PARSE_REAL(writeTimePeriod);
|
||||
PARSE_REAL(x1);
|
||||
PARSE_REAL(y1);
|
||||
PARSE_REAL(x2);
|
||||
PARSE_REAL(y2);
|
||||
|
||||
/* Added obstacle geometry parameters */
|
||||
PARSE_INT(shape);
|
||||
PARSE_REAL(xCenter);
|
||||
PARSE_REAL(yCenter);
|
||||
PARSE_REAL(xRectLength);
|
||||
PARSE_REAL(yRectLength);
|
||||
PARSE_REAL(circleRadius);
|
||||
}
|
||||
}
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
void printParameter(Parameter* param)
|
||||
{
|
||||
printf("Parameters for %s\n", param->name);
|
||||
printf("Boundary conditions Left:%d Right:%d Bottom:%d Top:%d\n",
|
||||
param->bcLeft,
|
||||
param->bcRight,
|
||||
param->bcBottom,
|
||||
param->bcTop);
|
||||
printf("\tReynolds number: %.2f\n", param->re);
|
||||
printf("\tInit arrays: U:%.2f V:%.2f P:%.2f\n",
|
||||
param->u_init,
|
||||
param->v_init,
|
||||
param->p_init);
|
||||
printf("Geometry data:\n");
|
||||
printf("\tDomain box size (x, y): %.2f, %.2f\n", param->xlength, param->ylength);
|
||||
printf("\tCells (x, y): %d, %d\n", param->imax, param->jmax);
|
||||
printf("Timestep parameters:\n");
|
||||
printf("\tDefault stepsize: %.2f, Final time %.2f\n", param->dt, param->te);
|
||||
printf("\tTau factor: %.2f\n", param->tau);
|
||||
printf("Iterative solver parameters:\n");
|
||||
printf("\tMax iterations: %d\n", param->itermax);
|
||||
printf("\tepsilon (stopping tolerance) : %f\n", param->eps);
|
||||
printf("\tgamma (stopping tolerance) : %f\n", param->gamma);
|
||||
printf("\tomega (SOR relaxation): %f\n", param->omg);
|
||||
printf("\trho (SOR relaxation): %f\n", param->rho);
|
||||
printf("\tMultiGrid levels : %d\n", param->levels);
|
||||
|
||||
printf("Particle Tracing data:\n");
|
||||
printf("\tNumber of particles : %d being injected for every period of %.2f\n",
|
||||
param->numberOfParticles,
|
||||
param->injectTimePeriod);
|
||||
printf("\tstartTime : %.2f\n", param->startTime);
|
||||
printf("\t(Line along which the particles are to be injected) \n\tx1 : %.2f, y1 : "
|
||||
"%.2f, x2 : %.2f, y2 : %.2f\n",
|
||||
param->x1,
|
||||
param->y1,
|
||||
param->x2,
|
||||
param->y2);
|
||||
}
|
35
EnhancedSolver/2D-seq/src/parameter.h
Normal file
35
EnhancedSolver/2D-seq/src/parameter.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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 __PARAMETER_H_
|
||||
#define __PARAMETER_H_
|
||||
|
||||
typedef struct {
|
||||
double xlength, ylength;
|
||||
int imax, jmax;
|
||||
int itermax, levels;
|
||||
double eps, omg, rho;
|
||||
double re, tau, gamma;
|
||||
double te, dt;
|
||||
double gx, gy;
|
||||
char* name;
|
||||
int bcLeft, bcRight, bcBottom, bcTop;
|
||||
double u_init, v_init, p_init;
|
||||
|
||||
int numberOfParticles;
|
||||
double startTime, injectTimePeriod, writeTimePeriod;
|
||||
|
||||
double x1, y1, x2, y2;
|
||||
|
||||
int shape;
|
||||
double xCenter, yCenter, xRectLength, yRectLength, circleRadius;
|
||||
|
||||
} Parameter;
|
||||
|
||||
void initParameter(Parameter*);
|
||||
void readParameter(Parameter*, const char*);
|
||||
void printParameter(Parameter*);
|
||||
#endif
|
246
EnhancedSolver/2D-seq/src/particletracing.c
Normal file
246
EnhancedSolver/2D-seq/src/particletracing.c
Normal file
@@ -0,0 +1,246 @@
|
||||
/*
|
||||
* 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 <string.h>
|
||||
|
||||
#include "grid.h"
|
||||
#include "vtkWriter.h"
|
||||
|
||||
#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)]
|
||||
|
||||
void printParticles(ParticleTracer* p)
|
||||
{
|
||||
for (int i = 0; i < p->totalParticles; ++i) {
|
||||
printf("Particle position X : %.2f, Y : %.2f, flag : %d\n",
|
||||
p->particlePool[i].x,
|
||||
p->particlePool[i].y,
|
||||
p->particlePool[i].flag);
|
||||
}
|
||||
}
|
||||
|
||||
static void injectParticles(ParticleTracer* p)
|
||||
{
|
||||
if (p->totalParticles + p->numParticlesInLine > p->numAllocatedParticles) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < p->numParticlesInLine; ++i) {
|
||||
p->particlePool[p->pointer].x = p->linSpaceLine[i].x;
|
||||
p->particlePool[p->pointer].y = p->linSpaceLine[i].y;
|
||||
p->particlePool[p->pointer].flag = true;
|
||||
p->pointer++;
|
||||
p->totalParticles++;
|
||||
}
|
||||
}
|
||||
|
||||
static void advanceParticles(
|
||||
ParticleTracer* p, double* restrict u, double* restrict v, double dt)
|
||||
{
|
||||
int imax = p->grid->imax;
|
||||
int jmax = p->grid->jmax;
|
||||
double dx = p->grid->dx;
|
||||
double dy = p->grid->dy;
|
||||
double xlength = p->grid->xlength;
|
||||
double ylength = p->grid->ylength;
|
||||
|
||||
for (int i = 0; i < p->totalParticles; ++i) {
|
||||
if (p->particlePool[i].flag == true) {
|
||||
double x = p->particlePool[i].x;
|
||||
double y = p->particlePool[i].y;
|
||||
|
||||
int iCoord = (int)(x / dx) + 1;
|
||||
int jCoord = (int)((y + 0.5 * dy) / dy) + 1;
|
||||
|
||||
double x1 = (double)(iCoord - 1) * dx;
|
||||
double y1 = ((double)(jCoord - 1) - 0.5) * dy;
|
||||
double x2 = (double)iCoord * dx;
|
||||
double y2 = ((double)jCoord - 0.5) * dy;
|
||||
|
||||
double intU = (1.0 / (dx * dy)) *
|
||||
((x2 - x) * (y2 - y) * U(iCoord - 1, jCoord - 1) +
|
||||
(x - x1) * (y2 - y) * U(iCoord, jCoord - 1) +
|
||||
(x2 - x) * (y - y1) * U(iCoord - 1, jCoord) +
|
||||
(x - x1) * (y - y1) * U(iCoord, jCoord));
|
||||
|
||||
double newX = x + dt * intU;
|
||||
p->particlePool[i].x = newX;
|
||||
|
||||
iCoord = (int)((x + 0.5 * dx) / dx) + 1;
|
||||
jCoord = (int)(y / dy) + 1;
|
||||
|
||||
x1 = ((double)(iCoord - 1) - 0.5) * dx;
|
||||
y1 = (double)(jCoord - 1) * dy;
|
||||
x2 = ((double)iCoord - 0.5) * dx;
|
||||
y2 = (double)jCoord * dy;
|
||||
|
||||
double intV = (1.0 / (dx * dy)) *
|
||||
((x2 - x) * (y2 - y) * V(iCoord - 1, jCoord - 1) +
|
||||
(x - x1) * (y2 - y) * V(iCoord, jCoord - 1) +
|
||||
(x2 - x) * (y - y1) * V(iCoord - 1, jCoord) +
|
||||
(x - x1) * (y - y1) * V(iCoord, jCoord));
|
||||
|
||||
double newY = y + dt * intV;
|
||||
p->particlePool[i].y = newY;
|
||||
|
||||
if (((newX < 0.0) || (newX > xlength) || (newY < 0.0) || (newY > ylength))) {
|
||||
p->particlePool[i].flag = false;
|
||||
p->removedParticles++;
|
||||
}
|
||||
|
||||
int newI = newX / dx, newJ = newY / dy;
|
||||
|
||||
if (!gridIsFluid(p->grid, newI, newJ)) {
|
||||
p->particlePool[i].flag = false;
|
||||
p->removedParticles++;
|
||||
printf("Forbidden movement of particle into obstacle!\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void compress(ParticleTracer* p)
|
||||
{
|
||||
Particle* memPool = p->particlePool;
|
||||
Particle tempPool[p->totalParticles];
|
||||
int totalParticles = 0;
|
||||
|
||||
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)
|
||||
{
|
||||
static int ts = 0;
|
||||
VtkOptions opts = { .particletracer = p };
|
||||
|
||||
char filename[50];
|
||||
// snprintf(filename, 50, "vtk_files/particles%d.vtk", ts);
|
||||
// vtkOpen(&opts, filename, ts);
|
||||
// vtkParticle(&opts, "particle");
|
||||
// vtkClose(&opts);
|
||||
|
||||
FILE* fp;
|
||||
Particle* particlePool = p->particlePool;
|
||||
|
||||
snprintf(filename, 50, "vis_files/particles_%d.dat", ts);
|
||||
fp = fopen(filename, "w");
|
||||
|
||||
if (fp == NULL) {
|
||||
printf("Error!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
for (int i = 0; i < p->totalParticles; ++i) {
|
||||
double x = particlePool[i].x;
|
||||
double y = particlePool[i].y;
|
||||
fprintf(fp, "%f %f\n", x, y);
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
++ts;
|
||||
}
|
||||
|
||||
void initParticleTracer(ParticleTracer* pt, Grid* g, Parameter* p)
|
||||
{
|
||||
pt->numParticlesInLine = p->numberOfParticles;
|
||||
pt->startTime = p->startTime;
|
||||
pt->injectTimePeriod = p->injectTimePeriod;
|
||||
pt->writeTimePeriod = p->writeTimePeriod;
|
||||
pt->grid = g;
|
||||
|
||||
pt->x1 = p->x1;
|
||||
pt->y1 = p->y1;
|
||||
pt->x2 = p->x2;
|
||||
pt->y2 = p->y2;
|
||||
|
||||
pt->lastInjectTime = p->startTime;
|
||||
pt->lastWriteTime = p->startTime;
|
||||
|
||||
pt->pointer = 0;
|
||||
pt->removedParticles = 0;
|
||||
pt->totalParticles = 0;
|
||||
|
||||
if (p->te > p->startTime) {
|
||||
pt->numAllocatedParticles = ((p->te - p->startTime) / p->injectTimePeriod) *
|
||||
p->numberOfParticles;
|
||||
pt->numAllocatedParticles += (2 * p->numberOfParticles);
|
||||
|
||||
pt->particlePool = malloc(sizeof(Particle) * pt->numAllocatedParticles);
|
||||
pt->linSpaceLine = malloc(sizeof(Particle) * pt->numParticlesInLine);
|
||||
|
||||
for (int i = 0; i < pt->numParticlesInLine; ++i) {
|
||||
double spacing = (double)i / (double)(pt->numParticlesInLine - 1);
|
||||
pt->linSpaceLine[i].x = spacing * pt->x1 + (1.0 - spacing) * pt->x2;
|
||||
pt->linSpaceLine[i].y = spacing * pt->y1 + (1.0 - spacing) * pt->y2;
|
||||
pt->linSpaceLine[i].flag = true;
|
||||
}
|
||||
} else {
|
||||
pt->particlePool = NULL;
|
||||
pt->linSpaceLine = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void printParticleTracerParameters(ParticleTracer* p)
|
||||
{
|
||||
printf("Particle Tracing data:\n");
|
||||
printf("\tNumber of particles : %d being injected for every period of %.2f\n",
|
||||
p->numParticlesInLine,
|
||||
p->injectTimePeriod);
|
||||
printf("\tstartTime : %.2f\n", p->startTime);
|
||||
printf("\t(Line along which the particles are to be injected) \n\tx1 : %.2f, y1 : "
|
||||
"%.2f, x2 : %.2f, y2 : %.2f\n",
|
||||
p->x1,
|
||||
p->y1,
|
||||
p->x2,
|
||||
p->y2);
|
||||
printf("\tPointer : %d, TotalParticles : %d\n", p->pointer, p->totalParticles);
|
||||
}
|
||||
|
||||
void trace(ParticleTracer* p, double* u, double* v, double dt, double time)
|
||||
{
|
||||
if (time >= p->startTime) {
|
||||
if ((time - p->lastInjectTime) >= p->injectTimePeriod) {
|
||||
injectParticles(p);
|
||||
p->lastInjectTime = time;
|
||||
}
|
||||
|
||||
if ((time - p->lastWriteTime) >= p->writeTimePeriod) {
|
||||
writeParticles(p);
|
||||
p->lastWriteTime = time;
|
||||
}
|
||||
|
||||
advanceParticles(p, u, v, dt);
|
||||
|
||||
if (p->removedParticles > (p->totalParticles * 0.2)) {
|
||||
compress(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void freeParticles(ParticleTracer* p)
|
||||
{
|
||||
if (p->particlePool != NULL) {
|
||||
free(p->particlePool);
|
||||
free(p->linSpaceLine);
|
||||
}
|
||||
}
|
41
EnhancedSolver/2D-seq/src/particletracing.h
Normal file
41
EnhancedSolver/2D-seq/src/particletracing.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved. This file is part of nusif-solver.
|
||||
* Use of this source code is governed by a MIT style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#ifndef __PARTICLETRACING_H_
|
||||
#define __PARTICLETRACING_H_
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
|
||||
typedef enum COORD { X = 0, Y, NCOORD } COORD;
|
||||
|
||||
typedef struct {
|
||||
double x, y;
|
||||
bool flag;
|
||||
} Particle;
|
||||
|
||||
typedef struct {
|
||||
int numParticlesInLine, removedParticles, totalParticles;
|
||||
double startTime, injectTimePeriod, writeTimePeriod;
|
||||
double lastInjectTime, lastUpdateTime, lastWriteTime;
|
||||
|
||||
int numAllocatedParticles;
|
||||
|
||||
Particle* linSpaceLine;
|
||||
Particle* particlePool;
|
||||
|
||||
int pointer;
|
||||
double x1, y1, x2, y2;
|
||||
Grid* grid;
|
||||
} ParticleTracer;
|
||||
|
||||
extern void initParticleTracer(ParticleTracer*, Grid*, Parameter*);
|
||||
extern void freeParticles(ParticleTracer*);
|
||||
extern void writeParticles(ParticleTracer*);
|
||||
extern void printParticleTracerParameters(ParticleTracer*);
|
||||
extern void trace(ParticleTracer*, double*, double*, double, double);
|
||||
#endif
|
51
EnhancedSolver/2D-seq/src/progress.c
Normal file
51
EnhancedSolver/2D-seq/src/progress.c
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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 <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "progress.h"
|
||||
|
||||
static double _end;
|
||||
static int _current;
|
||||
|
||||
void initProgress(double end)
|
||||
{
|
||||
_end = end;
|
||||
_current = 0;
|
||||
|
||||
printf("[ ]");
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void printProgress(double current)
|
||||
{
|
||||
int new = (int)rint((current / _end) * 10.0);
|
||||
|
||||
if (new > _current) {
|
||||
char progress[11];
|
||||
_current = new;
|
||||
progress[0] = 0;
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
if (i < _current) {
|
||||
sprintf(progress + strlen(progress), "#");
|
||||
} else {
|
||||
sprintf(progress + strlen(progress), " ");
|
||||
}
|
||||
}
|
||||
printf("\r[%s]", progress);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void stopProgress()
|
||||
{
|
||||
printf("\n");
|
||||
fflush(stdout);
|
||||
}
|
14
EnhancedSolver/2D-seq/src/progress.h
Normal file
14
EnhancedSolver/2D-seq/src/progress.h
Normal 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 __PROGRESS_H_
|
||||
#define __PROGRESS_H_
|
||||
|
||||
extern void initProgress(double);
|
||||
extern void printProgress(double);
|
||||
extern void stopProgress(void);
|
||||
|
||||
#endif
|
186
EnhancedSolver/2D-seq/src/solver-mg.c
Normal file
186
EnhancedSolver/2D-seq/src/solver-mg.c
Normal file
@@ -0,0 +1,186 @@
|
||||
/*
|
||||
* 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
|
||||
}
|
93
EnhancedSolver/2D-seq/src/solver-rb.c
Normal file
93
EnhancedSolver/2D-seq/src/solver-rb.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* 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 "grid.h"
|
||||
#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;
|
||||
|
||||
Grid* g = s->grid;
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
|
||||
s->totalFluidCells = 0;
|
||||
for (int j = 0; j < jmax + 2; j++) {
|
||||
for (int i = 0; i < imax + 2; i++) {
|
||||
if (gridIsFluid(g, i, j)) {
|
||||
s->totalFluidCells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
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 idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
Grid* g = s->grid;
|
||||
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) {
|
||||
if (gridIsFluid(g, i, j)) {
|
||||
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)(s->totalFluidCells);
|
||||
#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
|
||||
}
|
84
EnhancedSolver/2D-seq/src/solver-sor.c
Normal file
84
EnhancedSolver/2D-seq/src/solver-sor.c
Normal file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
* 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 "grid.h"
|
||||
#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;
|
||||
|
||||
Grid* g = s->grid;
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
|
||||
s->totalFluidCells = 0;
|
||||
for (int j = 0; j < jmax + 2; j++) {
|
||||
for (int i = 0; i < imax + 2; i++) {
|
||||
if (gridIsFluid(g, i, j)) {
|
||||
s->totalFluidCells++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void solve(Solver* s, double* p, double* rhs)
|
||||
{
|
||||
int imax = s->grid->imax;
|
||||
int jmax = s->grid->jmax;
|
||||
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 idx2 = 1.0 / dx2;
|
||||
double idy2 = 1.0 / dy2;
|
||||
double factor = s->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
|
||||
double epssq = eps * eps;
|
||||
int it = 0;
|
||||
Grid* g = s->grid;
|
||||
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++) {
|
||||
if (gridIsFluid(g, i, j)) {
|
||||
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)(s->totalFluidCells);
|
||||
#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
|
||||
}
|
27
EnhancedSolver/2D-seq/src/solver.h
Normal file
27
EnhancedSolver/2D-seq/src/solver.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* 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 __SOLVER_H_
|
||||
#define __SOLVER_H_
|
||||
#include "discretization.h"
|
||||
#include "grid.h"
|
||||
#include "parameter.h"
|
||||
|
||||
typedef struct {
|
||||
/* geometry and grid information */
|
||||
Grid* grid;
|
||||
/* parameters */
|
||||
double eps, omega, rho;
|
||||
int itermax;
|
||||
int levels;
|
||||
int totalFluidCells;
|
||||
double **r, **e;
|
||||
} Solver;
|
||||
|
||||
extern void initSolver(Solver*, Discretization*, Parameter*);
|
||||
extern void solve(Solver*, double*, double*);
|
||||
|
||||
#endif
|
22
EnhancedSolver/2D-seq/src/timing.c
Normal file
22
EnhancedSolver/2D-seq/src/timing.c
Normal file
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a MIT-style
|
||||
* license that can be found in the LICENSE file.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
|
||||
double getTimeStamp(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
|
||||
}
|
||||
|
||||
double getTimeResolution(void)
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_getres(CLOCK_MONOTONIC, &ts);
|
||||
return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
|
||||
}
|
13
EnhancedSolver/2D-seq/src/timing.h
Normal file
13
EnhancedSolver/2D-seq/src/timing.h
Normal file
@@ -0,0 +1,13 @@
|
||||
/*
|
||||
* 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 __TIMING_H_
|
||||
#define __TIMING_H_
|
||||
|
||||
extern double getTimeStamp(void);
|
||||
extern double getTimeResolution(void);
|
||||
|
||||
#endif // __TIMING_H_
|
29
EnhancedSolver/2D-seq/src/util.h
Normal file
29
EnhancedSolver/2D-seq/src/util.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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 __UTIL_H_
|
||||
#define __UTIL_H_
|
||||
#define HLINE \
|
||||
"----------------------------------------------------------------------------\n"
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(x, y) ((x) < (y) ? (x) : (y))
|
||||
#endif
|
||||
#ifndef MAX
|
||||
#define MAX(x, y) ((x) > (y) ? (x) : (y))
|
||||
#endif
|
||||
#ifndef ABS
|
||||
#define ABS(a) ((a) >= 0 ? (a) : -(a))
|
||||
#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_
|
125
EnhancedSolver/2D-seq/src/vtkWriter.c
Normal file
125
EnhancedSolver/2D-seq/src/vtkWriter.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/*
|
||||
* 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 <string.h>
|
||||
|
||||
#include "vtkWriter.h"
|
||||
|
||||
static float floatSwap(float f)
|
||||
{
|
||||
union {
|
||||
float f;
|
||||
char b[4];
|
||||
} dat1, dat2;
|
||||
|
||||
dat1.f = f;
|
||||
dat2.b[0] = dat1.b[3];
|
||||
dat2.b[1] = dat1.b[2];
|
||||
dat2.b[2] = dat1.b[1];
|
||||
dat2.b[3] = dat1.b[0];
|
||||
return dat2.f;
|
||||
}
|
||||
|
||||
static void writeHeader(VtkOptions* o, int ts)
|
||||
{
|
||||
fprintf(o->fh, "# vtk DataFile Version 3.0\n");
|
||||
fprintf(o->fh, "PAMPI cfd solver particle tracing file\n");
|
||||
if (o->fmt == ASCII) {
|
||||
fprintf(o->fh, "ASCII\n");
|
||||
} else if (o->fmt == BINARY) {
|
||||
fprintf(o->fh, "BINARY\n");
|
||||
}
|
||||
|
||||
fprintf(o->fh, "DATASET UNSTRUCTURED_GRID\n");
|
||||
fprintf(o->fh, "FIELD FieldData 2\n");
|
||||
fprintf(o->fh, "TIME 1 1 double\n");
|
||||
fprintf(o->fh, "%d\n", ts);
|
||||
fprintf(o->fh, "CYCLE 1 1 int\n");
|
||||
fprintf(o->fh, "1\n");
|
||||
}
|
||||
|
||||
void vtkOpen(VtkOptions* o, char* problem, int ts)
|
||||
{
|
||||
o->fh = fopen(problem, "w");
|
||||
|
||||
if (o->fh == NULL) {
|
||||
printf("vtkWriter not initialize! Call vtkOpen first!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
writeHeader(o, ts);
|
||||
|
||||
printf("Writing VTK output for %s\n", problem);
|
||||
}
|
||||
|
||||
void vtkParticle(VtkOptions* o, char* name)
|
||||
{
|
||||
Particle* particlePool = o->particletracer->particlePool;
|
||||
|
||||
int imax = o->particletracer->grid->imax;
|
||||
int jmax = o->particletracer->grid->jmax;
|
||||
|
||||
if (o->fh == NULL) {
|
||||
printf("vtkWriter not initialize! Call vtkOpen first!\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
fprintf(o->fh, "POINTS %d float\n", o->particletracer->totalParticles);
|
||||
|
||||
for (int i = 0; i < o->particletracer->totalParticles; ++i) {
|
||||
double x = particlePool[i].x;
|
||||
double y = particlePool[i].y;
|
||||
fprintf(o->fh, "%.2f %.2f 0.0\n", x, y);
|
||||
}
|
||||
|
||||
fprintf(o->fh,
|
||||
"CELLS %d %d\n",
|
||||
o->particletracer->totalParticles,
|
||||
2 * o->particletracer->totalParticles);
|
||||
|
||||
for (int i = 0; i < o->particletracer->totalParticles; ++i) {
|
||||
fprintf(o->fh, "1 %d\n", i);
|
||||
}
|
||||
|
||||
fprintf(o->fh, "CELL_TYPES %d\n", o->particletracer->totalParticles);
|
||||
|
||||
for (int i = 0; i < o->particletracer->totalParticles; ++i) {
|
||||
fprintf(o->fh, "1\n");
|
||||
}
|
||||
|
||||
/*
|
||||
for (int k = 0; k < kmax; k++) {
|
||||
for (int j = 0; j < jmax; j++) {
|
||||
for (int i = 0; i < imax; i++) {
|
||||
if (o->fmt == ASCII) {
|
||||
fprintf(o->fh,
|
||||
"%f %f %f\n",
|
||||
G(vec.u, i, j, k),
|
||||
G(vec.v, i, j, k),
|
||||
G(vec.w, i, j, k));
|
||||
} else if (o->fmt == BINARY) {
|
||||
fwrite((float[3]) { floatSwap(G(vec.u, i, j, k)),
|
||||
floatSwap(G(vec.v, i, j, k)),
|
||||
floatSwap(G(vec.w, i, j, k)) },
|
||||
sizeof(float),
|
||||
3,
|
||||
o->fh);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (o->fmt == BINARY) fprintf(o->fh, "\n");
|
||||
|
||||
*/
|
||||
}
|
||||
|
||||
void vtkClose(VtkOptions* o)
|
||||
{
|
||||
fclose(o->fh);
|
||||
o->fh = NULL;
|
||||
}
|
25
EnhancedSolver/2D-seq/src/vtkWriter.h
Normal file
25
EnhancedSolver/2D-seq/src/vtkWriter.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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 __VTKWRITER_H_
|
||||
#define __VTKWRITER_H_
|
||||
#include <stdio.h>
|
||||
|
||||
#include "particletracing.h"
|
||||
#include "solver.h"
|
||||
|
||||
typedef enum VtkFormat { ASCII = 0, BINARY } VtkFormat;
|
||||
|
||||
typedef struct VtkOptions {
|
||||
VtkFormat fmt;
|
||||
FILE* fh;
|
||||
ParticleTracer* particletracer;
|
||||
} VtkOptions;
|
||||
|
||||
extern void vtkOpen(VtkOptions* opts, char* filename, int ts);
|
||||
extern void vtkParticle(VtkOptions* opts, char* name);
|
||||
extern void vtkClose(VtkOptions* opts);
|
||||
#endif // __VTKWRITER_H_
|
7
EnhancedSolver/2D-seq/surface.plot
Normal file
7
EnhancedSolver/2D-seq/surface.plot
Normal file
@@ -0,0 +1,7 @@
|
||||
set terminal png size 1024,768 enhanced font ,12
|
||||
set output 'p.png'
|
||||
set datafile separator whitespace
|
||||
|
||||
set grid
|
||||
set hidden3d
|
||||
splot 'pressure.dat' using 1:2:3 with lines
|
7
EnhancedSolver/2D-seq/vector.plot
Normal file
7
EnhancedSolver/2D-seq/vector.plot
Normal file
@@ -0,0 +1,7 @@
|
||||
set terminal png size 3600,768 enhanced font ,28
|
||||
set output 'velocity.png'
|
||||
|
||||
set size ratio -1
|
||||
set datafile separator whitespace
|
||||
|
||||
plot 'velocity.dat' using 1:2:3:4:5 with vectors filled head size 0.01,20,60 lc palette
|
10
EnhancedSolver/2D-seq/vis_files/animate.plot
Normal file
10
EnhancedSolver/2D-seq/vis_files/animate.plot
Normal file
@@ -0,0 +1,10 @@
|
||||
unset border; unset tics; unset key;
|
||||
set term gif animate delay 30
|
||||
set output "trace.gif"
|
||||
set xrange [0:1]
|
||||
set yrange [0:1]
|
||||
|
||||
do for [ts=0:120] {
|
||||
plot "particles_".ts.".dat" with points pointtype 7
|
||||
}
|
||||
unset output
|
14
EnhancedSolver/2D-seq/vis_files/backstep_animate.plot
Normal file
14
EnhancedSolver/2D-seq/vis_files/backstep_animate.plot
Normal file
@@ -0,0 +1,14 @@
|
||||
unset border; unset tics; unset key;
|
||||
set term gif animate delay 10
|
||||
set output "trace.gif"
|
||||
set xrange [0:7]
|
||||
set yrange [0:1.5]
|
||||
set size ratio -1
|
||||
|
||||
set object 1 rect from 0.0,0.0 to 1.0,0.5 lw 5
|
||||
|
||||
|
||||
do for [ts=0:300] {
|
||||
plot "particles_".ts.".dat" with points pointtype 7
|
||||
}
|
||||
unset output
|
10
EnhancedSolver/2D-seq/vis_files/canal.plot
Normal file
10
EnhancedSolver/2D-seq/vis_files/canal.plot
Normal file
@@ -0,0 +1,10 @@
|
||||
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
|
13
EnhancedSolver/2D-seq/vis_files/karman_animate.plot
Normal file
13
EnhancedSolver/2D-seq/vis_files/karman_animate.plot
Normal file
@@ -0,0 +1,13 @@
|
||||
unset border; unset tics; unset key;
|
||||
set term gif animate delay 10
|
||||
set output "trace.gif"
|
||||
set xrange [0:30]
|
||||
set yrange [0:8]
|
||||
set size ratio -1
|
||||
set object 1 circle front at 5.0,4.0 size 1.0 fillcolor rgb "black" lw 2
|
||||
|
||||
|
||||
do for [ts=0:500] {
|
||||
plot "particles_".ts.".dat" with points pointtype 7 pointsize 0.3
|
||||
}
|
||||
unset output
|
Reference in New Issue
Block a user