forked from moebiusband/NuSiF-Solver
		
	Initial checkin
This commit is contained in:
		
							
								
								
									
										61
									
								
								BasicSolver/2D-seq/src/affinity.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								BasicSolver/2D-seq/src/affinity.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
 | 
			
		||||
 * All rights reserved.
 | 
			
		||||
 * Use of this source code is governed by a MIT-style
 | 
			
		||||
 * license that can be found in the LICENSE file.
 | 
			
		||||
 */
 | 
			
		||||
#ifdef __linux__
 | 
			
		||||
#ifdef _OPENMP
 | 
			
		||||
#include <pthread.h>
 | 
			
		||||
#include <sched.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <sys/syscall.h>
 | 
			
		||||
#include <sys/types.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#define MAX_NUM_THREADS 128
 | 
			
		||||
#define gettid()        syscall(SYS_gettid)
 | 
			
		||||
 | 
			
		||||
static int getProcessorID(cpu_set_t* cpu_set)
 | 
			
		||||
{
 | 
			
		||||
    int processorId;
 | 
			
		||||
 | 
			
		||||
    for (processorId = 0; processorId < MAX_NUM_THREADS; processorId++) {
 | 
			
		||||
        if (CPU_ISSET(processorId, cpu_set)) {
 | 
			
		||||
            break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return processorId;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int affinity_getProcessorId()
 | 
			
		||||
{
 | 
			
		||||
    cpu_set_t cpu_set;
 | 
			
		||||
    CPU_ZERO(&cpu_set);
 | 
			
		||||
    sched_getaffinity(gettid(), sizeof(cpu_set_t), &cpu_set);
 | 
			
		||||
 | 
			
		||||
    return getProcessorID(&cpu_set);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void affinity_pinThread(int processorId)
 | 
			
		||||
{
 | 
			
		||||
    cpu_set_t cpuset;
 | 
			
		||||
    pthread_t thread;
 | 
			
		||||
 | 
			
		||||
    thread = pthread_self();
 | 
			
		||||
    CPU_ZERO(&cpuset);
 | 
			
		||||
    CPU_SET(processorId, &cpuset);
 | 
			
		||||
    pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void affinity_pinProcess(int processorId)
 | 
			
		||||
{
 | 
			
		||||
    cpu_set_t cpuset;
 | 
			
		||||
 | 
			
		||||
    CPU_ZERO(&cpuset);
 | 
			
		||||
    CPU_SET(processorId, &cpuset);
 | 
			
		||||
    sched_setaffinity(0, sizeof(cpu_set_t), &cpuset);
 | 
			
		||||
}
 | 
			
		||||
#endif /*_OPENMP*/
 | 
			
		||||
#endif /*__linux__*/
 | 
			
		||||
							
								
								
									
										14
									
								
								BasicSolver/2D-seq/src/affinity.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								BasicSolver/2D-seq/src/affinity.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
 | 
			
		||||
 * All rights reserved.
 | 
			
		||||
 * Use of this source code is governed by a MIT-style
 | 
			
		||||
 * license that can be found in the LICENSE file.
 | 
			
		||||
 */
 | 
			
		||||
#ifndef AFFINITY_H
 | 
			
		||||
#define AFFINITY_H
 | 
			
		||||
 | 
			
		||||
extern int affinity_getProcessorId();
 | 
			
		||||
extern void affinity_pinProcess(int);
 | 
			
		||||
extern void affinity_pinThread(int);
 | 
			
		||||
 | 
			
		||||
#endif /*AFFINITY_H*/
 | 
			
		||||
							
								
								
									
										35
									
								
								BasicSolver/2D-seq/src/allocate.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								BasicSolver/2D-seq/src/allocate.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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 <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
 | 
			
		||||
void* allocate(int 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
									
								
								BasicSolver/2D-seq/src/allocate.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								BasicSolver/2D-seq/src/allocate.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,13 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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(int alignment, size_t bytesize);
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										54
									
								
								BasicSolver/2D-seq/src/likwid-marker.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								BasicSolver/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*/
 | 
			
		||||
							
								
								
									
										66
									
								
								BasicSolver/2D-seq/src/main.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								BasicSolver/2D-seq/src/main.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,66 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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 <float.h>
 | 
			
		||||
#include <limits.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <unistd.h>
 | 
			
		||||
 | 
			
		||||
#include "parameter.h"
 | 
			
		||||
#include "progress.h"
 | 
			
		||||
#include "solver.h"
 | 
			
		||||
#include "timing.h"
 | 
			
		||||
 | 
			
		||||
int main(int argc, char** argv)
 | 
			
		||||
{
 | 
			
		||||
    double S, E;
 | 
			
		||||
    Parameter params;
 | 
			
		||||
    Solver solver;
 | 
			
		||||
    initParameter(¶ms);
 | 
			
		||||
 | 
			
		||||
    if (argc != 2) {
 | 
			
		||||
        printf("Usage: %s <configFile>\n", argv[0]);
 | 
			
		||||
        exit(EXIT_SUCCESS);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    readParameter(¶ms, argv[1]);
 | 
			
		||||
    printParameter(¶ms);
 | 
			
		||||
    initSolver(&solver, ¶ms);
 | 
			
		||||
#ifndef VERBOSE
 | 
			
		||||
    initProgress(solver.te);
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
    double tau = solver.tau;
 | 
			
		||||
    double te  = solver.te;
 | 
			
		||||
    double t   = 0.0;
 | 
			
		||||
    int nt     = 0;
 | 
			
		||||
 | 
			
		||||
    S = getTimeStamp();
 | 
			
		||||
    while (t <= te) {
 | 
			
		||||
        if (tau > 0.0) computeTimestep(&solver);
 | 
			
		||||
        setBoundaryConditions(&solver);
 | 
			
		||||
        setSpecialBoundaryCondition(&solver);
 | 
			
		||||
        computeFG(&solver);
 | 
			
		||||
        computeRHS(&solver);
 | 
			
		||||
        if (nt % 100 == 0) normalizePressure(&solver);
 | 
			
		||||
        solve(&solver);
 | 
			
		||||
        adaptUV(&solver);
 | 
			
		||||
        t += solver.dt;
 | 
			
		||||
        nt++;
 | 
			
		||||
 | 
			
		||||
#ifdef VERBOSE
 | 
			
		||||
        printf("TIME %f , TIMESTEP %f\n", t, solver.dt);
 | 
			
		||||
#else
 | 
			
		||||
        printProgress(t);
 | 
			
		||||
#endif
 | 
			
		||||
    }
 | 
			
		||||
    E = getTimeStamp();
 | 
			
		||||
    stopProgress();
 | 
			
		||||
    printf("Solution took %.2fs\n", E - S);
 | 
			
		||||
    writeResult(&solver);
 | 
			
		||||
    return EXIT_SUCCESS;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										111
									
								
								BasicSolver/2D-seq/src/parameter.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										111
									
								
								BasicSolver/2D-seq/src/parameter.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,111 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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_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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    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);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										26
									
								
								BasicSolver/2D-seq/src/parameter.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										26
									
								
								BasicSolver/2D-seq/src/parameter.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,26 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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;
 | 
			
		||||
    double eps, omg;
 | 
			
		||||
    double re, tau, gamma;
 | 
			
		||||
    double te, dt;
 | 
			
		||||
    double gx, gy;
 | 
			
		||||
    char* name;
 | 
			
		||||
    int bcLeft, bcRight, bcBottom, bcTop;
 | 
			
		||||
    double u_init, v_init, p_init;
 | 
			
		||||
} Parameter;
 | 
			
		||||
 | 
			
		||||
void initParameter(Parameter*);
 | 
			
		||||
void readParameter(Parameter*, const char*);
 | 
			
		||||
void printParameter(Parameter*);
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										51
									
								
								BasicSolver/2D-seq/src/progress.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										51
									
								
								BasicSolver/2D-seq/src/progress.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,51 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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
									
								
								BasicSolver/2D-seq/src/progress.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								BasicSolver/2D-seq/src/progress.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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();
 | 
			
		||||
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										502
									
								
								BasicSolver/2D-seq/src/solver.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										502
									
								
								BasicSolver/2D-seq/src/solver.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,502 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
 | 
			
		||||
 * All rights reserved. This file is part of nusif-solver.
 | 
			
		||||
 * Use of this source code is governed by a MIT style
 | 
			
		||||
 * license that can be found in the LICENSE file.
 | 
			
		||||
 */
 | 
			
		||||
#include <float.h>
 | 
			
		||||
#include <math.h>
 | 
			
		||||
#include <stdio.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
 | 
			
		||||
#include "allocate.h"
 | 
			
		||||
#include "parameter.h"
 | 
			
		||||
#include "solver.h"
 | 
			
		||||
#include "util.h"
 | 
			
		||||
 | 
			
		||||
#define P(i, j)   p[(j) * (imax + 2) + (i)]
 | 
			
		||||
#define F(i, j)   f[(j) * (imax + 2) + (i)]
 | 
			
		||||
#define G(i, j)   g[(j) * (imax + 2) + (i)]
 | 
			
		||||
#define U(i, j)   u[(j) * (imax + 2) + (i)]
 | 
			
		||||
#define V(i, j)   v[(j) * (imax + 2) + (i)]
 | 
			
		||||
#define RHS(i, j) rhs[(j) * (imax + 2) + (i)]
 | 
			
		||||
 | 
			
		||||
static void print(Solver* solver, double* grid)
 | 
			
		||||
{
 | 
			
		||||
    int imax = solver->imax;
 | 
			
		||||
 | 
			
		||||
    for (int j = 0; j < solver->jmax + 2; j++) {
 | 
			
		||||
        printf("%02d: ", j);
 | 
			
		||||
        for (int i = 0; i < solver->imax + 2; i++) {
 | 
			
		||||
            printf("%12.8f  ", grid[j * (imax + 2) + i]);
 | 
			
		||||
        }
 | 
			
		||||
        printf("\n");
 | 
			
		||||
    }
 | 
			
		||||
    fflush(stdout);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static void printConfig(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    printf("Parameters for #%s#\n", solver->problem);
 | 
			
		||||
    printf("Boundary conditions Left:%d Right:%d Bottom:%d Top:%d\n",
 | 
			
		||||
        solver->bcLeft,
 | 
			
		||||
        solver->bcRight,
 | 
			
		||||
        solver->bcBottom,
 | 
			
		||||
        solver->bcTop);
 | 
			
		||||
    printf("\tReynolds number: %.2f\n", solver->re);
 | 
			
		||||
    printf("\tGx Gy: %.2f %.2f\n", solver->gx, solver->gy);
 | 
			
		||||
    printf("Geometry data:\n");
 | 
			
		||||
    printf("\tDomain box size (x, y): %.2f, %.2f\n", solver->xlength, solver->ylength);
 | 
			
		||||
    printf("\tCells (x, y): %d, %d\n", solver->imax, solver->jmax);
 | 
			
		||||
    printf("Timestep parameters:\n");
 | 
			
		||||
    printf("\tDefault stepsize: %.2f, Final time %.2f\n", solver->dt, solver->te);
 | 
			
		||||
    printf("\tdt bound: %.6f\n", solver->dtBound);
 | 
			
		||||
    printf("\tTau factor: %.2f\n", solver->tau);
 | 
			
		||||
    printf("Iterative solver parameters:\n");
 | 
			
		||||
    printf("\tMax iterations: %d\n", solver->itermax);
 | 
			
		||||
    printf("\tepsilon (stopping tolerance) : %f\n", solver->eps);
 | 
			
		||||
    printf("\tgamma factor: %f\n", solver->gamma);
 | 
			
		||||
    printf("\tomega (SOR relaxation): %f\n", solver->omega);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void initSolver(Solver* solver, Parameter* params)
 | 
			
		||||
{
 | 
			
		||||
    solver->problem  = params->name;
 | 
			
		||||
    solver->bcLeft   = params->bcLeft;
 | 
			
		||||
    solver->bcRight  = params->bcRight;
 | 
			
		||||
    solver->bcBottom = params->bcBottom;
 | 
			
		||||
    solver->bcTop    = params->bcTop;
 | 
			
		||||
    solver->imax     = params->imax;
 | 
			
		||||
    solver->jmax     = params->jmax;
 | 
			
		||||
    solver->xlength  = params->xlength;
 | 
			
		||||
    solver->ylength  = params->ylength;
 | 
			
		||||
    solver->dx       = params->xlength / params->imax;
 | 
			
		||||
    solver->dy       = params->ylength / params->jmax;
 | 
			
		||||
    solver->eps      = params->eps;
 | 
			
		||||
    solver->omega    = params->omg;
 | 
			
		||||
    solver->itermax  = params->itermax;
 | 
			
		||||
    solver->re       = params->re;
 | 
			
		||||
    solver->gx       = params->gx;
 | 
			
		||||
    solver->gy       = params->gy;
 | 
			
		||||
    solver->dt       = params->dt;
 | 
			
		||||
    solver->te       = params->te;
 | 
			
		||||
    solver->tau      = params->tau;
 | 
			
		||||
    solver->gamma    = params->gamma;
 | 
			
		||||
 | 
			
		||||
    int imax    = solver->imax;
 | 
			
		||||
    int jmax    = solver->jmax;
 | 
			
		||||
    size_t size = (imax + 2) * (jmax + 2) * sizeof(double);
 | 
			
		||||
    solver->u   = allocate(64, size);
 | 
			
		||||
    solver->v   = allocate(64, size);
 | 
			
		||||
    solver->p   = allocate(64, size);
 | 
			
		||||
    solver->rhs = allocate(64, size);
 | 
			
		||||
    solver->f   = allocate(64, size);
 | 
			
		||||
    solver->g   = allocate(64, size);
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < (imax + 2) * (jmax + 2); i++) {
 | 
			
		||||
        solver->u[i]   = params->u_init;
 | 
			
		||||
        solver->v[i]   = params->v_init;
 | 
			
		||||
        solver->p[i]   = params->p_init;
 | 
			
		||||
        solver->rhs[i] = 0.0;
 | 
			
		||||
        solver->f[i]   = 0.0;
 | 
			
		||||
        solver->g[i]   = 0.0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    double dx        = solver->dx;
 | 
			
		||||
    double dy        = solver->dy;
 | 
			
		||||
    double invSqrSum = 1.0 / (dx * dx) + 1.0 / (dy * dy);
 | 
			
		||||
    solver->dtBound  = 0.5 * solver->re * 1.0 / invSqrSum;
 | 
			
		||||
#ifdef VERBOSE
 | 
			
		||||
    printConfig(solver);
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void computeRHS(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int imax    = solver->imax;
 | 
			
		||||
    int jmax    = solver->jmax;
 | 
			
		||||
    double idx  = 1.0 / solver->dx;
 | 
			
		||||
    double idy  = 1.0 / solver->dy;
 | 
			
		||||
    double idt  = 1.0 / solver->dt;
 | 
			
		||||
    double* rhs = solver->rhs;
 | 
			
		||||
    double* f   = solver->f;
 | 
			
		||||
    double* g   = solver->g;
 | 
			
		||||
 | 
			
		||||
    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 solve(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int imax      = solver->imax;
 | 
			
		||||
    int jmax      = solver->jmax;
 | 
			
		||||
    double eps    = solver->eps;
 | 
			
		||||
    int itermax   = solver->itermax;
 | 
			
		||||
    double dx2    = solver->dx * solver->dx;
 | 
			
		||||
    double dy2    = solver->dy * solver->dy;
 | 
			
		||||
    double idx2   = 1.0 / dx2;
 | 
			
		||||
    double idy2   = 1.0 / dy2;
 | 
			
		||||
    double factor = solver->omega * 0.5 * (dx2 * dy2) / (dx2 + dy2);
 | 
			
		||||
    double* p     = solver->p;
 | 
			
		||||
    double* rhs   = solver->rhs;
 | 
			
		||||
    double epssq  = eps * eps;
 | 
			
		||||
    int it        = 0;
 | 
			
		||||
    double res    = 1.0;
 | 
			
		||||
 | 
			
		||||
    while ((res >= epssq) && (it < itermax)) {
 | 
			
		||||
        res = 0.0;
 | 
			
		||||
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
 | 
			
		||||
                double r = RHS(i, j) -
 | 
			
		||||
                           ((P(i + 1, j) - 2.0 * P(i, j) + P(i - 1, j)) * idx2 +
 | 
			
		||||
                               (P(i, j + 1) - 2.0 * P(i, j) + P(i, j - 1)) * idy2);
 | 
			
		||||
 | 
			
		||||
                P(i, j) -= (factor * r);
 | 
			
		||||
                res += (r * r);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            P(i, 0)        = P(i, 1);
 | 
			
		||||
            P(i, jmax + 1) = P(i, jmax);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            P(0, j)        = P(1, j);
 | 
			
		||||
            P(imax + 1, j) = P(imax, j);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        res = res / (double)(imax * jmax);
 | 
			
		||||
#ifdef DEBUG
 | 
			
		||||
        printf("%d Residuum: %e\n", it, res);
 | 
			
		||||
#endif
 | 
			
		||||
        it++;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
#ifdef VERBOSE
 | 
			
		||||
    printf("Solver took %d iterations to reach %f\n", it, sqrt(res));
 | 
			
		||||
#endif
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static double maxElement(Solver* solver, double* m)
 | 
			
		||||
{
 | 
			
		||||
    int size      = (solver->imax + 2) * (solver->jmax + 2);
 | 
			
		||||
    double maxval = DBL_MIN;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        maxval = MAX(maxval, fabs(m[i]));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return maxval;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void normalizePressure(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int size    = (solver->imax + 2) * (solver->jmax + 2);
 | 
			
		||||
    double* p   = solver->p;
 | 
			
		||||
    double avgP = 0.0;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        avgP += p[i];
 | 
			
		||||
    }
 | 
			
		||||
    avgP /= size;
 | 
			
		||||
 | 
			
		||||
    for (int i = 0; i < size; i++) {
 | 
			
		||||
        p[i] = p[i] - avgP;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void computeTimestep(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    double dt   = solver->dtBound;
 | 
			
		||||
    double dx   = solver->dx;
 | 
			
		||||
    double dy   = solver->dy;
 | 
			
		||||
    double umax = maxElement(solver, solver->u);
 | 
			
		||||
    double vmax = maxElement(solver, solver->v);
 | 
			
		||||
 | 
			
		||||
    if (umax > 0) {
 | 
			
		||||
        dt = (dt > dx / umax) ? dx / umax : dt;
 | 
			
		||||
    }
 | 
			
		||||
    if (vmax > 0) {
 | 
			
		||||
        dt = (dt > dy / vmax) ? dy / vmax : dt;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    solver->dt = dt * solver->tau;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setBoundaryConditions(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int imax  = solver->imax;
 | 
			
		||||
    int jmax  = solver->jmax;
 | 
			
		||||
    double* u = solver->u;
 | 
			
		||||
    double* v = solver->v;
 | 
			
		||||
 | 
			
		||||
    // Left boundary
 | 
			
		||||
    switch (solver->bcLeft) {
 | 
			
		||||
    case NOSLIP:
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            U(0, j) = 0.0;
 | 
			
		||||
            V(0, j) = -V(1, j);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case SLIP:
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            U(0, j) = 0.0;
 | 
			
		||||
            V(0, j) = V(1, j);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case OUTFLOW:
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            U(0, j) = U(1, j);
 | 
			
		||||
            V(0, j) = V(1, j);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case PERIODIC:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Right boundary
 | 
			
		||||
    switch (solver->bcRight) {
 | 
			
		||||
    case NOSLIP:
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            U(imax, j)     = 0.0;
 | 
			
		||||
            V(imax + 1, j) = -V(imax, j);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case SLIP:
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            U(imax, j)     = 0.0;
 | 
			
		||||
            V(imax + 1, j) = V(imax, j);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case OUTFLOW:
 | 
			
		||||
        for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
            U(imax, j)     = U(imax - 1, j);
 | 
			
		||||
            V(imax + 1, j) = V(imax, j);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case PERIODIC:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Bottom boundary
 | 
			
		||||
    switch (solver->bcBottom) {
 | 
			
		||||
    case NOSLIP:
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            V(i, 0) = 0.0;
 | 
			
		||||
            U(i, 0) = -U(i, 1);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case SLIP:
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            V(i, 0) = 0.0;
 | 
			
		||||
            U(i, 0) = U(i, 1);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case OUTFLOW:
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            U(i, 0) = U(i, 1);
 | 
			
		||||
            V(i, 0) = V(i, 1);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case PERIODIC:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Top boundary
 | 
			
		||||
    switch (solver->bcTop) {
 | 
			
		||||
    case NOSLIP:
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            V(i, jmax)     = 0.0;
 | 
			
		||||
            U(i, jmax + 1) = -U(i, jmax);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case SLIP:
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            V(i, jmax)     = 0.0;
 | 
			
		||||
            U(i, jmax + 1) = U(i, jmax);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case OUTFLOW:
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            U(i, jmax + 1) = U(i, jmax);
 | 
			
		||||
            V(i, jmax)     = V(i, jmax - 1);
 | 
			
		||||
        }
 | 
			
		||||
        break;
 | 
			
		||||
    case PERIODIC:
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setSpecialBoundaryCondition(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int imax   = solver->imax;
 | 
			
		||||
    int jmax   = solver->jmax;
 | 
			
		||||
    double mDy = solver->dy;
 | 
			
		||||
    double* u  = solver->u;
 | 
			
		||||
 | 
			
		||||
    if (strcmp(solver->problem, "dcavity") == 0) {
 | 
			
		||||
        for (int i = 1; i < imax; i++) {
 | 
			
		||||
            U(i, jmax + 1) = 2.0 - U(i, jmax);
 | 
			
		||||
        }
 | 
			
		||||
    } else if (strcmp(solver->problem, "canal") == 0) {
 | 
			
		||||
        double ylength = solver->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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void computeFG(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    double* u        = solver->u;
 | 
			
		||||
    double* v        = solver->v;
 | 
			
		||||
    double* f        = solver->f;
 | 
			
		||||
    double* g        = solver->g;
 | 
			
		||||
    int imax         = solver->imax;
 | 
			
		||||
    int jmax         = solver->jmax;
 | 
			
		||||
    double gx        = solver->gx;
 | 
			
		||||
    double gy        = solver->gy;
 | 
			
		||||
    double gamma     = solver->gamma;
 | 
			
		||||
    double dt        = solver->dt;
 | 
			
		||||
    double inverseRe = 1.0 / solver->re;
 | 
			
		||||
    double inverseDx = 1.0 / solver->dx;
 | 
			
		||||
    double inverseDy = 1.0 / solver->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++) {
 | 
			
		||||
            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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* ---------------------- boundary of F --------------------------- */
 | 
			
		||||
    for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
        F(0, j)    = U(0, j);
 | 
			
		||||
        F(imax, j) = U(imax, j);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /* ---------------------- boundary of G --------------------------- */
 | 
			
		||||
    for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
        G(i, 0)    = V(i, 0);
 | 
			
		||||
        G(i, jmax) = V(i, jmax);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void adaptUV(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int imax       = solver->imax;
 | 
			
		||||
    int jmax       = solver->jmax;
 | 
			
		||||
    double* p      = solver->p;
 | 
			
		||||
    double* u      = solver->u;
 | 
			
		||||
    double* v      = solver->v;
 | 
			
		||||
    double* f      = solver->f;
 | 
			
		||||
    double* g      = solver->g;
 | 
			
		||||
    double factorX = solver->dt / solver->dx;
 | 
			
		||||
    double factorY = solver->dt / solver->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(Solver* solver)
 | 
			
		||||
{
 | 
			
		||||
    int imax  = solver->imax;
 | 
			
		||||
    int jmax  = solver->jmax;
 | 
			
		||||
    double dx = solver->dx;
 | 
			
		||||
    double dy = solver->dy;
 | 
			
		||||
    double* p = solver->p;
 | 
			
		||||
    double* u = solver->u;
 | 
			
		||||
    double* v = solver->v;
 | 
			
		||||
    double x = 0.0, y = 0.0;
 | 
			
		||||
 | 
			
		||||
    FILE* fp;
 | 
			
		||||
    fp = fopen("pressure.dat", "w");
 | 
			
		||||
 | 
			
		||||
    if (fp == NULL) {
 | 
			
		||||
        printf("Error!\n");
 | 
			
		||||
        exit(EXIT_FAILURE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
        y = (double)(j - 0.5) * dy;
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            x = (double)(i - 0.5) * dx;
 | 
			
		||||
            fprintf(fp, "%.2f %.2f %f\n", x, y, P(i, j));
 | 
			
		||||
        }
 | 
			
		||||
        fprintf(fp, "\n");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fclose(fp);
 | 
			
		||||
 | 
			
		||||
    fp = fopen("velocity.dat", "w");
 | 
			
		||||
 | 
			
		||||
    if (fp == NULL) {
 | 
			
		||||
        printf("Error!\n");
 | 
			
		||||
        exit(EXIT_FAILURE);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    for (int j = 1; j < jmax + 1; j++) {
 | 
			
		||||
        y = dy * (j - 0.5);
 | 
			
		||||
        for (int i = 1; i < imax + 1; i++) {
 | 
			
		||||
            x            = dx * (i - 0.5);
 | 
			
		||||
            double vel_u = (U(i, j) + U(i - 1, j)) / 2.0;
 | 
			
		||||
            double vel_v = (V(i, j) + V(i, j - 1)) / 2.0;
 | 
			
		||||
            double len   = sqrt((vel_u * vel_u) + (vel_v * vel_v));
 | 
			
		||||
            fprintf(fp, "%.2f %.2f %f %f %f\n", x, y, vel_u, vel_v, len);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fclose(fp);
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										44
									
								
								BasicSolver/2D-seq/src/solver.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										44
									
								
								BasicSolver/2D-seq/src/solver.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,44 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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 "parameter.h"
 | 
			
		||||
 | 
			
		||||
enum BC { NOSLIP = 1, SLIP, OUTFLOW, PERIODIC };
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    /* geometry and grid information */
 | 
			
		||||
    double dx, dy;
 | 
			
		||||
    int imax, jmax;
 | 
			
		||||
    double xlength, ylength;
 | 
			
		||||
    /* arrays */
 | 
			
		||||
    double *p, *rhs;
 | 
			
		||||
    double *f, *g;
 | 
			
		||||
    double *u, *v;
 | 
			
		||||
    /* parameters */
 | 
			
		||||
    double eps, omega;
 | 
			
		||||
    double re, tau, gamma;
 | 
			
		||||
    double gx, gy;
 | 
			
		||||
    /* time stepping */
 | 
			
		||||
    int itermax;
 | 
			
		||||
    double dt, te;
 | 
			
		||||
    double dtBound;
 | 
			
		||||
    char* problem;
 | 
			
		||||
    int bcLeft, bcRight, bcBottom, bcTop;
 | 
			
		||||
} Solver;
 | 
			
		||||
 | 
			
		||||
void initSolver(Solver*, Parameter*);
 | 
			
		||||
void computeRHS(Solver*);
 | 
			
		||||
void solve(Solver*);
 | 
			
		||||
void normalizePressure(Solver*);
 | 
			
		||||
void computeTimestep(Solver*);
 | 
			
		||||
void setBoundaryConditions(Solver*);
 | 
			
		||||
void setSpecialBoundaryCondition(Solver*);
 | 
			
		||||
void computeFG(Solver*);
 | 
			
		||||
void adaptUV(Solver*);
 | 
			
		||||
void writeResult(Solver*);
 | 
			
		||||
#endif
 | 
			
		||||
							
								
								
									
										24
									
								
								BasicSolver/2D-seq/src/timing.c
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								BasicSolver/2D-seq/src/timing.c
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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()
 | 
			
		||||
{
 | 
			
		||||
    struct timespec ts;
 | 
			
		||||
    clock_gettime(CLOCK_MONOTONIC, &ts);
 | 
			
		||||
    return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
double getTimeResolution()
 | 
			
		||||
{
 | 
			
		||||
    struct timespec ts;
 | 
			
		||||
    clock_getres(CLOCK_MONOTONIC, &ts);
 | 
			
		||||
    return (double)ts.tv_sec + (double)ts.tv_nsec * 1.e-9;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
double getTimeStamp_() { return getTimeStamp(); }
 | 
			
		||||
							
								
								
									
										14
									
								
								BasicSolver/2D-seq/src/timing.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								BasicSolver/2D-seq/src/timing.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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();
 | 
			
		||||
extern double getTimeResolution();
 | 
			
		||||
extern double getTimeStamp_();
 | 
			
		||||
 | 
			
		||||
#endif // __TIMING_H_
 | 
			
		||||
							
								
								
									
										23
									
								
								BasicSolver/2D-seq/src/util.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								BasicSolver/2D-seq/src/util.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
/*
 | 
			
		||||
 * Copyright (C) 2022 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
 | 
			
		||||
 | 
			
		||||
#endif // __UTIL_H_
 | 
			
		||||
		Reference in New Issue
	
	Block a user