mirror of
https://git.gfz-potsdam.de/naaice/tug.git
synced 2025-12-16 02:48:23 +01:00
refactor: format all source files to LLVM standard
This commit is contained in:
parent
81774e72c1
commit
0d34752837
@ -14,7 +14,6 @@ int main(int argc, char *argv[]) {
|
||||
// TODO add option to set concentrations with a vector in 1D case
|
||||
grid.setConcentrations(concentrations);
|
||||
|
||||
|
||||
// ******************
|
||||
// **** BOUNDARY ****
|
||||
// ******************
|
||||
@ -24,13 +23,13 @@ int main(int argc, char *argv[]) {
|
||||
bc.setBoundarySideConstant(BC_SIDE_LEFT, 0);
|
||||
bc.setBoundarySideConstant(BC_SIDE_RIGHT, 0);
|
||||
|
||||
|
||||
// ************************
|
||||
// **** SIMULATION ENV ****
|
||||
// ************************
|
||||
|
||||
// set up a simulation environment
|
||||
Simulation simulation = Simulation(grid, bc, BTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
Simulation simulation =
|
||||
Simulation(grid, bc, BTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
|
||||
// set the timestep of the simulation
|
||||
simulation.setTimestep(0.1); // timestep
|
||||
@ -38,7 +37,8 @@ int main(int argc, char *argv[]) {
|
||||
// set the number of iterations
|
||||
simulation.setIterations(100);
|
||||
|
||||
// set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE]
|
||||
// set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON,
|
||||
// CSV_OUTPUT_VERBOSE]
|
||||
simulation.setOutputCSV(CSV_OUTPUT_VERBOSE);
|
||||
|
||||
// **** RUN SIMULATION ****
|
||||
|
||||
@ -16,19 +16,17 @@ int main(int argc, char *argv[]) {
|
||||
// grid.setDomain(20, 20);
|
||||
|
||||
// (optional) set the concentrations, e.g.:
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); // #row,#col,value
|
||||
// grid.setConcentrations(concentrations);
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); //
|
||||
// #row,#col,value grid.setConcentrations(concentrations);
|
||||
MatrixXd concentrations = MatrixXd::Constant(row, col, 0);
|
||||
concentrations(10, 10) = 2000;
|
||||
grid.setConcentrations(concentrations);
|
||||
|
||||
|
||||
// (optional) set alphas of the grid, e.g.:
|
||||
// MatrixXd alphax = MatrixXd::Constant(20,20,1); // row,col,value
|
||||
// MatrixXd alphay = MatrixXd::Constant(20,20,1); // row,col,value
|
||||
// grid.setAlpha(alphax, alphay);
|
||||
|
||||
|
||||
// ******************
|
||||
// **** BOUNDARY ****
|
||||
// ******************
|
||||
@ -44,7 +42,6 @@ int main(int argc, char *argv[]) {
|
||||
// bc.setBoundarySideConstant(BC_SIDE_TOP, 0);
|
||||
// bc.setBoundarySideConstant(BC_SIDE_BOTTOM, 0);
|
||||
|
||||
|
||||
// (optional) set boundary condition values for one side, e.g.:
|
||||
// VectorXd bc_left_values = VectorXd::Constant(20,1); // length,value
|
||||
// bc.setBoundaryConditionValue(BC_SIDE_LEFT, bc_left_values); // side,values
|
||||
@ -55,13 +52,13 @@ int main(int argc, char *argv[]) {
|
||||
// bc.setBoundaryConditionValue(BC_SIDE_TOP, bc_front_values);
|
||||
// bc.setBoundaryConditionValue(BC_SIDE_BOTTOM, bc_zero_values);
|
||||
|
||||
|
||||
// ************************
|
||||
// **** SIMULATION ENV ****
|
||||
// ************************
|
||||
|
||||
// set up a simulation environment
|
||||
Simulation simulation = Simulation(grid, bc, BTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
Simulation simulation =
|
||||
Simulation(grid, bc, BTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
|
||||
// set the timestep of the simulation
|
||||
simulation.setTimestep(0.1); // timestep
|
||||
@ -69,7 +66,8 @@ int main(int argc, char *argv[]) {
|
||||
// set the number of iterations
|
||||
simulation.setIterations(300);
|
||||
|
||||
// set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE]
|
||||
// set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON,
|
||||
// CSV_OUTPUT_VERBOSE]
|
||||
simulation.setOutputCSV(CSV_OUTPUT_XTREME);
|
||||
|
||||
// **** RUN SIMULATION ****
|
||||
|
||||
@ -13,7 +13,6 @@ int main(int argc, char *argv[]) {
|
||||
MatrixXd concentrations = MatrixXd::Constant(1, 20, 20);
|
||||
grid.setConcentrations(concentrations);
|
||||
|
||||
|
||||
// ******************
|
||||
// **** BOUNDARY ****
|
||||
// ******************
|
||||
@ -23,13 +22,13 @@ int main(int argc, char *argv[]) {
|
||||
bc.setBoundarySideConstant(BC_SIDE_LEFT, 1);
|
||||
bc.setBoundarySideConstant(BC_SIDE_RIGHT, 1);
|
||||
|
||||
|
||||
// ************************
|
||||
// **** SIMULATION ENV ****
|
||||
// ************************
|
||||
|
||||
// set up a simulation environment
|
||||
Simulation simulation = Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
Simulation simulation =
|
||||
Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
|
||||
// (optional) set the timestep of the simulation
|
||||
simulation.setTimestep(0.1); // timestep
|
||||
@ -37,7 +36,8 @@ int main(int argc, char *argv[]) {
|
||||
// (optional) set the number of iterations
|
||||
simulation.setIterations(100);
|
||||
|
||||
// (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE]
|
||||
// (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON,
|
||||
// CSV_OUTPUT_VERBOSE]
|
||||
simulation.setOutputCSV(CSV_OUTPUT_OFF);
|
||||
|
||||
// **** RUN SIMULATION ****
|
||||
|
||||
@ -1,7 +1,8 @@
|
||||
/**
|
||||
* @file FTCS_2D_proto_closed_mdl.cpp
|
||||
* @author Hannes Signer, Philipp Ungrund, MDL
|
||||
* @brief Creates a TUG simulation in 2D with FTCS approach and closed boundary condition; optional command line argument: number of cols and rows
|
||||
* @brief Creates a TUG simulation in 2D with FTCS approach and closed boundary
|
||||
* condition; optional command line argument: number of cols and rows
|
||||
*
|
||||
*/
|
||||
|
||||
@ -32,7 +33,8 @@ int main(int argc, char *argv[]) {
|
||||
// grid.setDomain(20, 20);
|
||||
|
||||
// (optional) set the concentrations, e.g.:
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); // #row,#col,value
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); //
|
||||
// #row,#col,value
|
||||
MatrixXd concentrations = MatrixXd::Constant(row, col, 0);
|
||||
concentrations(n2, n2) = 1;
|
||||
concentrations(n2, n2 + 1) = 1;
|
||||
@ -45,7 +47,6 @@ int main(int argc, char *argv[]) {
|
||||
MatrixXd alphay = MatrixXd::Constant(row, col, 1E-6); // row,col,value
|
||||
grid.setAlpha(alphax, alphay);
|
||||
|
||||
|
||||
// ******************
|
||||
// **** BOUNDARY ****
|
||||
// ******************
|
||||
@ -59,13 +60,13 @@ int main(int argc, char *argv[]) {
|
||||
bc.setBoundarySideClosed(BC_SIDE_TOP);
|
||||
bc.setBoundarySideClosed(BC_SIDE_BOTTOM);
|
||||
|
||||
|
||||
// ************************
|
||||
// **** SIMULATION ENV ****
|
||||
// ************************
|
||||
|
||||
// set up a simulation environment
|
||||
Simulation simulation = Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
Simulation simulation =
|
||||
Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
|
||||
// set the timestep of the simulation
|
||||
simulation.setTimestep(10000); // timestep
|
||||
@ -73,7 +74,8 @@ int main(int argc, char *argv[]) {
|
||||
// set the number of iterations
|
||||
simulation.setIterations(100);
|
||||
|
||||
// (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE]
|
||||
// (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON,
|
||||
// CSV_OUTPUT_VERBOSE]
|
||||
simulation.setOutputCSV(CSV_OUTPUT_VERBOSE);
|
||||
|
||||
// **** RUN SIMULATION ****
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* @file FTCS_2D_proto_example.cpp
|
||||
* @author Hannes Signer, Philipp Ungrund
|
||||
* @brief Creates a prototypical standard TUG simulation in 2D with FTCS approach
|
||||
* and constant boundary condition
|
||||
* @brief Creates a prototypical standard TUG simulation in 2D with FTCS
|
||||
* approach and constant boundary condition
|
||||
*
|
||||
*/
|
||||
|
||||
@ -10,7 +10,6 @@
|
||||
// #include <easy/profiler.h>
|
||||
// #define EASY_PROFILER_ENABLE ::profiler::setEnabled(true);
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
// EASY_PROFILER_ENABLE;
|
||||
// profiler::startListen();
|
||||
@ -27,19 +26,17 @@ int main(int argc, char *argv[]) {
|
||||
// grid.setDomain(20, 20);
|
||||
|
||||
// (optional) set the concentrations, e.g.:
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); // #row,#col,value
|
||||
// grid.setConcentrations(concentrations);
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); //
|
||||
// #row,#col,value grid.setConcentrations(concentrations);
|
||||
MatrixXd concentrations = MatrixXd::Constant(row, col, 0);
|
||||
concentrations(0, 0) = 1999;
|
||||
grid.setConcentrations(concentrations);
|
||||
|
||||
|
||||
// (optional) set alphas of the grid, e.g.:
|
||||
// MatrixXd alphax = MatrixXd::Constant(20,20,1); // row,col,value
|
||||
// MatrixXd alphay = MatrixXd::Constant(20,20,1); // row,col,value
|
||||
// grid.setAlpha(alphax, alphay);
|
||||
|
||||
|
||||
// ******************
|
||||
// **** BOUNDARY ****
|
||||
// ******************
|
||||
@ -51,7 +48,6 @@ int main(int argc, char *argv[]) {
|
||||
bc.setBoundarySideConstant(BC_SIDE_TOP, 0);
|
||||
bc.setBoundarySideConstant(BC_SIDE_BOTTOM, 0);
|
||||
|
||||
|
||||
// (optional) set boundary condition values for one side, e.g.:
|
||||
// VectorXd bc_left_values = VectorXd::Constant(20,1); // length,value
|
||||
// bc.setBoundaryConditionValue(BC_SIDE_LEFT, bc_left_values); // side,values
|
||||
@ -62,13 +58,13 @@ int main(int argc, char *argv[]) {
|
||||
// bc.setBoundaryConditionValue(BC_SIDE_TOP, bc_front_values);
|
||||
// bc.setBoundaryConditionValue(BC_SIDE_BOTTOM, bc_zero_values);
|
||||
|
||||
|
||||
// ************************
|
||||
// **** SIMULATION ENV ****
|
||||
// ************************
|
||||
|
||||
// set up a simulation environment
|
||||
Simulation simulation = Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
Simulation simulation =
|
||||
Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
|
||||
// set the timestep of the simulation
|
||||
simulation.setTimestep(0.1); // timestep
|
||||
@ -76,10 +72,10 @@ int main(int argc, char *argv[]) {
|
||||
// set the number of iterations
|
||||
simulation.setIterations(10000);
|
||||
|
||||
// set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE]
|
||||
// set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON,
|
||||
// CSV_OUTPUT_VERBOSE]
|
||||
simulation.setOutputCSV(CSV_OUTPUT_VERBOSE);
|
||||
|
||||
|
||||
// **** RUN SIMULATION ****
|
||||
|
||||
// run the simulation
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* @file FTCS_2D_proto_example.cpp
|
||||
* @author Hannes Signer, Philipp Ungrund
|
||||
* @brief Creates a prototypical standard TUG simulation in 2D with FTCS approach
|
||||
* and constant boundary condition
|
||||
* @brief Creates a prototypical standard TUG simulation in 2D with FTCS
|
||||
* approach and constant boundary condition
|
||||
*
|
||||
*/
|
||||
|
||||
@ -24,7 +24,8 @@ int main(int argc, char *argv[]) {
|
||||
// grid.setDomain(20, 20);
|
||||
|
||||
// (optional) set the concentrations, e.g.:
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); // #row,#col,value
|
||||
// MatrixXd concentrations = MatrixXd::Constant(20,20,1000); //
|
||||
// #row,#col,value
|
||||
MatrixXd concentrations = MatrixXd::Constant(row, col, 0);
|
||||
concentrations(n2, n2) = 1;
|
||||
concentrations(n2, n2 + 1) = 1;
|
||||
@ -37,7 +38,6 @@ int main(int argc, char *argv[]) {
|
||||
MatrixXd alphay = MatrixXd::Constant(row, col, 1E-6); // row,col,value
|
||||
grid.setAlpha(alphax, alphay);
|
||||
|
||||
|
||||
// ******************
|
||||
// **** BOUNDARY ****
|
||||
// ******************
|
||||
@ -51,13 +51,13 @@ int main(int argc, char *argv[]) {
|
||||
bc.setBoundarySideConstant(BC_SIDE_TOP, 0);
|
||||
bc.setBoundarySideConstant(BC_SIDE_BOTTOM, 0);
|
||||
|
||||
|
||||
// ************************
|
||||
// **** SIMULATION ENV ****
|
||||
// ************************
|
||||
|
||||
// set up a simulation environment
|
||||
Simulation simulation = Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
Simulation simulation =
|
||||
Simulation(grid, bc, FTCS_APPROACH); // grid,boundary,simulation-approach
|
||||
|
||||
// (optional) set the timestep of the simulation
|
||||
simulation.setTimestep(1000); // timestep
|
||||
@ -65,7 +65,8 @@ int main(int argc, char *argv[]) {
|
||||
// (optional) set the number of iterations
|
||||
simulation.setIterations(5);
|
||||
|
||||
// (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON, CSV_OUTPUT_VERBOSE]
|
||||
// (optional) set kind of output [CSV_OUTPUT_OFF (default), CSV_OUTPUT_ON,
|
||||
// CSV_OUTPUT_VERBOSE]
|
||||
simulation.setOutputCSV(CSV_OUTPUT_OFF);
|
||||
|
||||
// **** RUN SIMULATION ****
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tug/Simulation.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
@ -11,7 +11,6 @@ int main(int argc, char *argv[]) {
|
||||
int iterations[1] = {1};
|
||||
int repetition = 10;
|
||||
|
||||
|
||||
for (int l = 0; l < size(threads); l++) {
|
||||
// string filename = "ftcs_openmp_" + to_string(threads[l]) + ".csv";
|
||||
ofstream myfile;
|
||||
@ -42,8 +41,7 @@ int main(int argc, char *argv[]) {
|
||||
if (argc == 2) {
|
||||
int numThreads = atoi(argv[1]);
|
||||
sim.setNumberThreads(numThreads);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
sim.setNumberThreads(threads[l]);
|
||||
}
|
||||
|
||||
@ -54,13 +52,14 @@ int main(int argc, char *argv[]) {
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
sim.run();
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
|
||||
auto milliseconds =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end -
|
||||
begin);
|
||||
myfile << milliseconds.count() << endl;
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
myfile << endl;
|
||||
|
||||
}
|
||||
myfile.close();
|
||||
}
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <tug/Simulation.hpp>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <chrono>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
||||
@ -11,7 +11,6 @@ int main(int argc, char *argv[]) {
|
||||
int iterations[1] = {1};
|
||||
int repetition = 10;
|
||||
|
||||
|
||||
for (int l = 0; l < size(threads); l++) {
|
||||
// string filename = "ftcs_openmp_" + to_string(threads[l]) + ".csv";
|
||||
ofstream myfile;
|
||||
@ -42,8 +41,7 @@ int main(int argc, char *argv[]) {
|
||||
if (argc == 2) {
|
||||
int numThreads = atoi(argv[1]);
|
||||
sim.setNumberThreads(numThreads);
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
sim.setNumberThreads(threads[l]);
|
||||
}
|
||||
|
||||
@ -54,13 +52,14 @@ int main(int argc, char *argv[]) {
|
||||
auto begin = std::chrono::high_resolution_clock::now();
|
||||
sim.run();
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
|
||||
auto milliseconds =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end -
|
||||
begin);
|
||||
myfile << milliseconds.count() << endl;
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
myfile << endl;
|
||||
|
||||
}
|
||||
myfile.close();
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#include <tug/Simulation.hpp>
|
||||
#include "Eigen/Core"
|
||||
#include <iostream>
|
||||
#include <tug/Simulation.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -10,7 +10,6 @@ int main(int argc, char *argv[]) {
|
||||
int domain_row = 10;
|
||||
int domain_col = 10;
|
||||
|
||||
|
||||
// Grid
|
||||
Grid grid = Grid(row, col);
|
||||
grid.setDomain(domain_row, domain_col);
|
||||
@ -37,11 +36,9 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
grid.setAlpha(alpha, alpha);
|
||||
|
||||
|
||||
// Boundary
|
||||
Boundary bc = Boundary(grid);
|
||||
|
||||
|
||||
// Simulation
|
||||
Simulation sim = Simulation(grid, bc, FTCS_APPROACH);
|
||||
sim.setTimestep(0.001);
|
||||
@ -49,7 +46,6 @@ int main(int argc, char *argv[]) {
|
||||
sim.setOutputCSV(CSV_OUTPUT_OFF);
|
||||
sim.setOutputConsole(CONSOLE_OUTPUT_OFF);
|
||||
|
||||
|
||||
// RUN
|
||||
sim.run();
|
||||
}
|
||||
@ -1,14 +1,14 @@
|
||||
/**
|
||||
* @file Boundary.hpp
|
||||
* @brief API of Boundary class, that holds all information for each boundary condition
|
||||
* at the edges of the diffusion grid.
|
||||
* @brief API of Boundary class, that holds all information for each boundary
|
||||
* condition at the edges of the diffusion grid.
|
||||
*
|
||||
*/
|
||||
#ifndef BOUNDARY_H_
|
||||
#define BOUNDARY_H_
|
||||
|
||||
#include <cstddef>
|
||||
#include "Grid.hpp"
|
||||
#include <cstddef>
|
||||
|
||||
using namespace std;
|
||||
using namespace Eigen;
|
||||
@ -17,21 +17,13 @@ using namespace Eigen;
|
||||
* @brief Enum defining the two implemented boundary conditions.
|
||||
*
|
||||
*/
|
||||
enum BC_TYPE {
|
||||
BC_TYPE_CLOSED,
|
||||
BC_TYPE_CONSTANT
|
||||
};
|
||||
enum BC_TYPE { BC_TYPE_CLOSED, BC_TYPE_CONSTANT };
|
||||
|
||||
/**
|
||||
* @brief Enum defining all 4 possible sides to a 1D and 2D grid.
|
||||
*
|
||||
*/
|
||||
enum BC_SIDE {
|
||||
BC_SIDE_LEFT,
|
||||
BC_SIDE_RIGHT,
|
||||
BC_SIDE_TOP,
|
||||
BC_SIDE_BOTTOM
|
||||
};
|
||||
enum BC_SIDE { BC_SIDE_LEFT, BC_SIDE_RIGHT, BC_SIDE_TOP, BC_SIDE_BOTTOM };
|
||||
|
||||
/**
|
||||
* This class defines the boundary conditions of individual boundary elements.
|
||||
@ -40,7 +32,6 @@ enum BC_SIDE {
|
||||
*/
|
||||
class BoundaryElement {
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief Construct a new Boundary Element object for the closed case.
|
||||
* The boundary type is here automatically set to the type
|
||||
@ -97,7 +88,6 @@ class BoundaryElement {
|
||||
double value;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* This class implements the functionality and management of the boundary
|
||||
* conditions in the grid to be simulated.
|
||||
@ -127,7 +117,8 @@ class Boundary {
|
||||
* boundaries are set to the passed value.
|
||||
*
|
||||
* @param side Side to be set to constant, e.g. BC_SIDE_LEFT.
|
||||
* @param value Concentration to be set for all elements of the specified page.
|
||||
* @param value Concentration to be set for all elements of the specified
|
||||
* page.
|
||||
*/
|
||||
void setBoundarySideConstant(BC_SIDE side, double value);
|
||||
|
||||
@ -137,7 +128,8 @@ class Boundary {
|
||||
*
|
||||
* @param side Side in which an element is to be defined as closed.
|
||||
* @param index Index of the boundary element on the corresponding
|
||||
* boundary side. Must index an element of the corresponding side.
|
||||
* boundary side. Must index an element of the corresponding
|
||||
* side.
|
||||
*/
|
||||
void setBoundaryElementClosed(BC_SIDE side, int index);
|
||||
|
||||
@ -148,8 +140,10 @@ class Boundary {
|
||||
*
|
||||
* @param side Side in which an element is to be defined as constant.
|
||||
* @param index Index of the boundary element on the corresponding
|
||||
* boundary side. Must index an element of the corresponding side.
|
||||
* @param value Concentration value to which the boundary element should be set.
|
||||
* boundary side. Must index an element of the corresponding
|
||||
side.
|
||||
* @param value Concentration value to which the boundary element should be
|
||||
set.
|
||||
*/
|
||||
void setBoundaryElementConstant(BC_SIDE side, int index, double value);
|
||||
|
||||
@ -157,14 +151,16 @@ class Boundary {
|
||||
* @brief Returns the boundary condition of a specified side as a vector
|
||||
* of BoundarsElement objects.
|
||||
*
|
||||
* @param side Boundary side from which the boundary conditions are to be returned.
|
||||
* @return vector<BoundaryElement> Contains the boundary conditions as BoundaryElement objects.
|
||||
* @param side Boundary side from which the boundary conditions are to be
|
||||
* returned.
|
||||
* @return vector<BoundaryElement> Contains the boundary conditions as
|
||||
* BoundaryElement objects.
|
||||
*/
|
||||
const vector<BoundaryElement> getBoundarySide(BC_SIDE side);
|
||||
|
||||
/**
|
||||
* @brief Get thes Boundary Side Values as a vector. Value is -1 in case some specific
|
||||
boundary is closed.
|
||||
* @brief Get thes Boundary Side Values as a vector. Value is -1 in case some
|
||||
specific boundary is closed.
|
||||
*
|
||||
* @param side Boundary side for which the values are to be returned.
|
||||
* @return VectorXd Vector with values as doubles.
|
||||
@ -172,22 +168,25 @@ class Boundary {
|
||||
VectorXd getBoundarySideValues(BC_SIDE side);
|
||||
|
||||
/**
|
||||
* @brief Returns the boundary condition of a specified element on a given side.
|
||||
* @brief Returns the boundary condition of a specified element on a given
|
||||
* side.
|
||||
*
|
||||
* @param side Boundary side in which the boundary condition is located.
|
||||
* @param index Index of the boundary element on the corresponding
|
||||
* boundary side. Must index an element of the corresponding side.
|
||||
* boundary side. Must index an element of the corresponding
|
||||
* side.
|
||||
* @return BoundaryElement Boundary condition as a BoundaryElement object.
|
||||
*/
|
||||
BoundaryElement getBoundaryElement(BC_SIDE side, int index);
|
||||
|
||||
/**
|
||||
* @brief Returns the type of a boundary condition, i.e. either BC_TYPE_CLOSED or
|
||||
BC_TYPE_CONSTANT.
|
||||
* @brief Returns the type of a boundary condition, i.e. either BC_TYPE_CLOSED
|
||||
or BC_TYPE_CONSTANT.
|
||||
*
|
||||
* @param side Boundary side in which the boundary condition type is located.
|
||||
* @param index Index of the boundary element on the corresponding
|
||||
* boundary side. Must index an element of the corresponding side.
|
||||
* boundary side. Must index an element of the corresponding
|
||||
side.
|
||||
* @return BC_TYPE Boundary Type of the corresponding boundary condition.
|
||||
*/
|
||||
BC_TYPE getBoundaryElementType(BC_SIDE side, int index);
|
||||
@ -208,8 +207,8 @@ class Boundary {
|
||||
private:
|
||||
Grid grid; // Boundary is directly dependent on the dimensions of a predefined
|
||||
|
||||
vector<vector<BoundaryElement>> boundaries; // Vector with Boundary Element information
|
||||
vector<vector<BoundaryElement>>
|
||||
boundaries; // Vector with Boundary Element information
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@ -12,24 +12,23 @@ using namespace Eigen;
|
||||
|
||||
class Grid {
|
||||
public:
|
||||
|
||||
/**
|
||||
* @brief Constructs a new 1D-Grid object of a given length, which holds a matrix
|
||||
* with concentrations and a respective matrix of alpha coefficients.
|
||||
* The domain length is per default the same as the length. The concentrations
|
||||
* are all 20 by default and the alpha coefficients are 1.
|
||||
* @brief Constructs a new 1D-Grid object of a given length, which holds a
|
||||
* matrix with concentrations and a respective matrix of alpha coefficients.
|
||||
* The domain length is per default the same as the length. The
|
||||
* concentrations are all 20 by default and the alpha coefficients are 1.
|
||||
*
|
||||
* @param length Length of the 1D-Grid. Must be greater than 3.
|
||||
*/
|
||||
Grid(int length);
|
||||
|
||||
/**
|
||||
* @brief Constructs a new 2D-Grid object of given dimensions, which holds a matrix
|
||||
* with concentrations and the respective matrices of alpha coefficient for
|
||||
* each direction. The domain in x- and y-direction is per default equal to
|
||||
* the col length and row length, respectively.
|
||||
* The concentrations are all 20 by default across the entire grid and the
|
||||
* alpha coefficients 1 in both directions.
|
||||
* @brief Constructs a new 2D-Grid object of given dimensions, which holds a
|
||||
* matrix with concentrations and the respective matrices of alpha coefficient
|
||||
* for each direction. The domain in x- and y-direction is per default equal
|
||||
* to the col length and row length, respectively. The concentrations are all
|
||||
* 20 by default across the entire grid and the alpha coefficients 1 in both
|
||||
* directions.
|
||||
*
|
||||
* @param row Length of the 2D-Grid in y-direction. Must be greater than 3.
|
||||
* @param col Length of the 2D-Grid in x-direction. Must be greater than 3.
|
||||
@ -39,9 +38,9 @@ class Grid {
|
||||
/**
|
||||
* @brief Sets the concentrations matrix for a 1D or 2D-Grid.
|
||||
*
|
||||
* @param concentrations An Eigen3 MatrixXd holding the concentrations. Matrix must
|
||||
* have correct dimensions as defined in row and col. (Or length,
|
||||
* in 1D case).
|
||||
* @param concentrations An Eigen3 MatrixXd holding the concentrations. Matrix
|
||||
* must have correct dimensions as defined in row and col. (Or length, in 1D
|
||||
* case).
|
||||
*/
|
||||
void setConcentrations(MatrixXd concentrations);
|
||||
|
||||
@ -54,41 +53,44 @@ class Grid {
|
||||
const MatrixXd getConcentrations();
|
||||
|
||||
/**
|
||||
* @brief Set the alpha coefficients of a 1D-Grid. Grid must be one dimensional.
|
||||
* @brief Set the alpha coefficients of a 1D-Grid. Grid must be one
|
||||
* dimensional.
|
||||
*
|
||||
* @param alpha An Eigen3 MatrixXd with 1 row holding the alpha coefficients. Matrix
|
||||
* columns must have same size as length of grid.
|
||||
* @param alpha An Eigen3 MatrixXd with 1 row holding the alpha coefficients.
|
||||
* Matrix columns must have same size as length of grid.
|
||||
*/
|
||||
void setAlpha(MatrixXd alpha);
|
||||
|
||||
/**
|
||||
* @brief Set the alpha coefficients of a 2D-Grid. Grid must be two dimensional.
|
||||
* @brief Set the alpha coefficients of a 2D-Grid. Grid must be two
|
||||
* dimensional.
|
||||
*
|
||||
* @param alphaX An Eigen3 MatrixXd holding the alpha coefficients in x-direction.
|
||||
* Matrix must be of same size as the grid.
|
||||
* @param alphaY An Eigen3 MatrixXd holding the alpha coefficients in y-direction.
|
||||
* Matrix must be of same size as the grid.
|
||||
* @param alphaX An Eigen3 MatrixXd holding the alpha coefficients in
|
||||
* x-direction. Matrix must be of same size as the grid.
|
||||
* @param alphaY An Eigen3 MatrixXd holding the alpha coefficients in
|
||||
* y-direction. Matrix must be of same size as the grid.
|
||||
*/
|
||||
void setAlpha(MatrixXd alphaX, MatrixXd alphaY);
|
||||
|
||||
/**
|
||||
* @brief Gets the matrix of alpha coefficients of a 1D-Grid. Grid must be one dimensional.
|
||||
* @brief Gets the matrix of alpha coefficients of a 1D-Grid. Grid must be one
|
||||
* dimensional.
|
||||
*
|
||||
* @return MatrixXd A matrix with 1 row holding the alpha coefficients.
|
||||
*/
|
||||
const MatrixXd getAlpha();
|
||||
|
||||
/**
|
||||
* @brief Gets the matrix of alpha coefficients in x-direction of a 2D-Grid. Grid must be
|
||||
* two dimensional.
|
||||
* @brief Gets the matrix of alpha coefficients in x-direction of a 2D-Grid.
|
||||
* Grid must be two dimensional.
|
||||
*
|
||||
* @return MatrixXd A matrix holding the alpha coefficients in x-direction.
|
||||
*/
|
||||
const MatrixXd getAlphaX();
|
||||
|
||||
/**
|
||||
* @brief Gets the matrix of alpha coefficients in y-direction of a 2D-Grid. Grid must be
|
||||
* two dimensional.
|
||||
* @brief Gets the matrix of alpha coefficients in y-direction of a 2D-Grid.
|
||||
* Grid must be two dimensional.
|
||||
*
|
||||
* @return MatrixXd A matrix holding the alpha coefficients in y-direction.
|
||||
*/
|
||||
@ -132,8 +134,10 @@ class Grid {
|
||||
/**
|
||||
* @brief Sets the domain size of a 2D-Grid. Grid must be two dimensional.
|
||||
*
|
||||
* @param domainRow A double value of the domain size in y-direction. Must be positive.
|
||||
* @param domainCol A double value of the domain size in x-direction. Must be positive.
|
||||
* @param domainRow A double value of the domain size in y-direction. Must be
|
||||
* positive.
|
||||
* @param domainCol A double value of the domain size in x-direction. Must be
|
||||
* positive.
|
||||
*/
|
||||
void setDomain(double domainRow, double domainCol);
|
||||
|
||||
@ -158,9 +162,7 @@ class Grid {
|
||||
*/
|
||||
double getDeltaRow();
|
||||
|
||||
|
||||
private:
|
||||
|
||||
int col; // number of grid columns
|
||||
int row; // number of grid rows
|
||||
int dim; // 1D or 2D
|
||||
@ -171,5 +173,4 @@ class Grid {
|
||||
MatrixXd concentrations; // Matrix holding grid concentrations
|
||||
MatrixXd alphaX; // Matrix holding alpha coefficients in x-direction
|
||||
MatrixXd alphaY; // Matrix holding alpha coefficients in y-direction
|
||||
|
||||
};
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
/**
|
||||
* @file Simulation.hpp
|
||||
* @brief API of Simulation class, that holds all information regarding a specific simulation
|
||||
* run like its timestep, number of iterations and output options. Simulation object
|
||||
* also holds a predefined Grid and Boundary object.
|
||||
* @brief API of Simulation class, that holds all information regarding a
|
||||
* specific simulation run like its timestep, number of iterations and output
|
||||
* options. Simulation object also holds a predefined Grid and Boundary object.
|
||||
*
|
||||
*/
|
||||
#include "Boundary.hpp"
|
||||
@ -26,7 +26,8 @@ enum APPROACH {
|
||||
*/
|
||||
enum SOLVER {
|
||||
EIGEN_LU_SOLVER, // EigenLU solver
|
||||
THOMAS_ALGORITHM_SOLVER // Thomas Algorithm solver; more efficient for tridiagonal matrices
|
||||
THOMAS_ALGORITHM_SOLVER // Thomas Algorithm solver; more efficient for
|
||||
// tridiagonal matrices
|
||||
};
|
||||
|
||||
/**
|
||||
@ -37,7 +38,8 @@ enum CSV_OUTPUT {
|
||||
CSV_OUTPUT_OFF, // do not produce csv output
|
||||
CSV_OUTPUT_ON, // produce csv output with last concentration matrix
|
||||
CSV_OUTPUT_VERBOSE, // produce csv output with all concentration matrices
|
||||
CSV_OUTPUT_XTREME // csv output like VERBOSE but additional boundary conditions at beginning
|
||||
CSV_OUTPUT_XTREME // csv output like VERBOSE but additional boundary
|
||||
// conditions at beginning
|
||||
};
|
||||
|
||||
/**
|
||||
@ -68,11 +70,12 @@ enum TIME_MEASURE {
|
||||
class Simulation {
|
||||
public:
|
||||
/**
|
||||
* @brief Set up a simulation environment. The timestep and number of iterations
|
||||
* must be set. For the BTCS approach, the Thomas algorithm is used as
|
||||
* the default linear equation solver as this is faster for tridiagonal
|
||||
* matrices. CSV output, console output and time measure are off by default.
|
||||
* Also, the number of cores is set to the maximum number of cores -1 by default.
|
||||
* @brief Set up a simulation environment. The timestep and number of
|
||||
* iterations must be set. For the BTCS approach, the Thomas algorithm is used
|
||||
* as the default linear equation solver as this is faster for tridiagonal
|
||||
* matrices. CSV output, console output and time measure are off by
|
||||
* default. Also, the number of cores is set to the maximum number of cores -1
|
||||
* by default.
|
||||
*
|
||||
* @param grid Valid grid object
|
||||
* @param bc Valid boundary condition object
|
||||
@ -97,13 +100,17 @@ class Simulation {
|
||||
void setOutputCSV(CSV_OUTPUT csv_output);
|
||||
|
||||
/**
|
||||
* @brief Set the options for outputting information to the console. Off by default.
|
||||
* @brief Set the options for outputting information to the console. Off by
|
||||
* default.
|
||||
*
|
||||
* @param console_output Valid output option. The following options can be set
|
||||
* here:
|
||||
* - CONSOLE_OUTPUT_OFF: do not print any output to console
|
||||
* - CONSOLE_OUTPUT_ON: print before and after concentrations to console
|
||||
* - CONSOLE_OUTPUT_VERBOSE: print all concentration matrices to console
|
||||
* - CONSOLE_OUTPUT_OFF: do not print any output to
|
||||
* console
|
||||
* - CONSOLE_OUTPUT_ON: print before and after
|
||||
* concentrations to console
|
||||
* - CONSOLE_OUTPUT_VERBOSE: print all concentration
|
||||
* matrices to console
|
||||
*/
|
||||
void setOutputConsole(CONSOLE_OUTPUT console_output);
|
||||
|
||||
@ -111,8 +118,10 @@ class Simulation {
|
||||
* @brief Set the Time Measure option. Off by default.
|
||||
*
|
||||
* @param time_measure The following options are allowed:
|
||||
* - TIME_MEASURE_OFF: Time of simulation is not printed to console
|
||||
* - TIME_MEASURE_ON: Time of simulation run is printed to console
|
||||
* - TIME_MEASURE_OFF: Time of simulation is not printed
|
||||
* to console
|
||||
* - TIME_MEASURE_ON: Time of simulation run is printed to
|
||||
* console
|
||||
*/
|
||||
void setTimeMeasure(TIME_MEASURE time_measure);
|
||||
|
||||
@ -140,11 +149,11 @@ class Simulation {
|
||||
void setIterations(int iterations);
|
||||
|
||||
/**
|
||||
* @brief Set the desired linear equation solver to be used for BTCS approach. Without effect
|
||||
* in case of FTCS approach.
|
||||
* @brief Set the desired linear equation solver to be used for BTCS approach.
|
||||
* Without effect in case of FTCS approach.
|
||||
*
|
||||
* @param solver Solver to be used. Default is Thomas Algorithm as it is more efficient for
|
||||
* tridiagonal Matrices.
|
||||
* @param solver Solver to be used. Default is Thomas Algorithm as it is more
|
||||
* efficient for tridiagonal Matrices.
|
||||
*/
|
||||
void setSolver(SOLVER solver);
|
||||
|
||||
@ -152,8 +161,9 @@ class Simulation {
|
||||
* @brief Set the number of desired openMP Threads.
|
||||
*
|
||||
* @param num_threads Number of desired threads. Must have a value between
|
||||
* 1 and the maximum available number of processors. The maximum number of
|
||||
* processors is set as the default case during Simulation construction.
|
||||
* 1 and the maximum available number of processors. The
|
||||
* maximum number of processors is set as the default case during Simulation
|
||||
* construction.
|
||||
*/
|
||||
void setNumberThreads(int num_threads);
|
||||
|
||||
@ -172,9 +182,10 @@ class Simulation {
|
||||
|
||||
/**
|
||||
* @brief Creates a CSV file with a name containing the current simulation
|
||||
* parameters. If the data name already exists, an additional counter is
|
||||
* appended to the name. The name of the file is built up as follows:
|
||||
* <approach> + <number rows> + <number columns> + <number of iterations>+<counter>.csv
|
||||
* parameters. If the data name already exists, an additional counter
|
||||
* is appended to the name. The name of the file is built up as follows:
|
||||
* <approach> + <number rows> + <number columns> + <number of
|
||||
* iterations>+<counter>.csv
|
||||
*
|
||||
* @return string Filename with configured simulation parameters.
|
||||
*/
|
||||
@ -196,7 +207,6 @@ class Simulation {
|
||||
void run();
|
||||
|
||||
private:
|
||||
|
||||
double timestep;
|
||||
int iterations;
|
||||
int innerIterations;
|
||||
@ -209,5 +219,4 @@ class Simulation {
|
||||
Boundary &bc;
|
||||
APPROACH approach;
|
||||
SOLVER solver;
|
||||
|
||||
};
|
||||
|
||||
250
src/BTCSv2.cpp
250
src/BTCSv2.cpp
@ -1,73 +1,83 @@
|
||||
/**
|
||||
* @file BTCSv2.cpp
|
||||
* @brief Implementation of heterogenous BTCS (backward time-centered space) solution
|
||||
* of diffusion equation in 1D and 2D space. Internally the alternating-direction
|
||||
* implicit (ADI) method is used. Version 2, because Version 1 was an
|
||||
* implementation for the homogeneous BTCS solution.
|
||||
* @brief Implementation of heterogenous BTCS (backward time-centered space)
|
||||
* solution of diffusion equation in 1D and 2D space. Internally the
|
||||
* alternating-direction implicit (ADI) method is used. Version 2, because
|
||||
* Version 1 was an implementation for the homogeneous BTCS solution.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "FTCS.cpp"
|
||||
#include <tug/Boundary.hpp>
|
||||
#include <omp.h>
|
||||
#include <tug/Boundary.hpp>
|
||||
|
||||
#define NUM_THREADS_BTCS 10
|
||||
|
||||
using namespace Eigen;
|
||||
|
||||
|
||||
// calculates coefficient for left boundary in constant case
|
||||
static tuple<double, double> calcLeftBoundaryCoeffConstant(MatrixXd &alpha, int rowIndex, double sx) {
|
||||
static tuple<double, double>
|
||||
calcLeftBoundaryCoeffConstant(MatrixXd &alpha, int rowIndex, double sx) {
|
||||
double centerCoeff;
|
||||
double rightCoeff;
|
||||
|
||||
centerCoeff = 1 + sx * (calcAlphaIntercell(alpha(rowIndex,0), alpha(rowIndex,1))
|
||||
+ 2 * alpha(rowIndex,0));
|
||||
centerCoeff =
|
||||
1 + sx * (calcAlphaIntercell(alpha(rowIndex, 0), alpha(rowIndex, 1)) +
|
||||
2 * alpha(rowIndex, 0));
|
||||
rightCoeff = -sx * calcAlphaIntercell(alpha(rowIndex, 0), alpha(rowIndex, 1));
|
||||
|
||||
return {centerCoeff, rightCoeff};
|
||||
}
|
||||
|
||||
|
||||
// calculates coefficient for left boundary in closed case
|
||||
static tuple<double, double> calcLeftBoundaryCoeffClosed(MatrixXd &alpha, int rowIndex, double sx) {
|
||||
static tuple<double, double>
|
||||
calcLeftBoundaryCoeffClosed(MatrixXd &alpha, int rowIndex, double sx) {
|
||||
double centerCoeff;
|
||||
double rightCoeff;
|
||||
|
||||
centerCoeff = 1 + sx * calcAlphaIntercell(alpha(rowIndex,0), alpha(rowIndex,1));
|
||||
centerCoeff =
|
||||
1 + sx * calcAlphaIntercell(alpha(rowIndex, 0), alpha(rowIndex, 1));
|
||||
rightCoeff = -sx * calcAlphaIntercell(alpha(rowIndex, 0), alpha(rowIndex, 1));
|
||||
|
||||
return {centerCoeff, rightCoeff};
|
||||
}
|
||||
|
||||
|
||||
// calculates coefficient for right boundary in constant case
|
||||
static tuple<double, double> calcRightBoundaryCoeffConstant(MatrixXd &alpha, int rowIndex, int n, double sx) {
|
||||
static tuple<double, double> calcRightBoundaryCoeffConstant(MatrixXd &alpha,
|
||||
int rowIndex, int n,
|
||||
double sx) {
|
||||
double leftCoeff;
|
||||
double centerCoeff;
|
||||
|
||||
leftCoeff = -sx * calcAlphaIntercell(alpha(rowIndex,n-1), alpha(rowIndex,n));
|
||||
centerCoeff = 1 + sx * (calcAlphaIntercell(alpha(rowIndex,n-1), alpha(rowIndex,n))
|
||||
+ 2 * alpha(rowIndex,n));
|
||||
leftCoeff =
|
||||
-sx * calcAlphaIntercell(alpha(rowIndex, n - 1), alpha(rowIndex, n));
|
||||
centerCoeff =
|
||||
1 + sx * (calcAlphaIntercell(alpha(rowIndex, n - 1), alpha(rowIndex, n)) +
|
||||
2 * alpha(rowIndex, n));
|
||||
|
||||
return {leftCoeff, centerCoeff};
|
||||
}
|
||||
|
||||
|
||||
// calculates coefficient for right boundary in closed case
|
||||
static tuple<double, double> calcRightBoundaryCoeffClosed(MatrixXd &alpha, int rowIndex, int n, double sx) {
|
||||
static tuple<double, double>
|
||||
calcRightBoundaryCoeffClosed(MatrixXd &alpha, int rowIndex, int n, double sx) {
|
||||
double leftCoeff;
|
||||
double centerCoeff;
|
||||
|
||||
leftCoeff = -sx * calcAlphaIntercell(alpha(rowIndex,n-1), alpha(rowIndex,n));
|
||||
centerCoeff = 1 + sx * calcAlphaIntercell(alpha(rowIndex,n-1), alpha(rowIndex,n));
|
||||
leftCoeff =
|
||||
-sx * calcAlphaIntercell(alpha(rowIndex, n - 1), alpha(rowIndex, n));
|
||||
centerCoeff =
|
||||
1 + sx * calcAlphaIntercell(alpha(rowIndex, n - 1), alpha(rowIndex, n));
|
||||
|
||||
return {leftCoeff, centerCoeff};
|
||||
}
|
||||
|
||||
|
||||
// creates coefficient matrix for next time step from alphas in x-direction
|
||||
static SparseMatrix<double> createCoeffMatrix(MatrixXd &alpha, vector<BoundaryElement> &bcLeft, vector<BoundaryElement> &bcRight, int numCols, int rowIndex, double sx) {
|
||||
static SparseMatrix<double> createCoeffMatrix(MatrixXd &alpha,
|
||||
vector<BoundaryElement> &bcLeft,
|
||||
vector<BoundaryElement> &bcRight,
|
||||
int numCols, int rowIndex,
|
||||
double sx) {
|
||||
|
||||
// square matrix of column^2 dimension for the coefficients
|
||||
SparseMatrix<double> cm(numCols, numCols);
|
||||
@ -76,41 +86,48 @@ static SparseMatrix<double> createCoeffMatrix(MatrixXd &alpha, vector<BoundaryEl
|
||||
// left column
|
||||
BC_TYPE type = bcLeft[rowIndex].getType();
|
||||
if (type == BC_TYPE_CONSTANT) {
|
||||
auto [centerCoeffTop, rightCoeffTop] = calcLeftBoundaryCoeffConstant(alpha, rowIndex, sx);
|
||||
auto [centerCoeffTop, rightCoeffTop] =
|
||||
calcLeftBoundaryCoeffConstant(alpha, rowIndex, sx);
|
||||
cm.insert(0, 0) = centerCoeffTop;
|
||||
cm.insert(0, 1) = rightCoeffTop;
|
||||
} else if (type == BC_TYPE_CLOSED) {
|
||||
auto [centerCoeffTop, rightCoeffTop] = calcLeftBoundaryCoeffClosed(alpha, rowIndex, sx);
|
||||
auto [centerCoeffTop, rightCoeffTop] =
|
||||
calcLeftBoundaryCoeffClosed(alpha, rowIndex, sx);
|
||||
cm.insert(0, 0) = centerCoeffTop;
|
||||
cm.insert(0, 1) = rightCoeffTop;
|
||||
} else {
|
||||
throw_invalid_argument("Undefined Boundary Condition Type somewhere on Left or Top!");
|
||||
throw_invalid_argument(
|
||||
"Undefined Boundary Condition Type somewhere on Left or Top!");
|
||||
}
|
||||
|
||||
// inner columns
|
||||
int n = numCols - 1;
|
||||
for (int i = 1; i < n; i++) {
|
||||
cm.insert(i,i-1) = -sx * calcAlphaIntercell(alpha(rowIndex,i-1), alpha(rowIndex,i));
|
||||
cm.insert(i,i) = 1 + sx * (
|
||||
calcAlphaIntercell(alpha(rowIndex,i), alpha(rowIndex,i+1))
|
||||
+ calcAlphaIntercell(alpha(rowIndex,i-1), alpha(rowIndex,i))
|
||||
)
|
||||
;
|
||||
cm.insert(i,i+1) = -sx * calcAlphaIntercell(alpha(rowIndex,i), alpha(rowIndex,i+1));
|
||||
cm.insert(i, i - 1) =
|
||||
-sx * calcAlphaIntercell(alpha(rowIndex, i - 1), alpha(rowIndex, i));
|
||||
cm.insert(i, i) =
|
||||
1 +
|
||||
sx * (calcAlphaIntercell(alpha(rowIndex, i), alpha(rowIndex, i + 1)) +
|
||||
calcAlphaIntercell(alpha(rowIndex, i - 1), alpha(rowIndex, i)));
|
||||
cm.insert(i, i + 1) =
|
||||
-sx * calcAlphaIntercell(alpha(rowIndex, i), alpha(rowIndex, i + 1));
|
||||
}
|
||||
|
||||
// right column
|
||||
type = bcRight[rowIndex].getType();
|
||||
if (type == BC_TYPE_CONSTANT) {
|
||||
auto [leftCoeffBottom, centerCoeffBottom] = calcRightBoundaryCoeffConstant(alpha, rowIndex, n, sx);
|
||||
auto [leftCoeffBottom, centerCoeffBottom] =
|
||||
calcRightBoundaryCoeffConstant(alpha, rowIndex, n, sx);
|
||||
cm.insert(n, n - 1) = leftCoeffBottom;
|
||||
cm.insert(n, n) = centerCoeffBottom;
|
||||
} else if (type == BC_TYPE_CLOSED) {
|
||||
auto [leftCoeffBottom, centerCoeffBottom] = calcRightBoundaryCoeffClosed(alpha, rowIndex, n, sx);
|
||||
auto [leftCoeffBottom, centerCoeffBottom] =
|
||||
calcRightBoundaryCoeffClosed(alpha, rowIndex, n, sx);
|
||||
cm.insert(n, n - 1) = leftCoeffBottom;
|
||||
cm.insert(n, n) = centerCoeffBottom;
|
||||
} else {
|
||||
throw_invalid_argument("Undefined Boundary Condition Type somewhere on Right or Bottom!");
|
||||
throw_invalid_argument(
|
||||
"Undefined Boundary Condition Type somewhere on Right or Bottom!");
|
||||
}
|
||||
|
||||
cm.makeCompressed(); // important for Eigen solver
|
||||
@ -118,81 +135,72 @@ static SparseMatrix<double> createCoeffMatrix(MatrixXd &alpha, vector<BoundaryEl
|
||||
return cm;
|
||||
}
|
||||
|
||||
|
||||
// calculates explicity concentration at top boundary in constant case
|
||||
static double calcExplicitConcentrationsTopBoundaryConstant(MatrixXd &concentrations,
|
||||
MatrixXd &alpha, vector<BoundaryElement> &bcTop, int rowIndex, int i, double sy) {
|
||||
static double calcExplicitConcentrationsTopBoundaryConstant(
|
||||
MatrixXd &concentrations, MatrixXd &alpha, vector<BoundaryElement> &bcTop,
|
||||
int rowIndex, int i, double sy) {
|
||||
double c;
|
||||
|
||||
c = sy * calcAlphaIntercell(alpha(rowIndex,i), alpha(rowIndex+1,i))
|
||||
* concentrations(rowIndex,i)
|
||||
+ (
|
||||
1 - sy * (
|
||||
calcAlphaIntercell(alpha(rowIndex,i), alpha(rowIndex+1,i))
|
||||
+ 2 * alpha(rowIndex,i)
|
||||
)
|
||||
) * concentrations(rowIndex,i)
|
||||
+ sy * alpha(rowIndex,i) * bcTop[i].getValue();
|
||||
c = sy * calcAlphaIntercell(alpha(rowIndex, i), alpha(rowIndex + 1, i)) *
|
||||
concentrations(rowIndex, i) +
|
||||
(1 -
|
||||
sy * (calcAlphaIntercell(alpha(rowIndex, i), alpha(rowIndex + 1, i)) +
|
||||
2 * alpha(rowIndex, i))) *
|
||||
concentrations(rowIndex, i) +
|
||||
sy * alpha(rowIndex, i) * bcTop[i].getValue();
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
// calculates explicit concentration at top boundary in closed case
|
||||
static double calcExplicitConcentrationsTopBoundaryClosed(MatrixXd &concentrations,
|
||||
MatrixXd &alpha, int rowIndex, int i, double sy) {
|
||||
static double calcExplicitConcentrationsTopBoundaryClosed(
|
||||
MatrixXd &concentrations, MatrixXd &alpha, int rowIndex, int i, double sy) {
|
||||
double c;
|
||||
|
||||
c = sy * calcAlphaIntercell(alpha(rowIndex,i), alpha(rowIndex+1,i))
|
||||
* concentrations(rowIndex,i)
|
||||
+ (
|
||||
1 - sy * (
|
||||
calcAlphaIntercell(alpha(rowIndex,i), alpha(rowIndex+1,i))
|
||||
)
|
||||
) * concentrations(rowIndex,i);
|
||||
c = sy * calcAlphaIntercell(alpha(rowIndex, i), alpha(rowIndex + 1, i)) *
|
||||
concentrations(rowIndex, i) +
|
||||
(1 -
|
||||
sy * (calcAlphaIntercell(alpha(rowIndex, i), alpha(rowIndex + 1, i)))) *
|
||||
concentrations(rowIndex, i);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
// calculates explicit concentration at bottom boundary in constant case
|
||||
static double calcExplicitConcentrationsBottomBoundaryConstant(MatrixXd &concentrations,
|
||||
MatrixXd &alpha, vector<BoundaryElement> &bcBottom, int rowIndex, int i, double sy) {
|
||||
static double calcExplicitConcentrationsBottomBoundaryConstant(
|
||||
MatrixXd &concentrations, MatrixXd &alpha,
|
||||
vector<BoundaryElement> &bcBottom, int rowIndex, int i, double sy) {
|
||||
double c;
|
||||
|
||||
c = sy * alpha(rowIndex,i) * bcBottom[i].getValue()
|
||||
+ (
|
||||
1 - sy * (
|
||||
2 * alpha(rowIndex,i)
|
||||
+ calcAlphaIntercell(alpha(rowIndex-1,i), alpha(rowIndex,i))
|
||||
)
|
||||
) * concentrations(rowIndex,i)
|
||||
+ sy * calcAlphaIntercell(alpha(rowIndex-1,i), alpha(rowIndex,i))
|
||||
* concentrations(rowIndex-1,i);
|
||||
c = sy * alpha(rowIndex, i) * bcBottom[i].getValue() +
|
||||
(1 -
|
||||
sy * (2 * alpha(rowIndex, i) +
|
||||
calcAlphaIntercell(alpha(rowIndex - 1, i), alpha(rowIndex, i)))) *
|
||||
concentrations(rowIndex, i) +
|
||||
sy * calcAlphaIntercell(alpha(rowIndex - 1, i), alpha(rowIndex, i)) *
|
||||
concentrations(rowIndex - 1, i);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
// calculates explicit concentration at bottom boundary in closed case
|
||||
static double calcExplicitConcentrationsBottomBoundaryClosed(MatrixXd &concentrations,
|
||||
MatrixXd &alpha, int rowIndex, int i, double sy) {
|
||||
static double calcExplicitConcentrationsBottomBoundaryClosed(
|
||||
MatrixXd &concentrations, MatrixXd &alpha, int rowIndex, int i, double sy) {
|
||||
double c;
|
||||
|
||||
c = (
|
||||
1 - sy * (
|
||||
+ calcAlphaIntercell(alpha(rowIndex-1,i), alpha(rowIndex,i))
|
||||
)
|
||||
) * concentrations(rowIndex,i)
|
||||
+ sy * calcAlphaIntercell(alpha(rowIndex-1,i), alpha(rowIndex,i))
|
||||
* concentrations(rowIndex-1,i);
|
||||
c = (1 -
|
||||
sy * (+calcAlphaIntercell(alpha(rowIndex - 1, i), alpha(rowIndex, i)))) *
|
||||
concentrations(rowIndex, i) +
|
||||
sy * calcAlphaIntercell(alpha(rowIndex - 1, i), alpha(rowIndex, i)) *
|
||||
concentrations(rowIndex - 1, i);
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
// creates a solution vector for next time step from the current state of concentrations
|
||||
static VectorXd createSolutionVector(MatrixXd &concentrations, MatrixXd &alphaX, MatrixXd &alphaY,
|
||||
// creates a solution vector for next time step from the current state of
|
||||
// concentrations
|
||||
static VectorXd createSolutionVector(
|
||||
MatrixXd &concentrations, MatrixXd &alphaX, MatrixXd &alphaY,
|
||||
vector<BoundaryElement> &bcLeft, vector<BoundaryElement> &bcRight,
|
||||
vector<BoundaryElement> &bcTop, vector<BoundaryElement> &bcBottom,
|
||||
int length, int rowIndex, double sx, double sy) {
|
||||
@ -204,17 +212,18 @@ static VectorXd createSolutionVector(MatrixXd &concentrations, MatrixXd &alphaX,
|
||||
// inner rows
|
||||
if (rowIndex > 0 && rowIndex < numRows - 1) {
|
||||
for (int i = 0; i < length; i++) {
|
||||
sv(i) = sy * calcAlphaIntercell(alphaY(rowIndex,i), alphaY(rowIndex+1,i))
|
||||
* concentrations(rowIndex+1,i)
|
||||
+ (
|
||||
1 - sy * (
|
||||
calcAlphaIntercell(alphaY(rowIndex,i), alphaY(rowIndex+1,i))
|
||||
+ calcAlphaIntercell(alphaY(rowIndex-1,i), alphaY(rowIndex,i))
|
||||
)
|
||||
) * concentrations(rowIndex,i)
|
||||
+ sy * calcAlphaIntercell(alphaY(rowIndex-1,i), alphaY(rowIndex,i))
|
||||
* concentrations(rowIndex-1,i)
|
||||
;
|
||||
sv(i) =
|
||||
sy *
|
||||
calcAlphaIntercell(alphaY(rowIndex, i), alphaY(rowIndex + 1, i)) *
|
||||
concentrations(rowIndex + 1, i) +
|
||||
(1 - sy * (calcAlphaIntercell(alphaY(rowIndex, i),
|
||||
alphaY(rowIndex + 1, i)) +
|
||||
calcAlphaIntercell(alphaY(rowIndex - 1, i),
|
||||
alphaY(rowIndex, i)))) *
|
||||
concentrations(rowIndex, i) +
|
||||
sy *
|
||||
calcAlphaIntercell(alphaY(rowIndex - 1, i), alphaY(rowIndex, i)) *
|
||||
concentrations(rowIndex - 1, i);
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,11 +232,14 @@ static VectorXd createSolutionVector(MatrixXd &concentrations, MatrixXd &alphaX,
|
||||
for (int i = 0; i < length; i++) {
|
||||
type = bcTop[i].getType();
|
||||
if (type == BC_TYPE_CONSTANT) {
|
||||
sv(i) = calcExplicitConcentrationsTopBoundaryConstant(concentrations, alphaY, bcTop, rowIndex, i, sy);
|
||||
sv(i) = calcExplicitConcentrationsTopBoundaryConstant(
|
||||
concentrations, alphaY, bcTop, rowIndex, i, sy);
|
||||
} else if (type == BC_TYPE_CLOSED) {
|
||||
sv(i) = calcExplicitConcentrationsTopBoundaryClosed(concentrations, alphaY, rowIndex, i, sy);
|
||||
sv(i) = calcExplicitConcentrationsTopBoundaryClosed(
|
||||
concentrations, alphaY, rowIndex, i, sy);
|
||||
} else {
|
||||
throw_invalid_argument("Undefined Boundary Condition Type somewhere on Left or Top!");
|
||||
throw_invalid_argument(
|
||||
"Undefined Boundary Condition Type somewhere on Left or Top!");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -237,29 +249,34 @@ static VectorXd createSolutionVector(MatrixXd &concentrations, MatrixXd &alphaX,
|
||||
for (int i = 0; i < length; i++) {
|
||||
type = bcBottom[i].getType();
|
||||
if (type == BC_TYPE_CONSTANT) {
|
||||
sv(i) = calcExplicitConcentrationsBottomBoundaryConstant(concentrations, alphaY, bcBottom, rowIndex, i, sy);
|
||||
sv(i) = calcExplicitConcentrationsBottomBoundaryConstant(
|
||||
concentrations, alphaY, bcBottom, rowIndex, i, sy);
|
||||
} else if (type == BC_TYPE_CLOSED) {
|
||||
sv(i) = calcExplicitConcentrationsBottomBoundaryClosed(concentrations, alphaY, rowIndex, i, sy);
|
||||
sv(i) = calcExplicitConcentrationsBottomBoundaryClosed(
|
||||
concentrations, alphaY, rowIndex, i, sy);
|
||||
} else {
|
||||
throw_invalid_argument("Undefined Boundary Condition Type somewhere on Right or Bottom!");
|
||||
throw_invalid_argument(
|
||||
"Undefined Boundary Condition Type somewhere on Right or Bottom!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// first column -> additional fixed concentration change from perpendicular dimension in constant bc case
|
||||
// first column -> additional fixed concentration change from perpendicular
|
||||
// dimension in constant bc case
|
||||
if (bcLeft[rowIndex].getType() == BC_TYPE_CONSTANT) {
|
||||
sv(0) += 2 * sx * alphaX(rowIndex, 0) * bcLeft[rowIndex].getValue();
|
||||
}
|
||||
|
||||
// last column -> additional fixed concentration change from perpendicular dimension in constant bc case
|
||||
// last column -> additional fixed concentration change from perpendicular
|
||||
// dimension in constant bc case
|
||||
if (bcRight[rowIndex].getType() == BC_TYPE_CONSTANT) {
|
||||
sv(length-1) += 2 * sx * alphaX(rowIndex,length-1) * bcRight[rowIndex].getValue();
|
||||
sv(length - 1) +=
|
||||
2 * sx * alphaX(rowIndex, length - 1) * bcRight[rowIndex].getValue();
|
||||
}
|
||||
|
||||
return sv;
|
||||
}
|
||||
|
||||
|
||||
// solver for linear equation system; A corresponds to coefficient matrix,
|
||||
// b to the solution vector
|
||||
// use of EigenLU solver
|
||||
@ -316,9 +333,10 @@ static VectorXd ThomasAlgorithm(SparseMatrix<double> &A, VectorXd &b) {
|
||||
return x_vec;
|
||||
}
|
||||
|
||||
|
||||
// BTCS solution for 1D grid
|
||||
static void BTCS_1D(Grid &grid, Boundary &bc, double timestep, VectorXd (*solverFunc) (SparseMatrix<double> &A, VectorXd &b)) {
|
||||
static void BTCS_1D(Grid &grid, Boundary &bc, double timestep,
|
||||
VectorXd (*solverFunc)(SparseMatrix<double> &A,
|
||||
VectorXd &b)) {
|
||||
int length = grid.getLength();
|
||||
double sx = timestep / (grid.getDelta() * grid.getDelta());
|
||||
|
||||
@ -333,7 +351,8 @@ static void BTCS_1D(Grid &grid, Boundary &bc, double timestep, VectorXd (*solver
|
||||
|
||||
MatrixXd concentrations = grid.getConcentrations();
|
||||
int rowIndex = 0;
|
||||
A = createCoeffMatrix(alpha, bcLeft, bcRight, length, rowIndex, sx); // this is exactly same as in 2D
|
||||
A = createCoeffMatrix(alpha, bcLeft, bcRight, length, rowIndex,
|
||||
sx); // this is exactly same as in 2D
|
||||
for (int i = 0; i < length; i++) {
|
||||
b(i) = concentrations(0, i);
|
||||
}
|
||||
@ -353,9 +372,11 @@ static void BTCS_1D(Grid &grid, Boundary &bc, double timestep, VectorXd (*solver
|
||||
grid.setConcentrations(concentrations);
|
||||
}
|
||||
|
||||
|
||||
// BTCS solution for 2D grid
|
||||
static void BTCS_2D(Grid &grid, Boundary &bc, double timestep, VectorXd (*solverFunc) (SparseMatrix<double> &A, VectorXd &b), int numThreads) {
|
||||
static void BTCS_2D(Grid &grid, Boundary &bc, double timestep,
|
||||
VectorXd (*solverFunc)(SparseMatrix<double> &A,
|
||||
VectorXd &b),
|
||||
int numThreads) {
|
||||
int rowMax = grid.getRow();
|
||||
int colMax = grid.getCol();
|
||||
double sx = timestep / (2 * grid.getDeltaCol() * grid.getDeltaCol());
|
||||
@ -379,7 +400,6 @@ static void BTCS_2D(Grid &grid, Boundary &bc, double timestep, VectorXd (*solver
|
||||
#pragma omp parallel for num_threads(numThreads) private(A, b, row_t1)
|
||||
for (int i = 0; i < rowMax; i++) {
|
||||
|
||||
|
||||
A = createCoeffMatrix(alphaX, bcLeft, bcRight, colMax, i, sx);
|
||||
b = createSolutionVector(concentrations, alphaX, alphaY, bcLeft, bcRight,
|
||||
bcTop, bcBottom, colMax, i, sx, sy);
|
||||
@ -413,7 +433,6 @@ static void BTCS_2D(Grid &grid, Boundary &bc, double timestep, VectorXd (*solver
|
||||
grid.setConcentrations(concentrations);
|
||||
}
|
||||
|
||||
|
||||
// entry point for EigenLU solver; differentiate between 1D and 2D grid
|
||||
static void BTCS_LU(Grid &grid, Boundary &bc, double timestep, int numThreads) {
|
||||
if (grid.getDim() == 1) {
|
||||
@ -421,17 +440,20 @@ static void BTCS_LU(Grid &grid, Boundary &bc, double timestep, int numThreads) {
|
||||
} else if (grid.getDim() == 2) {
|
||||
BTCS_2D(grid, bc, timestep, EigenLUAlgorithm, numThreads);
|
||||
} else {
|
||||
throw_invalid_argument("Error: Only 1- and 2-dimensional grids are defined!");
|
||||
throw_invalid_argument(
|
||||
"Error: Only 1- and 2-dimensional grids are defined!");
|
||||
}
|
||||
}
|
||||
|
||||
// entry point for Thomas algorithm solver; differentiate 1D and 2D grid
|
||||
static void BTCS_Thomas(Grid &grid, Boundary &bc, double timestep, int numThreads) {
|
||||
static void BTCS_Thomas(Grid &grid, Boundary &bc, double timestep,
|
||||
int numThreads) {
|
||||
if (grid.getDim() == 1) {
|
||||
BTCS_1D(grid, bc, timestep, ThomasAlgorithm);
|
||||
} else if (grid.getDim() == 2) {
|
||||
BTCS_2D(grid, bc, timestep, ThomasAlgorithm, numThreads);
|
||||
} else {
|
||||
throw_invalid_argument("Error: Only 1- and 2-dimensional grids are defined!");
|
||||
throw_invalid_argument(
|
||||
"Error: Only 1- and 2-dimensional grids are defined!");
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,8 @@
|
||||
#include "TugUtils.cpp"
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
#include <tug/Boundary.hpp>
|
||||
#include <stdexcept>
|
||||
#include <tug/Boundary.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -17,9 +17,7 @@ BoundaryElement::BoundaryElement(double value) {
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
void BoundaryElement::setType(BC_TYPE type) {
|
||||
this->type = type;
|
||||
}
|
||||
void BoundaryElement::setType(BC_TYPE type) { this->type = type; }
|
||||
|
||||
void BoundaryElement::setValue(double value) {
|
||||
if (value < 0) {
|
||||
@ -33,27 +31,28 @@ void BoundaryElement::setValue(double value) {
|
||||
this->value = value;
|
||||
}
|
||||
|
||||
BC_TYPE BoundaryElement::getType() {
|
||||
return this->type;
|
||||
}
|
||||
BC_TYPE BoundaryElement::getType() { return this->type; }
|
||||
|
||||
double BoundaryElement::getValue() {
|
||||
return this->value;
|
||||
}
|
||||
double BoundaryElement::getValue() { return this->value; }
|
||||
|
||||
Boundary::Boundary(Grid grid) : grid(grid) {
|
||||
if (grid.getDim() == 1) {
|
||||
this->boundaries = vector<vector<BoundaryElement>>(2); // in 1D only left and right boundary
|
||||
this->boundaries = vector<vector<BoundaryElement>>(
|
||||
2); // in 1D only left and right boundary
|
||||
|
||||
this->boundaries[BC_SIDE_LEFT].push_back(BoundaryElement());
|
||||
this->boundaries[BC_SIDE_RIGHT].push_back(BoundaryElement());
|
||||
} else if (grid.getDim() == 2) {
|
||||
this->boundaries = vector<vector<BoundaryElement>>(4);
|
||||
|
||||
this->boundaries[BC_SIDE_LEFT] = vector<BoundaryElement>(grid.getRow(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_RIGHT] = vector<BoundaryElement>(grid.getRow(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_TOP] = vector<BoundaryElement>(grid.getCol(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_BOTTOM] = vector<BoundaryElement>(grid.getCol(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_LEFT] =
|
||||
vector<BoundaryElement>(grid.getRow(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_RIGHT] =
|
||||
vector<BoundaryElement>(grid.getRow(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_TOP] =
|
||||
vector<BoundaryElement>(grid.getCol(), BoundaryElement());
|
||||
this->boundaries[BC_SIDE_BOTTOM] =
|
||||
vector<BoundaryElement>(grid.getCol(), BoundaryElement());
|
||||
}
|
||||
}
|
||||
|
||||
@ -101,7 +100,8 @@ void Boundary::setBoundaryElementClosed(BC_SIDE side, int index) {
|
||||
this->boundaries[side][index].setType(BC_TYPE_CLOSED);
|
||||
}
|
||||
|
||||
void Boundary::setBoundaryElementConstant(BC_SIDE side, int index, double value) {
|
||||
void Boundary::setBoundaryElementConstant(BC_SIDE side, int index,
|
||||
double value) {
|
||||
// tests whether the index really points to an element of the boundary side.
|
||||
if ((boundaries[side].size() < index) || index < 0) {
|
||||
throw_invalid_argument("Index is selected either too large or too small.");
|
||||
@ -155,8 +155,8 @@ double Boundary::getBoundaryElementValue(BC_SIDE side, int index) {
|
||||
throw_invalid_argument("Index is selected either too large or too small.");
|
||||
}
|
||||
if (boundaries[side][index].getType() != BC_TYPE_CONSTANT) {
|
||||
throw_invalid_argument("A value can only be output if it is a constant boundary condition.");
|
||||
throw_invalid_argument(
|
||||
"A value can only be output if it is a constant boundary condition.");
|
||||
}
|
||||
return this->boundaries[side][index].getValue();
|
||||
}
|
||||
|
||||
|
||||
380
src/FTCS.cpp
380
src/FTCS.cpp
@ -1,85 +1,86 @@
|
||||
/**
|
||||
* @file FTCS.cpp
|
||||
* @brief Implementation of heterogenous FTCS (forward time-centered space) solution
|
||||
* of diffusion equation in 1D and 2D space.
|
||||
* @brief Implementation of heterogenous FTCS (forward time-centered space)
|
||||
* solution of diffusion equation in 1D and 2D space.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "TugUtils.cpp"
|
||||
#include <cstddef>
|
||||
#include <tug/Boundary.hpp>
|
||||
#include <iostream>
|
||||
#include <omp.h>
|
||||
#include <tug/Boundary.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
// calculates horizontal change on one cell independent of boundary type
|
||||
static double calcHorizontalChange(Grid &grid, int &row, int &col) {
|
||||
|
||||
double result =
|
||||
calcAlphaIntercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
|
||||
* grid.getConcentrations()(row,col+1)
|
||||
- (
|
||||
calcAlphaIntercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
|
||||
+ calcAlphaIntercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col))
|
||||
)
|
||||
* grid.getConcentrations()(row,col)
|
||||
+ calcAlphaIntercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col))
|
||||
* grid.getConcentrations()(row,col-1);
|
||||
double result = calcAlphaIntercell(grid.getAlphaX()(row, col + 1),
|
||||
grid.getAlphaX()(row, col)) *
|
||||
grid.getConcentrations()(row, col + 1) -
|
||||
(calcAlphaIntercell(grid.getAlphaX()(row, col + 1),
|
||||
grid.getAlphaX()(row, col)) +
|
||||
calcAlphaIntercell(grid.getAlphaX()(row, col - 1),
|
||||
grid.getAlphaX()(row, col))) *
|
||||
grid.getConcentrations()(row, col) +
|
||||
calcAlphaIntercell(grid.getAlphaX()(row, col - 1),
|
||||
grid.getAlphaX()(row, col)) *
|
||||
grid.getConcentrations()(row, col - 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// calculates vertical change on one cell independent of boundary type
|
||||
static double calcVerticalChange(Grid &grid, int &row, int &col) {
|
||||
|
||||
double result =
|
||||
calcAlphaIntercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col))
|
||||
* grid.getConcentrations()(row+1,col)
|
||||
- (
|
||||
calcAlphaIntercell(grid.getAlphaY()(row+1,col), grid.getAlphaY()(row,col))
|
||||
+ calcAlphaIntercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col))
|
||||
)
|
||||
* grid.getConcentrations()(row,col)
|
||||
+ calcAlphaIntercell(grid.getAlphaY()(row-1,col), grid.getAlphaY()(row,col))
|
||||
* grid.getConcentrations()(row-1,col);
|
||||
double result = calcAlphaIntercell(grid.getAlphaY()(row + 1, col),
|
||||
grid.getAlphaY()(row, col)) *
|
||||
grid.getConcentrations()(row + 1, col) -
|
||||
(calcAlphaIntercell(grid.getAlphaY()(row + 1, col),
|
||||
grid.getAlphaY()(row, col)) +
|
||||
calcAlphaIntercell(grid.getAlphaY()(row - 1, col),
|
||||
grid.getAlphaY()(row, col))) *
|
||||
grid.getConcentrations()(row, col) +
|
||||
calcAlphaIntercell(grid.getAlphaY()(row - 1, col),
|
||||
grid.getAlphaY()(row, col)) *
|
||||
grid.getConcentrations()(row - 1, col);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// calculates horizontal change on one cell with a constant left boundary
|
||||
static double calcHorizontalChangeLeftBoundaryConstant(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcHorizontalChangeLeftBoundaryConstant(Grid &grid, Boundary &bc,
|
||||
int &row, int &col) {
|
||||
|
||||
double result =
|
||||
calcAlphaIntercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
|
||||
* grid.getConcentrations()(row,col+1)
|
||||
- (
|
||||
calcAlphaIntercell(grid.getAlphaX()(row,col+1), grid.getAlphaX()(row,col))
|
||||
+ 2 * grid.getAlphaX()(row,col)
|
||||
)
|
||||
* grid.getConcentrations()(row,col)
|
||||
+ 2 * grid.getAlphaX()(row,col) * bc.getBoundaryElementValue(BC_SIDE_LEFT, row);
|
||||
double result = calcAlphaIntercell(grid.getAlphaX()(row, col + 1),
|
||||
grid.getAlphaX()(row, col)) *
|
||||
grid.getConcentrations()(row, col + 1) -
|
||||
(calcAlphaIntercell(grid.getAlphaX()(row, col + 1),
|
||||
grid.getAlphaX()(row, col)) +
|
||||
2 * grid.getAlphaX()(row, col)) *
|
||||
grid.getConcentrations()(row, col) +
|
||||
2 * grid.getAlphaX()(row, col) *
|
||||
bc.getBoundaryElementValue(BC_SIDE_LEFT, row);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// calculates horizontal change on one cell with a closed left boundary
|
||||
static double calcHorizontalChangeLeftBoundaryClosed(Grid &grid, int &row, int &col) {
|
||||
static double calcHorizontalChangeLeftBoundaryClosed(Grid &grid, int &row,
|
||||
int &col) {
|
||||
|
||||
double result =
|
||||
calcAlphaIntercell(grid.getAlphaX()(row, col+1), grid.getAlphaX()(row, col))
|
||||
* (grid.getConcentrations()(row, col+1) - grid.getConcentrations()(row, col));
|
||||
double result = calcAlphaIntercell(grid.getAlphaX()(row, col + 1),
|
||||
grid.getAlphaX()(row, col)) *
|
||||
(grid.getConcentrations()(row, col + 1) -
|
||||
grid.getConcentrations()(row, col));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// checks boundary condition type for a cell on the left edge of grid
|
||||
static double calcHorizontalChangeLeftBoundary(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcHorizontalChangeLeftBoundary(Grid &grid, Boundary &bc,
|
||||
int &row, int &col) {
|
||||
if (bc.getBoundaryElementType(BC_SIDE_LEFT, col) == BC_TYPE_CONSTANT) {
|
||||
return calcHorizontalChangeLeftBoundaryConstant(grid, bc, row, col);
|
||||
} else if (bc.getBoundaryElementType(BC_SIDE_LEFT, col) == BC_TYPE_CLOSED) {
|
||||
@ -89,37 +90,39 @@ static double calcHorizontalChangeLeftBoundary(Grid &grid, Boundary &bc, int &ro
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// calculates horizontal change on one cell with a constant right boundary
|
||||
static double calcHorizontalChangeRightBoundaryConstant(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcHorizontalChangeRightBoundaryConstant(Grid &grid,
|
||||
Boundary &bc, int &row,
|
||||
int &col) {
|
||||
|
||||
double result =
|
||||
2 * grid.getAlphaX()(row,col) * bc.getBoundaryElementValue(BC_SIDE_RIGHT, row)
|
||||
- (
|
||||
calcAlphaIntercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col))
|
||||
+ 2 * grid.getAlphaX()(row,col)
|
||||
)
|
||||
* grid.getConcentrations()(row,col)
|
||||
+ calcAlphaIntercell(grid.getAlphaX()(row,col-1), grid.getAlphaX()(row,col))
|
||||
* grid.getConcentrations()(row,col-1);
|
||||
double result = 2 * grid.getAlphaX()(row, col) *
|
||||
bc.getBoundaryElementValue(BC_SIDE_RIGHT, row) -
|
||||
(calcAlphaIntercell(grid.getAlphaX()(row, col - 1),
|
||||
grid.getAlphaX()(row, col)) +
|
||||
2 * grid.getAlphaX()(row, col)) *
|
||||
grid.getConcentrations()(row, col) +
|
||||
calcAlphaIntercell(grid.getAlphaX()(row, col - 1),
|
||||
grid.getAlphaX()(row, col)) *
|
||||
grid.getConcentrations()(row, col - 1);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// calculates horizontal change on one cell with a closed right boundary
|
||||
static double calcHorizontalChangeRightBoundaryClosed(Grid &grid, int &row, int &col) {
|
||||
static double calcHorizontalChangeRightBoundaryClosed(Grid &grid, int &row,
|
||||
int &col) {
|
||||
|
||||
double result =
|
||||
- (calcAlphaIntercell(grid.getAlphaX()(row, col-1), grid.getAlphaX()(row, col))
|
||||
* (grid.getConcentrations()(row, col) - grid.getConcentrations()(row, col-1)));
|
||||
double result = -(calcAlphaIntercell(grid.getAlphaX()(row, col - 1),
|
||||
grid.getAlphaX()(row, col)) *
|
||||
(grid.getConcentrations()(row, col) -
|
||||
grid.getConcentrations()(row, col - 1)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// checks boundary condition type for a cell on the right edge of grid
|
||||
static double calcHorizontalChangeRightBoundary(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcHorizontalChangeRightBoundary(Grid &grid, Boundary &bc,
|
||||
int &row, int &col) {
|
||||
if (bc.getBoundaryElementType(BC_SIDE_RIGHT, col) == BC_TYPE_CONSTANT) {
|
||||
return calcHorizontalChangeRightBoundaryConstant(grid, bc, row, col);
|
||||
} else if (bc.getBoundaryElementType(BC_SIDE_RIGHT, col) == BC_TYPE_CLOSED) {
|
||||
@ -129,37 +132,38 @@ static double calcHorizontalChangeRightBoundary(Grid &grid, Boundary &bc, int &r
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// calculates vertical change on one cell with a constant top boundary
|
||||
static double calcVerticalChangeTopBoundaryConstant(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcVerticalChangeTopBoundaryConstant(Grid &grid, Boundary &bc,
|
||||
int &row, int &col) {
|
||||
|
||||
double result =
|
||||
calcAlphaIntercell(grid.getAlphaY()(row+1, col), grid.getAlphaY()(row, col))
|
||||
* grid.getConcentrations()(row+1,col)
|
||||
- (
|
||||
calcAlphaIntercell(grid.getAlphaY()(row+1, col), grid.getAlphaY()(row, col))
|
||||
+ 2 * grid.getAlphaY()(row, col)
|
||||
)
|
||||
* grid.getConcentrations()(row, col)
|
||||
+ 2 * grid.getAlphaY()(row, col) * bc.getBoundaryElementValue(BC_SIDE_TOP, col);
|
||||
double result = calcAlphaIntercell(grid.getAlphaY()(row + 1, col),
|
||||
grid.getAlphaY()(row, col)) *
|
||||
grid.getConcentrations()(row + 1, col) -
|
||||
(calcAlphaIntercell(grid.getAlphaY()(row + 1, col),
|
||||
grid.getAlphaY()(row, col)) +
|
||||
2 * grid.getAlphaY()(row, col)) *
|
||||
grid.getConcentrations()(row, col) +
|
||||
2 * grid.getAlphaY()(row, col) *
|
||||
bc.getBoundaryElementValue(BC_SIDE_TOP, col);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// calculates vertical change on one cell with a closed top boundary
|
||||
static double calcVerticalChangeTopBoundaryClosed(Grid &grid, int &row, int &col) {
|
||||
static double calcVerticalChangeTopBoundaryClosed(Grid &grid, int &row,
|
||||
int &col) {
|
||||
|
||||
double result =
|
||||
calcAlphaIntercell(grid.getAlphaY()(row+1, col), grid.getConcentrations()(row, col))
|
||||
* (grid.getConcentrations()(row+1, col) - grid.getConcentrations()(row, col));
|
||||
double result = calcAlphaIntercell(grid.getAlphaY()(row + 1, col),
|
||||
grid.getConcentrations()(row, col)) *
|
||||
(grid.getConcentrations()(row + 1, col) -
|
||||
grid.getConcentrations()(row, col));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// checks boundary condition type for a cell on the top edge of grid
|
||||
static double calcVerticalChangeTopBoundary(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcVerticalChangeTopBoundary(Grid &grid, Boundary &bc, int &row,
|
||||
int &col) {
|
||||
if (bc.getBoundaryElementType(BC_SIDE_TOP, col) == BC_TYPE_CONSTANT) {
|
||||
return calcVerticalChangeTopBoundaryConstant(grid, bc, row, col);
|
||||
} else if (bc.getBoundaryElementType(BC_SIDE_TOP, col) == BC_TYPE_CLOSED) {
|
||||
@ -169,37 +173,38 @@ static double calcVerticalChangeTopBoundary(Grid &grid, Boundary &bc, int &row,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// calculates vertical change on one cell with a constant bottom boundary
|
||||
static double calcVerticalChangeBottomBoundaryConstant(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcVerticalChangeBottomBoundaryConstant(Grid &grid, Boundary &bc,
|
||||
int &row, int &col) {
|
||||
|
||||
double result =
|
||||
2 * grid.getAlphaY()(row, col) * bc.getBoundaryElementValue(BC_SIDE_BOTTOM, col)
|
||||
- (
|
||||
calcAlphaIntercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col))
|
||||
+ 2 * grid.getAlphaY()(row, col)
|
||||
)
|
||||
* grid.getConcentrations()(row, col)
|
||||
+ calcAlphaIntercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col))
|
||||
* grid.getConcentrations()(row-1,col);
|
||||
double result = 2 * grid.getAlphaY()(row, col) *
|
||||
bc.getBoundaryElementValue(BC_SIDE_BOTTOM, col) -
|
||||
(calcAlphaIntercell(grid.getAlphaY()(row, col),
|
||||
grid.getAlphaY()(row - 1, col)) +
|
||||
2 * grid.getAlphaY()(row, col)) *
|
||||
grid.getConcentrations()(row, col) +
|
||||
calcAlphaIntercell(grid.getAlphaY()(row, col),
|
||||
grid.getAlphaY()(row - 1, col)) *
|
||||
grid.getConcentrations()(row - 1, col);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// calculates vertical change on one cell with a closed bottom boundary
|
||||
static double calcVerticalChangeBottomBoundaryClosed(Grid &grid, int &row, int &col) {
|
||||
static double calcVerticalChangeBottomBoundaryClosed(Grid &grid, int &row,
|
||||
int &col) {
|
||||
|
||||
double result =
|
||||
- (calcAlphaIntercell(grid.getAlphaY()(row, col), grid.getAlphaY()(row-1, col))
|
||||
* (grid.getConcentrations()(row, col) - grid.getConcentrations()(row-1, col)));
|
||||
double result = -(calcAlphaIntercell(grid.getAlphaY()(row, col),
|
||||
grid.getAlphaY()(row - 1, col)) *
|
||||
(grid.getConcentrations()(row, col) -
|
||||
grid.getConcentrations()(row - 1, col)));
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
// checks boundary condition type for a cell on the bottom edge of grid
|
||||
static double calcVerticalChangeBottomBoundary(Grid &grid, Boundary &bc, int &row, int &col) {
|
||||
static double calcVerticalChangeBottomBoundary(Grid &grid, Boundary &bc,
|
||||
int &row, int &col) {
|
||||
if (bc.getBoundaryElementType(BC_SIDE_BOTTOM, col) == BC_TYPE_CONSTANT) {
|
||||
return calcVerticalChangeBottomBoundaryConstant(grid, bc, row, col);
|
||||
} else if (bc.getBoundaryElementType(BC_SIDE_BOTTOM, col) == BC_TYPE_CLOSED) {
|
||||
@ -209,7 +214,6 @@ static double calcVerticalChangeBottomBoundary(Grid &grid, Boundary &bc, int &ro
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// FTCS solution for 1D grid
|
||||
static void FTCS_1D(Grid &grid, Boundary &bc, double ×tep) {
|
||||
int colMax = grid.getCol();
|
||||
@ -224,40 +228,32 @@ static void FTCS_1D(Grid &grid, Boundary &bc, double ×tep) {
|
||||
// inner cells
|
||||
// independent of boundary condition type
|
||||
for (int col = 1; col < colMax - 1; col++) {
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChange(grid, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChange(grid, row, col));
|
||||
}
|
||||
|
||||
// left boundary; hold column constant at index 0
|
||||
int col = 0;
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row,col)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeLeftBoundary(grid, bc, row, col)
|
||||
)
|
||||
;
|
||||
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeLeftBoundary(grid, bc, row, col));
|
||||
|
||||
// right boundary; hold column constant at max index
|
||||
col = colMax - 1;
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeRightBoundary(grid, bc, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeRightBoundary(grid, bc, row, col));
|
||||
|
||||
// overwrite obsolete concentrations
|
||||
grid.setConcentrations(concentrations_t1);
|
||||
}
|
||||
|
||||
|
||||
// FTCS solution for 2D grid
|
||||
static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, int numThreads) {
|
||||
static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep,
|
||||
int numThreads) {
|
||||
int rowMax = grid.getRow();
|
||||
int colMax = grid.getCol();
|
||||
double deltaRow = grid.getDeltaRow();
|
||||
@ -272,16 +268,11 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, int numThreads)
|
||||
#pragma omp parallel for num_threads(numThreads)
|
||||
for (int row = 1; row < rowMax - 1; row++) {
|
||||
for (int col = 1; col < colMax - 1; col++) {
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row, col)
|
||||
+ timestep / (deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChange(grid, row, col)
|
||||
)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChange(grid, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChange(grid, row, col)) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChange(grid, row, col));
|
||||
}
|
||||
}
|
||||
|
||||
@ -291,16 +282,11 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, int numThreads)
|
||||
int col = 0;
|
||||
#pragma omp parallel for num_threads(numThreads)
|
||||
for (int row = 1; row < rowMax - 1; row++) {
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row,col)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeLeftBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep / (deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChange(grid, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeLeftBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaRow * deltaRow) * (calcVerticalChange(grid, row, col));
|
||||
}
|
||||
|
||||
// right without corners / looping over rows
|
||||
@ -308,34 +294,24 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, int numThreads)
|
||||
col = colMax - 1;
|
||||
#pragma omp parallel for num_threads(numThreads)
|
||||
for (int row = 1; row < rowMax - 1; row++) {
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeRightBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep / (deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChange(grid, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeRightBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaRow * deltaRow) * (calcVerticalChange(grid, row, col));
|
||||
}
|
||||
|
||||
|
||||
// top without corners / looping over columns
|
||||
// hold row constant at index 0
|
||||
int row = 0;
|
||||
#pragma omp parallel for num_threads(numThreads)
|
||||
for (int col = 1; col < colMax - 1; col++) {
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row, col)
|
||||
+ timestep / (deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChangeTopBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChange(grid, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChangeTopBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChange(grid, row, col));
|
||||
}
|
||||
|
||||
// bottom without corners / looping over columns
|
||||
@ -343,84 +319,63 @@ static void FTCS_2D(Grid &grid, Boundary &bc, double ×tep, int numThreads)
|
||||
row = rowMax - 1;
|
||||
#pragma omp parallel for num_threads(numThreads)
|
||||
for (int col = 1; col < colMax - 1; col++) {
|
||||
concentrations_t1(row, col) = grid.getConcentrations()(row, col)
|
||||
+ timestep / (deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChangeBottomBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep / (deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChange(grid, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChangeBottomBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChange(grid, row, col));
|
||||
}
|
||||
|
||||
// corner top left
|
||||
// hold row and column constant at 0
|
||||
row = 0;
|
||||
col = 0;
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep/(deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeLeftBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep/(deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChangeTopBoundary(grid, bc, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeLeftBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChangeTopBoundary(grid, bc, row, col));
|
||||
|
||||
// corner top right
|
||||
// hold row constant at 0 and column constant at max index
|
||||
row = 0;
|
||||
col = colMax - 1;
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep/(deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeRightBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep/(deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChangeTopBoundary(grid, bc, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeRightBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChangeTopBoundary(grid, bc, row, col));
|
||||
|
||||
// corner bottom left
|
||||
// hold row constant at max index and column constant at 0
|
||||
row = rowMax - 1;
|
||||
col = 0;
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep/(deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeLeftBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep/(deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChangeBottomBoundary(grid, bc, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeLeftBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChangeBottomBoundary(grid, bc, row, col));
|
||||
|
||||
// corner bottom right
|
||||
// hold row and column constant at max index
|
||||
row = rowMax - 1;
|
||||
col = colMax - 1;
|
||||
concentrations_t1(row,col) = grid.getConcentrations()(row,col)
|
||||
+ timestep/(deltaCol*deltaCol)
|
||||
* (
|
||||
calcHorizontalChangeRightBoundary(grid, bc, row, col)
|
||||
)
|
||||
+ timestep/(deltaRow*deltaRow)
|
||||
* (
|
||||
calcVerticalChangeBottomBoundary(grid, bc, row, col)
|
||||
)
|
||||
;
|
||||
concentrations_t1(row, col) =
|
||||
grid.getConcentrations()(row, col) +
|
||||
timestep / (deltaCol * deltaCol) *
|
||||
(calcHorizontalChangeRightBoundary(grid, bc, row, col)) +
|
||||
timestep / (deltaRow * deltaRow) *
|
||||
(calcVerticalChangeBottomBoundary(grid, bc, row, col));
|
||||
|
||||
// overwrite obsolete concentrations
|
||||
grid.setConcentrations(concentrations_t1);
|
||||
// }
|
||||
}
|
||||
|
||||
|
||||
// entry point; differentiate between 1D and 2D grid
|
||||
static void FTCS(Grid &grid, Boundary &bc, double ×tep, int &numThreads) {
|
||||
if (grid.getDim() == 1) {
|
||||
@ -428,6 +383,7 @@ static void FTCS(Grid &grid, Boundary &bc, double ×tep, int &numThreads) {
|
||||
} else if (grid.getDim() == 2) {
|
||||
FTCS_2D(grid, bc, timestep, numThreads);
|
||||
} else {
|
||||
throw_invalid_argument("Error: Only 1- and 2-dimensional grids are defined!");
|
||||
throw_invalid_argument(
|
||||
"Error: Only 1- and 2-dimensional grids are defined!");
|
||||
}
|
||||
}
|
||||
|
||||
73
src/Grid.cpp
73
src/Grid.cpp
@ -1,10 +1,11 @@
|
||||
#include "TugUtils.cpp"
|
||||
#include <tug/Grid.hpp>
|
||||
#include <iostream>
|
||||
#include <tug/Grid.hpp>
|
||||
|
||||
Grid::Grid(int length) {
|
||||
if (length <= 3) {
|
||||
throw_invalid_argument("Given grid length too small. Must be greater than 3.");
|
||||
throw_invalid_argument(
|
||||
"Given grid length too small. Must be greater than 3.");
|
||||
}
|
||||
|
||||
this->row = 1;
|
||||
@ -19,7 +20,8 @@ Grid::Grid(int length) {
|
||||
|
||||
Grid::Grid(int row, int col) {
|
||||
if (row <= 3 || col <= 3) {
|
||||
throw_invalid_argument("Given grid dimensions too small. Must each be greater than 3.");
|
||||
throw_invalid_argument(
|
||||
"Given grid dimensions too small. Must each be greater than 3.");
|
||||
}
|
||||
|
||||
this->row = row;
|
||||
@ -36,23 +38,25 @@ Grid::Grid(int row, int col) {
|
||||
}
|
||||
|
||||
void Grid::setConcentrations(MatrixXd concentrations) {
|
||||
if (concentrations.rows() != this->row || concentrations.cols() != this->col) {
|
||||
throw_invalid_argument("Given matrix of concentrations mismatch with Grid dimensions!");
|
||||
if (concentrations.rows() != this->row ||
|
||||
concentrations.cols() != this->col) {
|
||||
throw_invalid_argument(
|
||||
"Given matrix of concentrations mismatch with Grid dimensions!");
|
||||
}
|
||||
|
||||
this->concentrations = concentrations;
|
||||
}
|
||||
|
||||
const MatrixXd Grid::getConcentrations() {
|
||||
return this->concentrations;
|
||||
}
|
||||
const MatrixXd Grid::getConcentrations() { return this->concentrations; }
|
||||
|
||||
void Grid::setAlpha(MatrixXd alpha) {
|
||||
if (dim != 1) {
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably use 2D setter function!");
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably "
|
||||
"use 2D setter function!");
|
||||
}
|
||||
if (alpha.rows() != 1 || alpha.cols() != this->col) {
|
||||
throw_invalid_argument("Given matrix of alpha coefficients mismatch with Grid dimensions!");
|
||||
throw_invalid_argument(
|
||||
"Given matrix of alpha coefficients mismatch with Grid dimensions!");
|
||||
}
|
||||
|
||||
this->alphaX = alpha;
|
||||
@ -60,13 +64,16 @@ void Grid::setAlpha(MatrixXd alpha) {
|
||||
|
||||
void Grid::setAlpha(MatrixXd alphaX, MatrixXd alphaY) {
|
||||
if (dim != 2) {
|
||||
throw_invalid_argument("Grid is not two dimensional, you should probably use 1D setter function!");
|
||||
throw_invalid_argument("Grid is not two dimensional, you should probably "
|
||||
"use 1D setter function!");
|
||||
}
|
||||
if (alphaX.rows() != this->row || alphaX.cols() != this->col) {
|
||||
throw_invalid_argument("Given matrix of alpha coefficients in x-direction mismatch with GRid dimensions!");
|
||||
throw_invalid_argument("Given matrix of alpha coefficients in x-direction "
|
||||
"mismatch with GRid dimensions!");
|
||||
}
|
||||
if (alphaY.rows() != this->row || alphaY.cols() != this->col) {
|
||||
throw_invalid_argument("Given matrix of alpha coefficients in y-direction mismatch with GRid dimensions!");
|
||||
throw_invalid_argument("Given matrix of alpha coefficients in y-direction "
|
||||
"mismatch with GRid dimensions!");
|
||||
}
|
||||
|
||||
this->alphaX = alphaX;
|
||||
@ -75,7 +82,8 @@ void Grid::setAlpha(MatrixXd alphaX, MatrixXd alphaY) {
|
||||
|
||||
const MatrixXd Grid::getAlpha() {
|
||||
if (dim != 1) {
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably use either getAlphaX() or getAlphaY()!");
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably "
|
||||
"use either getAlphaX() or getAlphaY()!");
|
||||
}
|
||||
|
||||
return this->alphaX;
|
||||
@ -83,7 +91,8 @@ const MatrixXd Grid::getAlpha() {
|
||||
|
||||
const MatrixXd Grid::getAlphaX() {
|
||||
if (dim != 2) {
|
||||
throw_invalid_argument("Grid is not two dimensional, you should probably use getAlpha()!");
|
||||
throw_invalid_argument(
|
||||
"Grid is not two dimensional, you should probably use getAlpha()!");
|
||||
}
|
||||
|
||||
return this->alphaX;
|
||||
@ -91,35 +100,32 @@ const MatrixXd Grid::getAlphaX() {
|
||||
|
||||
const MatrixXd Grid::getAlphaY() {
|
||||
if (dim != 2) {
|
||||
throw_invalid_argument("Grid is not two dimensional, you should probably use getAlpha()!");
|
||||
throw_invalid_argument(
|
||||
"Grid is not two dimensional, you should probably use getAlpha()!");
|
||||
}
|
||||
|
||||
return this->alphaY;
|
||||
}
|
||||
|
||||
int Grid::getDim() {
|
||||
return dim;
|
||||
}
|
||||
int Grid::getDim() { return dim; }
|
||||
|
||||
int Grid::getLength() {
|
||||
if (dim != 1) {
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably use getRow() or getCol()!");
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably "
|
||||
"use getRow() or getCol()!");
|
||||
}
|
||||
|
||||
return col;
|
||||
}
|
||||
|
||||
int Grid::getRow() {
|
||||
return row;
|
||||
}
|
||||
int Grid::getRow() { return row; }
|
||||
|
||||
int Grid::getCol() {
|
||||
return col;
|
||||
}
|
||||
int Grid::getCol() { return col; }
|
||||
|
||||
void Grid::setDomain(double domainLength) {
|
||||
if (dim != 1) {
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probaly use the 2D domain setter!");
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probaly "
|
||||
"use the 2D domain setter!");
|
||||
}
|
||||
if (domainLength <= 0) {
|
||||
throw_invalid_argument("Given domain length is not positive!");
|
||||
@ -131,7 +137,8 @@ void Grid::setDomain(double domainLength) {
|
||||
|
||||
void Grid::setDomain(double domainRow, double domainCol) {
|
||||
if (dim != 2) {
|
||||
throw_invalid_argument("Grid is not two dimensional, you should probably use the 1D domain setter!");
|
||||
throw_invalid_argument("Grid is not two dimensional, you should probably "
|
||||
"use the 1D domain setter!");
|
||||
}
|
||||
if (domainRow <= 0 || domainCol <= 0) {
|
||||
throw_invalid_argument("Given domain size is not positive!");
|
||||
@ -145,19 +152,19 @@ void Grid::setDomain(double domainRow, double domainCol) {
|
||||
|
||||
double Grid::getDelta() {
|
||||
if (dim != 1) {
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably use the 2D delta getters");
|
||||
throw_invalid_argument("Grid is not one dimensional, you should probably "
|
||||
"use the 2D delta getters");
|
||||
}
|
||||
|
||||
return this->deltaCol;
|
||||
}
|
||||
|
||||
double Grid::getDeltaCol() {
|
||||
return this->deltaCol;
|
||||
}
|
||||
double Grid::getDeltaCol() { return this->deltaCol; }
|
||||
|
||||
double Grid::getDeltaRow() {
|
||||
if (dim != 2) {
|
||||
throw_invalid_argument("Grid is not two dimensional, meaning there is no delta in y-direction!");
|
||||
throw_invalid_argument("Grid is not two dimensional, meaning there is no "
|
||||
"delta in y-direction!");
|
||||
}
|
||||
|
||||
return this->deltaRow;
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
#include <cmath>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
#include <omp.h>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <tug/Simulation.hpp>
|
||||
#include <omp.h>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
@ -13,10 +13,10 @@
|
||||
|
||||
#include "BTCSv2.cpp"
|
||||
|
||||
|
||||
using namespace std;
|
||||
|
||||
Simulation::Simulation(Grid &grid, Boundary &bc, APPROACH approach) : grid(grid), bc(bc) {
|
||||
Simulation::Simulation(Grid &grid, Boundary &bc, APPROACH approach)
|
||||
: grid(grid), bc(bc) {
|
||||
|
||||
this->approach = approach;
|
||||
this->solver = THOMAS_ALGORITHM_SOLVER;
|
||||
@ -39,7 +39,8 @@ void Simulation::setOutputCSV(CSV_OUTPUT csv_output) {
|
||||
}
|
||||
|
||||
void Simulation::setOutputConsole(CONSOLE_OUTPUT console_output) {
|
||||
if (console_output < CONSOLE_OUTPUT_OFF && console_output > CONSOLE_OUTPUT_VERBOSE) {
|
||||
if (console_output < CONSOLE_OUTPUT_OFF &&
|
||||
console_output > CONSOLE_OUTPUT_VERBOSE) {
|
||||
throw_invalid_argument("Invalid console output option given!");
|
||||
}
|
||||
|
||||
@ -71,7 +72,8 @@ void Simulation::setTimestep(double timestep) {
|
||||
|
||||
deltaRowSquare = grid.getDeltaRow() * grid.getDeltaRow();
|
||||
|
||||
minDeltaSquare = (deltaRowSquare < deltaColSquare) ? deltaRowSquare : deltaColSquare;
|
||||
minDeltaSquare =
|
||||
(deltaRowSquare < deltaColSquare) ? deltaRowSquare : deltaColSquare;
|
||||
maxAlphaX = grid.getAlphaX().maxCoeff();
|
||||
maxAlphaY = grid.getAlphaY().maxCoeff();
|
||||
maxAlpha = (maxAlphaX > maxAlphaY) ? maxAlphaX : maxAlphaY;
|
||||
@ -88,12 +90,16 @@ void Simulation::setTimestep(double timestep) {
|
||||
// Courant-Friedrichs-Lewy condition
|
||||
double cfl = minDeltaSquare / (4 * maxAlpha);
|
||||
|
||||
// stability equation from Wikipedia; might be useful if applied cfl does not work in some cases
|
||||
// double CFL_Wiki = 1 / (4 * maxAlpha * ((1/deltaRowSquare) + (1/deltaColSquare)));
|
||||
// stability equation from Wikipedia; might be useful if applied cfl does
|
||||
// not work in some cases double CFL_Wiki = 1 / (4 * maxAlpha *
|
||||
// ((1/deltaRowSquare) + (1/deltaColSquare)));
|
||||
|
||||
string approachPrefix = (approach == 0) ? "FTCS" : ((approach == 1) ? "BTCS" : "CRNI");
|
||||
cout << approachPrefix << "_" << dim << " :: CFL condition: " << cfl << endl;
|
||||
cout << approachPrefix << "_" << dim << " :: required dt=" << timestep << endl;
|
||||
string approachPrefix =
|
||||
(approach == 0) ? "FTCS" : ((approach == 1) ? "BTCS" : "CRNI");
|
||||
cout << approachPrefix << "_" << dim << " :: CFL condition: " << cfl
|
||||
<< endl;
|
||||
cout << approachPrefix << "_" << dim << " :: required dt=" << timestep
|
||||
<< endl;
|
||||
|
||||
if (timestep > cfl) {
|
||||
|
||||
@ -104,25 +110,23 @@ void Simulation::setTimestep(double timestep) {
|
||||
"conditions. Time duration was approximately preserved by "
|
||||
"adjusting internal number of iterations."
|
||||
<< endl;
|
||||
cout << approachPrefix << "_" << dim << " :: Required " << this->innerIterations
|
||||
cout << approachPrefix << "_" << dim << " :: Required "
|
||||
<< this->innerIterations
|
||||
<< " inner iterations with dt=" << this->timestep << endl;
|
||||
|
||||
} else {
|
||||
|
||||
this->timestep = timestep;
|
||||
cout << approachPrefix << "_" << dim << " :: No inner iterations required, dt=" << timestep << endl;
|
||||
|
||||
cout << approachPrefix << "_" << dim
|
||||
<< " :: No inner iterations required, dt=" << timestep << endl;
|
||||
}
|
||||
|
||||
} else {
|
||||
this->timestep = timestep;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
double Simulation::getTimestep() {
|
||||
return this->timestep;
|
||||
}
|
||||
double Simulation::getTimestep() { return this->timestep; }
|
||||
|
||||
void Simulation::setIterations(int iterations) {
|
||||
if (iterations <= 0) {
|
||||
@ -133,7 +137,8 @@ void Simulation::setIterations(int iterations) {
|
||||
|
||||
void Simulation::setSolver(SOLVER solver) {
|
||||
if (this->approach == FTCS_APPROACH) {
|
||||
cerr << "Warning: Solver was set, but FTCS approach initialized. Setting the solver "
|
||||
cerr << "Warning: Solver was set, but FTCS approach initialized. Setting "
|
||||
"the solver "
|
||||
"is thus without effect."
|
||||
<< endl;
|
||||
}
|
||||
@ -144,19 +149,17 @@ void Simulation::setSolver(SOLVER solver) {
|
||||
void Simulation::setNumberThreads(int numThreads) {
|
||||
if (numThreads > 0 && numThreads <= omp_get_num_procs()) {
|
||||
this->numThreads = numThreads;
|
||||
}
|
||||
else{
|
||||
} else {
|
||||
int maxThreadNumber = omp_get_num_procs();
|
||||
string outputMessage = "Number of threads exceeds the number of processor cores ("
|
||||
+ to_string(maxThreadNumber) + ") or is less than 1.";
|
||||
string outputMessage =
|
||||
"Number of threads exceeds the number of processor cores (" +
|
||||
to_string(maxThreadNumber) + ") or is less than 1.";
|
||||
|
||||
throw_invalid_argument(outputMessage);
|
||||
}
|
||||
}
|
||||
|
||||
int Simulation::getIterations() {
|
||||
return this->iterations;
|
||||
}
|
||||
int Simulation::getIterations() { return this->iterations; }
|
||||
|
||||
void Simulation::printConcentrationsConsole() {
|
||||
cout << grid.getConcentrations() << endl;
|
||||
@ -169,17 +172,20 @@ string Simulation::createCSVfile() {
|
||||
string appendIdentString;
|
||||
|
||||
// string approachString = (approach == 0) ? "FTCS" : "BTCS";
|
||||
string approachString = (approach == 0) ? "FTCS" : ((approach == 1) ? "BTCS" : "CRNI");
|
||||
string approachString =
|
||||
(approach == 0) ? "FTCS" : ((approach == 1) ? "BTCS" : "CRNI");
|
||||
string row = to_string(grid.getRow());
|
||||
string col = to_string(grid.getCol());
|
||||
string numIterations = to_string(iterations);
|
||||
|
||||
string filename = approachString + "_" + row + "_" + col + "_" + numIterations + ".csv";
|
||||
string filename =
|
||||
approachString + "_" + row + "_" + col + "_" + numIterations + ".csv";
|
||||
|
||||
while (filesystem::exists(filename)) {
|
||||
appendIdent += 1;
|
||||
appendIdentString = to_string(appendIdent);
|
||||
filename = approachString + "_" + row + "_" + col + "_" + numIterations + "-" + appendIdentString + ".csv";
|
||||
filename = approachString + "_" + row + "_" + col + "_" + numIterations +
|
||||
"-" + appendIdentString + ".csv";
|
||||
}
|
||||
|
||||
file.open(filename);
|
||||
@ -187,14 +193,18 @@ string Simulation::createCSVfile() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
// adds lines at the beginning of verbose output csv that represent the boundary conditions and their values
|
||||
// -1 in case of closed boundary
|
||||
// adds lines at the beginning of verbose output csv that represent the
|
||||
// boundary conditions and their values -1 in case of closed boundary
|
||||
if (csv_output == CSV_OUTPUT_XTREME) {
|
||||
IOFormat one_row(StreamPrecision, DontAlignCols, "", " ");
|
||||
file << bc.getBoundarySideValues(BC_SIDE_LEFT).format(one_row) << endl; // boundary left
|
||||
file << bc.getBoundarySideValues(BC_SIDE_RIGHT).format(one_row) << endl; // boundary right
|
||||
file << bc.getBoundarySideValues(BC_SIDE_TOP).format(one_row) << endl; // boundary top
|
||||
file << bc.getBoundarySideValues(BC_SIDE_BOTTOM).format(one_row) << endl; // boundary bottom
|
||||
file << bc.getBoundarySideValues(BC_SIDE_LEFT).format(one_row)
|
||||
<< endl; // boundary left
|
||||
file << bc.getBoundarySideValues(BC_SIDE_RIGHT).format(one_row)
|
||||
<< endl; // boundary right
|
||||
file << bc.getBoundarySideValues(BC_SIDE_TOP).format(one_row)
|
||||
<< endl; // boundary top
|
||||
file << bc.getBoundarySideValues(BC_SIDE_BOTTOM).format(one_row)
|
||||
<< endl; // boundary bottom
|
||||
file << endl << endl;
|
||||
}
|
||||
|
||||
@ -248,8 +258,8 @@ void Simulation::run() {
|
||||
FTCS(this->grid, this->bc, this->timestep, this->numThreads);
|
||||
|
||||
// if (i % (iterations * innerIterations / 100) == 0) {
|
||||
// double percentage = (double)i / ((double)iterations * (double)innerIterations) * 100;
|
||||
// if ((int)percentage % 10 == 0) {
|
||||
// double percentage = (double)i / ((double)iterations *
|
||||
// (double)innerIterations) * 100; if ((int)percentage % 10 == 0) {
|
||||
// cout << "Progress: " << percentage << "%" << endl;
|
||||
// }
|
||||
// }
|
||||
@ -267,7 +277,6 @@ void Simulation::run() {
|
||||
}
|
||||
|
||||
BTCS_LU(this->grid, this->bc, this->timestep, this->numThreads);
|
||||
|
||||
}
|
||||
} else if (solver == THOMAS_ALGORITHM_SOLVER) {
|
||||
for (int i = 0; i < iterations; i++) {
|
||||
@ -279,7 +288,6 @@ void Simulation::run() {
|
||||
}
|
||||
|
||||
BTCS_Thomas(this->grid, this->bc, this->timestep, this->numThreads);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -288,7 +296,8 @@ void Simulation::run() {
|
||||
double beta = 0.5;
|
||||
|
||||
// TODO this implementation is very inefficient!
|
||||
// a separate implementation that sets up a specific tridiagonal matrix for Crank-Nicolson would be better
|
||||
// a separate implementation that sets up a specific tridiagonal matrix for
|
||||
// Crank-Nicolson would be better
|
||||
MatrixXd concentrations;
|
||||
MatrixXd concentrationsFTCS;
|
||||
MatrixXd concentrationsResult;
|
||||
@ -305,14 +314,15 @@ void Simulation::run() {
|
||||
concentrationsFTCS = grid.getConcentrations();
|
||||
grid.setConcentrations(concentrations);
|
||||
BTCS_Thomas(this->grid, this->bc, this->timestep, this->numThreads);
|
||||
concentrationsResult = beta * concentrationsFTCS + (1-beta) * grid.getConcentrations();
|
||||
concentrationsResult =
|
||||
beta * concentrationsFTCS + (1 - beta) * grid.getConcentrations();
|
||||
grid.setConcentrations(concentrationsResult);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
auto end = std::chrono::high_resolution_clock::now();
|
||||
auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
|
||||
auto milliseconds =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin);
|
||||
|
||||
if (this->console_output > CONSOLE_OUTPUT_OFF) {
|
||||
printConcentrationsConsole();
|
||||
@ -321,11 +331,12 @@ void Simulation::run() {
|
||||
printConcentrationsCSV(filename);
|
||||
}
|
||||
if (this->time_measure > TIME_MEASURE_OFF) {
|
||||
string approachString = (approach == 0) ? "FTCS" : ((approach == 1) ? "BTCS" : "CRNI");
|
||||
string approachString =
|
||||
(approach == 0) ? "FTCS" : ((approach == 1) ? "BTCS" : "CRNI");
|
||||
string dimString = (grid.getDim() == 1) ? "-1D" : "-2D";
|
||||
cout << approachString << dimString << ":: run() finished in " << milliseconds.count() << "ms" << endl;
|
||||
cout << approachString << dimString << ":: run() finished in "
|
||||
<< milliseconds.count() << "ms" << endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@ -29,7 +29,8 @@ using namespace std;
|
||||
})
|
||||
|
||||
// calculates arithmetic or harmonic mean of alpha between two cells
|
||||
static double calcAlphaIntercell(const double &alpha1, const double &alpha2, bool useHarmonic = true) {
|
||||
static double calcAlphaIntercell(const double &alpha1, const double &alpha2,
|
||||
bool useHarmonic = true) {
|
||||
if (useHarmonic) {
|
||||
return double(2) / ((double(1) / alpha1) + (double(1) / alpha2));
|
||||
} else {
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <Eigen/Core>
|
||||
#include <Eigen/Dense>
|
||||
#include <fstream>
|
||||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
@ -32,7 +32,9 @@ MatrixXd CSV2Eigen(string file2Convert) {
|
||||
}
|
||||
}
|
||||
|
||||
return Map<Matrix<double, Dynamic, Dynamic, RowMajor>>(matrixEntries.data(), matrixRowNumber, matrixEntries.size() / matrixRowNumber);
|
||||
return Map<Matrix<double, Dynamic, Dynamic, RowMajor>>(
|
||||
matrixEntries.data(), matrixRowNumber,
|
||||
matrixEntries.size() / matrixRowNumber);
|
||||
}
|
||||
|
||||
bool checkSimilarity(MatrixXd a, MatrixXd b, double precision = 1e-5) {
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
#include <stdio.h>
|
||||
#include <doctest/doctest.h>
|
||||
#include <tug/Boundary.hpp>
|
||||
#include <string>
|
||||
#include <typeinfo>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <tug/Boundary.hpp>
|
||||
#include <typeinfo>
|
||||
|
||||
TEST_CASE("BoundaryElement") {
|
||||
|
||||
@ -36,7 +36,8 @@ TEST_CASE("Boundary Class"){
|
||||
CHECK_NOTHROW(Boundary boundary(grid1D));
|
||||
CHECK_EQ(boundary1D.getBoundarySide(BC_SIDE_LEFT).size(), 1);
|
||||
CHECK_EQ(boundary1D.getBoundarySide(BC_SIDE_RIGHT).size(), 1);
|
||||
CHECK_EQ(boundary1D.getBoundaryElementType(BC_SIDE_LEFT, 0), BC_TYPE_CLOSED);
|
||||
CHECK_EQ(boundary1D.getBoundaryElementType(BC_SIDE_LEFT, 0),
|
||||
BC_TYPE_CLOSED);
|
||||
CHECK_THROWS(boundary1D.getBoundarySide(BC_SIDE_TOP));
|
||||
CHECK_THROWS(boundary1D.getBoundarySide(BC_SIDE_BOTTOM));
|
||||
CHECK_NOTHROW(boundary1D.setBoundarySideClosed(BC_SIDE_LEFT));
|
||||
@ -44,8 +45,10 @@ TEST_CASE("Boundary Class"){
|
||||
CHECK_NOTHROW(boundary1D.setBoundarySideConstant(BC_SIDE_LEFT, 1.0));
|
||||
CHECK_EQ(boundary1D.getBoundaryElementValue(BC_SIDE_LEFT, 0), 1.0);
|
||||
CHECK_THROWS(boundary1D.getBoundaryElementValue(BC_SIDE_LEFT, 2));
|
||||
CHECK_EQ(boundary1D.getBoundaryElementType(BC_SIDE_LEFT, 0), BC_TYPE_CONSTANT);
|
||||
CHECK_EQ(boundary1D.getBoundaryElement(BC_SIDE_LEFT, 0).getType(), boundary1DVector[0].getType());
|
||||
CHECK_EQ(boundary1D.getBoundaryElementType(BC_SIDE_LEFT, 0),
|
||||
BC_TYPE_CONSTANT);
|
||||
CHECK_EQ(boundary1D.getBoundaryElement(BC_SIDE_LEFT, 0).getType(),
|
||||
boundary1DVector[0].getType());
|
||||
}
|
||||
|
||||
SUBCASE("Boundaries 2D case") {
|
||||
@ -54,7 +57,8 @@ TEST_CASE("Boundary Class"){
|
||||
CHECK_EQ(boundary2D.getBoundarySide(BC_SIDE_RIGHT).size(), 10);
|
||||
CHECK_EQ(boundary2D.getBoundarySide(BC_SIDE_TOP).size(), 12);
|
||||
CHECK_EQ(boundary2D.getBoundarySide(BC_SIDE_BOTTOM).size(), 12);
|
||||
CHECK_EQ(boundary2D.getBoundaryElementType(BC_SIDE_LEFT, 0), BC_TYPE_CLOSED);
|
||||
CHECK_EQ(boundary2D.getBoundaryElementType(BC_SIDE_LEFT, 0),
|
||||
BC_TYPE_CLOSED);
|
||||
CHECK_NOTHROW(boundary2D.getBoundarySide(BC_SIDE_TOP));
|
||||
CHECK_NOTHROW(boundary2D.getBoundarySide(BC_SIDE_BOTTOM));
|
||||
CHECK_NOTHROW(boundary2D.setBoundarySideClosed(BC_SIDE_LEFT));
|
||||
@ -62,7 +66,9 @@ TEST_CASE("Boundary Class"){
|
||||
CHECK_NOTHROW(boundary2D.setBoundarySideConstant(BC_SIDE_LEFT, 1.0));
|
||||
CHECK_EQ(boundary2D.getBoundaryElementValue(BC_SIDE_LEFT, 0), 1.0);
|
||||
CHECK_THROWS(boundary2D.getBoundaryElementValue(BC_SIDE_LEFT, 12));
|
||||
CHECK_EQ(boundary2D.getBoundaryElementType(BC_SIDE_LEFT, 0), BC_TYPE_CONSTANT);
|
||||
CHECK_EQ(boundary2D.getBoundaryElement(BC_SIDE_LEFT, 0).getType(), boundary1DVector[0].getType());
|
||||
CHECK_EQ(boundary2D.getBoundaryElementType(BC_SIDE_LEFT, 0),
|
||||
BC_TYPE_CONSTANT);
|
||||
CHECK_EQ(boundary2D.getBoundaryElement(BC_SIDE_LEFT, 0).getType(),
|
||||
boundary1DVector[0].getType());
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,5 @@
|
||||
#include <doctest/doctest.h>
|
||||
#include <../src/FTCS.cpp>
|
||||
#include <doctest/doctest.h>
|
||||
#include <limits>
|
||||
|
||||
TEST_CASE("Maths") {
|
||||
@ -7,13 +7,13 @@ TEST_CASE("Maths") {
|
||||
double alpha1 = 10;
|
||||
double alpha2 = 20;
|
||||
double average = 15;
|
||||
double harmonicMean = double(2) / ((double(1)/alpha1)+(double(1)/alpha2));
|
||||
double harmonicMean =
|
||||
double(2) / ((double(1) / alpha1) + (double(1) / alpha2));
|
||||
|
||||
// double difference = std::fabs(calcAlphaIntercell(alpha1, alpha2) - harmonicMean);
|
||||
// CHECK(difference < std::numeric_limits<double>::epsilon());
|
||||
// double difference = std::fabs(calcAlphaIntercell(alpha1, alpha2) -
|
||||
// harmonicMean); CHECK(difference <
|
||||
// std::numeric_limits<double>::epsilon());
|
||||
CHECK_EQ(calcAlphaIntercell(alpha1, alpha2), harmonicMean);
|
||||
CHECK_EQ(calcAlphaIntercell(alpha1, alpha2, false), average);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
#include <Eigen/Core>
|
||||
#include <doctest/doctest.h>
|
||||
#include <tug/Grid.hpp>
|
||||
#include <Eigen/Core>
|
||||
|
||||
TEST_CASE("1D Grid, too small length") {
|
||||
int l = 2;
|
||||
@ -112,7 +112,6 @@ TEST_CASE("2D Grid quadratic") {
|
||||
MatrixXd concentrations = MatrixXd::Constant(rc, rc, 2);
|
||||
CHECK_NOTHROW(grid.setConcentrations(concentrations));
|
||||
|
||||
|
||||
// false concentrations matrix
|
||||
MatrixXd wConcentrations = MatrixXd::Constant(rc, rc + 3, 1);
|
||||
CHECK_THROWS(grid.setConcentrations(wConcentrations));
|
||||
@ -195,7 +194,6 @@ TEST_CASE("2D Grid non-quadratic") {
|
||||
MatrixXd concentrations = MatrixXd::Constant(r, c, 2);
|
||||
CHECK_NOTHROW(grid.setConcentrations(concentrations));
|
||||
|
||||
|
||||
// false concentrations matrix
|
||||
MatrixXd wConcentrations = MatrixXd::Constant(r, c + 3, 6);
|
||||
CHECK_THROWS(grid.setConcentrations(wConcentrations));
|
||||
|
||||
@ -1,19 +1,19 @@
|
||||
#include <stdio.h>
|
||||
#include <doctest/doctest.h>
|
||||
#include <tug/Simulation.hpp>
|
||||
#include "TestUtils.cpp"
|
||||
#include <doctest/doctest.h>
|
||||
#include <stdio.h>
|
||||
#include <string>
|
||||
#include <tug/Simulation.hpp>
|
||||
|
||||
// include the configured header file
|
||||
#include <testSimulation.hpp>
|
||||
|
||||
static Grid setupSimulation(APPROACH approach, double timestep, int iterations) {
|
||||
static Grid setupSimulation(APPROACH approach, double timestep,
|
||||
int iterations) {
|
||||
int row = 11;
|
||||
int col = 11;
|
||||
int domain_row = 10;
|
||||
int domain_col = 10;
|
||||
|
||||
|
||||
// Grid
|
||||
Grid grid = Grid(row, col);
|
||||
grid.setDomain(domain_row, domain_col);
|
||||
@ -40,11 +40,9 @@ static Grid setupSimulation(APPROACH approach, double timestep, int iterations)
|
||||
}
|
||||
grid.setAlpha(alpha, alpha);
|
||||
|
||||
|
||||
// Boundary
|
||||
Boundary bc = Boundary(grid);
|
||||
|
||||
|
||||
// Simulation
|
||||
Simulation sim = Simulation(grid, bc, approach);
|
||||
// sim.setOutputConsole(CONSOLE_OUTPUT_ON);
|
||||
@ -54,7 +52,6 @@ static Grid setupSimulation(APPROACH approach, double timestep, int iterations)
|
||||
|
||||
// RUN
|
||||
return grid;
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("equality to reference matrix with FTCS") {
|
||||
@ -91,9 +88,7 @@ TEST_CASE("Simulation environment"){
|
||||
Boundary boundary(grid);
|
||||
Simulation sim(grid, boundary, FTCS_APPROACH);
|
||||
|
||||
SUBCASE("default paremeters") {
|
||||
CHECK_EQ(sim.getIterations(), -1);
|
||||
}
|
||||
SUBCASE("default paremeters") { CHECK_EQ(sim.getIterations(), -1); }
|
||||
|
||||
SUBCASE("set iterations") {
|
||||
CHECK_NOTHROW(sim.setIterations(2000));
|
||||
@ -107,4 +102,3 @@ TEST_CASE("Simulation environment"){
|
||||
CHECK_THROWS(sim.setTimestep(-0.3));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user