BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_SimplicialComplex.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#include "cl_Mesh.hpp"
13#include "cl_Chain.hpp"
14#include "cl_Cochain.hpp"
15#include "cl_Map.hpp"
16#include "cl_Matrix.hpp"
17#include "cl_Cell.hpp"
18#include "cl_DynamicBitset.hpp"
19#include "cl_EdgeFactory.hpp"
21
22#ifndef BELFEM_CL_SIMPLICIALCOMPLEX_HPP
23#define BELFEM_CL_SIMPLICIALCOMPLEX_HPP
24
25namespace belfem
26{
27 namespace mesh
28 {
29//-----------------------------------------------------------------------------
37 {
38 Mesh * mMesh ;
39
41
43
44 // Edge indices of the original complex (before reduction). Usefull for the clean() function
45 DynamicBitset * mOriginalEdges = nullptr ;
46
47 uint mNumLoopsReduce = 0;
48 uint mNumLoopsCoreduce = 0;
49
50
51
52//-----------------------------------------------------------------------------
53 public:
54//-----------------------------------------------------------------------------
55
56 SimplicialComplex( Mesh * aMesh, bool aPeriodicity = false );
57
58//-----------------------------------------------------------------------------
59
61
62//-----------------------------------------------------------------------------
63
64 void
65 reset();
66
67//-----------------------------------------------------------------------------
68
69 void
70 create_complex( Mesh * aMesh, bool aPeriodicity );
71
72//-----------------------------------------------------------------------------
73
74 void
76
77//-----------------------------------------------------------------------------
78
79 void
81
82//-----------------------------------------------------------------------------
83
84 void
86
87//-----------------------------------------------------------------------------
88
89 void
91
92//-----------------------------------------------------------------------------
93
94 void
95 reduce_pair( const uint k, const uint a, Map<index_t, Chain*>::const_iterator & bit, const int aCoeff );
96
97//-----------------------------------------------------------------------------
98
99 void
100 coreduce_pair( const uint k, const uint a, Map<index_t, Cochain*>::const_iterator & bit, const int aCoeff );
101
102//-----------------------------------------------------------------------------
103
104 void
105 pReduce(const uint p);
106
107//-----------------------------------------------------------------------------
108
109 void
110 pCombine(const uint p);
111
112//-----------------------------------------------------------------------------
113
114 void
115 pGeneralizedCombine(const uint p);
116
117//-----------------------------------------------------------------------------
118
119 void
120 reduceOmit();
121
122//-----------------------------------------------------------------------------
123
124 void
126
127//-----------------------------------------------------------------------------
128
129 void
131
132//-----------------------------------------------------------------------------
133
134 void
135 pCoreduce(const uint p);
136
137//-----------------------------------------------------------------------------
138
139 void
140 pCocombine(const uint p);
141
142//-----------------------------------------------------------------------------
143
144 void
146
147//-----------------------------------------------------------------------------
148
149 void
150 coreduceOmit();
151
152//-----------------------------------------------------------------------------
153
154 void
156
157//-----------------------------------------------------------------------------
158
159 void
161
162//-----------------------------------------------------------------------------
163
164 void
165 remove_kchainFromMap( const uint k, const index_t aID );
166
167//------------------------------------------------------------------------------
168
169 void
170 remove_kcochainFromMap( const uint k, const index_t aID );
171
172//------------------------------------------------------------------------------
173
174 uint
175 number_of_0simplices() const;
176
177//------------------------------------------------------------------------------
178
179 uint
180 number_of_1simplices() const;
181
182//------------------------------------------------------------------------------
183
184 uint
185 number_of_2simplices() const;
186
187//------------------------------------------------------------------------------
188
189 uint
190 number_of_ksimplices( const uint k ) const;
191
192//------------------------------------------------------------------------------
193
194 uint
195 number_of_kcosimplices( const uint k ) const;
196
197//------------------------------------------------------------------------------
198
199 Chain *
200 get_kchain( const uint k, const index_t aID );
201
202//------------------------------------------------------------------------------
203
204 Cochain *
205 get_kcochain( const uint k, const index_t aID );
206
207//------------------------------------------------------------------------------
208
210 get_kchainMap( const uint k );
211
212//------------------------------------------------------------------------------
213
215 get_kcochainMap( const uint k );
216
217//------------------------------------------------------------------------------
218
219 Chain *
220 boundary_of_kchain( const uint k, const index_t aID );
221
222//------------------------------------------------------------------------------
223
226
227//------------------------------------------------------------------------------
228
231
232//------------------------------------------------------------------------------
233
234 void
235 print_kchains( const uint k );
236
237//------------------------------------------------------------------------------
238
239 void
240 create_kComplexField(const uint k, Mesh * aMesh, string aFieldName ) ;
241
242//------------------------------------------------------------------------------
243
244 void
245 cocreate_kComplexField( const uint k, Mesh * aMesh, string aFieldName );
246
247//------------------------------------------------------------------------------
248
249 const DynamicBitset &
250 original_edges() const ;
251
252//------------------------------------------------------------------------------
253
254 };
255
256//------------------------------------------------------------------------------
257
258 inline const DynamicBitset &
260 {
261 return * mOriginalEdges ;
262 }
263
264 inline uint
266 {
267 return mChainsMap( 0 ).size();
268 }
269
270//------------------------------------------------------------------------------
271
272 inline uint
274 {
275 return mChainsMap( 1 ).size();
276 }
277
278//------------------------------------------------------------------------------
279
280 inline uint
282 {
283 return mChainsMap( 2 ).size();
284 }
285
286//------------------------------------------------------------------------------
287
288 inline uint
290 {
291 if ( k < 0 )
292 {
293 return 0;
294 }
295 else
296 {
297 return mChainsMap( k ).size();
298 }
299 }
300
301//------------------------------------------------------------------------------
302
303 inline uint
305 {
306 if ( k < 0 )
307 {
308 return 0;
309 }
310 else
311 {
312 return mCochainsMap( k ).size();
313 }
314 }
315
316
317//------------------------------------------------------------------------------
318
319 inline void
321 {
322 if (k <= 3)
323 {
324 auto & tMap = mCochainsMap( k ).map_data();
325 auto it = tMap.find( aID );
326 if ( it != tMap.end() )
327 {
328 delete it->second;
329 tMap.erase( it );
330 }
331 }
332 }
333
334//------------------------------------------------------------------------------
335
336 // pReduce from Pellikka et al.
337 inline void
339 {
340 if (p == 3)
341 {
342 return ;
343 }
344
345#ifdef PERFORMANCE_CHECK
346 std::ofstream tFile;
347 tFile.open ("Coreduce.txt",std::ios_base::app);
348 tFile << mNumLoopsCoreduce << " " << mCochainsMap(0).size()+mCochainsMap(1).size()+mCochainsMap(2).size()+mCochainsMap(3).size() << " \n";
349#endif
350 bool tRemoved = true;
351 while (tRemoved)
352 {
353 tRemoved = false;
354 mNumLoopsCoreduce+=1;
355
356 //Loop over all the p+1 cochains
357 Map< index_t, Cochain * > & tMap1 = mCochainsMap(p+1);
358
359 for (auto it = tMap1.begin(), next_it = it; it != tMap1.end(); it = next_it)
360 {
361 ++next_it;
362
363 //Check if the p+1 cochain is a coboundary of exactly one p cochain
364 if (it->second->getBoundary()->getSimplicesMap().size() == 1)
365 {
366 index_t a = it->first;
367 index_t b = it->second->getBoundary()->getSimplicesMap().begin()->first;
368
369 OrderedMap< index_t, int > & tSimpMap0 = mCochainsMap(p)(b)->getCoboundary()->getSimplicesMap() ;
370 //Update the boundaries
371 for(const auto [tID2, tCoeff2]: tSimpMap0 )
372 {
373 if (tID2 != it->first )
374 {
375 tMap1(tID2)->getBoundary()->setCoefficient(b,0);
376 }
377 }
378 if (p < 2)
379 {
380 OrderedMap< index_t, int > & tSimpMap1 = tMap1(a)->getCoboundary()->getSimplicesMap() ;
381 for(const auto [tID2, tCoeff2]: tSimpMap1 )
382 {
383 mCochainsMap(p+2)(tID2)->getBoundary()->setCoefficient(a,0);
384 }
385 }
386
387 //Remove a and b from the complex
388 this->remove_kcochainFromMap( p, b );
389 this->remove_kcochainFromMap( p + 1, a );
390 tRemoved = true;
391 }
392 }
393#ifdef PERFORMANCE_CHECK
394 tFile << mNumLoopsCoreduce << " " << mCochainsMap(0).size()+mCochainsMap(1).size()+mCochainsMap(2).size()+mCochainsMap(3).size() << " \n";
395#endif
396 }
397#ifdef PERFORMANCE_CHECK
398 tFile.close();
399#endif
400 }
401//------------------------------------------------------------------------------
402 }
403}
404
405#endif //BELFEM_CL_SIMPLICIALCOMPLEX_HPP
Cell is a wrapper around the standard vector.
Definition cl_Cell.hpp:42
Runtime-sized bitset; one bit per flag, packed into 64-bit words.
Definition cl_DynamicBitset.hpp:33
Hash map (unordered key-value).
Definition cl_Map.hpp:75
typename std::unordered_map< Key, Value >::const_iterator const_iterator
Definition cl_Map.hpp:83
auto end() const -> decltype(mMap.end())
Definition cl_Map.hpp:175
auto begin() const -> decltype(mMap.begin())
Definition cl_Map.hpp:169
Top-level container for all mesh entities.
Definition cl_Mesh.hpp:60
Formal sum of k-simplices (homology).
Definition cl_Chain.hpp:36
Cochain: a functional on chains (cohomology).
Definition cl_Cochain.hpp:37
void reduce_complexCCR()
Definition cl_SimplicialComplex.cpp:480
uint number_of_1simplices() const
Definition cl_SimplicialComplex.hpp:273
uint number_of_0simplices() const
Definition cl_SimplicialComplex.hpp:265
void pReduce(const uint p)
Definition cl_SimplicialComplex.cpp:695
Chain * get_kchain(const uint k, const index_t aID)
Definition cl_SimplicialComplex.cpp:1329
void pCoreduce(const uint p)
Definition cl_SimplicialComplex.hpp:338
void coreduce_complexPellikka()
Definition cl_SimplicialComplex.cpp:1263
Cell< Matrix< int > > createMatrixFromCoboundaryMap()
Definition cl_SimplicialComplex.cpp:1410
Cell< Matrix< int > > createMatrixFromBoundaryMap()
Definition cl_SimplicialComplex.cpp:1382
void pCocombine(const uint p)
Definition cl_SimplicialComplex.cpp:1023
Chain * boundary_of_kchain(const uint k, const index_t aID)
void coreduceOmit()
Definition cl_SimplicialComplex.cpp:1236
void reduceOmit()
Definition cl_SimplicialComplex.cpp:939
void remove_kcochainFromMap(const uint k, const index_t aID)
Definition cl_SimplicialComplex.hpp:320
void reduce_complexPellikka()
Definition cl_SimplicialComplex.cpp:967
Map< index_t, Chain * > get_kchainMap(const uint k)
Definition cl_SimplicialComplex.cpp:1351
uint number_of_kcosimplices(const uint k) const
Definition cl_SimplicialComplex.hpp:304
uint number_of_ksimplices(const uint k) const
Definition cl_SimplicialComplex.hpp:289
uint number_of_2simplices() const
Definition cl_SimplicialComplex.hpp:281
void reset()
Definition cl_SimplicialComplex.cpp:41
void create_complex(Mesh *aMesh, bool aPeriodicity)
Definition cl_SimplicialComplex.cpp:109
void coreduce_complexCCR()
Definition cl_SimplicialComplex.cpp:507
void remove_kchainFromMap(const uint k, const index_t aID)
Definition cl_SimplicialComplex.cpp:1319
Map< index_t, Cochain * > get_kcochainMap(const uint k)
Definition cl_SimplicialComplex.cpp:1358
Cochain * get_kcochain(const uint k, const index_t aID)
Definition cl_SimplicialComplex.cpp:1340
SimplicialComplex(Mesh *aMesh, bool aPeriodicity=false)
Definition cl_SimplicialComplex.cpp:23
void coreduce_complexCCR_old()
Definition cl_SimplicialComplex.cpp:586
void pCombine(const uint p)
Definition cl_SimplicialComplex.cpp:761
void print_kchains(const uint k)
Definition cl_SimplicialComplex.cpp:1441
void cocreate_kComplexField(const uint k, Mesh *aMesh, string aFieldName)
Definition cl_SimplicialComplex.cpp:1468
void pGeneralizedCocombine(const uint p)
Definition cl_SimplicialComplex.cpp:1119
void create_kComplexField(const uint k, Mesh *aMesh, string aFieldName)
Definition cl_SimplicialComplex.cpp:1451
void reduce_complexCCR_old()
Definition cl_SimplicialComplex.cpp:535
void reduce_complexPellikkaGeneralized()
Definition cl_SimplicialComplex.cpp:994
void coreduce_pair(const uint k, const uint a, Map< index_t, Cochain * >::const_iterator &bit, const int aCoeff)
Definition cl_SimplicialComplex.cpp:667
void pGeneralizedCombine(const uint p)
Definition cl_SimplicialComplex.cpp:858
void reduce_pair(const uint k, const uint a, Map< index_t, Chain * >::const_iterator &bit, const int aCoeff)
Definition cl_SimplicialComplex.cpp:639
void coreduce_complexPellikkaGeneralized()
Definition cl_SimplicialComplex.cpp:1291
const DynamicBitset & original_edges() const
Definition cl_SimplicialComplex.hpp:259
Definition cl_EF_EdgeFunction.hpp:17
USER GUIDES:
Definition cl_Capacitor.cpp:16
unsigned int uint
Definition typedefs.hpp:30
uint32_t index_t
Definition typedefs.hpp:52