Fully working BasicSolver

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

View File

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

View File

@ -1,10 +1,11 @@
# Supported: GCC, CLANG, ICC # Supported: GCC, CLANG, ICC
TAG ?= ICC TAG ?= ICC
# Supported: true, false
ENABLE_MPI ?= true ENABLE_MPI ?= true
ENABLE_OPENMP ?= false ENABLE_OPENMP ?= false
# Supported: rb, mg # Supported: rb, mg
SOLVER ?= mg SOLVER ?= mg
# Run in debug settings ?= mg # Supported: v1, v2, v3
COMM_TYPE ?= v3 COMM_TYPE ?= v3
#Feature options #Feature options

View File

@ -33,14 +33,14 @@ jmax 128 # number of interior cells in y-direction
# --------- # ---------
te 10.0 # final time te 10.0 # final time
dt 0.02 # time stepsize dt 0.02 # time stepsize
tau 0.5 # safety factor for time stepsize control (<0 constant delt) tau 0.5 # safety factor for time stepsize control (<0 constant delt)
# Multigrid data: # Multigrid data:
# --------- # ---------
levels 2 # Multigrid levels levels 2 # Multigrid levels
presmooth 20 # Pre-smoothning iterations presmooth 20 # Pre-smoothning iterations
postsmooth 5 # Post-smoothning iterations postsmooth 5 # Post-smoothning iterations
# Pressure Iteration Data: # Pressure Iteration Data:
@ -48,6 +48,6 @@ postsmooth 5 # Post-smoothning iterations
itermax 1000 # maximal number of pressure iteration in one time step itermax 1000 # maximal number of pressure iteration in one time step
eps 0.001 # stopping tolerance for pressure iteration eps 0.001 # stopping tolerance for pressure iteration
omg 1.9 # relaxation parameter for SOR iteration omg 1.7 # relaxation parameter for SOR iteration
gamma 0.9 # upwind differencing factor gamma gamma 0.9 # upwind differencing factor gamma
#=============================================================================== #===============================================================================

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

