1 #ifndef SM_BeamHysteresis_H
2 #define SM_BeamHysteresis_H
5 #include "SM_BeamCycle.h"
8 #include "SM_ZeemanOperator.h"
14 #include "SM_LandauLifschitzSystem.h"
17 #include "SM_StochasticHysteresis.h"
20 #include "CORE_Chrono.h"
37 std::array<tReal,7> mHextRange;
40 tInteger mCycleStepsNumber;
42 std::array<tReal,SM_Constants::DIM> mHextMax;
43 std::array<tReal,SM_Constants::DIM> mHextMin;
44 std::array<tReal,SM_Constants::DIM> mHext;
47 std::array<tReal,SM_Constants::DIM> mDHext;
54 tCInt mHysteresisDirection;
55 tRelativeInteger mHysteresisIndex;
58 tInteger mBackupIndex;
60 tInteger mBackupsNumber;
61 tInteger mBackupStepsNumber;
64 tBoolean mAreCriticalPointsSaved;
65 tInteger mCriticalPointIndex;
66 tBoolean mIsCriticalPoint;
69 tBoolean mIsRestoring;
90 mHysteresisDirection=0;
102 mZeemanOperator=
null;
103 mAreCriticalPointsSaved=
true;
104 mCriticalPointIndex=0;
105 mIsCriticalPoint=
false;
155 mem+=mHysFileName.length()*
sizeof(tChar);
156 mem+=mHextRange.size()*
sizeof(tReal);
164 static CORE_UniquePointer<SelfClass>
New() {
165 return CORE_UniquePointer<SelfClass>(
new SelfClass(),
177 mAreCriticalPointsSaved=flag;
184 mBackupsNumber=nBackups;
192 mBackupStepsNumber=s;
209 mHysFileName=hysteresisFile;
210 tIndex iExt=mHysFileName.rfind(
".hys");
211 if (iExt==tString::npos) {
212 mHysFileName+=
".hys";
226 const tInteger& outputId,
227 tIndex& iCycleStep) {
237 const tReal *iRange=mHextRange.data();
238 for(
auto& Hk:mHextMin) {
242 for(
auto& Hk:mHextMax) {
247 mHextNumber=mHextRange[6];
253 for(
auto& Hk:mHextMax) {
256 for(
auto& Hk:mHextMin) {
267 const tReal *iHext=mHextMin.data();
268 for(
auto& Dk:mDHext) {
276 mDH=mAHext/mHextNumber;
279 for(
auto& Dk:mDHext) Dk/=mAHext;
283 if (mZeemanOperator==
null) {
284 std::cout<<
"fatal error : use-zeeman must be true in options\n";
289 memcpy(mHext.data(),mHextMax.data(),
sizeof(tReal)*mHextMax.size());
290 mZeemanOperator->
setHext(mHext);
293 mCycleStepsNumber=2*mHextNumber+1;
302 if (hysOutput==
null) {
304 "SM_BeamHysteresis::open(...)",
305 "Fatal Error: bad stochastic output for this cycle. Aborted");
314 if (!restoreFromLastBackup(system,iCycleStep,outputId)) {
316 "SM_BeamHysteresis::open(...)",
317 "Fatal Error: impossible to restore hysterisis cycle. Aborted");
324 std::ofstream file(mHysFileName.c_str(),std::ios::out);
327 std::cout<<
"impossible to open Hysteresis file : "<<mHysFileName<<
"\n";
332 printHeader(file,beam);
339 mHysteresisDirection=-1;
341 mCriticalPointIndex=0;
342 mIsCriticalPoint=
false;
370 tReal oldHysValue=mHysValue;
374 if (!mIsCriticalPoint) mIsCriticalPoint=(mHysValue*oldHysValue<=0);
378 const tReal *iHext=mHext.data();
379 for(
const auto& Dk:mDHext) {
389 std::ofstream file(mHysFileName.c_str(),std::ios::app);
390 file<<((int)mHysteresisDirection)<<
"\t"<<mHysteresisIndex<<
"\t"<<std::setprecision(12)<<mH<<
"\t"<<system.
getStepIndex();
391 file<<
"\t"<<std::setprecision(12)<<mHysValue;
392 if (llSystem!=
null) {
393 file<<
"\t"<<std::setprecision(12)<<llSystem->
getTime();
394 file<<
"\t"<<std::setprecision(12)<<netTorque;
396 file<<
"\t"<<(mTimer.
stop()/1000);
400 if (mIsCriticalPoint && mAreCriticalPointsSaved) {
401 tString SFileName=getSFile()+std::to_string(mCriticalPointIndex)+
".cpt";
402 std::stringstream SHeader;
403 SHeader<<
"dir="<<((int)mHysteresisDirection)<<
"\n";
404 SHeader<<
"i="<<((int)mHysteresisIndex)<<
"\n";
405 SHeader<<
"h="<<std::setprecision(12)<<mH<<
"\n";
406 SHeader<<
"criticalPointIndex="<<mCriticalPointIndex<<
"\n";
407 S.saveToFile(SHeader.str(),SFileName,12);
408 mCriticalPointIndex++;
409 mIsCriticalPoint=
false;
413 if ((mBackupStep==mBackupStepsNumber)) {
414 tString SFileName=getSFile()+std::to_string(mBackupIndex)+
".back";
415 std::stringstream SHeader;
416 SHeader<<
"dir="<<((int)mHysteresisDirection)<<
"\n";
417 SHeader<<
"i="<<((int)mHysteresisIndex)<<
"\n";
418 SHeader<<
"h="<<std::setprecision(12)<<mH<<
"\n";
419 SHeader<<
"criticalPointIndex="<<mCriticalPointIndex<<
"\n";
420 if(S.saveToFile(SHeader.str(),SFileName,12)) {
422 mBackupIndex%=mBackupsNumber;
432 moveToNextCycle(iCycleStep,S,system,outputId);
443 inline void moveToNextCycle(
const tIndex& iCycleStep,
const SM_RealField& S0,
SM_System& system,
const tInteger& outputId) {
445 if (iCycleStep==mHextNumber) {
448 mHysteresisDirection*=-1;
450 mHysteresisIndex-=mHysteresisDirection;
456 mH+=mDH*mHysteresisDirection;
459 if (!mIsCriticalPoint) mIsCriticalPoint=(mH*Hold<=0);
462 const tReal *iD=mDHext.data();
463 for(
auto& Hk:mHext) {
469 mZeemanOperator->
setHext(mHext);
479 inline tBoolean restoreFromLastBackup(
SM_System& system,tIndex& iCycleIndex,
const tInteger& outputId) {
481 std::valarray<tReal> hValues;
482 std::valarray<tReal> hysValues;
484 if (!
LoadFromFile(mHysFileName,mDHext,mAHext,mDH,mCycleStepsNumber,
485 mHysteresisDirection,mHysteresisIndex,mH,
486 nHysValues,hValues,hysValues))
return false;
487 mHysValue=hysValues[nHysValues-1];
490 mHextNumber=(mCycleStepsNumber-1)/2;
496 tRelativeInteger d=0;
497 tRelativeInteger i=-1;
501 for (mBackupIndex=0;mBackupIndex<mBackupsNumber;mBackupIndex++) {
502 SFile=getSFile()+std::to_string(mBackupIndex)+
".back";
503 std::ifstream file(SFile.c_str(),std::ios::in);
505 while ( (!file.eof()) && (std::getline(file,line)) && (nReadData<nMaxReadData)) {
506 nReadData+=ReadKeyValue(line,
"#dir",d);
507 nReadData+=ReadKeyValue(line,
"#i",i);
509 if ( (((tCInt)d)==mHysteresisDirection) && (i==mHysteresisIndex)) {
512 while ( (!file.eof()) && (std::getline(file,line)) && (nReadData<nMaxReadData)) {
513 nReadData+=ReadKeyValue(line,
"#criticalPointIndex",mCriticalPointIndex);
518 if (mBackupIndex==mBackupsNumber) {
519 std::cout<<
"can not find the S backup file from backup files of type "<<SFile<<
" and index in [0,"<<mBackupsNumber<<
"[ with hysteresis direction \n";
520 std::cout<<std::to_string((
int)mHysteresisDirection)+
" and index ";
521 std::cout<<std::to_string((
int)mHysteresisIndex)+
" ";
530 iCycleIndex=mHysteresisIndex;
531 if (mHysteresisDirection==1) {
532 iCycleIndex=mHextNumber;
533 iCycleIndex+=mHextNumber;
534 iCycleIndex-=mHysteresisIndex;
536 moveToNextCycle(iCycleIndex,S,system,outputId);
548 inline tString getSFile()
const {
549 tString SFileName=mHysFileName;
550 tIndex iExt=SFileName.rfind(
".hys");
572 std::array<tReal,SM_Constants::DIM>& direction,
575 tInteger& nCycleSteps,
577 tRelativeInteger& hysStep,
579 tInteger& nHysValues,std::valarray<tReal>& hs,std::valarray<tReal>& hys) {
582 std::ifstream file(fileName.c_str(),std::ios::in);
584 std::cout<<
"file "<<fileName<<
" does not exist\n";
591 std::vector<tString> keys={
597 int nMaxReadData=keys.size();
599 std::vector<tString>::const_iterator iKey;
600 while ( (!file.eof()) && (std::getline(file,line)) && (nReadData<nMaxReadData)) {
602 nReadData+=ReadKeyValue(line,*iKey,direction);iKey++;
603 nReadData+=ReadKeyValue(line,*iKey,amplitude);iKey++;
604 nReadData+=ReadKeyValue(line,*iKey,dh);iKey++;
605 nReadData+=ReadKeyValue(line,*iKey,nCycleSteps);iKey++;
607 if (nReadData!=nMaxReadData) {
608 std::cout<<
"error in reading the data from hysteresis file "<<fileName<<
" nReadData:"<<nReadData<<
" != "<<nMaxReadData<<
"\n";
609 for(
const auto& key:keys) {
610 std::cout<<
"\t "<<key<<
"\n";
614 hs.resize(nCycleSteps);
615 hys.resize(nCycleSteps);
620 while ( (!file.eof()) && (std::getline(file,line)) && (line[0]==
'#'));
624 std::stringstream sstream;
626 sstream>>step;hysDirection=step;
628 sstream>>h;(*ihs)=h;ihs++;
630 sstream>>(*ihys);ihys++;
633 }
while ( (!file.eof()) && (std::getline(file,line)) && (line.length()>0));
646 return (iCycle==mCycleStepsNumber);
652 std::stringstream ret;
654 ret<<
"\t Direction of Hext="<<functions_array::toString(mDHext)<<
"\n";
655 ret<<std::setprecision(12)<<
"\t Amplitude of Hext="<<mAHext<<
"\n";
656 ret<<std::setprecision(12)<<
"\t Variation of Hext="<<mDH<<
"\n";
657 ret<<std::setprecision(12)<<
"\t Adimensionized Max of Hext="<<functions_array::toString(mHextMax)<<
"\n";
658 ret<<
"\t number of steps:"<<mCycleStepsNumber<<
"\n";
673 inline void printHeader(std::ofstream& file,
const SM_Beam& beam)
const {
676 tString str=
"Hysteresis Cycle:"+this->
toString();
677 functions_string::replaceAll(
"\n",
"\n#",str);
678 file<<
"#"<<str<<
"\n";
681 functions_string::replaceAll(
"\n",
"\n#",str);
682 file<<
"#"<<str<<
"\n";
684 file<<
"# [...] : for Landau Lifschitz system only \n";
685 file<<
"#<hysteresis direction> <hysteresis index> <H> <Step Index> <HysValue> [<time> <netTorque>] <duration in s>\n";
692 template<
typename T,
size_t D>
693 static tBoolean ReadKeyValue(tString& line,
const tString& key,std::array<T,D>& values) {
694 auto *iValues=values.data();
697 tIndex iKey=line.find(key);
698 if (iKey!=tString::npos) {
699 line=line.substr(iKey+key.length());
701 if (iKey==tString::npos) iKey=line.find(
":");
702 if (iKey!=tString::npos) {
703 line=line.substr(iKey+1);
705 if (iKey==tString::npos) iKey=line.find(
"[");
706 if (iKey!=tString::npos) {
707 line=line.substr(iKey+1);
708 iKey=line.rfind(
"}");
709 if (iKey==tString::npos) iKey=line.find(
"]");
710 if (iKey!=tString::npos) {
713 line=line.substr(0,iKey);
714 iKey=line.rfind(
",");
715 while (iKey!=tString::npos) {
716 std::stringstream vss;
717 vss<<std::setprecision(12)<<line.substr(0,iKey);
724 line=line.substr(iKey+1);
725 iKey=line.rfind(
",");
727 std::stringstream vss;
728 vss<<std::setprecision(12)<<line;
745 static tBoolean ReadKeyValue(tString& line,
const tString& key,T& value) {
746 tIndex iKey=line.find(key);
747 if (iKey!=tString::npos) {
748 line=line.substr(iKey+key.length());
750 if (iKey==tString::npos) iKey=line.find(
":");
751 if (iKey!=tString::npos) {
752 line=line.substr(iKey+1);
753 std::stringstream vss;
754 vss<<std::setprecision(12)<<line;
this class describes the chono class by default write on standart output
Definition: CORE_Chrono.h:14
tULLInt stop()
stop the chrono and return the duration time in micro seconds as an int
Definition: CORE_Chrono.h:95
void start()
start the chrono
Definition: CORE_Chrono.h:89
this class describes the exceptions raised for CORE package
Definition: CORE_Exception.h:17
tIndex getElementsNumber() const
return the number values of the container
Definition: CORE_Field.h:135
tBoolean loadFromFile(const tString &filename, tString &comment, tInt &retCode)
load the field from a txt file with the format :
Definition: CORE_Field.h:1003
static tString GetAbsolutePath(const tString &path)
get the absolute path of the path
Definition: CORE_IO.h:365
static tBoolean Exists(const tString &f)
return true if the file or the path exists
Definition: CORE_IO.h:91
class Free introduced for deleting a smart pointer
Definition: CORE_Object.h:113
virtual tMemSize getContentsMemorySize() const
return nthe memory size of the included associations
Definition: CORE_Object.h:278
static const tString & GetSoftName()
get soft name
Definition: CORE_Run.h:115
static const tString & GetVersion()
get the version
Definition: CORE_Run.h:129
This class is an interface of Beam Cycle class for Stoch Microm package.
Definition: SM_BeamCycle.h:20
virtual tString toString() const override
return the string representation of the class
Definition: SM_BeamCycle.h:193
const tBoolean & isRestoring() const
return true if the cycle starts at last computed cycle index
Definition: SM_BeamCycle.h:137
This class is an hysteresis cycle manager for running a cycle of a beam class for Stoch Microm packag...
Definition: SM_BeamHysteresis.h:28
static tBoolean LoadFromFile(const tString &fileName, std::array< tReal, SM_Constants::DIM > &direction, tReal &litude, tReal &dh, tInteger &nCycleSteps, tCInt &hysDirection, tRelativeInteger &hysStep, tReal &h, tInteger &nHysValues, std::valarray< tReal > &hs, std::valarray< tReal > &hys)
read an hysteresis file
Definition: SM_BeamHysteresis.h:571
static CORE_UniquePointer< SelfClass > New()
create an unique pointer to a new instance of this
Definition: SM_BeamHysteresis.h:164
virtual ~SM_BeamHysteresis(void)
destroy
Definition: SM_BeamHysteresis.h:112
SM_BeamHysteresis(void)
create
Definition: SM_BeamHysteresis.h:85
tBoolean close(const tIndex &iCycle, const SM_Beam &beam)
return true if the cycle is close without error
Definition: SM_BeamHysteresis.h:667
void setBackupsNumber(const tInteger &nBackups)
set the number of backups
Definition: SM_BeamHysteresis.h:183
tBoolean isCycleFinished(const tIndex &iCycle, const SM_Beam &beam)
return true if the cycle is finished
Definition: SM_BeamHysteresis.h:645
virtual tMemSize getMemorySize() const
return the memory size of the class and the memory size of all its attributes/associations
Definition: SM_BeamHysteresis.h:141
void setAreCriticalPointsSaved(const tBoolean &flag)
true to save magnetic moment dierctions at critical points
Definition: SM_BeamHysteresis.h:176
virtual tString toString() const override
return the string representation of the class
Definition: SM_BeamHysteresis.h:651
void nextCycleStep(const tIndex &iCycleStep, SM_Beam &beam, const tInteger &outputId, const SM_StochasticOutputComponent &output)
next the cycle of beam run
Definition: SM_BeamHysteresis.h:360
void setHysteresisFileName(const tString &hysteresisFile)
set the hystersis file
Definition: SM_BeamHysteresis.h:208
void setHextRange(const std::array< tReal, 7 > &range)
set the hext range
Definition: SM_BeamHysteresis.h:199
void setBackupStepsNumber(const tInteger &s)
set the number of steps between 2 backups
Definition: SM_BeamHysteresis.h:191
tBoolean open(SM_Beam &beam, SM_StochasticOutputComponent &stochasticOutput, const tInteger &outputId, tIndex &iCycleStep)
init the cycle of beam run
Definition: SM_BeamHysteresis.h:225
virtual tMemSize getContentsMemorySize() const
return the memory size of the included associations
Definition: SM_BeamHysteresis.h:152
This class defines a general stochastic beam of trajectories of system.
Definition: SM_Beam.h:61
const SM_System & getSystem() const
get the system
Definition: SM_Beam.h:264
virtual tString toString() const override
turn the class into a string representation
Definition: SM_Beam.h:315
static constexpr tDimension DIM
space dimension
Definition: SM_Constants.h:80
This class is a simulation of one trajectory class for Stoch Magnet package.
Definition: SM_LandauLifschitzSystem.h:59
const tReal & getTime() const
get the time
Definition: SM_LandauLifschitzSystem.h:214
tReal computeNetTorque() const
compute Net Torque
Definition: SM_LandauLifschitzSystem.h:347
const tReal & getCharacteristicField() const
get the characteristic field in T
Definition: SM_Material.h:367
This class stores stochastic hysteresis values.
Definition: SM_StochasticHysteresis.h:30
void setHysteresisDirection(const std::array< tReal, SM_Constants::DIM > &D)
set the direction of hysteresis
Definition: SM_StochasticHysteresis.h:155
This class stores stochastic outpus computed for each trajectory of a simulation of trajectories of a...
Definition: SM_StochasticOutputComponent.h:36
const std::valarray< tReal > & getOutputValues() const
get the output values of the stochastic output for reading
Definition: SM_StochasticOutputComponent.h:330
This class is a one simulation of a beam for Stoch Magnet package.
Definition: SM_System.h:53
const SM_RealField & getMagneticMomentDirections() const
get the unit direction of spins at time
Definition: SM_System.h:267
void setInitialMagneticMomentDirections(const std::valarray< tReal > &S0)
set the initial unit direction of spins
Definition: SM_System.h:214
const SM_SliceOperator * getOperator(const tString &name) const
get the operator with name
Definition: SM_System.h:305
const SM_RealField & getInitialMagneticMomentDirections() const
get the initial unit direction of spins
Definition: SM_System.h:253
const SM_Material & getMaterial() const
get the material of the network
Definition: SM_System.h:193
const tIndex & getStepIndex() const
get the step index
Definition: SM_System.h:415
virtual tBoolean updateInitialMagneticMomentDirections(const SM_RealField &S0)
update the initial unit direction of spins
Definition: SM_System.h:241
This class is describes a Zeeman operator interface.
Definition: SM_ZeemanOperator.h:26
void setHext(std::initializer_list< tReal > &&values)
set the Hext value
Definition: SM_ZeemanOperator.h:200