BELFEM 0.9.3
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_FEM_Kernel.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#ifndef BELFEM_CL_FEM_KERNEL_HPP
13#define BELFEM_CL_FEM_KERNEL_HPP
14
15#include "typedefs.hpp"
16#include "cl_Vector.hpp"
17#include "cl_Matrix.hpp"
18#include "cl_Mesh.hpp"
19#include "cl_Cell.hpp"
20#include "cl_Map.hpp"
21#include "cl_Material.hpp"
22
23#include "cl_FEM_Dof.hpp"
25#include "cl_FEM_Block.hpp"
28
29#include "cl_FEM_DofManager.hpp"
30#include "cl_CommTable.hpp"
31#include "en_IWGs.hpp"
32
33namespace belfem
34{
35//------------------------------------------------------------------------------
36
37 namespace fem
38 {
39 class IwgFactory ;
40
41 class Controller ;
42
55 class Kernel
56 {
57 // rank of this proc
58 const proc_t mCommRank;
59
60 // how many procs contribute to this kernel
61 const proc_t mCommSize;
62
63 // pointer to parameter list
64 KernelParameters * mParams;
65
66 // pointer to mesh (points to empty mesh unless root kernel)
67 Mesh * mMesh;
68
69 // offset for fields, in case fields already exist on mesh
70 // when Kernel is generated
71 const uint mFieldOffset ;
72
73 // submesh ( points to local mesh unless this is root)
74 Mesh * mSubMesh = nullptr;
75
76 Controller * mController = nullptr ;
77
78
79 // flag telling if the kernel destroys the parameters on exit
80 bool mOwnParameters = false ;
81
82 proc_t mMyCommIndex = 0 ;
83
84 Vector< index_t > mNumElementsPerBlock;
85
86 Cell< DofManager * > mDofManagers ;
87 Cell< IWG * > mIWGs ;
88
89 Cell< Material * > mMaterials;
90 Map< string, Material * > mMaterialMap;
91
92 Cell< PhysicalBoundaryCondition * > mBoundaryConditions ;
93
95
96 bool mOwnCommTables = false ;
97 bool mOwnMesh = false ;
98 bool mOwnSubmesh = false ;
99
100//------------------------------------------------------------------------------
101 public:
102//------------------------------------------------------------------------------
103
106 Kernel( KernelParameters * aKernelParameters );
107
108//------------------------------------------------------------------------------
109
110 ~Kernel();
111
112 // NON-COPYABLE, NON-MOVABLE. The destructor deletes the dof
113 // managers, IWGs, materials, boundary conditions and, under their
114 // mOwn* flags, the comm tables, mesh, submesh and parameters. An
115 // implicit copy would copy those flags too, and both objects would
116 // free the same resources. Copy assignment is already deleted by
117 // the const members, and the user-declared destructor suppresses
118 // the implicit moves, so a move request would bind to the copy
119 // constructor -- the same hazard under another name
120 Kernel( const Kernel & ) = delete ;
121 Kernel( Kernel && ) = delete ;
122 Kernel & operator=( const Kernel & ) = delete ;
123 Kernel & operator=( Kernel && ) = delete ;
124
125//------------------------------------------------------------------------------
126
131 const KernelParameters *
132 params();
133
134//------------------------------------------------------------------------------
135
140 DofManager *
141 dofmgr( const uint aIndex=0 );
142
143//------------------------------------------------------------------------------
144
150 Material *
151 material( const string & aLabel );
152
153//------------------------------------------------------------------------------
154
159 void
160 add_material( const string aLabel, Material * aMaterial );
161
162//------------------------------------------------------------------------------
163
168 void
170
171//------------------------------------------------------------------------------
172
179
180//------------------------------------------------------------------------------
181
187
188//------------------------------------------------------------------------------
189
194 void
196
197//------------------------------------------------------------------------------
198
199
202 const mesh::CommTable *
203 comm_table( const uint aProc ) const ;
204
206 comm_tables() const ;
207
208//------------------------------------------------------------------------------
209
210 const proc_t &
211 number_of_procs() const ;
212
213//------------------------------------------------------------------------------
214
218 const uint &
219 field_offset() const ;
220
221//------------------------------------------------------------------------------
222
230 Mesh *
231 mesh();
232
233//------------------------------------------------------------------------------
234
237 IWG *
238 create_equation( const IwgType aEquationType,
239 const ModelDimensionality aModelDimensionality = ModelDimensionality::UNDEFINED,
240 const Vector< id_t > aBlocks={},
241 const Vector< id_t > aSideSets={} );
242
243//------------------------------------------------------------------------------
244
250 void
251 add_equation( IWG * aEquation );
252
253//------------------------------------------------------------------------------
254
259 DofManager *
260 create_field( IWG * aEquation );
261
262//------------------------------------------------------------------------------
263
264 bool
265 is_master() const ;
266
267//------------------------------------------------------------------------------
268
272 void
273 claim_parameter_ownership( const bool aFlag = true );
274
281 void
282 claim_mesh_ownership( const bool aFlag = true );
283
284//------------------------------------------------------------------------------
285
287 void
288 set_controller( Controller * aController );
289
290//------------------------------------------------------------------------------
291
292 Controller *
293 controller() ;
294
295//------------------------------------------------------------------------------
296
302 bool
303 has_controller() const ;
304
305//------------------------------------------------------------------------------
306
310 void
312
313//------------------------------------------------------------------------------
314
315 uint
316 number_of_dof_managers() const ;
317
318//------------------------------------------------------------------------------
319 private:
320//------------------------------------------------------------------------------
321
327 void
328 distribute_mesh();
329
330//------------------------------------------------------------------------------
331
337 void
338 create_dof_graph( Cell< Dof * > & aGraph );
339
340//------------------------------------------------------------------------------
341
351 void
352 collect_elements(
353 const Vector< id_t > & aBlockIDs,
354 const Vector< id_t > & aSideSetIDs,
355 Cell< mesh::Element * > & aElements );
356
357
358//------------------------------------------------------------------------------
359
361 get_entity( const id_t aID, const EntityType aType ) ;
362
363//------------------------------------------------------------------------------
364
365 void
366 partition_mesh();
367
368//------------------------------------------------------------------------------
369 };
370//------------------------------------------------------------------------------
371
372 inline const mesh::CommTable *
373 Kernel::comm_table( const uint aProc ) const
374 {
375 BELFEM_ASSERT( mCommRank == 0, "Only master may call the comm table." );
376 return mCommTables( aProc );
377 }
378
381 {
382 return mCommTables ;
383 }
384
385 inline const proc_t &
387 {
388 return mCommSize ;
389 }
390
391//------------------------------------------------------------------------------
392
393 inline const uint &
395 {
396 return mFieldOffset ;
397 }
398
399//------------------------------------------------------------------------------
400
403 {
404 return mBoundaryConditions ;
405 }
406
407//------------------------------------------------------------------------------
408
411 {
412 for ( PhysicalBoundaryCondition* tBC : mBoundaryConditions )
413 {
414 for (id_t tID : tBC->domains())
415 {
416 if (tID == aID)
417 {
418 return tBC ;
419 }
420 }
421 }
422 BELFEM_ERROR(false, "Sideset is not associated to a boundary contition") ;
423 return nullptr ;
424 }
425
426//------------------------------------------------------------------------------
427
428 inline Mesh *
430 {
431 return mCommRank == 0 ? mMesh : mSubMesh ;
432 }
433
434//------------------------------------------------------------------------------
435
436 inline uint
438 {
439 return mDofManagers.size();
440 }
441
442//------------------------------------------------------------------------------
443
444 inline bool
446 {
447 return mCommRank == 0 ;
448 }
449
450//------------------------------------------------------------------------------
451
452 inline bool
454 {
455 return mController != nullptr ;
456 }
457
458//------------------------------------------------------------------------------
459
460 inline mesh::Basis *
461 Kernel::get_entity( const id_t aID, const EntityType aType )
462 {
463 switch( aType )
464 {
465 case( EntityType::NODE ) :
466 {
467 return this->mesh()->node( aID );
468 }
469 case( EntityType::EDGE ) :
470 {
471 return this->mesh()->edge( aID );
472 }
473 case( EntityType::FACE ) :
474 {
475 return this->mesh()->face( aID );
476 }
477 case( EntityType::FACET ) :
478 {
479 return this->mesh()->facet( aID );
480 }
481 case( EntityType::ELEMENT ) :
482 {
483 return this->mesh()->element( aID );
484 }
485 default:
486 {
487 BELFEM_ERROR( false, "Invalid entity type");
488 return nullptr ;
489 }
490 }
491 }
492
493 }
494//------------------------------------------------------------------------------
495}
496#endif //BELFEM_CL_FEM_KERNEL_HPP
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
A bounds-checked wrapper around std::vector.
Definition cl_Cell.hpp:45
Hash map (unordered key-value).
Definition cl_Map.hpp:75
Base class for all materials in BELFEM.
Definition cl_Material.hpp:270
Top-level container for all mesh entities.
Definition cl_Mesh.hpp:60
mesh::Facet * facet(const id_t aID)
Definition cl_Mesh.hpp:1444
mesh::Element * element(const id_t aID)
Definition cl_Mesh.hpp:1368
Nonlinear iteration controller: relaxation, timestep adaptation and convergence policy.
Definition cl_FEM_Controller.hpp:38
Main workhorse: DOF management, assembly coordination and the solver interface.
Definition cl_FEM_DofManager.hpp:52
Prototype for Integrator of Weak Form Governing Equation.
Definition cl_IWG.hpp:81
Creates IWG instances by equation type.
Definition cl_IwgFactory.hpp:30
Cell< PhysicalBoundaryCondition * > & boundary_conditions()
expose the boundary condition container
Definition cl_FEM_Kernel.hpp:402
Kernel(KernelParameters *aKernelParameters)
Collective: all ranks construct the kernel.
Definition cl_FEM_Kernel.cpp:42
const Cell< const mesh::CommTable * > & comm_tables() const
Definition cl_FEM_Kernel.hpp:380
const KernelParameters * params()
The parameters are borrowed unless claim_parameter_ownership() was called.
Definition cl_FEM_Kernel.cpp:631
PhysicalBoundaryCondition * boundary_condition(id_t aID)
expose a boundary condition for a specific sideset ID
Definition cl_FEM_Kernel.hpp:410
bool is_master() const
Definition cl_FEM_Kernel.hpp:445
Kernel & operator=(const Kernel &)=delete
Material * material(const string &aLabel)
returns the material object registered under aLabel ( add_material ).
Definition cl_FEM_Kernel.cpp:736
Controller * controller()
Definition cl_FEM_Kernel.cpp:1178
void compute_element_volumes()
Collective: every rank must enter it.
Definition cl_FEM_Kernel.cpp:853
bool has_controller() const
true once set_controller() has attached a Controller; callers that may legitimately run without one a...
Definition cl_FEM_Kernel.hpp:453
IWG * create_equation(const IwgType aEquationType, const ModelDimensionality aModelDimensionality=ModelDimensionality::UNDEFINED, const Vector< id_t > aBlocks={}, const Vector< id_t > aSideSets={})
The kernel owns and destroys the returned equation; the caller only borrows it.
Definition cl_FEM_Kernel.cpp:532
void claim_parameter_ownership(const bool aFlag=true)
if this flag is set, the kernel will destroy the parameters
Definition cl_FEM_Kernel.cpp:780
void set_controller(Controller *aController)
Borrowed and never deleted: the controller must outlive the kernel.
Definition cl_FEM_Kernel.cpp:1170
DofManager * create_field(IWG *aEquation)
Collective; it performs a barrier.
Definition cl_FEM_Kernel.cpp:590
uint number_of_dof_managers() const
Definition cl_FEM_Kernel.hpp:437
DofManager * dofmgr(const uint aIndex=0)
The dof manager at aIndex; owned by the kernel, never deleted by the caller.
Definition cl_FEM_Kernel.cpp:728
Kernel(const Kernel &)=delete
void add_material(const string aLabel, Material *aMaterial)
manually add a material to the kernel.
Definition cl_FEM_Kernel.cpp:748
void compute_boundary_conditions(real aTime)
Computes the boundary conditions and updates the dofs at aTime.
Definition cl_FEM_Kernel.cpp:769
Kernel & operator=(Kernel &&)=delete
const uint & field_offset() const
returns the number of fields that existed before Kernel was created
Definition cl_FEM_Kernel.hpp:394
Kernel(Kernel &&)=delete
Mesh * mesh()
The mesh on the master, the submesh on every other rank.
Definition cl_FEM_Kernel.hpp:429
void add_boundary_condition(PhysicalBoundaryCondition *aBC)
manually add the BCs to the kernel.
Definition cl_FEM_Kernel.cpp:761
const proc_t & number_of_procs() const
Definition cl_FEM_Kernel.hpp:386
const mesh::CommTable * comm_table(const uint aProc) const
Borrowed: the kernel deletes the comm tables only when it built them itself, so the caller never dele...
Definition cl_FEM_Kernel.hpp:373
void add_equation(IWG *aEquation)
special function to add an equation that has already been created.
Definition cl_FEM_Kernel.cpp:582
void claim_mesh_ownership(const bool aFlag=true)
Hands the mesh the parameters point at over to this kernel, which then deletes it in its destructor.
Definition cl_FEM_Kernel.cpp:788
Definition cl_FEM_KernelParameters.hpp:29
Definition cl_FEM_PhysicalBoundaryCondition.hpp:37
Definition cl_Mesh_Basis.hpp:33
Definition cl_CommTable.hpp:25
Definition cl_IFB_LINE3.hpp:21
USER GUIDES:
Definition cl_Capacitor.cpp:16
IwgType
Definition en_IWGs.hpp:64
unsigned int uint
Definition typedefs.hpp:30
unsigned int id_t
Definition typedefs.hpp:41
ModelDimensionality
Definition en_IWGs.hpp:20
@ UNDEFINED
Definition en_IWGs.hpp:25
int proc_t
Definition commtypes.hpp:29
EntityType
Definition Mesh_Enums.hpp:116
@ NODE
Definition Mesh_Enums.hpp:117
@ FACE
Definition Mesh_Enums.hpp:119
@ EDGE
Definition Mesh_Enums.hpp:118
@ FACET
Definition Mesh_Enums.hpp:121
@ ELEMENT
Definition Mesh_Enums.hpp:122
double real
Definition typedefs.hpp:36
Definition __init__.py:1