@ -14,7 +14,7 @@ static int sum(int* sizes, int position)
{ {
int sum = 0; int sum = 0;
for (int i = 0; i < position; i++) { for (int i = 0; i < position; i += position) {
sum += sizes[i]; sum += sizes[i];
} }
@ -67,6 +67,7 @@ int commIsBoundary(Comm* c, int direction)
void commExchange(Comm* c, double* grid) void commExchange(Comm* c, double* grid)
{ {
// printf("Rank : %d In exchange \n", c->rank);
#ifdef _MPI #ifdef _MPI
MPI_Request requests[4] = { MPI_REQUEST_NULL, MPI_Request requests[4] = { MPI_REQUEST_NULL,
MPI_REQUEST_NULL, MPI_REQUEST_NULL,
@ -174,6 +175,17 @@ void commPartition(Comm* c, int jmax, int imax)
#ifdef _MPI #ifdef _MPI
c->imaxLocal = imax; c->imaxLocal = imax;
c->jmaxLocal = sizeOfRank(c->coords[JDIM], c->size, jmax); c->jmaxLocal = sizeOfRank(c->coords[JDIM], c->size, jmax);
c->neighbours[BOTTOM] = c->rank == 0 ? -1 : c->rank - 1;
c->neighbours[TOP] = c->rank == (c->size - 1) ? -1 : c->rank + 1;
c->neighbours[LEFT] = -1;
c->neighbours[RIGHT] = -1;
c->coords[IDIM] = 0;
c->coords[JDIM] = c->rank;
c->dims[IDIM] = 1;
c->dims[JDIM] = c->size;
#else #else
c->imaxLocal = imax; c->imaxLocal = imax;
c->jmaxLocal = jmax; c->jmaxLocal = jmax;
@ -182,15 +194,33 @@ void commPartition(Comm* c, int jmax, int imax)
void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal) void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLocal)
{ {
Comm* newcomm;
#if defined _MPI #if defined _MPI
newcomm->comm = MPI_COMM_NULL; newcomm->comm = MPI_COMM_NULL;
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm); int result = MPI_Comm_dup(MPI_COMM_WORLD, &newcomm->comm);
if (result == MPI_ERR_COMM) { if (result == MPI_ERR_COMM) {
printf("\nNull communicator. Duplication failed !!\n"); printf("\nNull communicator. Duplication failed !!\n");
} }
newcomm->rank = oldcomm->rank;
newcomm->size = oldcomm->size;
newcomm->imaxLocal = imaxLocal / 2;
newcomm->jmaxLocal = jmaxLocal / 2;
newcomm->neighbours[BOTTOM] = newcomm->rank == 0 ? -1 : newcomm->rank - 1;
newcomm->neighbours[TOP] = newcomm->rank == (newcomm->size - 1) ? -1 : newcomm->rank + 1;
newcomm->neighbours[LEFT] = -1;
newcomm->neighbours[RIGHT] = -1;
newcomm->coords[IDIM] = 0;
newcomm->coords[JDIM] = newcomm->rank;
newcomm->dims[IDIM] = 1;
newcomm->dims[JDIM] = newcomm->size;
#endif #endif
newcomm->imaxLocal = imaxLocal; newcomm->imaxLocal = imaxLocal;
newcomm->jmaxLocal = jmaxLocal; newcomm->jmaxLocal = jmaxLocal;

View File

@ -4,18 +4,18 @@
* Use of this source code is governed by a MIT style * Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include "comm.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "comm.h"
#ifdef _MPI #ifdef _MPI
// subroutines local to this module // subroutines local to this module
static int sum(int* sizes, int position) static int sum(int* sizes, int init, int offset, int coord)
{ {
int sum = 0; int sum = 0;
for (int i = 0; i < position; i++) { for (int i = init - offset; coord > 0; i -= offset, --coord) {
sum += sizes[i]; sum += sizes[i];
} }
@ -79,8 +79,8 @@ static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] }; int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
int coords[NDIMS]; int coords[NDIMS];
MPI_Cart_coords(c->comm, i, NDIMS, coords); MPI_Cart_coords(c->comm, i, NDIMS, coords);
int starts[NDIMS] = { sum(newSizesJ, coords[JDIM]), int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
sum(newSizesI, coords[IDIM]) }; sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
printf( printf(
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d " "Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
"Starts(i,j): %d %d\n", "Starts(i,j): %d %d\n",
@ -290,14 +290,21 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
{ {
#if defined _MPI #if defined _MPI
newcomm->comm = MPI_COMM_NULL; newcomm->comm = MPI_COMM_NULL;
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm); int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
if (result == MPI_ERR_COMM) { if (result == MPI_ERR_COMM) {
printf("\nNull communicator. Duplication failed !!\n"); printf("\nNull communicator. Duplication failed !!\n");
} }
newcomm->imaxLocal = imaxLocal; newcomm->rank = oldcomm->rank;
newcomm->jmaxLocal = jmaxLocal; newcomm->size = oldcomm->size;
memcpy(&newcomm->neighbours, &oldcomm->neighbours, sizeof(oldcomm->neighbours));
memcpy(&newcomm->coords, &oldcomm->coords, sizeof(oldcomm->coords));
memcpy(&newcomm->dims, &oldcomm->dims, sizeof(oldcomm->dims));
newcomm->imaxLocal = imaxLocal/2;
newcomm->jmaxLocal = jmaxLocal/2;
MPI_Datatype jBufferType; MPI_Datatype jBufferType;
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType); MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
@ -329,7 +336,7 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
void commFreeCommunicator(Comm* comm) void commFreeCommunicator(Comm* comm)
{ {
#ifdef _MPI #ifdef _MPI
MPI_Comm_free(&comm->comm); MPI_Comm_free(&comm->comm);
#endif #endif
} }

View File

@ -11,11 +11,11 @@
#ifdef _MPI #ifdef _MPI
// subroutines local to this module // subroutines local to this module
static int sum(int* sizes, int position) static int sum(int* sizes, int init, int offset, int coord)
{ {
int sum = 0; int sum = 0;
for (int i = 0; i < position; i++) { for (int i = init - offset; coord > 0; i -= offset, --coord) {
sum += sizes[i]; sum += sizes[i];
} }
@ -79,8 +79,8 @@ static void assembleResult(Comm* c, double* src, double* dst, int imax, int jmax
int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] }; int newSizes[NDIMS] = { newSizesJ[i], newSizesI[i] };
int coords[NDIMS]; int coords[NDIMS];
MPI_Cart_coords(c->comm, i, NDIMS, coords); MPI_Cart_coords(c->comm, i, NDIMS, coords);
int starts[NDIMS] = { sum(newSizesJ, coords[JDIM]), int starts[NDIMS] = { sum(newSizesJ, i, 1, coords[JDIM]),
sum(newSizesI, coords[IDIM]) }; sum(newSizesI, i, c->dims[JDIM], coords[IDIM]) };
printf( printf(
"Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d " "Rank: %d, Coords(i,j): %d %d, Size(i,j): %d %d, Target Size(i,j): %d %d "
"Starts(i,j): %d %d\n", "Starts(i,j): %d %d\n",
@ -271,14 +271,18 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
{ {
#if defined _MPI #if defined _MPI
int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm); int result = MPI_Comm_dup(oldcomm->comm, &newcomm->comm);
if (result == MPI_ERR_COMM) { if (result == MPI_ERR_COMM) {
printf("\nNull communicator. Duplication failed !!\n"); printf("\nNull communicator. Duplication failed !!\n");
} }
newcomm->imaxLocal = imaxLocal; newcomm->rank = oldcomm->rank;
newcomm->jmaxLocal = jmaxLocal; newcomm->size = oldcomm->size;
newcomm->imaxLocal = imaxLocal / 2;
newcomm->jmaxLocal = jmaxLocal / 2;
MPI_Datatype jBufferType; MPI_Datatype jBufferType;
MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType); MPI_Type_contiguous(imaxLocal, MPI_DOUBLE, &jBufferType);
@ -310,7 +314,7 @@ void commUpdateDatatypes(Comm* oldcomm, Comm* newcomm, int imaxLocal, int jmaxLo
void commFreeCommunicator(Comm* comm) void commFreeCommunicator(Comm* comm)
{ {
#ifdef _MPI #ifdef _MPI
MPI_Comm_free(&comm->comm); MPI_Comm_free(&comm->comm);
#endif #endif
} }

View File

@ -49,6 +49,9 @@ int main(int argc, char** argv)
commInit(&d.comm, argc, argv); commInit(&d.comm, argc, argv);
initParameter(&p); initParameter(&p);
FILE* fp;
if (commIsMaster(&d.comm)) fp = initResidualWriter();
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <configFile>\n", argv[0]); printf("Usage: %s <configFile>\n", argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -79,16 +82,21 @@ int main(int argc, char** argv)
double tau = d.tau; double tau = d.tau;
double te = d.te; double te = d.te;
double t = 0.0; double t = 0.0;
double res = 0.0;
timeStart = getTimeStamp(); timeStart = getTimeStamp();
while (t <= te) { while (t <= te) {
if (tau > 0.0) computeTimestep(&d); if (tau > 0.0) computeTimestep(&d);
setBoundaryConditions(&d); setBoundaryConditions(&d);
setSpecialBoundaryCondition(&d); setSpecialBoundaryCondition(&d);
computeFG(&d); computeFG(&d);
computeRHS(&d); computeRHS(&d);
solve(&s, d.p, d.rhs); res = solve(&s, d.p, d.rhs);
adaptUV(&d); adaptUV(&d);
if (commIsMaster(&d.comm)) writeResidual(fp, t, res);
t += d.dt; t += d.dt;
#ifdef VERBOSE #ifdef VERBOSE
@ -106,7 +114,7 @@ int main(int argc, char** argv)
if (commIsMaster(s.comm)) { if (commIsMaster(s.comm)) {
printf("Solution took %.2fs\n", timeStop - timeStart); printf("Solution took %.2fs\n", timeStop - timeStart);
} }
if (commIsMaster(&d.comm)) fclose(fp);
writeResults(&d); writeResults(&d);
commFinalize(s.comm); commFinalize(s.comm);
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -8,7 +8,7 @@
#include <mpi.h> #include <mpi.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "progress.h" #include "progress.h"
static double _end; static double _end;
@ -49,3 +49,22 @@ void stopProgress()
printf("\n"); printf("\n");
fflush(stdout); 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

@ -10,5 +10,6 @@
extern void initProgress(double); extern void initProgress(double);
extern void printProgress(double); extern void printProgress(double);
extern void stopProgress(); extern void stopProgress();
extern FILE* initResidualWriter(void);
extern void writeResidual(FILE*, double, double);
#endif #endif

View File

@ -14,9 +14,9 @@
#define FINEST_LEVEL 0 #define FINEST_LEVEL 0
#define COARSEST_LEVEL (s->levels - 1) #define COARSEST_LEVEL (s->levels - 1)
// #define S(i, j) s[(j) * (imaxLocal + 2) + (i)] // #define S(i, j) s[(j) * (imaxLocal + 2) + (i)]
#define E(i, j) e[(j) * (imaxLocal + 2) + (i)] #define E(i, j) e[(j) * (imaxLocal + 2) + (i)]
#define R(i, j) r[(j) * (imaxLocal + 2) + (i)] #define R(i, j) r[(j) * (imaxLocal + 2) + (i)]
#define OLD(i, j) old[(j) * (imaxLocal + 2) + (i)] #define OLD(i, j) old[(j) * (imaxLocal + 2) + (i)]
static void restrictMG(Solver* s, int level, Comm* comm) static void restrictMG(Solver* s, int level, Comm* comm)
{ {
@ -109,7 +109,6 @@ static void setBoundaryCondition(Solver* s, double* p, int imaxLocal, int jmaxLo
#endif #endif
} }
static double smooth(Solver* s, double* p, double* rhs, int level, Comm* comm) static double smooth(Solver* s, double* p, double* rhs, int level, Comm* comm)
{ {
int imaxLocal = comm->imaxLocal; int imaxLocal = comm->imaxLocal;
@ -144,8 +143,7 @@ static double smooth(Solver* s, double* p, double* rhs, int level, Comm* comm)
P(i, j) -= factor * P(i, j) -= factor *
(RHS(i, j) - (RHS(i, j) -
((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 + ((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)) * (P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2));
idy2));
} }
isw = 3 - isw; isw = 3 - isw;
} }
@ -231,10 +229,10 @@ static double multiGrid(Solver* s, double* p, double* rhs, int level, Comm* comm
restrictMG(s, level, comm); restrictMG(s, level, comm);
Comm newcomm; Comm newcomm;
commUpdateDatatypes(s->comm, &newcomm, comm->imaxLocal / 2, comm->jmaxLocal / 2); commUpdateDatatypes(s->comm, &newcomm, comm->imaxLocal, comm->jmaxLocal);
// MGSolver on residual and error. // MGSolver on residual and error.
// TODO: What if there is a rest?
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm); multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm);
commFreeCommunicator(&newcomm); commFreeCommunicator(&newcomm);
@ -290,7 +288,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); double res = multiGrid(s, p, rhs, 0, s->comm);
@ -299,4 +297,6 @@ void solve(Solver* s, double* p, double* rhs)
printf("Residuum: %.6f\n", res); printf("Residuum: %.6f\n", res);
} }
#endif #endif
return res;
} }

View File

@ -23,7 +23,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
s->comm = &d->comm; s->comm = &d->comm;
} }
void solve(Solver* s, double* p, double* rhs) double solve(Solver* s, double* p, double* rhs)
{ {
int imax = s->grid->imax; int imax = s->grid->imax;
int jmax = s->grid->jmax; int jmax = s->grid->jmax;
@ -101,4 +101,6 @@ void solve(Solver* s, double* p, double* rhs)
printf("Solver took %d iterations to reach %f\n", it, sqrt(res)); printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
} }
#endif #endif
return res;
} }

View File

@ -25,5 +25,5 @@ typedef struct {
} Solver; } Solver;
void initSolver(Solver*, Discretization*, Parameter*); void initSolver(Solver*, Discretization*, Parameter*);
void solve(Solver*, double*, double*); double solve(Solver*, double*, double*);
#endif #endif

View File

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

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

@ -20,8 +20,10 @@ int main(int argc, char** argv)
Parameter p; Parameter p;
Discretization d; Discretization d;
Solver s; Solver s;
initParameter(&p); initParameter(&p);
FILE* fp;
fp = initResidualWriter();
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <configFile>\n", argv[0]); printf("Usage: %s <configFile>\n", argv[0]);
@ -32,7 +34,7 @@ int main(int argc, char** argv)
printParameter(&p); printParameter(&p);
initDiscretization(&d, &p); initDiscretization(&d, &p);
initSolver(&s, &d, &p); initSolver(&s, &d, &p);
#ifndef VERBOSE #ifndef VERBOSE
initProgress(d.te); initProgress(d.te);
#endif #endif
@ -41,8 +43,9 @@ int main(int argc, char** argv)
double te = d.te; double te = d.te;
double t = 0.0; double t = 0.0;
int nt = 0; int nt = 0;
double res = 0.0;
timeStart = getTimeStamp(); timeStart = getTimeStamp();
while (t <= te) { while (t <= te) {
if (tau > 0.0) computeTimestep(&d); if (tau > 0.0) computeTimestep(&d);
setBoundaryConditions(&d); setBoundaryConditions(&d);
@ -50,8 +53,11 @@ int main(int argc, char** argv)
computeFG(&d); computeFG(&d);
computeRHS(&d); computeRHS(&d);
if (nt % 100 == 0) normalizePressure(&d); if (nt % 100 == 0) normalizePressure(&d);
solve(&s, d.p, d.rhs); res = solve(&s, d.p, d.rhs);
adaptUV(&d); adaptUV(&d);
writeResidual(fp, t, res);
t += d.dt; t += d.dt;
nt++; nt++;
@ -61,6 +67,7 @@ int main(int argc, char** argv)
printProgress(t); printProgress(t);
#endif #endif
} }
fclose(fp);
timeStop = getTimeStamp(); timeStop = getTimeStamp();
stopProgress(); stopProgress();
printf("Solution took %.2fs\n", timeStop - timeStart); printf("Solution took %.2fs\n", timeStop - timeStart);

View File

@ -49,3 +49,22 @@ void stopProgress()
printf("\n"); printf("\n");
fflush(stdout); 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

@ -10,5 +10,7 @@
extern void initProgress(double); extern void initProgress(double);
extern void printProgress(double); extern void printProgress(double);
extern void stopProgress(void); extern void stopProgress(void);
extern FILE* initResidualWriter(void);
extern void writeResidual(FILE*, double, double);
#endif #endif

View File

@ -209,11 +209,13 @@ 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->grid->imax, s->grid->jmax); double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax);
#ifdef VERBOSE #ifdef VERBOSE
printf("Residuum: %.6f\n", res); printf("Residuum: %.6f\n", res);
#endif #endif
return res;
} }

