Fully working BasicSolver

This commit is contained in:
2024-07-24 22:56:05 +02:00
parent 312af6f663
commit d9361a8086
45 changed files with 372 additions and 110 deletions

View File

@@ -43,9 +43,19 @@ $(BUILD_DIR)/%.s: %.c
$(info ===> GENERATE ASM $@)
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@
.PHONY: clean distclean tags info asm format
.PHONY: clean distclean vis vis_clean tags info asm format
clean:
vis:
$(info ===> GENERATE VISUALIZATION)
@gnuplot -e "filename='residual.dat'" ./residual.plot
vis_clean:
$(info ===> CLEAN VISUALIZATION)
@rm -f *.dat
@rm -f *.vtk
@rm -f *.png
clean: vis_clean
$(info ===> CLEAN)
@rm -rf $(BUILD_DIR)
@rm -f tags

View File

@@ -32,8 +32,8 @@ xlength 30.0 # domain size in x-direction
ylength 4.0 # domain size in y-direction
zlength 4.0 # domain size in z-direction
imax 200 # number of interior cells in x-direction
jmax 50 # number of interior cells in y-direction
kmax 50 # number of interior cells in z-direction
jmax 40 # number of interior cells in y-direction
kmax 40 # number of interior cells in z-direction
# Time Data:
# ---------
@@ -45,8 +45,8 @@ tau 0.5 # safety factor for time stepsize control (<0 constant delt)
# Multigrid data:
# ---------
levels 3 # Multigrid levels
presmooth 5 # Pre-smoothning iterations
levels 2 # Multigrid levels
presmooth 20 # Pre-smoothning iterations
postsmooth 5 # Post-smoothning iterations
# Pressure Iteration Data:

View File

@@ -1,5 +1,6 @@
# Supported: GCC, CLANG, ICC
TAG ?= ICC
# Supported: true, false
ENABLE_MPI ?= true
ENABLE_OPENMP ?= false
# Supported: rb, mg

View File

@@ -18,7 +18,7 @@ gx 0.0 # Body forces (e.g. gravity)
gy 0.0 #
gz 0.0 #
re 1000.0 # Reynolds number
re 1000.0 # Reynolds number
u_init 0.0 # initial value for velocity in x-direction
v_init 0.0 # initial value for velocity in y-direction
@@ -46,7 +46,7 @@ tau 0.5 # safety factor for time stepsize control (<0 constant delt)
# ---------
levels 3 # Multigrid levels
presmooth 10 # Pre-smoothning iterations
presmooth 20 # Pre-smoothning iterations
postsmooth 5 # Post-smoothning iterations
# Pressure Iteration Data:

View File

@@ -0,0 +1,9 @@
set terminal png size 1800,768 enhanced font ,12
set output 'residual.png'
set datafile separator whitespace
set xlabel "Timestep"
set ylabel "Residual"
set logscale y 2
plot 'residual.dat' using 1:2 title "Residual"

View File

