BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_Material_UserDefined.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, through
4 * 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_MATERIAL_USERDEFINED_HPP
13#define BELFEM_CL_MATERIAL_USERDEFINED_HPP
14
15#include "cl_Material.hpp"
16
17// note: NEVER include cl_Vector.hpp or cl_Matrix.hpp here, or any class that uses it.
18// Doing so would break the API for the user defined materials.
19
20namespace belfem
21{
22 namespace material
23 {
24//------------------------------------------------------------------------------
25
26 template < typename T >
27 T
28 polyval( const std::vector< T > & coeffs, const T x )
29 {
30 const index_t n = coeffs.size() ;
31 T y = coeffs[ 0 ];
32
33 for( index_t k=1; k<n; ++k )
34 {
35 y *= x;
36 y += coeffs[ k ];
37 }
38
39 return y;
40 }
41
42//------------------------------------------------------------------------------
43
44 template < typename T >
45 T
46 dpolyval( const std::vector< T > & coeffs, const T x )
47 {
48 const index_t n = coeffs.size() - 1 ;
49 T p = ( T ) n ;
50 T dydx = p * coeffs[ 0 ];
51
52 for( index_t k=1; k<n; ++k )
53 {
54 dydx *= x;
55 p -= 1.0 ;
56 dydx += p * coeffs[ k ];
57 }
58
59 return dydx;
60 }
61
62//------------------------------------------------------------------------------
63
137 {
138 void * mHandle ;
139
140 Cell< MatFunc1 * > mUserFunctions ;
141
142 Cell< Cell< real > > mPolynomials ;
143
144 real ( UserDefinedMaterial::*mMuFunction ) ( const real H, const real T ) const = nullptr ;
145
146 MatFunc2 * mUserMuFunction = nullptr ;
147 MatFunc3 * mUserRhoFunction = nullptr ;
148 MatFunc3 * mUserLambdaFunction = nullptr ;
149
150
151 public :
152
163 UserDefinedMaterial( const string & aLibraryPath, const string & aLabel ) ;
164
168 ~UserDefinedMaterial() override;
169
194 void
196 const MaterialProperty Property,
197 const MaterialDependency Dependency,
198 MatFunc1 * Function ) override ;
199
226 void
228 const MaterialProperty Property,
229 const MaterialDependency Dependency1,
230 const MaterialDependency Dependency2,
231 MatFunc2 * Function ) override ;
232
267 void
269 const MaterialProperty Property,
270 const MaterialDependency Dependency1,
271 const MaterialDependency Dependency2,
272 const MaterialDependency Dependency3,
273 MatFunc3 * Function ) override ;
274
297 void
299 const MaterialProperty Property,
300 const Cell< real > & Coefficients ) override;
301
302 void
304 const MaterialProperty Property,
305 const std::vector< real > & Coefficients ) override ;
306
318 real
320 const MaterialProperty Property,
321 const real T ) const override ;
322
323 real
325 const MaterialProperty Property,
326 const real T ) const override ;
327
328 protected:
329
331 real
332 E_custom(const real T) const override
333 {
334 return mUserFunctions( static_cast<size_t>(MaterialProperty::E) )( this, T );
335 }
336
338 real
339 nu_custom(const real T) const override
340 {
341 return mUserFunctions( static_cast<size_t>(MaterialProperty::nu))( this, T);
342 }
343
345 real
346 cp_custom(const real T) const override
347 {
348 return mUserFunctions( static_cast<size_t>(MaterialProperty::cp))( this, T);
349 }
350
352 real
353 lambda_custom( const real T ) const override
354 {
355 return mUserFunctions( static_cast<size_t>(MaterialProperty::lambda))( this, T);
356 }
357
359 real
360 lambda_custom(const real T, const real normB, const real angle) const override
361 {
362 return ( * mUserLambdaFunction ) (this, T, normB, angle );
363 }
364
366 real
367 rho_custom(const real T) const override
368 {
369 return mUserFunctions( static_cast<size_t>(MaterialProperty::rho))( this, T );
370 }
371
373 real
374 rho_kohler(const real T, const real normB, const real angle) const override
375 {
376 return ( * mUserRhoFunction ) (this, T, normB, angle );
377 }
378
380 real
381 jc_custom(const real T) const override
382 {
383 return mUserFunctions( static_cast<size_t>(MaterialProperty::jc))( this, T );
384 }
385
387 real
388 n_custom(const real T) const override
389 {
390 return mUserFunctions( static_cast<size_t>(MaterialProperty::n))( this, T );
391 }
392
394 real
395 alpha_custom(const real T) const override
396 {
397 return mUserFunctions( static_cast<size_t>(MaterialProperty::alpha))( this, T);
398 }
399
401 real
402 Rp02_custom(const real T) const override
403 {
404 return mUserFunctions( static_cast<size_t>(MaterialProperty::Rp02))( this, T);
405 }
406
408 real
409 mu_custom( const real H, const real T ) const override
410 {
411 return ( this->*mMuFunction )( H, T );
412 }
413
414 private:
415
417 real
418 mu_polynomial( const real H, const real T ) const
419 {
421 }
422
424 real
425 mu_user( const real H, const real T ) const
426 {
427 return mUserMuFunction( this, H, T );
428 }
429
430 void
432
433 };
434
435//------------------------------------------------------------------------------
436
437 inline real
439 {
440 BELFEM_ASSERT( mPolynomials( static_cast<size_t>(Property) ).size() > 0,
441 "return polynomial for %s of %s is not defined",
442 to_string( Property ).c_str(),
443 this->label().c_str()
444 );
445
446 return polyval( mPolynomials( static_cast<size_t>(Property) ).vector_data(), T );
447 }
448
449 inline real
451 {
452 BELFEM_ASSERT( mPolynomials( static_cast<size_t>(Property) ).size() > 0,
453 "return polynomial for %s of %s is not defined",
454 to_string( Property ).c_str(),
455 this->label().c_str()
456 );
457
458 return dpolyval( mPolynomials( static_cast<size_t>(Property) ).vector_data(), T );
459 }
460
461 }
462}
463#endif //BELFEM_CL_MATERIAL_USERDEFINED_HPP
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
virtual real H(const real B) const
Magnetic field strength from flux density.
Definition cl_Material.hpp:2137
Material(const MaterialType aType, const bool aIsIsotropic=true)
Constructor.
Definition cl_Material.cpp:149
const string & label() const
Get material label.
Definition cl_Material.hpp:1683
void set_user_defined_polynomial(const MaterialProperty Property, const Cell< real > &Coefficients) override
Set a polynomial function for a material property.
Definition cl_Material_UserDefined.cpp:230
real nu_custom(const real T) const override
Custom Poisson's ratio evaluation - calls user function.
Definition cl_Material_UserDefined.hpp:339
real jc_custom(const real T) const override
Custom jc evaluation (temperature only) - calls user function.
Definition cl_Material_UserDefined.hpp:381
real Rp02_custom(const real T) const override
Custom 0.2% proof stress evaluation - calls user function.
Definition cl_Material_UserDefined.hpp:402
real alpha_custom(const real T) const override
Custom thermal expansion coefficient evaluation - calls user function.
Definition cl_Material_UserDefined.hpp:395
real cp_custom(const real T) const override
Custom specific heat evaluation - calls user function.
Definition cl_Material_UserDefined.hpp:346
real n_custom(const real T) const override
Custom n evaluation (temperature only) - calls user function.
Definition cl_Material_UserDefined.hpp:388
real E_custom(const real T) const override
Custom Young's modulus evaluation - calls user function.
Definition cl_Material_UserDefined.hpp:332
real mu_custom(const real H, const real T) const override
Custom magnetic permeability evaluation - dispatches to polynomial or user function.
Definition cl_Material_UserDefined.hpp:409
real evaluate_polynomial(const MaterialProperty Property, const real T) const override
Evaluate a polynomial function for a given property and temperature.
Definition cl_Material_UserDefined.hpp:438
UserDefinedMaterial(const string &aLibraryPath, const string &aLabel)
Constructor - loads and initializes user-defined material.
Definition cl_Material_UserDefined.cpp:24
real lambda_custom(const real T, const real normB, const real angle) const override
Custom thermal conductivity evaluation (with field) - calls user function.
Definition cl_Material_UserDefined.hpp:360
real lambda_custom(const real T) const override
Custom thermal conductivity evaluation (temperature only) - calls user function.
Definition cl_Material_UserDefined.hpp:353
real rho_kohler(const real T, const real normB, const real angle) const override
Custom electrical resistivity evaluation (with field) - calls user function.
Definition cl_Material_UserDefined.hpp:374
void set_user_defined_function(const MaterialProperty Property, const MaterialDependency Dependency, MatFunc1 *Function) override
Set a user-defined function with one dependency (typically temperature).
Definition cl_Material_UserDefined.cpp:55
real rho_custom(const real T) const override
Custom electrical resistivity evaluation (temperature only) - calls user function.
Definition cl_Material_UserDefined.hpp:367
real evaluate_derivative_of_polynomial(const MaterialProperty Property, const real T) const override
Definition cl_Material_UserDefined.hpp:450
Definition cl_BhCurve.cpp:19
T dpolyval(const std::vector< T > &coeffs, const T x)
Definition cl_Material_UserDefined.hpp:46
T polyval(const std::vector< T > &coeffs, const T x)
Definition cl_Material_UserDefined.hpp:28
@ T
Definition cl_JcFunction.hpp:83
USER GUIDES:
Definition cl_Capacitor.cpp:16
MaterialDependency
Dependencies that material properties can have.
Definition cl_Material.hpp:121
real MatFunc1(const Material *, const real)
Definition cl_Material.hpp:241
real MatFunc3(const Material *, const real, const real, const real)
Definition cl_Material.hpp:243
MaterialProperty
All material properties that can be defined.
Definition cl_Material.hpp:153
@ nu
Definition cl_Material.hpp:156
@ alpha
Definition cl_Material.hpp:161
@ E
Definition cl_Material.hpp:155
@ n
Definition cl_Material.hpp:187
@ rho
Definition cl_Material.hpp:160
@ mu
Definition cl_Material.hpp:159
@ lambda
Definition cl_Material.hpp:158
@ cp
Definition cl_Material.hpp:157
@ Rp02
Definition cl_Material.hpp:162
@ jc
Definition cl_Material.hpp:186
uint32_t index_t
Definition typedefs.hpp:52
real MatFunc2(const Material *, const real, const real)
Definition cl_Material.hpp:242
double real
Definition typedefs.hpp:36
string to_string(const DomainType aDomainType)
Definition en_DomainType.cpp:21