View File

@ -15,7 +15,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
s->omega = p->omg; s->omega = p->omg;
} }
void solve(Solver* solver, double* p, double* rhs) double solve(Solver* solver, double* p, double* rhs)
{ {
int imax = solver->grid->imax; int imax = solver->grid->imax;
int jmax = solver->grid->jmax; int jmax = solver->grid->jmax;
@ -73,4 +73,6 @@ void solve(Solver* solver, double* p, double* rhs)
#ifdef VERBOSE #ifdef VERBOSE
printf("Solver took %d iterations to reach %f\n", it, sqrt(res)); printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
#endif #endif
return res;
} }

View File

@ -15,7 +15,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
s->omega = p->omg; s->omega = p->omg;
} }
void solve(Solver* solver, double* p, double* rhs) double solve(Solver* solver, double* p, double* rhs)
{ {
int imax = solver->grid->imax; int imax = solver->grid->imax;
int jmax = solver->grid->jmax; int jmax = solver->grid->jmax;
@ -65,4 +65,6 @@ void solve(Solver* solver, double* p, double* rhs)
#ifdef VERBOSE #ifdef VERBOSE
printf("Solver took %d iterations to reach %f\n", it, sqrt(res)); printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
#endif #endif
return res;
} }

View File

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

View File

@ -43,9 +43,19 @@ $(BUILD_DIR)/%.s: %.c
$(info ===> GENERATE ASM $@) $(info ===> GENERATE ASM $@)
$(CC) -S $(CPPFLAGS) $(CFLAGS) $< -o $@ $(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) $(info ===> CLEAN)
@rm -rf $(BUILD_DIR) @rm -rf $(BUILD_DIR)
@rm -f tags @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 ylength 4.0 # domain size in y-direction
zlength 4.0 # domain size in z-direction zlength 4.0 # domain size in z-direction
imax 200 # number of interior cells in x-direction imax 200 # number of interior cells in x-direction
jmax 50 # number of interior cells in y-direction jmax 40 # number of interior cells in y-direction
kmax 50 # number of interior cells in z-direction kmax 40 # number of interior cells in z-direction
# Time Data: # Time Data:
# --------- # ---------
@ -45,8 +45,8 @@ tau 0.5 # safety factor for time stepsize control (<0 constant delt)
# Multigrid data: # Multigrid data:
# --------- # ---------
levels 3 # Multigrid levels levels 2 # Multigrid levels
presmooth 5 # Pre-smoothning iterations presmooth 20 # Pre-smoothning iterations
postsmooth 5 # Post-smoothning iterations postsmooth 5 # Post-smoothning iterations
# Pressure Iteration Data: # Pressure Iteration Data:

