TkN 2.4
Toolkit for Nuclei
Loading...
Searching...
No Matches
tkunit.cpp
1/********************************************************************************
2 * Copyright (c) : Université de Lyon 1, CNRS/IN2P3, UMR5822, *
3 * IP2I, F-69622 Villeurbanne Cedex, France *
4 * Normandie Université, ENSICAEN, UNICAEN, CNRS/IN2P3, *
5 * LPC Caen, F-14000 Caen, France *
6 * Contibutor(s) : *
7 * Jérémie Dudouet jeremie.dudouet@cnrs.fr [2020] *
8 * Diego Gruyer diego.gruyer@cnrs.fr [2020] *
9 * *
10 * Licensed under the MIT License <http://opensource.org/licenses/MIT>. *
11 * SPDX-License-Identifier: MIT *
12 ********************************************************************************/
13
14#include "tkunit.h"
15#include "tklog.h"
16#include <cmath>
17
18namespace tkn {
30}
31
32using namespace std;
33using namespace tkn;
34
35std::vector<std::tuple<tkstring, tkunit_manager::units_type, double>> tkunit_manager::funits_properties = {
36 {"Undefined", kUndef_type, 0. }, // undefined
37 {"eV", kEnergy, 1e-3 }, // energy
38 {"keV", kEnergy, 1. },
39 {"MeV", kEnergy, 1e3 },
40 {"GeV", kEnergy, 1e6 },
41 {"TeV", kEnergy, 1e9 },
42 {"as", kTime, 1e-18 }, // time
43 {"fs", kTime, 1e-15 },
44 {"ps", kTime, 1e-12 },
45 {"ns", kTime, 1e-9 },
46 {"us", kTime, 1e-6 },
47 {"ms", kTime, 1e-3 },
48 {"s", kTime, 1. },
49 {"min", kTime, 60. },
50 {"h", kTime, 3600. },
51 {"d", kTime, 86400. },
52 {"y", kTime, 31556926. },
53 {"am", kLenght, 1e-18 }, // lenght
54 {"fm", kLenght, 1e-15 },
55 {"pm", kLenght, 1e-12 },
56 {"nm", kLenght, 1e-9 },
57 {"um", kLenght, 1e-6 },
58 {"mm", kLenght, 1e-3 },
59 {"cm", kLenght, 1e-2 },
60 {"m", kLenght, 1. },
61 {"K", kNoConvUnits, 1. }, // units without conversions: Kelvin
62 {"C", kNoConvUnits, 1. }, // Celcius
63 {"F", kNoConvUnits, 1. }, // Farenheit
64 {"u", kNoConvUnits, 1. }, // atomic mass unit
65 {"", kNoConvUnits, 1. }, // no unit
66 {"%", kNoConvUnits, 1. }, // percentage
67};
68
69tkunit_manager *tkunit_manager::g_unit_manager = nullptr;
70
72 for(size_t i= 0 ; i<funits_properties.size() ; i++) {
73 funits_map_id_to_string[(units_keys)i] = std::get<0>(funits_properties.at(i));
74 funits_map_string_to_id[std::get<0>(funits_properties.at(i))] = (units_keys)i;
75 funits_map_string_to_id_low[std::get<0>(funits_properties.at(i)).copy().to_lower()] = (units_keys)i;
76 funits_map_conv_coeffs[(units_keys)i] = std::get<2>(funits_properties.at(i));
77 funits_map_types[(units_keys)i] = std::get<1>(funits_properties.at(i));
78 }
79 g_unit_manager = this;
81
83{
84 if ( g_unit_manager == nullptr ) {
85 g_unit_manager = new tkunit_manager();
86 }
87
88 return g_unit_manager;
89}
90
91tkunit::tkunit(double _value, const tkunit_manager::units_keys &_unit) :
92 fUnit_key(_unit),
93 fFactor(gunits->get_factor(fUnit_key)),
94 fValue(_value/fFactor)
95{
96
97}
98
99void tkunit::set_value(const double &_val, const tkstring &_unit)
101 if(_unit.is_empty()) {
102 set_value(_val,fUnit_key);
103 }
104 else {
105 set_value(_val,gunits->get_key(_unit));
106 }
107}
109void tkunit::set_value(const double &_val, const tkunit_manager::units_keys &_unit) {
110 fUnit_key = _unit;
111 fFactor = gunits->get_factor(fUnit_key);
112 fValue = _val*fFactor;
113}
114
115bool tkunit::set_unit(const tkstring &_unit) {
116 return set_unit(gunits->get_key(_unit));
117}
120 if(_unit == gunits->Undef_unit) return false;
121 // if(gunits->get_type(fUnit_key) == gunits->kNoConvUnits) {
122 // glog << warning_o << " sorry, unit conversion from " << gunits->get_name(fUnit_key) << " to " << gunits->get_name(_unit) << " not handle by TkN for the moment" << do_endl;
123 // return false;
124 // }
125 // case where the unit type (energy, time...) does not change => easy mode, juste change the coeff
126 if((gunits->get_type(fUnit_key) == gunits->kUndef_type) || (gunits->get_type(_unit) == gunits->get_type(fUnit_key))) {
127 fUnit_key = _unit;
128 fFactor = gunits->get_factor(fUnit_key);
129 }
130 else {
131 // case Energy to time, or time to Energy
132 if( ((gunits->get_type(fUnit_key) == gunits->kEnergy) && (gunits->get_type(_unit) == gunits->kTime)) ||
133 ((gunits->get_type(fUnit_key) == gunits->kTime) && (gunits->get_type(_unit) == gunits->kEnergy))) {
134 // ev to s
135 // E x tau = hbar
136 // tau = hbar / E = 1.054571818×10−34 (Jxs) / E (keV)
137 // 1J = 6.2415091e+18 eV
138 // tau (s) = (1.054571818e−34*6.2415091e+15 (keVxs) / E (keV)
139 // T (s) = (1.054571818e−34*6.2415091e+15*ln(2) (keVxs) / E (keV)
140 // fFactor = (1.054571818e-34*6.2415091e+15)*ln(2); // ~ 4.5e-19 s
141 fUnit_key = _unit;
142 fValue = (1.054571818e-34*6.2415091e+15)*log(2) / fValue;
143 fFactor = gunits->get_factor(fUnit_key);
144 }
145 else {
146 glog << warning_o << " sorry, unit conversion from " << gunits->get_name(fUnit_key) << " to " << gunits->get_name(_unit) << " not handle by TkN for the moment" << do_endl;
147 return false;
148 }
149 }
150 return true;
151}
152
153#ifdef HAS_ROOT
154ClassImp(tkunit);
155#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:31
bool is_empty() const
Definition tkstring.h:140
Manages the different units.
Definition tkunit.h:32
units_keys
units identifiers
Definition tkunit.h:46
static std::vector< std::tuple< tkstring, units_type, double > > funits_properties
Definition tkunit.h:52
static tkunit_manager * the_unit_manager()
db_manager is a singleton
Definition tkunit.cpp:91
A measured value associated to its unit.
Definition tkunit.h:120
void set_value(const double &_val, const tkstring &_unit="")
set the value and unit from string, if _unit not defined, let in the current unit
Definition tkunit.cpp:108
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition tkunit.cpp:124
Definition tklog.cpp:16
tklog & warning_o(tklog &log)
Definition tklog.h:325
tklog & do_endl(tklog &log)
Definition tklog.h:212