BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_SourceFunction.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_SOURCEFUNCTION_HPP
13#define BELFEM_CL_SOURCEFUNCTION_HPP
14
15#if defined(__clang__)
16#pragma clang diagnostic push
17#pragma clang diagnostic ignored "-Wformat-security"
18#pragma clang diagnostic ignored "-Wformat"
19#pragma clang diagnostic ignored "-Wunused-variable"
20#pragma clang diagnostic ignored "-Wunused-parameter"
21
22#elif defined(__GNUC__) || defined(__GNUG__)
23#pragma GCC diagnostic push
24#pragma GCC diagnostic ignored "-Wformat"
25#pragma GCC diagnostic ignored "-Wunused-variable"
26#pragma GCC diagnostic ignored "-Wunused-parameter"
27
28#elif defined(BELFEM_INTEL)
29 // Intel compiler diagnostics
30#pragma warning(push)
31 // disable format warnings
32#pragma warning(disable: 1011)
33 // disable unused variable warnings
34#pragma warning(disable: 177)
35 // keep your original one
36#pragma warning(disable: 1595)
37
38#endif
39
40#include "typedefs.hpp"
41#include "cl_Cell.hpp"
42
43// note: NEVER include cl_Vector.hpp or cl_Matrix.hpp here, or any class that uses it.
44// Doing so would break the API for the user defined materials.
45
46#define BELFEM_BCVAL_AMPLITUDE 0
47#define BELFEM_BCVAL_PERIOD 1
48#define BELFEM_BCVAL_FREQUENCY 2
49#define BELFEM_BCVAL_OMEGA 3
50#define BELFEM_BCVAL_PHASE 4
51#define BELFEM_BCVAL_TIMEOFFSET 5
52#define BELFEM_BCVAL_FUZZYNESS 6
53namespace belfem
54{
55//-----------------------------------------------------------------------------
56
58 {
60 Ramp = 1,
62 Sine = 3,
63 Square = 4,
67 UNDEFINED = 8
68 };
69
70//-----------------------------------------------------------------------------
71
73 boundary_condition_function_type( const string & aString );
74
75//------------------------------------------------------------------------------
76
77 typedef real ( UserFunc )( const real ) ;
78
80 {
81 const proc_t mCommRank ;
82 const proc_t mCommSize ;
83
85
86 Cell< real > mValues ;
87
88 real
89 ( SourceFunction::*mFun )( const real aTime ) const ;
90
91 UserFunc *mUserFunction = nullptr ;
92
96 void *mHandle = nullptr ;
97
98//-----------------------------------------------------------------------------
99 public:
100//-----------------------------------------------------------------------------
101
103
104//-----------------------------------------------------------------------------
105
107
108//-----------------------------------------------------------------------------
109
110 // This object can own a dlopen handle ( mHandle ) and a function
111 // pointer into that library ( mUserFunction ). Copying either would
112 // give two objects one mapping and close it twice. Nothing in the tree
113 // copies or moves one -- production always allocates and passes a
114 // pointer, and the tests default-construct on the stack, which needs
115 // neither -- so deleting these costs nothing today and keeps it that way
116 SourceFunction( const SourceFunction & ) = delete ;
120
121//-----------------------------------------------------------------------------
122
124 type() const ;
125
126//-----------------------------------------------------------------------------
127
128 void
129 set_constant( const real aAmplitude );
130
131//-----------------------------------------------------------------------------
132
133 void
134 set_ramp(
135 const real aAmplitude,
136 const real aPeriod,
137 const real aTimeOffset=0.0 );
138
139//-----------------------------------------------------------------------------
140
141 void
143 const real aAmplitude,
144 const real aPeriod,
145 const real aTimeOffset,
146 const real aFuzzyness=0.01 );
147
148//-----------------------------------------------------------------------------
149
150 void
152 const SourceFunctionType aType,
153 const real aAmplitude,
154 const real aPeriod,
155 const real aPhase );
156
157//-----------------------------------------------------------------------------
158
159 void
160 set_user_defined( UserFunc * Function ) ;
161
162//-----------------------------------------------------------------------------
163
164 void
165 read_user_defined( const string & aLibraryPath,
166 const string & aLabel ) ;
167
168//-----------------------------------------------------------------------------
169
170 real
171 compute( const real aTime ) const ;
172
173//-----------------------------------------------------------------------------
174
175 real
176 amplitude() const ;
177
178 real
179 period() const ;
180
181 real
182 frequency() const ;
183
184 real
185 omega() const ;
186
187 real
188 phase() const ;
189
190 real
191 time_offset() const ;
192
193 real
194 fuzziness() const ;
195
196//-----------------------------------------------------------------------------
197 private:
198//-----------------------------------------------------------------------------
199
200 void
201 set_defaults();
202
203//-----------------------------------------------------------------------------
204
205 void
206 synch();
207
208//-----------------------------------------------------------------------------
209
210 void
211 set_amplitude( const real aValue ) ;
212
213 void
214 set_period( const real aValue ) ;
215
216 void
217 set_frequency( const real aValue ) ;
218
219 void
220 set_omega( const real aValue ) ;
221
222 void
223 set_phase( const real aValue ) ;
224
225 void
226 set_time_offset( const real aValue ) ;
227
228 void
229 set_fuzziness( const real aValue ) ;
230
231//-----------------------------------------------------------------------------
232
233 real
234 function_constant( const real aTime ) const ;
235
236 real
237 function_ramp( const real aTime ) const ;
238
239 real
240 function_sigmoid( const real aTime ) const ;
241
242 real
243 function_sine( const real aTime ) const ;
244
255 real
256 wrap_tau( const real aTime ) const ;
257
258 real
259 function_square( const real aTime ) const ;
260
261 real
262 function_triangle( const real aTime ) const ;
263
264 real
265 function_sawtooth( const real aTime ) const ;
266
267 real
268 function_userdefined( const real aTime ) const ;
269
270 };
271
272//-----------------------------------------------------------------------------
273
274 inline SourceFunctionType
276 {
277 return mType ;
278 }
279
280//-----------------------------------------------------------------------------
281
282 inline real
283 SourceFunction::compute( const real aTime ) const
284 {
285 return ( this->*mFun )( aTime ) ;
286 }
287
288//-----------------------------------------------------------------------------
289
290 inline real
292 {
293 return mValues( BELFEM_BCVAL_AMPLITUDE ) ;
294 }
295
296//-----------------------------------------------------------------------------
297
298 inline real
300 {
301 return mValues( BELFEM_BCVAL_PERIOD );
302 }
303
304//-----------------------------------------------------------------------------
305
306 inline real
308 {
309 return mValues( BELFEM_BCVAL_FREQUENCY );
310 }
311
312//-----------------------------------------------------------------------------
313
314 inline real
316 {
317 return mValues( BELFEM_BCVAL_OMEGA );
318 }
319
320//-----------------------------------------------------------------------------
321
322 inline real
324 {
325 return mValues( BELFEM_BCVAL_PHASE );
326 }
327
328
329//-----------------------------------------------------------------------------
330
331 inline real
333 {
334 return mValues( BELFEM_BCVAL_TIMEOFFSET );
335 }
336
337//-----------------------------------------------------------------------------
338
339 inline real
341 {
342 return mValues( BELFEM_BCVAL_FUZZYNESS );
343 }
344
345//-----------------------------------------------------------------------------
346
347 inline real
348 SourceFunction::function_constant( const real aTime ) const
349 {
350 return this->amplitude() ;
351 }
352
353//-----------------------------------------------------------------------------
354
355 inline real
356 SourceFunction::function_ramp( const real aTime ) const
357 {
358 if( aTime < this->time_offset() )
359 {
360 return 0.0 ;
361 }
362 else if ( aTime > this->time_offset() + this->period() )
363 {
364 return this->amplitude() ;
365 }
366 else
367 {
368 return this->amplitude() * ( aTime - this->time_offset() ) * this->frequency() ;
369 }
370 }
371
372//-----------------------------------------------------------------------------
373
374 inline real
375 SourceFunction::function_sine( const real aTime ) const
376 {
377 return this->amplitude() * std::sin( this->omega() * aTime + this->phase() );
378 }
379
380//------------------------------------------------------------------------------
381
382 inline real
383 SourceFunction::function_sigmoid( const real aTime ) const
384 {
385 // see doi:10.1016/j.fss.2005.02.016
386
387 real d = this->time_offset() + this->period()/2.0;
388 real beta = -2.0*log(this->fuzziness()/(1.0-this->fuzziness()))/this->period() ;
389 real f = 1./(1+std::exp(beta * (d - aTime)));
390
391 return this->amplitude() * f ;
392 }
393
394//------------------------------------------------------------------------------
395
396 inline real
397 SourceFunction::wrap_tau( const real aTime ) const
398 {
399 real tTau = std::fmod( aTime, this->period() ) / this->period() ;
400
401 // strictly less: an exact zero must stay zero, or the square would
402 // flip at its own rising edge
403 return tTau < 0.0 ? tTau + 1.0 : tTau ;
404 }
405
406//------------------------------------------------------------------------------
407
408 inline real
409 SourceFunction::function_square( const real aTime ) const
410 {
411 real tTau = this->wrap_tau( aTime + this->time_offset() ) ;
412
413
414 if( tTau < 0.5 )
415 {
416 return this->amplitude() ;
417 }
418 else
419 {
420 return -this->amplitude() ;
421 }
422 }
423
424//------------------------------------------------------------------------------
425
426 inline real
427 SourceFunction::function_triangle( const real aTime ) const
428 {
429 real tTau = this->wrap_tau( aTime + this->time_offset() + 3.0*this->period()/4.0 );
430
431 // Shift tTau to range from -0.5 to 0.5
432 tTau -= 0.5;
433
434 // Calculate triangle wave value
435 return 4.0 * this->amplitude() * std::fabs(tTau) - this->amplitude();
436 }
437
438//------------------------------------------------------------------------------
439
440 inline real
441 SourceFunction::function_sawtooth( const real aTime ) const
442 {
443 return this->amplitude()
444 * this->wrap_tau( aTime + this->time_offset() ) ;
445 }
446
447//-----------------------------------------------------------------------------
448
449 inline real
450 SourceFunction::function_userdefined( const real aTime ) const
451 {
452 return mUserFunction(aTime) ;
453 }
454
455 //-----------------------------------------------------------------------------
456}
457
458#ifdef BELFEM_CLANG
459#pragma clang diagnostic pop
460#elif BELFEM_GCC
461#pragma GCC diagnostic pop
462#elif BELFEM_INTEL
463#pragma warning pop
464#endif
465
466#endif //BELFEM_CL_SOURCEFUNCTION_HPP
#define BELFEM_BCVAL_PHASE
Definition cl_SourceFunction.hpp:50
#define BELFEM_BCVAL_TIMEOFFSET
Definition cl_SourceFunction.hpp:51
#define BELFEM_BCVAL_OMEGA
Definition cl_SourceFunction.hpp:49
#define BELFEM_BCVAL_AMPLITUDE
Definition cl_SourceFunction.hpp:46
#define BELFEM_BCVAL_FUZZYNESS
Definition cl_SourceFunction.hpp:52
#define BELFEM_BCVAL_FREQUENCY
Definition cl_SourceFunction.hpp:48
#define BELFEM_BCVAL_PERIOD
Definition cl_SourceFunction.hpp:47
SourceFunction(const SourceFunction &)=delete
SourceFunction(SourceFunction &&)=delete
real frequency() const
Definition cl_SourceFunction.hpp:307
SourceFunction()
Definition cl_SourceFunction.cpp:72
SourceFunction & operator=(const SourceFunction &)=delete
real fuzziness() const
Definition cl_SourceFunction.hpp:340
void set_ramp(const real aAmplitude, const real aPeriod, const real aTimeOffset=0.0)
Definition cl_SourceFunction.cpp:124
SourceFunction & operator=(SourceFunction &&)=delete
void set_sigmoid(const real aAmplitude, const real aPeriod, const real aTimeOffset, const real aFuzzyness=0.01)
Definition cl_SourceFunction.cpp:142
void set_constant(const real aAmplitude)
Definition cl_SourceFunction.cpp:111
real amplitude() const
Definition cl_SourceFunction.hpp:291
SourceFunctionType type() const
Definition cl_SourceFunction.hpp:275
real omega() const
Definition cl_SourceFunction.hpp:315
real period() const
Definition cl_SourceFunction.hpp:299
real compute(const real aTime) const
Definition cl_SourceFunction.hpp:283
void set_user_defined(UserFunc *Function)
Definition cl_SourceFunction.cpp:209
void read_user_defined(const string &aLibraryPath, const string &aLabel)
Definition cl_SourceFunction.cpp:219
real phase() const
Definition cl_SourceFunction.hpp:323
real time_offset() const
Definition cl_SourceFunction.hpp:332
void set_periodic(const SourceFunctionType aType, const real aAmplitude, const real aPeriod, const real aPhase)
Definition cl_SourceFunction.cpp:161
@ beta
Definition cl_FEM_Calculator.hpp:114
USER GUIDES:
Definition cl_Capacitor.cpp:16
SourceFunctionType
Definition cl_SourceFunction.hpp:58
@ UNDEFINED
Definition cl_SourceFunction.hpp:67
@ Sigmoid
Definition cl_SourceFunction.hpp:61
@ Triangle
Definition cl_SourceFunction.hpp:64
@ Sine
Definition cl_SourceFunction.hpp:62
@ Ramp
Definition cl_SourceFunction.hpp:60
@ UserDefined
Definition cl_SourceFunction.hpp:66
@ Sawtooth
Definition cl_SourceFunction.hpp:65
@ Constant
Definition cl_SourceFunction.hpp:59
@ Square
Definition cl_SourceFunction.hpp:63
real UserFunc(const real)
Definition cl_SourceFunction.hpp:77
int proc_t
Definition commtypes.hpp:29
SourceFunctionType boundary_condition_function_type(const string &aString)
Definition cl_SourceFunction.cpp:27
double real
Definition typedefs.hpp:36
f
Definition test_curve_frame.py:26
d
Definition test_twist_crosscheck.py:91