hummingbird
Solving the self-adjoint angular flux transport equation using spectral elements on Cartesian geometry
Loading...
Searching...
No Matches
main.cc
Go to the documentation of this file.
1#include <filesystem>
2#include <fstream>
3#include <iostream>
4#include <limits>
5#include <nlohmann/json.hpp>
6#include <valarray>
7
8#include "banks/bc_bank.h"
10#include "banks/source_bank.h"
11#include "input_parameters.h"
12#include "mesh/mesh.h"
13#include "problem/sem_problem.h"
16#include "simulation.h"
17#include "utils/json.h"
18#include "utils/misc.h"
19#include "utils/output.h"
20#include "utils/results.h"
21
22using nlohmann::json;
23using namespace hummingbird;
24
25int main(int argc, char** argv) {
26 if (argc < 2) {
27 printf("Usage: %s <input_file_name>\n", argv[0]);
28 return 1;
29 };
30
32
33 std::filesystem::path working_directory(argv[1]);
34 working_directory.remove_filename();
35
36 json user_input_json = JSONFromFile(argv[1]);
37
38 InputParams input_params = user_input_json.get<InputParams>();
39 input_params.mesh_params.mesh_file =
40 (working_directory / input_params.mesh_params.mesh_file).string();
41
42 print_input_files(argv[1], input_params.mesh_params.mesh_file);
43
44 fmt::print("Creating banks...");
45 // Create banks
46 BCBank bc_bank(user_input_json);
47 MaterialBank material_bank(user_input_json);
48 SourceBank source_bank(user_input_json);
49 fmt::print("Done\n");
50
51 fmt::print("Creating quadrature sets...");
52 // Create quadrature sets
53 GaussLobattoLegendre gll_quad(input_params.sem_params.n_points);
54 AngularQuadratureSet angular_quad(
58 size_t n_ordinates = angular_quad.get()->n_points();
59 fmt::print("Done\n\n");
60
61 // build Mesh object
62 Mesh mesh(input_params.mesh_params.mesh_file);
63 mesh.Prepare(gll_quad);
64 mesh.ResolveIDs(material_bank, source_bank, bc_bank);
65 mesh.FindBoundaryNodes();
66 mesh.SetOutwardNormals();
67 mesh.InitializeNodeSolutions(n_ordinates, *angular_quad.get(), source_bank);
68
69 print_mesh_info(mesh.n_nodes(), mesh.n_elements(), mesh.dimension());
70
71 // build SEMProblem
72 SEMProblem sem_problem(input_params.sem_params.fe_formulation, mesh,
73 angular_quad.get()->n_points());
74
75 Simulation simulation;
76
77 fmt::print("Beginning source iterations.\n\n");
79
80 std::valarray<double> old_scalar_flux(0.0, sem_problem.get()->n_dofs());
81 std::valarray<double> new_scalar_flux(0.0, sem_problem.get()->n_dofs());
82 for (auto s_iter = 1;
83 s_iter <= input_params.source_iter_params.max_iterations; s_iter++) {
85 simulation.flux_relative_error, s_iter);
86
87 if (simulation.flux_relative_error <
88 input_params.source_iter_params.tolerance)
89 break;
90
91 // Solve for all ordinates
92 for (auto n = 0; n < n_ordinates; n++) {
93 const auto ordinate = angular_quad.get()->GetAbscissa(n);
94
95 // assemble to global system data
96 auto global_matrix_data = sem_problem.get()->AssembleGlobalMatrixData(
97 mesh, gll_quad, material_bank, ordinate);
98 auto global_forcing_data = sem_problem.get()->AssembleGlobalForcingData(
99 gll_quad, mesh, material_bank, source_bank, ordinate, n,
100 *angular_quad.get());
101
102 // form linear system
103 sem_problem.get()->AssembleGlobalSystem(global_matrix_data, n);
104 sem_problem.get()->AssembleGlobalForcing(global_forcing_data, n);
105 sem_problem.get()->ApplyBCs(mesh, ordinate, bc_bank, n);
106
107 // solve system
108 sem_problem.get()->Solve(n);
109 }
110 // update scattering source
111 mesh.UpdateNodeAngularFluxes(sem_problem, n_ordinates);
112 mesh.UpdateNodeScalarFluxes(*angular_quad.get());
113
114 // check source iteration convergence
115 for (auto i = 0; i < mesh.n_nodes(); i++)
116 new_scalar_flux[i] = mesh.GetNode(i).scalar_flux;
117 std::valarray<double> error = new_scalar_flux - old_scalar_flux;
118
119 double new_l2_error = 0;
120 double new_flux_l2 = 0;
121 for (const auto& elem : mesh.elements()) {
122 std::valarray<double> element_error(0.0, gll_quad.n_points());
123 std::valarray<double> element_flux(0.0, gll_quad.n_points());
124 for (auto i = 0; i < gll_quad.n_points(); i++) {
125 element_error[i] = error[elem->node_ids()[i]];
126 element_flux[i] = new_scalar_flux[elem->node_ids()[i]];
127 }
128 new_l2_error +=
129 gll_quad.IntegrateGridFunction(element_error * element_error);
130 new_flux_l2 +=
131 gll_quad.IntegrateGridFunction(element_flux * element_flux);
132 }
133 new_l2_error = std::sqrt(new_l2_error);
134 new_flux_l2 = std::sqrt(new_flux_l2);
135
136 simulation.flux_relative_error =
137 (new_flux_l2 == 0.0)
138 ? (new_l2_error == 0.0 ? 0.0
139 : std::numeric_limits<double>::infinity())
140 : new_l2_error / new_flux_l2;
141
142 simulation.flux_error_l2 = new_l2_error;
143 old_scalar_flux = new_scalar_flux;
144 new_scalar_flux = 0.0; // this sets all flux values to 0
145 }
146 print_scatter_complete(simulation.k_eff,
147 input_params.problem_params.run_mode);
148
149 fmt::print("Exporting results...");
150 // export results
151 Results results(input_params.problem_params.name,
152 input_params.problem_params.output_format, mesh.nodes(),
153 *angular_quad.get(), mesh.dimension());
154 results.Export();
155 fmt::print("Done.\n\n");
156
157 return 0;
158}
Wrapper class that owns an angular QuadratureBase<Ordinate> of the type corresponding to the given An...
const QuadratureBase< Ordinate > * get() const
Get the underlying quadrature set object.
Bank holding the boundary conditions defined in the input file.
Definition bc_bank.h:21
Class defining a 1D Gauss-Legendre-Lobatto quadrature set on [-1,1]. The quadrature set approximates ...
double IntegrateGridFunction(const std::valarray< double > &grid_function_vals)
Integrate a function defined on the abscissae given in the order the abscissae are stored.
Bank holding the materials defined in the input file.
Class defing a mesh.
Definition mesh.h:27
const Node & GetNode(const size_t id) const
Get Node by ID.
Definition mesh.h:93
void ResolveIDs(const MaterialBank &material_bank, const SourceBank &source_bank, const BCBank &bc_bank)
Resolve the raw gmsh Physical Group tags currently stored in material_id()/source_id()/bc_id (set fro...
Definition mesh.cc:337
size_t n_nodes() const
Get the number of nodes in the mesh.
Definition mesh.h:107
void FindBoundaryNodes()
Populates the boundary_node_ids_ member. Must be called after ResolveIDs!
Definition mesh.cc:367
size_t n_elements() const
Get the number of Elements in the mesh.
Definition mesh.h:100
const std::vector< Node > & nodes() const
Get the Nodes in the mesh.
Definition mesh.h:76
void SetOutwardNormals()
Set the outward-pointing unit normal vector on each boundary node (Node::outward_normal),...
Definition mesh.cc:373
void InitializeNodeSolutions(const size_t n_ordinates, const QuadratureBase< Ordinate > &angular_quad_set, const SourceBank &source_bank)
Initialize node solution vectors to the correct lengths. Scalar and angular fluxes are set to 0 and s...
Definition mesh.cc:320
void UpdateNodeScalarFluxes(const QuadratureBase< Ordinate > &angular_quad_set)
Update the nodes' scalar flux values.
Definition mesh.cc:386
void Prepare(const GaussLobattoLegendre &gll_quadrature)
Prepare the mesh for running a simulation by generating interior nodes using the GLL quadrature set,...
Definition mesh.cc:54
void UpdateNodeAngularFluxes(const SEMProblem &sem_problem, const size_t n_ordinates)
Update the nodes' angular flux values.
Definition mesh.cc:391
unsigned int dimension() const
Get the spatial dimension of the mesh, derived from the elements it contains (e.g....
Definition mesh.h:116
const std::vector< std::unique_ptr< Element > > & elements() const
Get the elements in the mesh.
Definition mesh.h:83
void Solve(const size_t ordinate_index)
Solve the linear system using Armadillo's sparse matrix solver. Stores the results in the solution_ve...
virtual std::vector< GlobalForcingData > AssembleGlobalForcingData(const GaussLobattoLegendre &gll_quad, Mesh &mesh, const MaterialBank &material_bank, const SourceBank &source_bank, const Ordinate &ordinate, const size_t ordinate_index, const QuadratureBase< Ordinate > &angular_quad_set)=0
Assemble the vector of GlobalForcingData structs that will be using in AssembleGlobalForcing to creat...
virtual std::vector< GlobalMatrixData > AssembleGlobalMatrixData(const Mesh &mesh, const GaussLobattoLegendre &gll_quad, const MaterialBank &material_bank, const Ordinate &ordinate)=0
Assemble the vector of GlobalMatrixData structs that will be used in AssembleGlobalSystem to create t...
void ApplyBCs(const Mesh &mesh, const Ordinate &ordinate, const BCBank &bc_bank, const size_t ordinate_index)
Apply boundary conditions by adding in values needed at boundary nodes. Internally,...
size_t n_dofs() const
Get the number of degrees of freedom.
void AssembleGlobalSystem(const std::vector< GlobalMatrixData > &global_matrix_data, const size_t ordinate_index)
Assemble the global element system. That is, form the matrix in . This only should be called once pe...
void AssembleGlobalForcing(const std::vector< GlobalForcingData > &global_forcing_data, const size_t ordinate_index)
Assemble the global forcing vector for a given ordinate index. The global vector is sized from the n_...
T GetAbscissa(const unsigned int index) const
Get the abscissa corresponding to the index.
size_t n_points() const
Get total number of abscissas.
Exports simulation results to a file.
Definition results.h:20
void Export()
Export the results in the format given by output_format_.
Definition results.cc:19
Wrapper class that owns a ProblemBase of the type corresponding to the given FEFormulation.
Definition sem_problem.h:18
ProblemBase * get() const
Get the underlying ProblemBase object.
Definition sem_problem.h:37
Bank holding the volumetric sources defined in the input file.
Definition source_bank.h:25
int main(int argc, char **argv)
Definition main.cc:25
void print_input_files(const std::string input, const std::string mesh)
Print the paths of the input and mesh files being used.
Definition output.cc:30
void print_scatter_status(const double flux_l2_error, const double relative_error, const unsigned int iter)
Print the scalar flux convergence status for the current source iteration.
Definition output.cc:43
void print_scatter_complete(const double final_k_eff, const RunMode sim_typ)
Print a message indicating source iterations have completed.
Definition output.cc:58
json JSONFromFile(const std::string filename)
Create a json object from a string. Will throw an error if the file could not be opened.
Definition json.cc:11
void print_header()
Print the program banner, description, and license information.
Definition output.cc:6
void print_mesh_info(const size_t n_nodes, const size_t n_elements, const unsigned int dimension)
Print information about the mesh.
Definition output.cc:66
void print_columns()
Print the column headers for the k-eff/scatter iteration status table.
Definition output.cc:23
AngularQuadSet angular_quad_set
Angular quadrature set to use.
unsigned int n_polar
Number of polar quadrature points.
unsigned int n_azim
Number of azimuthal quadrature points.
All parameters parsed from the input file.
MeshParams mesh_params
Mesh parameters.
AngularTreatmentParams angular_treatment_params
Angular treatment parameters.
SourceIterationParams source_iter_params
Source iteration parameters.
ProblemParams problem_params
Problem parameters.
SpectralElementParams sem_params
Spectral element parameters.
std::string mesh_file
Path to the gmsh .msh file.
double scalar_flux
Scalar flux on the node.
Definition node.h:48
OutputFormat output_format
Format results are exported in.
RunMode run_mode
Run mode for the simulation.
std::string name
Problem name.
Tracks the running state of a simulation across source iterations.
Definition simulation.h:15
double flux_relative_error
Relative L2 error in the scalar flux (flux_error_l2 normalized by the current iteration's flux L2 nor...
Definition simulation.h:33
double flux_error_l2
L2 norm of the change in scalar flux between the current and previous source iteration.
Definition simulation.h:21
double k_eff
Effective multiplication factor.
Definition simulation.h:17
double tolerance
Convergence tolerance for source iteration.
unsigned int max_iterations
Maximum number of source iterations before giving up.
FEFormulation fe_formulation
Finite element formulation to use.
unsigned int n_points
Number of points per spectral element.