@@ -167,11 +167,12 @@ static void assembleResult(Comm* c,
MPI_Waitall(numRequests, requests, MPI_STATUSES_IGNORE);
}
static int sum(int* sizes, int position)
// subroutines local to this module
static int sum(int* sizes, int init, int offset, int coord)
{
int sum = 0;
for (int i = 0; i < position; i++) {
for (int i = init - offset; coord > 0; i -= offset, --coord) {
sum += sizes[i];
}
@@ -350,9 +351,13 @@ void commCollectResult(Comm* c,
for (int i = 0; i < c->size; i++) {
int coords[NCORDS];
MPI_Cart_coords(c->comm, i, NDIMS, coords);
offset[i * NDIMS + IDIM] = sum(imaxLocalAll, coords[ICORD]);
offset[i * NDIMS + JDIM] = sum(jmaxLocalAll, coords[JCORD]);
offset[i * NDIMS + KDIM] = sum(kmaxLocalAll, coords[KCORD]);
offset[i * NDIMS + IDIM] = sum(imaxLocalAll,
i,
c->dims[IDIM] * c->dims[JDIM],
coords[ICORD]);
offset[i * NDIMS + JDIM] = sum(jmaxLocalAll, i, c->dims[IDIM], coords[JCORD]);
offset[i * NDIMS + KDIM] = sum(kmaxLocalAll, i, 1, coords[KCORD]);
printf("Rank: %d, Coords(k,j,i): %d %d %d, Size(k,j,i): %d %d %d, "
"Offset(k,j,i): %d %d %d\n",
i,
@@ -561,9 +566,9 @@ void commPartition(Comm* c, int kmax, int jmax, int imax)
MPI_Cart_shift(c->comm, KCORD, 1, &c->neighbours[FRONT], &c->neighbours[BACK]);
MPI_Cart_get(c->comm, NCORDS, c->dims, periods, c->coords);
c->imaxLocal = sizeOfRank(c->coords[IDIM], dims[ICORD], imax);
c->imaxLocal = sizeOfRank(c->coords[KDIM], dims[ICORD], imax);
c->jmaxLocal = sizeOfRank(c->coords[JDIM], dims[JCORD], jmax);
c->kmaxLocal = sizeOfRank(c->coords[KDIM], dims[KCORD], kmax);
c->kmaxLocal = sizeOfRank(c->coords[IDIM], dims[KCORD], kmax);
// setup buffer types for communication
setupCommunication(c, LEFT, BULK);
@@ -608,9 +613,12 @@ void commUpdateDatatypes(
printf("\nNull communicator. Duplication failed !!\n");
}
newcomm->imaxLocal = imaxLocal;
newcomm->jmaxLocal = jmaxLocal;
newcomm->kmaxLocal = kmaxLocal;
newcomm->rank = oldcomm->rank;
newcomm->size = oldcomm->size;
newcomm->imaxLocal = imaxLocal / 2;
newcomm->jmaxLocal = jmaxLocal / 2;
newcomm->kmaxLocal = kmaxLocal / 2;
setupCommunication(newcomm, LEFT, BULK);
setupCommunication(newcomm, LEFT, HALO);

View File

@@ -25,6 +25,8 @@ int main(int argc, char** argv)
commInit(&d.comm, argc, argv);
initParameter(&p);
FILE* fp;
if (commIsMaster(&d.comm)) fp = initResidualWriter();
if (argc != 2) {
printf("Usage: %s <configFile>\n", argv[0]);
@@ -37,8 +39,8 @@ int main(int argc, char** argv)
if (commIsMaster(&d.comm)) {
printParameter(&p);
}
initDiscretization(&d, &p);
initSolver(&s, &d, &p);
#ifndef VERBOSE
@@ -49,6 +51,7 @@ int main(int argc, char** argv)
double te = d.te;
double t = 0.0;
int nt = 0;
double res = 0.0;
timeStart = getTimeStamp();
while (t <= te) {
@@ -58,8 +61,11 @@ int main(int argc, char** argv)
computeFG(&d);
computeRHS(&d);
if (nt % 100 == 0) normalizePressure(&d);
solve(&s, d.p, d.rhs);
res = solve(&s, d.p, d.rhs);
adaptUV(&d);
if (commIsMaster(&d.comm)) writeResidual(fp, t, res);
t += d.dt;
nt++;
@@ -87,6 +93,8 @@ int main(int argc, char** argv)
vtkVector(&opts, "velocity", (VtkVector) { d.u, d.v, d.w });
vtkClose(&opts);
#else
if (commIsMaster(&d.comm)) fclose(fp);
double *pg, *ug, *vg, *wg;
if (commIsMaster(s.comm)) {

View File

@@ -69,6 +69,9 @@ void readParameter(Parameter* param, const char* filename)
PARSE_INT(jmax);
PARSE_INT(kmax);
PARSE_INT(itermax);
PARSE_INT(levels);
PARSE_INT(presmooth);
PARSE_INT(postsmooth);
PARSE_REAL(eps);
PARSE_REAL(omg);
PARSE_REAL(re);

View File

@@ -4,12 +4,12 @@
* Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file.
*/
#include "progress.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "progress.h"
static double _end;
static int _current;
@@ -48,3 +48,18 @@ void stopProgress()
printf("\n");
fflush(stdout);
}
FILE* initResidualWriter()
{
FILE* fp;
fp = fopen("residual.dat", "w");
if (fp == NULL) {
printf("Error!\n");
exit(EXIT_FAILURE);
}
return fp;
}
void writeResidual(FILE* fp, double ts, double res) { fprintf(fp, "%f, %f\n", ts, res); }

View File

@@ -4,11 +4,14 @@
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file.
*/
#include <stdio.h>
#ifndef __PROGRESS_H_
#define __PROGRESS_H_
extern void initProgress(double);
extern void printProgress(double);
extern void stopProgress(void);
extern FILE* initResidualWriter(void);
extern void writeResidual(FILE*, double, double);
#endif

View File

@@ -328,9 +328,9 @@ static double multiGrid(Solver* s, double* p, double* rhs, int level, Comm* comm
Comm newcomm;
commUpdateDatatypes(s->comm,
&newcomm,
comm->imaxLocal / 2,
comm->jmaxLocal / 2,
comm->kmaxLocal / 2);
imaxLocal,
jmaxLocal,
kmaxLocal);
// MGSolver on residual and error.
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm);
@@ -371,7 +371,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
int jmax = s->grid->jmax;
int kmax = s->grid->kmax;
int levels = s->levels;
printf("Using Multigrid solver with %d levels\n", levels);
if (commIsMaster(s->comm)) printf("Using Multigrid solver with %d levels\n", levels);
s->r = malloc(levels * sizeof(double*));
s->e = malloc(levels * sizeof(double*));
@@ -389,7 +389,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
}
}
void solve(Solver* s, double* p, double* rhs)
double solve(Solver* s, double* p, double* rhs)
{
double res = multiGrid(s, p, rhs, 0, s->comm);
@@ -398,4 +398,6 @@ void solve(Solver* s, double* p, double* rhs)
printf("Residuum: %.6f\n", res);
}
#endif
return res;
}

View File

@@ -16,7 +16,20 @@
#include "solver.h"
#include "util.h"
void solve(Solver* s, double* p, double* rhs)
void initSolver(Solver* s, Discretization* d, Parameter* p)
{
s->eps = p->eps;
s->omega = p->omg;
s->itermax = p->itermax;
s->levels = p->levels;
s->grid = &d->grid;
s->presmooth = p->presmooth;
s->postsmooth = p->postsmooth;
s->comm = &d->comm;
s->problem = p->name;
}
double solve(Solver* s, double* p, double* rhs)
{
int imaxLocal = s->comm->imaxLocal;
int jmaxLocal = s->comm->jmaxLocal;
@@ -157,17 +170,6 @@ void solve(Solver* s, double* p, double* rhs)
printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
}
#endif
}
void initSolver(Solver* s, Discretization* d, Parameter* p)
{
s->eps = p->eps;
s->omega = p->omg;
s->itermax = p->itermax;
s->levels = p->levels;
s->grid = &d->grid;
s->presmooth = p->presmooth;
s->postsmooth = p->postsmooth;
s->comm = &d->comm;
s->problem = p->name;
return res;
}

View File

@@ -34,6 +34,6 @@ typedef struct {
Comm* comm;
} Solver;
extern void solve(Solver* , double* , double* );
extern double solve(Solver* , double* , double* );
extern void initSolver(Solver*, Discretization*, Parameter*);
#endif