Plugins
domain_util.h
Go to the documentation of this file.
1 # ifndef __util__domain_util__h__
2 # define __util__domain_util__h__
3 
4 // UG4 base libs.
5 #include "common/common.h"
6 #include "common/log.h"
8 
9 
11 #include "lib_grid/algorithms/problem_detection_util.h" // CheckForUnconnectedSides
12 
13 
14 #include "lib_disc/domain.h"
15 #include "lib_disc/domain_util.h"
16 
17 // C++ libs.
18 #include <vector>
19 #include <string>
20 
21 namespace ug {
22 namespace Util {
23 
24 namespace aux {
25  template <typename TObject>
26  void write(TObject &text) { UG_LOG(text); }
27 }
28 
29 using aux::write;
30 
31 // util.CheckSubsets
32 // checks if all required subsets are contained in the SubsetHandler
33 // @param dom Domain
34 // @param neededSubsets List of subsets the SubsetHandler must contain
35 // @return true if all subsets are contained, false else
36 
37 template <typename TDomain>
38 bool CheckSubsets(const TDomain &dom, const std::vector<std::string> &neededSubsets)
39 {
40  auto sh = dom.subset_handler();
41 
42  for (auto &subset : neededSubsets) {
43  if (sh->get_subset_index(subset.c_str()) == -1) {
44  UG_LOG("Domain does not contain subset '" << subset.c_str() <<"'.");
45  return false;
46  }
47  }
48 
49  return true;
50 }
51 
52 
53 // Creates a new domain and loads the specified grid.
54 // The method then performs numRefs global refinements.
55 // A list of subset-names can be specified which have to be present in the loaded grid.
56 // The method returns the created domain.
57 // @note Some paramters are optional. nil is a valid value for each optional parameter.
58 // @return (Domain) the created domain
59 // @param gridName (string) The filename of the grid which shall be loaded.
60 // The grid is searched in a path relative to the current path
61 // first. If it isn't found there, the path is interpreted as
62 // an absolute path. If the grid still can't be found, the method
63 // tries to load it from UG_BASE/data/grids.
64 // @param numRefs (int) The total number of global refinements
65 // @param neededSubsets (optional, list of strings) The subsets that are required
66 // by the simulation. If not all those subsets are present,
67 // the method aborts. Default is an empty list.
68 // @param noIntegrityCheck (optional, bool) Disables integrity check if 'true'.
69 
70 
71 template <typename TDomain>
72 SmartPtr<TDomain> CreateDomain(const std::string &gridName, int numRefs,
73  const std::vector<std::string> &neededSubsets, bool noIntegrityCheck=false) {
74 
75  // Create domain instance.
76  auto dom = make_sp<TDomain>(new TDomain());
77 
78  // load domain
79  UG_LOG("Loading Domain " << gridName << " ... ")
80  LoadDomain(*dom, gridName.c_str()); // from lib_disc/domain_util.cpp
81  UG_LOG("done." << std::endl)
82 
83 
84  if (noIntegrityCheck == false) {
85  UG_LOG("Performing integrity check on domain ... ");
86  if (CheckForUnconnectedSides(*dom->grid()))
87  {
88  UG_LOG("WARNING: unconnected sides found (see above).\n");
89  std::string note("NOTE: You may disable this check by passing 'true' to 'noIntegrityCheck' in 'util.CreateDomain'.\n");
90  UG_LOG(note);
91  UG_ERR_LOG(note);
92  }
93  UG_LOG("done.\n");
94  }
95 
96  // Create a refiner instance. This is a factory method
97  // which automatically creates a parallel refiner if required.
98  // if numRefs == nil then numRefs = 0 end
99  if (numRefs > 0) {
100  UG_LOG("Refining(" << numRefs << "): ");
101  auto refiner = GlobalDomainRefiner(*dom);
102  for (int i=0; i<numRefs; ++i){
103  // TerminateAbortedRun();
104  refiner->refine();
105  UG_LOG(i << " ");
106  }
107  UG_LOG("done.\n");
108  // delete(refiner)
109  }
110 
111  // check whether required subsets are present
112  if (!neededSubsets.empty())
113  {
114 
115  UG_ASSERT(CheckSubsets(*dom, neededSubsets) == true,
116  "Something wrong with required subsets. Aborting.");
117  }
118 
119  // return the created domain
120  return dom;
121 }
122 
123 
124 template <typename TDomain>
125 SmartPtr<TDomain> CreateDomain(const std::string &gridName, int numRefs, const std::vector<std::string> &neededSubsets)
126 {
127  return CreateDomain<TDomain>(gridName, numRefs, neededSubsets, false);
128 }
129 
130 template <typename TDomain>
131 SmartPtr<TDomain> CreateDomain(const std::string &gridName, int numRefs)
132 {
133  return CreateDomain<TDomain>(gridName,numRefs, std::vector<std::string>());
134 }
135 
136 template <typename TDomain>
137 SmartPtr<TDomain> CreateDomain(const std::string &gridName)
138 {
139  return CreateDomain<TDomain>(gridName, 0);
140 }
141 
142 
143 // Creates a new domain and loads the specified grid. The method then performs
144 // numPreRefs refinements before it distributes the domain onto the available
145 // processes. The partitioning method can be chosen through distributionMethod.
146 // After distribution the domain is refined until a total of numRefs refinement
147 // steps has been performed (including numPreRefs).
148 // A list of subset-names can be specified. After distribution the methods checks
149 // Whether all processes received the required subsets.
150 // The method returns the created domain.
151 // @note Some paramters are optional. nil is a valid value for each optional parameter.
152 // @return (Domain) the created domain
153 // @param gridName (string) The filename of the grid which shall be loaded.
154 // The grid is searched in a path relative to the current path
155 // first. If it isn't found there, the path is interpreted as
156 // an absolute path. If the grid still can't be found, the method
157 // tries to load it from UG_BASE/data/grids.
158 // @param numRefs (int) The total number of global refinements
159 // @param numPreRefs (int) The number of refinements that are performed before
160 // distribution.
161 // @param neededSubsets (optional, list of strings) The subsets that are required
162 // by the simulation. If not all those subsets are present,
163 // the method aborts. Default is an empty list.
164 // @param distributionMethod (optional, string) The distribution method.
165 // Either "bisection" or "metis". Default is "bisection".
166 // See util.DistributeDomain for more information
167 // (in UG_BASE/scripts/util/domain_distribution.lua)
168 // @param verticalInterfaces (optional, bool) Vertical interfaces are required
169 // by multi-grid solvers. Default is true.
170 // See util.DistributeDomain for more information
171 // (in UG_BASE/scripts/util/domain_distribution.lua)
172 // @param numTargetProcs (optional, int) The number of target processes to which
173 // the domain shall be distributed. Make sure that the
174 // number of target processes is not higher than the
175 // number of elements in the distributionLevel.
176 // Default is NumProcs()
177 // See util.DistributeDomain for more information
178 // (in UG_BASE/scripts/util/domain_distribution.lua)
179 // @param distributionLevel (optional, int) The level on which the distribution
180 // is performed. Default is the domains top-level
181 // after pre-refinement.
182 // See util.DistributeDomain for more information
183 // (in UG_BASE/scripts/util/domain_distribution.lua)
184 // @param wFct (optional SmartPtr<EdgeWeighting>) Sets the weighting function for the
185 // 'metisReweigh' partitioning method.
186 // @param noIntegrityCheck (optional, bool) Disables integrity check if 'true'.
187 
188 /*function util.CreateAndDistributeDomain(gridName, numRefs, numPreRefs,
189  neededSubsets, distributionMethod,
190  verticalInterfaces, numTargetProcs,
191  distributionLevel, wFct, noIntegrityCheck)
192 
193  // create Instance of a Domain
194  local dom = Domain()
195 
196  // load domain
197  write("Loading Domain "..gridName.." ... ")
198  LoadDomain(dom, gridName)
199  write("done.\n")
200 
201  if noIntegrityCheck ~= true then
202  write("Performing integrity check on domain ... ")
203  if CheckForUnconnectedSides(dom:grid()) == true then
204  write("WARNING: unconnected sides found (see above).\n")
205  local note = "NOTE: You may disable this check by passing 'true' "..
206  "to 'noIntegrityCheck' in 'util.CreateAndDistributeDomain'.\n"
207  write(note)
208  errlog(note)
209  end
210  write("done.\n")
211  end
212 
213  // create Refiner
214  ug_assert(numPreRefs <= numRefs, "numPreRefs must be smaller than numRefs. Aborting.");
215 
216  if numPreRefs > numRefs then
217  numPreRefs = numRefs
218  end
219 
220  // Create a refiner instance. This is a factory method
221  // which automatically creates a parallel refiner if required.
222  local refiner = nil
223  if numRefs > 0 then
224  refiner = GlobalDomainRefiner(dom)
225  end
226 
227  write("Pre-Refining("..numPreRefs.."): ")
228  // Performing pre-refines
229  for i=1,numPreRefs do
230  TerminateAbortedRun()
231  write(i .. " ")
232  refiner:refine()
233  end
234  write("done.\nDistributing...")
235  // Distribute the domain to all involved processes
236  if util.DistributeDomain(dom, distributionMethod, verticalInterfaces, numTargetProcs, distributionLevel, wFct) == false then
237  ug_error("Error while Distributing Grid. Aborting.")
238  end
239  write(" done.\nPost-Refining("..(numRefs-numPreRefs).."): ")
240 
241  if numRefs > 0 then
242  // Perform post-refine
243  for i=numPreRefs+1,numRefs do
244  TerminateAbortedRun()
245  refiner:refine()
246  write(i-numPreRefs .. " ")
247  end
248  end
249  write("done.\n")
250 
251  // Now we loop all subsets an search for it in the SubsetHandler of the domain
252  if neededSubsets ~= nil then
253  if util.CheckSubsets(dom, neededSubsets) == false then
254  ug_error("Something wrong with required subsets. Aborting.");
255  end
256  end
257 
258 
259  //clean up
260  if refiner ~= nil then
261  delete(refiner)
262  end
263 
264  // return the created domain
265  return dom
266 end
267 */
268 } // namespace util
269 } // namespace ug
270 
271 # endif //__util__domain_util__h__
parameterNumber numRefs
Definition: evaluate.lua:2
void LoadDomain(TDomain &domain, const char *filename)
#define UG_ASSERT(expr, msg)
#define UG_ERR_LOG(msg)
#define UG_LOG(msg)
void write(TObject &text)
Definition: domain_util.h:26
bool CheckSubsets(const TDomain &dom, const std::vector< std::string > &neededSubsets)
Definition: domain_util.h:38
SmartPtr< TDomain > CreateDomain(const std::string &gridName, int numRefs, const std::vector< std::string > &neededSubsets, bool noIntegrityCheck=false)
Definition: domain_util.h:72
static SmartPtr< IRefiner > GlobalDomainRefiner(TDomain &dom)
bool CheckForUnconnectedSides(Grid &grid)