View File

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

View File

@ -18,7 +18,7 @@ gx 0.0 # Body forces (e.g. gravity)
gy 0.0 # gy 0.0 #
gz 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 u_init 0.0 # initial value for velocity in x-direction
v_init 0.0 # initial value for velocity in y-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 levels 3 # Multigrid levels
presmooth 10 # Pre-smoothning iterations presmooth 20 # Pre-smoothning iterations
postsmooth 5 # Post-smoothning iterations postsmooth 5 # Post-smoothning iterations
# Pressure Iteration Data: # 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); 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; int sum = 0;
for (int i = 0; i < position; i++) { for (int i = init - offset; coord > 0; i -= offset, --coord) {
sum += sizes[i]; sum += sizes[i];
} }
@ -350,9 +351,13 @@ void commCollectResult(Comm* c,
for (int i = 0; i < c->size; i++) { for (int i = 0; i < c->size; i++) {
int coords[NCORDS]; int coords[NCORDS];
MPI_Cart_coords(c->comm, i, NDIMS, coords); MPI_Cart_coords(c->comm, i, NDIMS, coords);
offset[i * NDIMS + IDIM] = sum(imaxLocalAll, coords[ICORD]); offset[i * NDIMS + IDIM] = sum(imaxLocalAll,
offset[i * NDIMS + JDIM] = sum(jmaxLocalAll, coords[JCORD]); i,
offset[i * NDIMS + KDIM] = sum(kmaxLocalAll, coords[KCORD]); 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, " 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", "Offset(k,j,i): %d %d %d\n",
i, 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_shift(c->comm, KCORD, 1, &c->neighbours[FRONT], &c->neighbours[BACK]);
MPI_Cart_get(c->comm, NCORDS, c->dims, periods, c->coords); 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->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 // setup buffer types for communication
setupCommunication(c, LEFT, BULK); setupCommunication(c, LEFT, BULK);
@ -608,9 +613,12 @@ void commUpdateDatatypes(
printf("\nNull communicator. Duplication failed !!\n"); printf("\nNull communicator. Duplication failed !!\n");
} }
newcomm->imaxLocal = imaxLocal; newcomm->rank = oldcomm->rank;
newcomm->jmaxLocal = jmaxLocal; newcomm->size = oldcomm->size;
newcomm->kmaxLocal = kmaxLocal;
newcomm->imaxLocal = imaxLocal / 2;
newcomm->jmaxLocal = jmaxLocal / 2;
newcomm->kmaxLocal = kmaxLocal / 2;
setupCommunication(newcomm, LEFT, BULK); setupCommunication(newcomm, LEFT, BULK);
setupCommunication(newcomm, LEFT, HALO); setupCommunication(newcomm, LEFT, HALO);

View File

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

View File

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

View File

@ -4,12 +4,12 @@
* Use of this source code is governed by a MIT style * Use of this source code is governed by a MIT style
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include "progress.h"
#include <math.h> #include <math.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "progress.h"
static double _end; static double _end;
static int _current; static int _current;
@ -48,3 +48,18 @@ void stopProgress()
printf("\n"); printf("\n");
fflush(stdout); 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 * Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file. * license that can be found in the LICENSE file.
*/ */
#include <stdio.h>
#ifndef __PROGRESS_H_ #ifndef __PROGRESS_H_
#define __PROGRESS_H_ #define __PROGRESS_H_
extern void initProgress(double); extern void initProgress(double);
extern void printProgress(double); extern void printProgress(double);
extern void stopProgress(void); extern void stopProgress(void);
extern FILE* initResidualWriter(void);
extern void writeResidual(FILE*, double, double);
#endif #endif

View File

@ -328,9 +328,9 @@ static double multiGrid(Solver* s, double* p, double* rhs, int level, Comm* comm
Comm newcomm; Comm newcomm;
commUpdateDatatypes(s->comm, commUpdateDatatypes(s->comm,
&newcomm, &newcomm,
comm->imaxLocal / 2, imaxLocal,
comm->jmaxLocal / 2, jmaxLocal,
comm->kmaxLocal / 2); kmaxLocal);
// MGSolver on residual and error. // MGSolver on residual and error.
multiGrid(s, s->e[level + 1], s->r[level + 1], level + 1, &newcomm); 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 jmax = s->grid->jmax;
int kmax = s->grid->kmax; int kmax = s->grid->kmax;
int levels = s->levels; 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->r = malloc(levels * sizeof(double*));
s->e = 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); 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); printf("Residuum: %.6f\n", res);
} }
#endif #endif
return res;
} }

