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 "TBTKMacros.h"
27 
28 #include <string>
29 #include <vector>
30 #include <math.h>
31 
32 #ifdef M_E //Avoid name clash with math.h macro M_E
33  #define M_E_temp M_E
34  #undef M_E
35 #endif
36 
37 namespace TBTK{
38 
77 public:
84  enum class TemperatureUnit {kK, K, mK, uK, nK};
85 
86  /* Time units (base unit):<br/>
87  * s - second<br/>
88  * ms - millisecond<br/>
89  * us - microsecond<br/>
90  * ns - nanosecond<br/>
91  * ps - picosecond<br/>
92  * fs - femtosecond<br/>
93  * as - attosecond */
94  enum class TimeUnit {s, ms, us, ns, ps, fs, as};
95 
105  enum class LengthUnit{m, mm, um, nm, pm, fm, am, Ao};
106 
115  enum class EnergyUnit{GeV, MeV, keV, eV, meV, ueV, J};
116 
131  enum class ChargeUnit{
132  kC, C, mC, uC, nC, pC, fC, aC, Te, Ge, Me, ke, e
133  };
134 
138  enum class CountUnit{pcs, mol};
139 
150  enum class MassUnit{kg, g, mg, ug, ng, pg, fg, ag, u};
151 
165  enum class MagneticFieldUnit{
166  MT, kT, T, mT, uT, nT, GG, MG, kG, G, mG, uG
167  };
168 
177  enum class VoltageUnit{
178  GV, MV, kV, V, mV, uV, nV
179  };
180 
182  static double getHbarB();
183 
185  static double getHbarN();
186 
188  static double getK_BB();
189 
191  static double getK_BN();
192 
194  static double getEB();
195 
197  static double getEN();
198 
200  static double getCB();
201 
203  static double getCN();
204 
206  static double getN_AB();
207 
209  static double getN_AN();
210 
212  static double getM_eB();
213 
215  static double getM_eN();
216 
218  static double getM_pB();
219 
221  static double getM_pN();
222 
224  static double getMu_BB();
225 
227  static double getMu_BN();
228 
230  static double getMu_nB();
231 
233  static double getMu_nN();
234 
236  static double getMu_0B();
237 
239  static double getMu_0N();
240 
242  static double getEpsilon_0B();
243 
245  static double getEpsilon_0N();
246 
248  static void setTemperatureUnit(TemperatureUnit unit);
249 
251  static void setTimeUnit(TimeUnit unit);
252 
254  static void setLengthUnit(LengthUnit unit);
255 
257  static void setEnergyUnit(EnergyUnit unit);
258 
260  static void setChargeUnit(ChargeUnit unit);
261 
263  static void setCountUnit(CountUnit unit);
264 
266  static void setTemperatureScale(double scale);
267 
269  static void setTimeScale(double scale);
270 
272  static void setLengthScale(double scale);
273 
275  static void setEnergyScale(double scale);
276 
278  static void setChargeScale(double scale);
279 
281  static void setCountScale(double scale);
282 
284  static void setTemperatureScale(double scale, TemperatureUnit unit);
285 
287  static void setTimeScale(double scale, TimeUnit unit);
288 
290  static void setLengthScale(double scale, LengthUnit unit);
291 
293  static void setEnergyScale(double scale, EnergyUnit unit);
294 
296  static void setChargeScale(double scale, ChargeUnit unit);
297 
299  static void setCountScale(double scale, CountUnit unit);
300 
302  static void setTemperatureScale(std::string scale);
303 
305  static void setTimeScale(std::string scale);
306 
308  static void setLengthScale(std::string scale);
309 
311  static void setEnergyScale(std::string scale);
312 
314  static void setChargeScale(std::string scale);
315 
317  static void setCountScale(std::string scale);
318 
320  static void setScales(const std::vector<std::string> &scales);
321 
323  static double convertTemperatureNtB(double temperature);
324 
326  static double convertTimeNtB(double time);
327 
329  static double convertLengthNtB(double length);
330 
332  static double convertEnergyNtB(double energy);
333 
335  static double convertChargeNtB(double charge);
336 
338  static double convertCountNtB(double count);
339 
341  static double convertTemperatureBtN(double temperature);
342 
344  static double convertTimeBtN(double time);
345 
347  static double convertLengthBtN(double length);
348 
350  static double convertEnergyBtN(double energy);
351 
353  static double convertChargeBtN(double charge);
354 
356  static double convertCountBtN(double count);
357 
359  static double convertMassDtB(double mass, MassUnit unit);
360 
362  static double convertMassBtD(double mass, MassUnit unit);
363 
365  static double convertMassDtN(double mass, MassUnit unit);
366 
368  static double convertMassNtD(double mass, MassUnit unit);
369 
371  static double convertMagneticFieldDtB(
372  double field,
373  MagneticFieldUnit unit
374  );
375 
377  static double convertMagneticFieldBtD(
378  double field,
379  MagneticFieldUnit unit
380  );
381 
383  static double convertMagneticFieldDtN(
384  double field,
385  MagneticFieldUnit unit
386  );
387 
389  static double convertMagneticFieldNtD(
390  double field,
391  MagneticFieldUnit unit
392  );
393 
395  static double convertVoltageDtB(double voltage, VoltageUnit unit);
396 
398  static double convertVoltageBtD(double voltage, VoltageUnit unit);
399 
401  static double convertVoltageDtN(double voltage, VoltageUnit unit);
402 
404  static double convertVoltageNtD(double voltage, VoltageUnit unit);
405 
410  static std::string getTemperatureUnitString();
411 
415  static std::string getTimeUnitString();
416 
420  static std::string getLengthUnitString();
421 
425  static std::string getEnergyUnitString();
426 
430  static std::string getChargeUnitString();
431 
435  static std::string getCountUnitString();
436 
441  static std::string getMassUnitString();
442 
447  static std::string getMagneticFieldUnitString();
448 
453  static std::string getVoltageUnitString();
454 
459  static std::string getHBARUnitString();
460 
465  static std::string getK_BUnitString();
466 
471  static std::string getEUnitString();
472 
477  static std::string getCUnitString();
478 
482  static std::string getN_AUnitString();
483 
487  static std::string getM_eUnitString();
488 
492  static std::string getM_pUnitString();
493 
497  static std::string getMu_BUnitString();
498 
502  static std::string getMu_nUnitString();
503 
508  static std::string getMu_0UnitString();
509 
514  static std::string getEpsilon_0UnitString();
515 private:
517  static constexpr double HBAR = 6.582119514e-16;
518 
520  static constexpr double K_B = 8.6173324e-5;
521 
523  static constexpr double E = 1.6021766208e-19;
524 
526  static constexpr double C = 2.99792458e8;
527 
529  static constexpr double N_A = 6.022140857e23;
530 
532  static constexpr double M_E = 5.109989461e5/(C*C);
533 
535  static constexpr double M_P = 9.38272046e8/(C*C);
536 
538  static constexpr double MU_B = E*HBAR/(2.*M_E);
539 
541  static constexpr double MU_N = E*HBAR/(2*M_P);
542 
544  static constexpr double MU_0 = 4*M_PI*1e-7/1.602176565e-19;
545 
547  static constexpr double EPSILON_0 = 8.854187817620e-12*1.602176565e-19;
548 
550  static double hbar;
551 
553  static double k_B;
554 
556  static double e;
557 
559  static double c;
560 
562  static double n_A;
563 
565  static double m_e;
566 
568  static double m_p;
569 
571  static double mu_B;
572 
574  static double mu_n;
575 
577  static double mu_0;
578 
580  static double epsilon_0;
581 
583  static constexpr double J_per_eV = 1.602176565e-19;
584 
586  static constexpr double eV_per_J = 1./J_per_eV;
587 
589  static constexpr double kg_per_baseMass = 1.602176565e-19;
590 
592  static constexpr double baseMass_per_kg = 1./kg_per_baseMass;
593 
595 // static constexpr double u_per_baseMass = 9.31494095e8/(C*C);
596  static constexpr double u_per_baseMass = (C*C)/9.31494095e8;
597 
599  static constexpr double baseMass_per_u = 1./u_per_baseMass;
600 
602  static constexpr double T_per_baseMagneticField = 1.602176565e-19;
603 
605  static constexpr double baseMagneticField_per_T = 1./T_per_baseMagneticField;
606 
608  static constexpr double V_per_baseVoltage = 1.602176565e-19;
609 
611  static constexpr double baseVoltage_per_V = 1./V_per_baseVoltage;
612 
614  static TemperatureUnit temperatureUnit;
615 
617  static TimeUnit timeUnit;
618 
620  static LengthUnit lengthUnit;
621 
623  static EnergyUnit energyUnit;
624 
626  static ChargeUnit chargeUnit;
627 
629  static CountUnit countUnit;
630 
632  static double temperatureScale;
633 
635  static double timeScale;
636 
638  static double lengthScale;
639 
641  static double energyScale;
642 
644  static double chargeScale;
645 
647  static double countScale;
648 
650  static void updateHbar();
651 
653  static void updateK_B();
654 
656  static void updateE();
657 
659  static void updateC();
660 
662  static void updateN_A();
663 
665  static void updateM_e();
666 
668  static void updateM_p();
669 
671  static void updateMu_B();
672 
674  static void updateMu_n();
675 
677  static void updateMu_0();
678 
680  static void updateEpsilon_0();
681 
684  static double getTemperatureConversionFactor();
685 
688  static double getTimeConversionFactor();
689 
692  static double getLengthConversionFactor();
693 
696  static double getEnergyConversionFactor();
697 
700  static double getChargeConversionFactor();
701 
704  static double getCountConversionFactor();
705 
708  static double getMassConversionFactor(MassUnit unit);
709 
713  static double getMagneticFieldConversionFactor(MagneticFieldUnit unit);
714 
717  static double getVoltageConversionFactor(VoltageUnit unit);
718 
720  static TemperatureUnit getTemperatureUnit(std::string unit);
721 
723  static TimeUnit getTimeUnit(std::string unit);
724 
726  static LengthUnit getLengthUnit(std::string unit);
727 
729  static EnergyUnit getEnergyUnit(std::string unit);
730 
732  static ChargeUnit getChargeUnit(std::string unit);
733 
735  static CountUnit getCountUnit(std::string unit);
736 };
737 
738 inline double UnitHandler::getHbarB(){
739  return hbar;
740 }
741 
742 inline double UnitHandler::getHbarN(){
743  return hbar/(energyScale*timeScale);
744 }
745 
746 inline double UnitHandler::getK_BB(){
747  return k_B;
748 }
749 
750 inline double UnitHandler::getK_BN(){
751  return k_B*temperatureScale/energyScale;
752 }
753 
754 inline double UnitHandler::getEB(){
755  return e;
756 }
757 
758 inline double UnitHandler::getEN(){
759  return e/chargeScale;
760 }
761 
762 inline double UnitHandler::getCB(){
763  return c;
764 }
765 
766 inline double UnitHandler::getCN(){
767  return c*timeScale/lengthScale;
768 }
769 
770 inline double UnitHandler::getN_AB(){
771  return n_A;
772 }
773 
774 inline double UnitHandler::getN_AN(){
775  return n_A/countScale;
776 }
777 
778 inline double UnitHandler::getM_eB(){
779  return m_e;
780 }
781 
782 inline double UnitHandler::getM_eN(){
783  return m_e*lengthScale*lengthScale/(energyScale*timeScale*timeScale);
784 }
785 
786 inline double UnitHandler::getM_pB(){
787  return m_p;
788 }
789 
790 inline double UnitHandler::getM_pN(){
791  return m_p*lengthScale*lengthScale/(energyScale*timeScale*timeScale);
792 }
793 
794 inline double UnitHandler::getMu_BB(){
795  return mu_B;
796 }
797 
798 inline double UnitHandler::getMu_BN(){
799  return mu_B*timeScale/(chargeScale*lengthScale*lengthScale);
800 }
801 
802 inline double UnitHandler::getMu_nB(){
803  return mu_n;
804 }
805 
806 inline double UnitHandler::getMu_nN(){
807  return mu_n*timeScale/(chargeScale*lengthScale*lengthScale);
808 }
809 
810 inline double UnitHandler::getMu_0B(){
811  return mu_0;
812 }
813 
814 inline double UnitHandler::getMu_0N(){
815  return mu_0*chargeScale*chargeScale*lengthScale/(energyScale*timeScale*timeScale);
816 }
817 
819  return epsilon_0;
820 }
821 
823  return epsilon_0*energyScale*lengthScale/(chargeScale*chargeScale);
824 }
825 
826 inline void UnitHandler::setTemperatureScale(double scale, TemperatureUnit unit){
827  setTemperatureUnit(unit);
828  setTemperatureScale(scale);
829 }
830 
831 inline void UnitHandler::setTimeScale(double scale, TimeUnit unit){
832  setTimeUnit(unit);
833  setTimeScale(scale);
834 }
835 
836 inline void UnitHandler::setLengthScale(double scale, LengthUnit unit){
837  setLengthUnit(unit);
838  setLengthScale(scale);
839 }
840 
841 inline void UnitHandler::setEnergyScale(double scale, EnergyUnit unit){
842  setEnergyUnit(unit);
843  setEnergyScale(scale);
844 }
845 
846 inline void UnitHandler::setChargeScale(double scale, ChargeUnit unit){
847  setChargeUnit(unit);
848  setChargeScale(scale);
849 }
850 
851 inline void UnitHandler::setCountScale(double scale, CountUnit unit){
852  setCountUnit(unit);
853  setCountScale(scale);
854 }
855 
856 inline void UnitHandler::setScales(const std::vector<std::string> &scales){
857  TBTKAssert(
858  scales.size() == 6,
859  "UnitHandler::setScales()",
860  "'scales' must contain six strings.",
861  ""
862  );
863 
864  setChargeScale(scales[0]);
865  setCountScale(scales[1]);
866  setEnergyScale(scales[2]);
867  setLengthScale(scales[3]);
868  setTemperatureScale(scales[4]);
869  setTimeScale(scales[5]);
870 }
871 
872 inline double UnitHandler::convertTemperatureNtB(double temperature){
873  return temperature*temperatureScale;
874 }
875 
876 inline double UnitHandler::convertTimeNtB(double time){
877  return time*timeScale;
878 }
879 
880 inline double UnitHandler::convertLengthNtB(double length){
881  return length*lengthScale;
882 }
883 
884 inline double UnitHandler::convertEnergyNtB(double energy){
885  return energy*energyScale;
886 }
887 
888 inline double UnitHandler::convertChargeNtB(double charge){
889  return charge*chargeScale;
890 }
891 
892 inline double UnitHandler::convertCountNtB(double count){
893  return count*countScale;
894 }
895 
896 inline double UnitHandler::convertTemperatureBtN(double temperature){
897  return temperature/temperatureScale;
898 }
899 
900 inline double UnitHandler::convertTimeBtN(double time){
901  return time/timeScale;
902 }
903 
904 inline double UnitHandler::convertLengthBtN(double length){
905  return length/lengthScale;
906 }
907 
908 inline double UnitHandler::convertEnergyBtN(double energy){
909  return energy/energyScale;
910 }
911 
912 inline double UnitHandler::convertChargeBtN(double charge){
913  return charge/chargeScale;
914 }
915 
916 inline double UnitHandler::convertCountBtN(double count){
917  return count/countScale;
918 }
919 
920 };
921 
922 #ifdef M_E_temp //Avoid name clash with math.h macro M_E
923  #define M_E M_E_temp
924  #undef M_E_temp
925 #endif
926 
927 #endif
static double convertMagneticFieldDtB(double field, MagneticFieldUnit unit)
static std::string getEUnitString()
static std::string getCountUnitString()
static std::string getCUnitString()
EnergyUnit
Definition: UnitHandler.h:115
static double getK_BN()
Definition: UnitHandler.h:750
static double convertCountNtB(double count)
Definition: UnitHandler.h:892
ChargeUnit
Definition: UnitHandler.h:131
LengthUnit
Definition: UnitHandler.h:105
static std::string getTimeUnitString()
CountUnit
Definition: UnitHandler.h:138
static double getMu_nN()
Definition: UnitHandler.h:806
static void setChargeUnit(ChargeUnit unit)
Precompiler macros.
static double convertMagneticFieldBtD(double field, MagneticFieldUnit unit)
static double convertEnergyNtB(double energy)
Definition: UnitHandler.h:884
static double convertVoltageNtD(double voltage, VoltageUnit unit)
static double convertLengthBtN(double length)
Definition: UnitHandler.h:904
static double convertChargeNtB(double charge)
Definition: UnitHandler.h:888
static std::string getHBARUnitString()
static double convertMassDtB(double mass, MassUnit unit)
static void setTimeScale(double scale)
static double convertCountBtN(double count)
Definition: UnitHandler.h:916
static double convertMassBtD(double mass, MassUnit unit)
static double convertTimeNtB(double time)
Definition: UnitHandler.h:876
static double convertMassDtN(double mass, MassUnit unit)
static double convertVoltageDtN(double voltage, VoltageUnit unit)
static double getN_AB()
Definition: UnitHandler.h:770
static double getMu_BN()
Definition: UnitHandler.h:798
static std::string getEnergyUnitString()
static double convertChargeBtN(double charge)
Definition: UnitHandler.h:912
static double getK_BB()
Definition: UnitHandler.h:746
MagneticFieldUnit
Definition: UnitHandler.h:165
static void setScales(const std::vector< std::string > &scales)
Definition: UnitHandler.h:856
static double getM_pN()
Definition: UnitHandler.h:790
static double getEB()
Definition: UnitHandler.h:754
static double convertVoltageBtD(double voltage, VoltageUnit unit)
static double convertTemperatureBtN(double temperature)
Definition: UnitHandler.h:896
static std::string getN_AUnitString()
static double convertTemperatureNtB(double temperature)
Definition: UnitHandler.h:872
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:802
static std::string getM_eUnitString()
static std::string getTemperatureUnitString()
static double getM_eN()
Definition: UnitHandler.h:782
static void setTimeUnit(TimeUnit unit)
static double getMu_0B()
Definition: UnitHandler.h:810
static std::string getMassUnitString()
static void setTemperatureUnit(TemperatureUnit unit)
static double getHbarN()
Definition: UnitHandler.h:742
static double getM_pB()
Definition: UnitHandler.h:786
Definition: UnitHandler.h:76
static void setChargeScale(double scale)
TemperatureUnit
Definition: UnitHandler.h:84
static double convertMassNtD(double mass, MassUnit unit)
Definition: ModelFactory.h:35
static std::string getMu_0UnitString()
static double getMu_BB()
Definition: UnitHandler.h:794
static double getM_eB()
Definition: UnitHandler.h:778
static void setEnergyScale(double scale)
static double getCN()
Definition: UnitHandler.h:766
static double getEpsilon_0B()
Definition: UnitHandler.h:818
static double convertVoltageDtB(double voltage, VoltageUnit unit)
static std::string getLengthUnitString()
static void setTemperatureScale(double scale)
static double convertTimeBtN(double time)
Definition: UnitHandler.h:900
MassUnit
Definition: UnitHandler.h:150
static double getCB()
Definition: UnitHandler.h:762
static std::string getMagneticFieldUnitString()
static void setLengthUnit(LengthUnit unit)
static double convertEnergyBtN(double energy)
Definition: UnitHandler.h:908
static void setLengthScale(double scale)
static double convertLengthNtB(double length)
Definition: UnitHandler.h:880
static void setCountScale(double scale)
static std::string getEpsilon_0UnitString()
static std::string getM_pUnitString()
static double getHbarB()
Definition: UnitHandler.h:738
static double convertMagneticFieldDtN(double field, MagneticFieldUnit unit)
VoltageUnit
Definition: UnitHandler.h:177
static std::string getVoltageUnitString()
static double getN_AN()
Definition: UnitHandler.h:774
static std::string getChargeUnitString()
static double getMu_0N()
Definition: UnitHandler.h:814
static double getEpsilon_0N()
Definition: UnitHandler.h:822
static double getEN()
Definition: UnitHandler.h:758