TBTK
UnitHandler.h
Go to the documentation of this file.
1 /* Copyright 2016 Kristofer Bj√∂rnson
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
23 #ifndef COM_DAFER45_TBTK_UNIT_HANDLER
24 #define COM_DAFER45_TBTK_UNIT_HANDLER
25 
26 #include <string>
27 #include <math.h>
28 
29 #ifdef M_E //Avoid name clash with math.h macro M_E
30  #define M_E_temp M_E
31  #undef M_E
32 #endif
33 
34 namespace TBTK{
35 
74 public:
81  enum class TemperatureUnit {kK, K, mK, uK, nK};
82 
83  /* Time units (base unit):<br/>
84  * s - second<br/>
85  * ms - millisecond<br/>
86  * us - microsecond<br/>
87  * ns - nanosecond<br/>
88  * ps - picosecond<br/>
89  * fs - femtosecond<br/>
90  * as - attosecond */
91  enum class TimeUnit {s, ms, us, ns, ps, fs, as};
92 
102  enum class LengthUnit{m, mm, um, nm, pm, fm, am, Ao};
103 
112  enum class EnergyUnit{GeV, MeV, keV, eV, meV, ueV, J};
113 
128  enum class ChargeUnit{
129  kC, C, mC, uC, nC, pC, fC, aC, Te, Ge, Me, ke, e
130  };
131 
135  enum class CountUnit{pcs, mol};
136 
147  enum class MassUnit{kg, g, mg, ug, ng, pg, fg, ag, u};
148 
162  enum class MagneticFieldUnit{
163  MT, kT, T, mT, uT, nT, GG, MG, kG, G, mG, uG
164  };
165 
167  static double getHbarB();
168 
170  static double getHbarN();
171 
173  static double getK_BB();
174 
176  static double getK_BN();
177 
179  static double getEB();
180 
182  static double getEN();
183 
185  static double getCB();
186 
188  static double getCN();
189 
191  static double getN_AB();
192 
194  static double getN_AN();
195 
197  static double getM_eB();
198 
200  static double getM_eN();
201 
203  static double getM_pB();
204 
206  static double getM_pN();
207 
209  static double getMu_BB();
210 
212  static double getMu_BN();
213 
215  static double getMu_nB();
216 
218  static double getMu_nN();
219 
221  static double getMu_0B();
222 
224  static double getMu_0N();
225 
227  static double getEpsilon_0B();
228 
230  static double getEpsilon_0N();
231 
233  static void setTemperatureUnit(TemperatureUnit unit);
234 
236  static void setTimeUnit(TimeUnit unit);
237 
239  static void setLengthUnit(LengthUnit unit);
240 
242  static void setEnergyUnit(EnergyUnit unit);
243 
245  static void setChargeUnit(ChargeUnit unit);
246 
248  static void setCountUnit(CountUnit unit);
249 
251  static void setTemperatureScale(double scale);
252 
254  static void setTimeScale(double scale);
255 
257  static void setLengthScale(double scale);
258 
260  static void setEnergyScale(double scale);
261 
263  static void setChargeScale(double scale);
264 
266  static void setCountScale(double scale);
267 
269  static double convertTemperatureNtB(double temperature);
270 
272  static double convertTimeNtB(double time);
273 
275  static double convertLengthNtB(double length);
276 
278  static double convertEnergyNtB(double energy);
279 
281  static double convertChargeNtB(double charge);
282 
284  static double convertCountNtB(double count);
285 
287  static double convertTemperatureBtN(double temperature);
288 
290  static double convertTimeBtN(double time);
291 
293  static double convertLengthBtN(double length);
294 
296  static double convertEnergyBtN(double energy);
297 
299  static double convertChargeBtN(double charge);
300 
302  static double convertCountBtN(double count);
303 
305  static double convertMassDtB(double mass, MassUnit unit);
306 
308  static double convertMassBtD(double mass, MassUnit unit);
309 
311  static double convertMassDtN(double mass, MassUnit unit);
312 
314  static double convertMassNtD(double mass, MassUnit unit);
315 
317  static double convertMagneticFieldDtB(
318  double field,
319  MagneticFieldUnit unit
320  );
321 
323  static double convertMagneticFieldBtD(
324  double field,
325  MagneticFieldUnit unit
326  );
327 
329  static double convertMagneticFieldDtN(
330  double field,
331  MagneticFieldUnit unit
332  );
333 
335  static double convertMagneticFieldNtD(
336  double field,
337  MagneticFieldUnit unit
338  );
339 
344  static std::string getTemperatureUnitString();
345 
349  static std::string getTimeUnitString();
350 
354  static std::string getLengthUnitString();
355 
359  static std::string getEnergyUnitString();
360 
364  static std::string getChargeUnitString();
365 
369  static std::string getCountUnitString();
370 
375  static std::string getMassUnitString();
376 
381  static std::string getMagneticFieldUnitString();
382 
387  static std::string getHBARUnitString();
388 
393  static std::string getK_BUnitString();
394 
399  static std::string getEUnitString();
400 
405  static std::string getCUnitString();
406 
410  static std::string getN_AUnitString();
411 
415  static std::string getM_eUnitString();
416 
420  static std::string getM_pUnitString();
421 
425  static std::string getMu_BUnitString();
426 
430  static std::string getMu_nUnitString();
431 
436  static std::string getMu_0UnitString();
437 
442  static std::string getEpsilon_0UnitString();
443 private:
445  static constexpr double HBAR = 6.582119514e-16;
446 
448  static constexpr double K_B = 8.6173324e-5;
449 
451  static constexpr double E = 1.6021766208e-19;
452 
454  static constexpr double C = 2.99792458e8;
455 
457  static constexpr double N_A = 6.022140857e23;
458 
460  static constexpr double M_E = 5.109989461e5/(C*C);
461 
463  static constexpr double M_P = 9.38272046e8/(C*C);
464 
466  static constexpr double MU_B = E*HBAR/(2.*M_E);
467 
469  static constexpr double MU_N = E*HBAR/(2*M_P);
470 
472  static constexpr double MU_0 = 4*M_PI*1e-7/1.602176565e-19;
473 
475  static constexpr double EPSILON_0 = 8.854187817620e-12*1.602176565e-19;
476 
478  static double hbar;
479 
481  static double k_B;
482 
484  static double e;
485 
487  static double c;
488 
490  static double n_A;
491 
493  static double m_e;
494 
496  static double m_p;
497 
499  static double mu_B;
500 
502  static double mu_n;
503 
505  static double mu_0;
506 
508  static double epsilon_0;
509 
511  static constexpr double J_per_eV = 1.602176565e-19;
512 
514  static constexpr double eV_per_J = 1./J_per_eV;
515 
517  static constexpr double kg_per_baseMass = 1.602176565e-19;
518 
520  static constexpr double baseMass_per_kg = 1./kg_per_baseMass;
521 
523 // static constexpr double u_per_baseMass = 9.31494095e8/(C*C);
524  static constexpr double u_per_baseMass = (C*C)/9.31494095e8;
525 
527  static constexpr double baseMass_per_u = 1./u_per_baseMass;
528 
530  static constexpr double T_per_baseMagneticField = 1.602176565e-19;
531 
533  static constexpr double baseMagneticField_per_T = 1./T_per_baseMagneticField;
534 
536  static TemperatureUnit temperatureUnit;
537 
539  static TimeUnit timeUnit;
540 
542  static LengthUnit lengthUnit;
543 
545  static EnergyUnit energyUnit;
546 
548  static ChargeUnit chargeUnit;
549 
551  static CountUnit countUnit;
552 
554  static double temperatureScale;
555 
557  static double timeScale;
558 
560  static double lengthScale;
561 
563  static double energyScale;
564 
566  static double chargeScale;
567 
569  static double countScale;
570 
572  static void updateHbar();
573 
575  static void updateK_B();
576 
578  static void updateE();
579 
581  static void updateC();
582 
584  static void updateN_A();
585 
587  static void updateM_e();
588 
590  static void updateM_p();
591 
593  static void updateMu_B();
594 
596  static void updateMu_n();
597 
599  static void updateMu_0();
600 
602  static void updateEpsilon_0();
603 
606  static double getTemperatureConversionFactor();
607 
610  static double getTimeConversionFactor();
611 
614  static double getLengthConversionFactor();
615 
618  static double getEnergyConversionFactor();
619 
622  static double getChargeConversionFactor();
623 
626  static double getCountConversionFactor();
627 
630  static double getMassConversionFactor(MassUnit unit);
631 
635  static double getMagneticFieldConversionFactor(MagneticFieldUnit unit);
636 };
637 
638 inline double UnitHandler::getHbarB(){
639  return hbar;
640 }
641 
642 inline double UnitHandler::getHbarN(){
643  return hbar/(energyScale*timeScale);
644 }
645 
646 inline double UnitHandler::getK_BB(){
647  return k_B;
648 }
649 
650 inline double UnitHandler::getK_BN(){
651  return k_B*temperatureScale/energyScale;
652 }
653 
654 inline double UnitHandler::getEB(){
655  return e;
656 }
657 
658 inline double UnitHandler::getEN(){
659  return e/chargeScale;
660 }
661 
662 inline double UnitHandler::getCB(){
663  return c;
664 }
665 
666 inline double UnitHandler::getCN(){
667  return c*timeScale/lengthScale;
668 }
669 
670 inline double UnitHandler::getN_AB(){
671  return n_A;
672 }
673 
674 inline double UnitHandler::getN_AN(){
675  return n_A/countScale;
676 }
677 
678 inline double UnitHandler::getM_eB(){
679  return m_e;
680 }
681 
682 inline double UnitHandler::getM_eN(){
683  return m_e*lengthScale*lengthScale/(energyScale*timeScale*timeScale);
684 }
685 
686 inline double UnitHandler::getM_pB(){
687  return m_p;
688 }
689 
690 inline double UnitHandler::getM_pN(){
691  return m_p*lengthScale*lengthScale/(energyScale*timeScale*timeScale);
692 }
693 
694 inline double UnitHandler::getMu_BB(){
695  return mu_B;
696 }
697 
698 inline double UnitHandler::getMu_BN(){
699  return mu_B*timeScale/(chargeScale*lengthScale*lengthScale);
700 }
701 
702 inline double UnitHandler::getMu_nB(){
703  return mu_n;
704 }
705 
706 inline double UnitHandler::getMu_nN(){
707  return mu_n*timeScale/(chargeScale*lengthScale*lengthScale);
708 }
709 
710 inline double UnitHandler::getMu_0B(){
711  return mu_0;
712 }
713 
714 inline double UnitHandler::getMu_0N(){
715  return mu_0*chargeScale*chargeScale*lengthScale/(energyScale*timeScale*timeScale);
716 }
717 
719  return epsilon_0;
720 }
721 
723  return epsilon_0*energyScale*lengthScale/(chargeScale*chargeScale);
724 }
725 
726 inline double UnitHandler::convertTemperatureNtB(double temperature){
727  return temperature*temperatureScale;
728 }
729 
730 inline double UnitHandler::convertTimeNtB(double time){
731  return time*timeScale;
732 }
733 
734 inline double UnitHandler::convertLengthNtB(double length){
735  return length*lengthScale;
736 }
737 
738 inline double UnitHandler::convertEnergyNtB(double energy){
739  return energy*energyScale;
740 }
741 
742 inline double UnitHandler::convertChargeNtB(double charge){
743  return charge*chargeScale;
744 }
745 
746 inline double UnitHandler::convertCountNtB(double count){
747  return count*countScale;
748 }
749 
750 inline double UnitHandler::convertTemperatureBtN(double temperature){
751  return temperature/temperatureScale;
752 }
753 
754 inline double UnitHandler::convertTimeBtN(double time){
755  return time/timeScale;
756 }
757 
758 inline double UnitHandler::convertLengthBtN(double length){
759  return length/lengthScale;
760 }
761 
762 inline double UnitHandler::convertEnergyBtN(double energy){
763  return energy/energyScale;
764 }
765 
766 inline double UnitHandler::convertChargeBtN(double charge){
767  return charge/chargeScale;
768 }
769 
770 inline double UnitHandler::convertCountBtN(double count){
771  return count/countScale;
772 }
773 
774 };
775 
776 #ifdef M_E_temp //Avoid name clash with math.h macro M_E
777  #define M_E M_E_temp
778  #undef M_E_temp
779 #endif
780 
781 #endif
static double convertMagneticFieldDtB(double field, MagneticFieldUnit unit)
static std::string getEUnitString()
static std::string getCountUnitString()
static std::string getCUnitString()
EnergyUnit
Definition: UnitHandler.h:112
static double getK_BN()
Definition: UnitHandler.h:650
static double convertCountNtB(double count)
Definition: UnitHandler.h:746
ChargeUnit
Definition: UnitHandler.h:128
LengthUnit
Definition: UnitHandler.h:102
static std::string getTimeUnitString()
CountUnit
Definition: UnitHandler.h:135
static double getMu_nN()
Definition: UnitHandler.h:706
static void setChargeUnit(ChargeUnit unit)
static double convertMagneticFieldBtD(double field, MagneticFieldUnit unit)
static double convertEnergyNtB(double energy)
Definition: UnitHandler.h:738
static double convertLengthBtN(double length)
Definition: UnitHandler.h:758
static double convertChargeNtB(double charge)
Definition: UnitHandler.h:742
static std::string getHBARUnitString()
static double convertMassDtB(double mass, MassUnit unit)
static void setTimeScale(double scale)
static double convertCountBtN(double count)
Definition: UnitHandler.h:770
static double convertMassBtD(double mass, MassUnit unit)
static double convertTimeNtB(double time)
Definition: UnitHandler.h:730
static double convertMassDtN(double mass, MassUnit unit)
static double getN_AB()
Definition: UnitHandler.h:670
static double getMu_BN()
Definition: UnitHandler.h:698
static std::string getEnergyUnitString()
static double convertChargeBtN(double charge)
Definition: UnitHandler.h:766
static double getK_BB()
Definition: UnitHandler.h:646
MagneticFieldUnit
Definition: UnitHandler.h:162
static double getM_pN()
Definition: UnitHandler.h:690
static double getEB()
Definition: UnitHandler.h:654
static double convertTemperatureBtN(double temperature)
Definition: UnitHandler.h:750
static std::string getN_AUnitString()
static double convertTemperatureNtB(double temperature)
Definition: UnitHandler.h:726
static double convertMagneticFieldNtD(double field, MagneticFieldUnit unit)
static void setCountUnit(CountUnit unit)
static std::string getMu_nUnitString()
static std::string getMu_BUnitString()
static std::string getK_BUnitString()
static void setEnergyUnit(EnergyUnit unit)
static double getMu_nB()
Definition: UnitHandler.h:702
static std::string getM_eUnitString()
static std::string getTemperatureUnitString()
static double getM_eN()
Definition: UnitHandler.h:682
static void setTimeUnit(TimeUnit unit)
static double getMu_0B()
Definition: UnitHandler.h:710
static std::string getMassUnitString()
static void setTemperatureUnit(TemperatureUnit unit)
static double getHbarN()
Definition: UnitHandler.h:642
static double getM_pB()
Definition: UnitHandler.h:686
Definition: UnitHandler.h:73
static void setChargeScale(double scale)
TemperatureUnit
Definition: UnitHandler.h:81
static double convertMassNtD(double mass, MassUnit unit)
Definition: ModelFactory.h:35
static std::string getMu_0UnitString()
static double getMu_BB()
Definition: UnitHandler.h:694
static double getM_eB()
Definition: UnitHandler.h:678
static void setEnergyScale(double scale)
static double getCN()
Definition: UnitHandler.h:666
static double getEpsilon_0B()
Definition: UnitHandler.h:718
static std::string getLengthUnitString()
static void setTemperatureScale(double scale)
static double convertTimeBtN(double time)
Definition: UnitHandler.h:754
MassUnit
Definition: UnitHandler.h:147
static double getCB()
Definition: UnitHandler.h:662
static std::string getMagneticFieldUnitString()
static void setLengthUnit(LengthUnit unit)
static double convertEnergyBtN(double energy)
Definition: UnitHandler.h:762
static void setLengthScale(double scale)
static double convertLengthNtB(double length)
Definition: UnitHandler.h:734
static void setCountScale(double scale)
static std::string getEpsilon_0UnitString()
static std::string getM_pUnitString()
static double getHbarB()
Definition: UnitHandler.h:638
static double convertMagneticFieldDtN(double field, MagneticFieldUnit unit)
static double getN_AN()
Definition: UnitHandler.h:674
static std::string getChargeUnitString()
static double getMu_0N()
Definition: UnitHandler.h:714
static double getEpsilon_0N()
Definition: UnitHandler.h:722
static double getEN()
Definition: UnitHandler.h:658