Chi-Tech
main_exec.cc
Go to the documentation of this file.
1#include "diffusion_solver.h"
2
4
5#include "chi_runtime.h"
6#include "chi_log.h"
7
8#include "utils/chi_timer.h"
9
10//###################################################################
11/**Executes the diffusion solver using the PETSc library.*/
12int chi_diffusion::Solver::ExecuteS(bool suppress_assembly,
13 bool suppress_solve)
14{
16
17 if (Chi::material_stack.empty())
18 {
20 << "No materials added to simulation. Add materials.";
21 exit(0);
22 }
23
24 VecSet(x_, 0.0);
25 VecSet(b_, 0.0);
26
27 if (!suppress_assembly)
29 << TextName() << ": Assembling A locally";
30
31 //================================================== Loop over locally owned
32 // cells
33 auto fem_method = basic_options_("discretization_method").StringValue();
34 if (fem_method == "PWLC")
35 {
36 if (!suppress_assembly)
37 for (auto& cell : grid_ptr_->local_cells)
39 else {}
40 }
41 else if (fem_method == "PWLD_MIP")
42 {
43 if (!suppress_assembly)
44 for (auto& cell : grid_ptr_->local_cells)
46 else
47 for (auto& cell : grid_ptr_->local_cells)
48 PWLD_Assemble_b(cell, gi_);
49 }
50 else
51 {
53 << "Diffusion Solver: Finite Element Discretization "
54 "method not specified.";
55 Chi::Exit(EXIT_FAILURE);
56 }
57
58 if (!suppress_assembly)
60 << TextName() << ": Done Assembling A locally";
61 MPI_Barrier(Chi::mpi.comm);
62
63 //=================================== Call matrix assembly
64 if (verbose_info_ ||
65 Chi::log.GetVerbosity() >= chi::ChiLog::LOG_0VERBOSE_1)
68 << TextName() << ": Communicating matrix assembly";
69
70 if (!suppress_assembly)
71 {
73 << TextName() << ": Assembling A globally";
74 MatAssemblyBegin(A_, MAT_FINAL_ASSEMBLY);
75 MatAssemblyEnd(A_, MAT_FINAL_ASSEMBLY);
76
77 //================================= Matrix symmetry check
78// PetscBool is_symmetric;
79// ierr = MatIsSymmetric(A,1.0e-4,&is_symmetric);
80// if (!is_symmetric)
81// {
82// chi::log.Log0Warning()
83// << "Assembled matrix is not symmetric";
84// }
85
86 //================================= Matrix diagonal check
88 << TextName() << ": Diagonal check";
89 PetscBool missing_diagonal;
90 PetscInt row;
91 MatMissingDiagonal(A_, &missing_diagonal, &row);
92 if (missing_diagonal)
94 << TextName() << ": Missing diagonal detected";
95
96 //================================= Matrix sparsity info
97 MatInfo info;
98 ierr_ = MatGetInfo(A_, MAT_GLOBAL_SUM, &info);
99
100 Chi::log.Log() << "Number of mallocs used = " << info.mallocs
101 << "\nNumber of non-zeros allocated = "
102 << info.nz_allocated
103 << "\nNumber of non-zeros used = "
104 << info.nz_used
105 << "\nNumber of unneeded non-zeros = "
106 << info.nz_unneeded;
107 }
108 if (verbose_info_ ||
109 Chi::log.GetVerbosity() >= chi::ChiLog::LOG_0VERBOSE_1)
111 << TextName() << ": Assembling x and b";
112 VecAssemblyBegin(x_);
113 VecAssemblyEnd(x_);
114 VecAssemblyBegin(b_);
115 VecAssemblyEnd(b_);
116
118
119 //=================================== Execute solve
120 if (suppress_solve)
121 {
122 Chi::log.Log()
124 << TextName()
125 << ": Setting up solver and preconditioner\n";
126 PCSetUp(pc_);
127 KSPSetUp(ksp_);
128 }
129 else
130 {
131 if (verbose_info_ ||
132 Chi::log.GetVerbosity() >= chi::ChiLog::LOG_0VERBOSE_1)
133 Chi::log.Log()
135 << TextName() << ": Solving system\n";
136 t_solve_.Reset();
137 PCSetUp(pc_);
138 KSPSetUp(ksp_);
139 KSPSolve(ksp_, b_, x_);
140 time_solve_ = t_solve_.GetTime() / 1000.0;
141
142 //=================================== Populate field vector
143// if (fem_method == "PWLD_MIP" or fem_method == "PWLD_MIP_GAGG")
144// {
145// const double* x_ref;
146// VecGetArrayRead(x,&x_ref);
147//
148// for (int i=0; i < local_dof_count; i++)
149// pwld_phi_local[i] = x_ref[i];
150//
151// VecRestoreArrayRead(x,&x_ref);
152// }
153
154 //=================================== Get convergence reason
155 KSPConvergedReason reason;
156 KSPGetConvergedReason(ksp_, &reason);
157 if (verbose_info_ || reason != KSP_CONVERGED_RTOL)
158 Chi::log.Log()
159 << "Convergence reason: "
161
162 //=================================== Location wise view
163 if (Chi::mpi.location_id == 0)
164 {
165 int64_t its;
166 ierr_ = KSPGetIterationNumber(ksp_, &its);
167 Chi::log.Log()
169 << TextName() << "[g=" << gi_ << "-" << gi_ + G_ - 1
170 << "]: Number of iterations =" << its;
171
172 if (verbose_info_ ||
173 Chi::log.GetVerbosity() >= chi::ChiLog::LOG_0VERBOSE_1)
174 {
175 Chi::log.Log() << "Timing:";
176 Chi::log.Log() << "Assembling the matrix: " << time_assembly_;
177 Chi::log.Log() << "Solving the system : " << time_solve_;
178 }
179 }
180
182
183 if (verbose_info_ ||
184 Chi::log.GetVerbosity() >= chi::ChiLog::LOG_0VERBOSE_1)
185 Chi::log.Log() << "Diffusion Solver execution completed!\n";
186 }//if not suppressed solve
187
188 return 0;
189}
static std::vector< chi_physics::MaterialPtr > material_stack
Definition: chi_runtime.h:90
static chi::Timer program_timer
Definition: chi_runtime.h:79
static void Exit(int error_code)
Definition: chi_runtime.cc:342
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream LogAllError()
Definition: chi_log.h:239
LogStream Log0Error()
Definition: chi_log.h:232
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
@ LOG_0VERBOSE_1
Used only if verbosity level equals 1.
Definition: chi_log.h:203
void Reset()
Definition: chi_timer.cc:16
double GetTime() const
Definition: chi_timer.cc:23
std::string GetTimeString() const
Definition: chi_timer.cc:38
void CFEM_Assemble_A_and_b(chi_mesh::Cell &cell, int group=0)
Definition: assemble_pwlc.cc:7
void UpdateFieldFunctions()
Definition: general.cc:185
void PWLD_Assemble_b(const chi_mesh::Cell &cell, int component=0)
int ExecuteS(bool suppress_assembly=false, bool suppress_solve=false)
Definition: main_exec.cc:12
chi_mesh::MeshContinuumPtr grid_ptr_
void PWLD_Assemble_A_and_b(const chi_mesh::Cell &cell, int component=0)
std::string TextName() const
Definition: chi_solver.cc:116
BasicOptions basic_options_
Definition: chi_solver.h:57
std::string GetPETScConvergedReasonstring(KSPConvergedReason reason)