View File

@ -16,7 +16,20 @@
#include "solver.h" #include "solver.h"
#include "util.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 imaxLocal = s->comm->imaxLocal;
int jmaxLocal = s->comm->jmaxLocal; 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)); printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
} }
#endif #endif
}
void initSolver(Solver* s, Discretization* d, Parameter* p) return res;
{
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;
} }

View File

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

View File

@ -40,6 +40,18 @@ $(BUILD_DIR)/%.s: %.c
.PHONY: clean distclean tags info asm format .PHONY: clean distclean tags info asm format
vis:
$(info ===> GENERATE VISUALIZATION)
@gnuplot -e "filename='residual.dat'" ./residual.plot
vis_clean:
$(info ===> CLEAN VISUALIZATION)
@rm -f *.dat
@rm -f *.vtk
@rm -f *.png
clean: vis_clean
clean: clean:
$(info ===> CLEAN) $(info ===> CLEAN)
@rm -rf $(BUILD_DIR) @rm -rf $(BUILD_DIR)

View File

@ -38,7 +38,7 @@ kmax 50 # number of interior cells in z-direction
# Time Data: # Time Data:
# --------- # ---------
te 100.0 # final time te 60.0 # final time
dt 0.02 # time stepsize dt 0.02 # time stepsize
tau 0.5 # safety factor for time stepsize control (<0 constant delt) tau 0.5 # safety factor for time stepsize control (<0 constant delt)

