12#ifndef BELFEM_CL_JCFUNCTION_DATABASE_HPP
13#define BELFEM_CL_JCFUNCTION_DATABASE_HPP
68 const real mAngleMin ;
69 const real mAngleMax ;
70 const real mInvLn10 = 1.0/std::log( 10. );
78 bool mHaveSelfField = false ;
111 mDatabase( new
Database( aPath, aLabel ) ),
112 mTmin( mDatabase->
min( 0 ) ),
113 mTmax( mDatabase->
max( 0 ) ),
114 mBmin( std::pow( 10., mDatabase->
min( 1 ) ) ),
115 mBmax( std::pow( 10., mDatabase->
max( 1 ) ) ),
116 mAngleMin( mDatabase->
min( 2 ) ),
117 mAngleMax( mDatabase->
max( 2 ) )
126 "angular range of database %s spans [ %g, %g ] rad, but the unfolded\n"
127 "bn_angle contract requires coverage of [ 0, pi ]: a folded export\n"
128 "would silently mis-sample theta > pi/2",
129 aLabel.c_str(), (
double ) mAngleMin, (
double ) mAngleMax );
155 const string & aLabel )
162 const bool tIsJc = aLabel ==
"jc" ;
163 const string tColumn = tIsJc ?
"icw" :
"n" ;
175 hid_t tFile = H5Fopen( aPath.c_str(),
176 H5F_ACC_RDONLY, H5P_DEFAULT );
183 if ( H5Lexists( tFile,
"source", H5P_DEFAULT ) > 0 )
185 hid_t tGroup = H5Gopen2( tFile,
"source", H5P_DEFAULT );
196 tGroup,
"columns", tColumns, tStatus );
200 tGroup,
"points", tPoints, tStatus );
210 if ( tColumns( k ) ==
"temperature" ) tIt = k ;
211 if ( tColumns( k ) ==
"field" ) tIb = k ;
212 if ( tColumns( k ) == tColumn ) tIv = k ;
218 if ( tIt < tPoints.
n_cols()
221 && tIt < tColumns.
size()
222 && tIb < tColumns.
size()
223 && tIv < tColumns.
size() )
228 tPoints, tIt, tIb, tIv,
231 if ( tIsJc && tT.
length() > 0 )
248 hid_t tMeta = H5Gopen2( tFile,
"meta",
255 "Icw_77p5K_sf_A_per_m" ) )
258 tMeta,
"Icw_77p5K_sf_A_per_m",
270 if ( tIcw > 0.0 && tTeff > 0.0 )
273 const real tCurve77p5 =
276 if ( tCurve77p5 > 0.0 )
278 tScale = ( tIcw / tTeff )
288 k < tValue.
length(); ++k )
290 tValue( k ) *= tScale ;
317 if ( tN == 0 ) return ;
328 mSelfFieldValue = tValue ;
329 mHaveSelfField = true ;
356 constexpr real tTol = 0.25 ;
362 for (
index_t i = 0; i < tNumRows; ++i )
364 const real tB = aPoints( i, aColB ) ;
365 const real tTi = aPoints( i, aColT ) ;
366 const real tVi = aPoints( i, aColV ) ;
371 if ( tB != 0.0 ) continue ;
372 if ( ! std::isfinite( tTi ) ) continue ;
373 if ( ! std::isfinite( tVi ) ) continue ;
374 if ( tVi <= 0.0 ) continue ;
379 if ( std::abs( tT( k ) - tTi ) < tTol ) { tBin = k ; break ; }
382 if ( tBin == tT.
size() )
390 tSum( tBin ) += tVi ;
396 if ( tNumBins < 2 ) return ;
400 for (
index_t k = 0; k < tNumBins; ++k ) tOrder( k ) = k ;
402 for (
index_t a = 0; a + 1 < tNumBins; ++a )
404 for (
index_t b = a + 1; b < tNumBins; ++b )
406 if ( tT( tOrder( b ) ) < tT( tOrder( a ) ) )
408 const index_t tSwap = tOrder( a ) ;
409 tOrder( a ) = tOrder( b ) ;
410 tOrder( b ) = tSwap ;
418 for (
index_t k = 0; k < tNumBins; ++k )
420 const index_t j = tOrder( k ) ;
422 aValue( k ) = aScale * tSum( j )
423 /
static_cast< real >( tCount( j ) ) ;
440 return std::pow( 10.,
min( mDatabase->values() ) ) ;
501 if ( T >= aT( tN - 1 ) )
504 return aValue( tN - 1 ) ;
508 while ( k < tN - 1 && aT( k ) < T ) ++k ;
510 const real tDT = aT( k ) - aT( k - 1 ) ;
511 dJ0dT = ( aValue( k ) - aValue( k - 1 ) ) / tDT ;
513 return aValue( k - 1 )
514 + dJ0dT * ( T - aT( k - 1 ) ) ;
524 "self_field() called on a table without a self-field curve" );
525 return interp_curve( mSelfFieldT, mSelfFieldValue, T, dJ0dT ) ;
533 const real tTc = std::clamp( T, mTmin, mTmax ) ;
534 const real tU = std::log( mBmin ) * mInvLn10 ;
542 V = this->
eval( mBmin, angle, T ) ;
543 S = V * mDatabase->evaluate_derivy( tTc, tU, tTh ) / mBmin ;
558 return angle < mAngleMin - 1e-6 ? angle +
constant::pi :
560 std::clamp( angle, mAngleMin, mAngleMax );
589 if ( mHaveSelfField && normB < mBmin )
599 if ( tJ0 < tV )
return tV ;
601 const real tD = tV - tJ0 ;
602 real tC = 2. * tD / mBmin - tS ;
616 tB = tD / ( mBmin * mBmin ) ;
620 tB = ( tS - tD / mBmin ) / mBmin ;
623 return tJ0 + normB * ( tC + normB * tB ) ;
626 return std::pow( 10.,
628 std::clamp( T, mTmin, mTmax ),
629 std::log( std::clamp( normB, mBmin, mBmax ) ) * mInvLn10,
630 this->wrap_angle( angle ) ) );
652 if ( mHaveSelfField && normB < mBmin )
658 if ( tJ0 < tV )
return 0.0 ;
660 const real tD = tV - tJ0 ;
661 real tC = 2. * tD / mBmin - tS ;
669 tB = tD / ( mBmin * mBmin ) ;
673 tB = ( tS - tD / mBmin ) / mBmin ;
678 return tC + 2. * tB * normB ;
681 if ( normB <= mBmin || normB >= mBmax )
return 0.0 ;
683 return this->
eval( normB, angle, T )
684 * mDatabase->evaluate_derivy(
685 std::clamp( T, mTmin, mTmax ),
686 std::log( normB ) * mInvLn10,
701 if ( mHaveSelfField && normB < mBmin )
721 const real tVth0 = tV * mDatabase->evaluate_derivz(
722 std::clamp( T, mTmin, mTmax ),
723 std::log( mBmin ) * mInvLn10,
731 if ( tJ0 < tV )
return tVth0 ;
733 const real tBeta = normB / mBmin ;
738 const real tD = tV - tJ0 ;
739 const bool tLimited = ( 2. * tD / mBmin - tS ) > 0.0 ;
741 return tVth0 * ( tLimited ? tBeta * tBeta
742 : tBeta * ( 2. - tBeta ) ) ;
745 return this->
eval( normB, angle, T )
746 * mDatabase->evaluate_derivz(
747 std::clamp( T, mTmin, mTmax ),
748 std::log( std::clamp( normB, mBmin, mBmax ) ) * mInvLn10,
763 if ( mHaveSelfField && normB < mBmin )
775 const real tVT = ( T <= mTmin || T >= mTmax ) ? 0.0 :
776 tV * mDatabase->evaluate_derivx(
778 std::log( mBmin ) * mInvLn10,
783 if ( tJ0 < tV )
return tVT ;
785 const real tBeta = normB / mBmin ;
788 const real tD = tV - tJ0 ;
789 const bool tLimited = ( 2. * tD / mBmin - tS ) > 0.0 ;
790 const real tShape = tLimited ? tBeta * tBeta
791 : tBeta * ( 2. - tBeta ) ;
793 return tJ0dT + ( tVT - tJ0dT ) * tShape ;
796 if ( T <= mTmin || T >= mTmax )
return 0.0 ;
798 return this->
eval( normB, angle, T )
799 * mDatabase->evaluate_derivx(
801 std::log( std::clamp( normB, mBmin, mBmax ) ) * mInvLn10,
#define BELFEM_ERROR(aCheck,...)
Definition assert.hpp:264
#define BELFEM_ASSERT(aCheck,...)
Definition assert.hpp:244
Base class hierarchy for critical current density (Jc) and n-value functions.
size_t size() const
return the size of the Cell
Definition cl_Cell.hpp:181
void push(const T &aValue)
push an entry to the end of the cell (copy version)
Definition cl_Cell.hpp:262
Precomputed lookup table on a tensor grid; evaluation, derivatives and HDF5 persistence.
Definition cl_Database.hpp:30
size_t n_rows() const
Definition cl_AR_Matrix.hpp:205
size_t n_cols() const
Definition cl_AR_Matrix.hpp:213
void set_size(const size_t aNumRows)
change the size of the vector
Definition cl_AR_Vector.hpp:237
size_t length() const
get the length of the vector
Definition cl_AR_Vector.hpp:257
real deval_dbeta(const real normB, const real angle, const real T) const override
d(value)/dθ from the spline
Definition cl_JcFunction_Database.hpp:699
~JcFunctionDatabase() override
Destructor - deletes the database.
Definition cl_JcFunction_Database.hpp:430
void edge_value_and_slope(const real angle, const real T, real &V, real &S) const
the table's own value and d/dB at the low-field edge
Definition cl_JcFunction_Database.hpp:530
real deval_dT(const real normB, const real angle, const real T) const override
d(value)/dT from the spline, clamp-consistent
Definition cl_JcFunction_Database.hpp:761
real self_field(const real T, real &dJ0dT) const
Definition cl_JcFunction_Database.hpp:518
real wrap_angle(const real angle) const
Wrap the caller's angle into the table window.
Definition cl_JcFunction_Database.hpp:556
static real interp_curve(const Vector< real > &aT, const Vector< real > &aValue, const real T, real &dJ0dT)
Definition cl_JcFunction_Database.hpp:490
void reduce_self_field_rows(const Matrix< real > &aPoints, const index_t aColT, const index_t aColB, const index_t aColV, const real aScale, Vector< real > &aT, Vector< real > &aValue) const
Bin the B = 0 rows by temperature and average over angle.
Definition cl_JcFunction_Database.hpp:343
real eval(const real normB, const real angle, const real T) const override
Evaluate Jc or n at given field, angle, and temperature.
Definition cl_JcFunction_Database.hpp:587
JcFunctionDatabase(const string &aPath, const string &aLabel)
Constructor - loads database from file.
Definition cl_JcFunction_Database.hpp:109
void load_self_field(const string &aPath, const string &aLabel)
Read the measured B = 0 level from the table's /source.
Definition cl_JcFunction_Database.hpp:154
real deval_dB(const real normB, const real angle, const real T) const override
d(value)/d|B| from the spline, clamp-consistent
Definition cl_JcFunction_Database.hpp:650
real min_value() const override
Definition cl_JcFunction_Database.hpp:438
void set_dependency(const JcParameter aParameter)
Mark function as depending on a parameter.
Definition cl_JcFunction.cpp:20
JcFunction()=default
Default constructor.
Smallest entry of a vector, matrix or column view.
const real pi
circle number
Definition constants.hpp:41
bool group_exists(hid_t aLoc, const std::string &aLabel)
test if a group exists ( tests for a link of that name; the link type is not checked )
Definition hdf5_tools.hpp:54
void load_scalar_from_file(hid_t &aLoc, const std::string &aLabel, T &aValue, herr_t &aStatus)
Definition hdf5_tools.hpp:300
void load_strings_from_file(hid_t &aLoc, const std::string &aLabel, Cell< string > &aCell, herr_t &aStatus)
Definition hdf5_tools.hpp:1023
void load_matrix_from_file(hid_t &aLoc, const std::string &aLabel, Matrix< T > &aMatrix, herr_t &aStatus, const bool aTranspose=false)
Definition hdf5_tools.hpp:841
bool dataset_exists(hid_t aLoc, const std::string &aLabel)
test if a dataset exists ( tests for a link of that name; the link type is not checked )
Definition hdf5_tools.hpp:37
Definition cl_BhCurve.cpp:19
@ angleNxB
Definition cl_JcFunction.hpp:82
@ normB
Definition cl_JcFunction.hpp:81
@ T
Definition cl_JcFunction.hpp:83
USER GUIDES:
Definition cl_Capacitor.cpp:16
proc_t comm_rank()
Returns the rank of the current process in the communicator.
Definition commtools.cpp:30
int hid_t
Definition hdf5_types.hpp:20
unsigned int uint
Definition typedefs.hpp:30
T max(const Vector< T > &aVector)
Definition fn_AR_max.hpp:24
T min(const Vector< T > &aVector)
Definition fn_AR_min.hpp:25
void broadcast(Cell< string > &aData, const proc_t aRoot)
Definition commtools.cpp:276
uint32_t index_t
Definition typedefs.hpp:52
int herr_t
Definition hdf5_types.hpp:21
double real
Definition typedefs.hpp:36