Chi-Tech
lbs_01h_init_boundaries.cc
Go to the documentation of this file.
1#include "lbs_solver.h"
2
5
6#include "chi_runtime.h"
7#include "chi_log.h"
8
9#define mk_shrd(x) std::make_shared<x>
10#define SweepVaccuumBndry \
11chi_mesh::sweep_management::BoundaryVaccuum
12#define SweepIncHomoBndry \
13chi_mesh::sweep_management::BoundaryIsotropicHomogenous
14#define SweepReflectingBndry \
15chi_mesh::sweep_management::BoundaryReflecting
16#define SweepAniHeteroBndry \
17chi_mesh::sweep_management::BoundaryIncidentHeterogeneous
18
19#define ExceptionLocalFaceNormalsDiffer \
20std::logic_error(fname + ": Not all face normals are," \
21" within tolerance, locally the same for the reflecting boundary" \
22" condition requested.")
23
24#define ExceptionGlobalFaceNormalsDiffer \
25std::logic_error(fname + ": Not all face normals are," \
26" within tolerance, globally the same for the reflecting boundary" \
27" condition requested.")
28
29//###################################################################
30/**Initializes transport related boundaries. */
32{
33 const std::string fname = "lbs::LBSSolver::InitializeBoundaries";
34 //================================================== Determine boundary-ids
35 // involved in the problem
36 std::set<uint64_t> globl_unique_bids_set;
37 {
38 std::set<uint64_t> local_unique_bids_set;
39 for (const auto& cell : grid_ptr_->local_cells)
40 for (const auto& face : cell.faces_)
41 if (not face.has_neighbor_)
42 local_unique_bids_set.insert(face.neighbor_id_);
43
44 std::vector<uint64_t> local_unique_bids(local_unique_bids_set.begin(),
45 local_unique_bids_set.end());
46 const int local_num_unique_bids = static_cast<int>(local_unique_bids.size());
47 std::vector<int> recvcounts(Chi::mpi.process_count, 0);
48
49 MPI_Allgather(&local_num_unique_bids, //sendbuf
50 1, MPI_INT, //sendcount+type
51 recvcounts.data(), //recvbuf
52 1, MPI_INT, //recvcount+type
53 Chi::mpi.comm); //comm
54
55 std::vector<int> recvdispls(Chi::mpi.process_count, 0);
56
57 int running_displacement = 0;
58 for (int locI=0; locI< Chi::mpi.process_count; ++locI)
59 {
60 recvdispls[locI] = running_displacement;
61 running_displacement += recvcounts[locI];
62 }
63
64 std::vector<uint64_t> recvbuf(running_displacement, 0);
65
66 MPI_Allgatherv(local_unique_bids.data(), //sendbuf
67 local_num_unique_bids, //sendcount
68 MPI_UINT64_T, //sendtype
69 recvbuf.data(), //recvbuf
70 recvcounts.data(), //recvcounts
71 recvdispls.data(), //recvdispls
72 MPI_UINT64_T, //recvtype
73 Chi::mpi.comm); //comm
74
75 globl_unique_bids_set = local_unique_bids_set; //give it a head start
76
77 for (uint64_t bid : recvbuf)
78 globl_unique_bids_set.insert(bid);
79 }
80
81
82 //================================================== Initialize default
83 // incident boundary
84 const size_t G = num_groups_;
85
86 sweep_boundaries_.clear();
87 for (uint64_t bid : globl_unique_bids_set)
88 {
89 const bool has_no_preference = boundary_preferences_.count(bid) == 0;
90 const bool has_not_been_set = sweep_boundaries_.count(bid) == 0;
91 if (has_no_preference and has_not_been_set)
92 {
94 }//defaulted
95 else if (has_not_been_set)
96 {
97 const auto& bndry_pref = boundary_preferences_.at(bid);
98 const auto& mg_q = bndry_pref.isotropic_mg_source;
99
100 if (bndry_pref.type == lbs::BoundaryType::VACUUM)
102 else if (bndry_pref.type == lbs::BoundaryType::INCIDENT_ISOTROPIC)
104 else if (bndry_pref.type == BoundaryType::INCIDENT_ANISTROPIC_HETEROGENEOUS)
105 {
107 std::make_unique<BoundaryFunctionToLua>(bndry_pref.source_function),
108 bid);
109 }
110 else if (bndry_pref.type == lbs::BoundaryType::REFLECTING)
111 {
112 //Locally check all faces, that subscribe to this boundary,
113 //have the same normal
114 typedef chi_mesh::Vector3 Vec3;
115 const double EPSILON = 1.0e-12;
116 std::unique_ptr<Vec3> n_ptr = nullptr;
117 for (const auto& cell : grid_ptr_->local_cells)
118 for (const auto &face: cell.faces_)
119 if (not face.has_neighbor_ and face.neighbor_id_ == bid)
120 {
121 if (not n_ptr) n_ptr = std::make_unique<Vec3>(face.normal_);
122 if (std::fabs(face.normal_.Dot(*n_ptr) - 1.0) > EPSILON)
124 }
125
126 //Now check globally
127 const int local_has_bid = n_ptr != nullptr ? 1 : 0;
128 const Vec3 local_normal = local_has_bid ? *n_ptr : Vec3(0.0,0.0,0.0);
129
130 std::vector<int> locJ_has_bid(Chi::mpi.process_count, 1);
131 std::vector<double> locJ_n_val(Chi::mpi.process_count*3, 0.0);
132
133 MPI_Allgather(&local_has_bid, //sendbuf
134 1, MPI_INT, //sendcount + datatype
135 locJ_has_bid.data(), //recvbuf
136 1, MPI_INT, //recvcount + datatype
137 Chi::mpi.comm); //communicator
138
139 MPI_Allgather(&local_normal, //sendbuf
140 3, MPI_DOUBLE, //sendcount + datatype
141 locJ_n_val.data(), //recvbuf
142 3, MPI_DOUBLE, //recvcount + datatype
143 Chi::mpi.comm); //communicator
144
145 Vec3 global_normal;
146 for (int j=0; j< Chi::mpi.process_count; ++j)
147 {
148 if (locJ_has_bid[j])
149 {
150 int offset = 3*j;
151 const double* n = &locJ_n_val[offset];
152 const Vec3 locJ_normal(n[0], n[1], n[2]);
153
154 if (local_has_bid)
155 if (std::fabs(local_normal.Dot(locJ_normal) - 1.0) > EPSILON)
157
158 global_normal = locJ_normal;
159 }
160 }
161
162 sweep_boundaries_[bid] =
163 mk_shrd(SweepReflectingBndry)(G, global_normal,
165 }
166 }//non-defaulted
167 }//for bndry id
168}
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
const MPI_Comm & comm
MPI communicator.
Definition: mpi_info.h:28
const int & process_count
Total number of processes.
Definition: mpi_info.h:27
chi_mesh::MeshContinuumPtr grid_ptr_
Definition: lbs_solver.h:75
std::map< uint64_t, std::shared_ptr< SweepBndry > > sweep_boundaries_
Definition: lbs_solver.h:86
std::map< uint64_t, BoundaryPreference > boundary_preferences_
Definition: lbs_solver.h:85
size_t num_groups_
Definition: lbs_solver.h:63
lbs::Options options_
Definition: lbs_solver.h:61
#define SweepVaccuumBndry
#define SweepReflectingBndry
#define SweepIncHomoBndry
#define ExceptionLocalFaceNormalsDiffer
#define ExceptionGlobalFaceNormalsDiffer
#define SweepAniHeteroBndry
#define mk_shrd(x)
@ INCIDENT_ANISTROPIC_HETEROGENEOUS
chi_math::CoordinateSystemType MapGeometryTypeToCoordSys(const GeometryType gtype)
Definition: lbs_structs.h:45
GeometryType geometry_type
Definition: lbs_structs.h:124