View File

@ -38,7 +38,7 @@ kmax 128 # number of interior cells in z-direction
# Time Data: # Time Data:
# --------- # ---------
te 2.0 # final time te 10.0 # final time
dt 0.02 # time stepsize dt 0.02 # time stepsize
tau 0.5 # safety factor for time stepsize control (<0 constant delt) tau 0.5 # safety factor for time stepsize control (<0 constant delt)
@ -46,7 +46,7 @@ tau 0.5 # safety factor for time stepsize control (<0 constant delt)
# --------- # ---------
levels 3 # Multigrid levels levels 3 # Multigrid levels
presmooth 10 # Pre-smoothning iterations presmooth 20 # Pre-smoothning iterations
postsmooth 5 # Post-smoothning iterations postsmooth 5 # Post-smoothning iterations
# Pressure Iteration Data: # 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

@ -73,6 +73,9 @@ int main(int argc, char** argv)
Solver s; Solver s;
initParameter(&p); initParameter(&p);
FILE* fp;
fp = initResidualWriter();
if (argc != 2) { if (argc != 2) {
printf("Usage: %s <configFile>\n", argv[0]); printf("Usage: %s <configFile>\n", argv[0]);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
@ -90,6 +93,7 @@ int main(int argc, char** argv)
double te = d.te; double te = d.te;
double t = 0.0; double t = 0.0;
int nt = 0; int nt = 0;
double res = 0.0;
timeStart = getTimeStamp(); timeStart = getTimeStamp();
while (t <= te) { while (t <= te) {
@ -99,8 +103,11 @@ int main(int argc, char** argv)
computeFG(&d); computeFG(&d);
computeRHS(&d); computeRHS(&d);
if (nt % 100 == 0) normalizePressure(&d); if (nt % 100 == 0) normalizePressure(&d);
solve(&s, d.p, d.rhs); res = solve(&s, d.p, d.rhs);
adaptUV(&d); adaptUV(&d);
writeResidual(fp, t, res);
t += d.dt; t += d.dt;
nt++; nt++;
@ -123,7 +130,8 @@ int main(int argc, char** argv)
ug = allocate(64, bytesize); ug = allocate(64, bytesize);
vg = allocate(64, bytesize); vg = allocate(64, bytesize);
wg = allocate(64, bytesize); wg = allocate(64, bytesize);
fclose(fp);
createBulkArrays(&d, pg, ug, vg, wg); createBulkArrays(&d, pg, ug, vg, wg);
VtkOptions opts = { .grid = d.grid }; VtkOptions opts = { .grid = d.grid };
vtkOpen(&opts, d.problem); vtkOpen(&opts, d.problem);

View File

@ -49,3 +49,22 @@ void stopProgress()
printf("\n"); printf("\n");
fflush(stdout); 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

@ -10,5 +10,6 @@
extern void initProgress(double); extern void initProgress(double);
extern void printProgress(double); extern void printProgress(double);
extern void stopProgress(void); extern void stopProgress(void);
extern FILE* initResidualWriter(void);
extern void writeResidual(FILE*, double, double);
#endif #endif

View File

@ -292,11 +292,13 @@ 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->grid->imax, s->grid->jmax, s->grid->kmax); double res = multiGrid(s, p, rhs, 0, s->grid->imax, s->grid->jmax, s->grid->kmax);
#ifdef VERBOSE #ifdef VERBOSE
printf("Residuum: %.6f\n", res); printf("Residuum: %.6f\n", res);
#endif #endif
return res;
} }

View File

@ -15,7 +15,7 @@ void initSolver(Solver* s, Discretization* d, Parameter* p)
s->omega = p->omg; s->omega = p->omg;
} }
void solve(Solver* s, double* p, double* rhs) double solve(Solver* s, double* p, double* rhs)
{ {
int imax = s->grid->imax; int imax = s->grid->imax;
int jmax = s->grid->jmax; int jmax = s->grid->jmax;
@ -96,4 +96,6 @@ void solve(Solver* s, double* p, double* rhs)
#ifdef VERBOSE #ifdef VERBOSE
printf("Solver took %d iterations to reach %f\n", it, sqrt(res)); printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
#endif #endif
return res;
} }

View File

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