TkN 2.1
Toolkit for Nuclei
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 * This software is governed by the CeCILL-B license under French law and *
11 * abiding by the rules of distribution of free software. You can use, *
12 * modify and/ or redistribute the software under the terms of the *
13 * CeCILL-B license as circulated by CEA, CNRS and INRIA at the following *
14 * URL \"http://www.cecill.info\". *
15 * *
16 * As a counterpart to the access to the source code and rights to copy, *
17 * modify and redistribute granted by the license, users are provided *
18 * only with a limited warranty and the software's author, the holder of *
19 * the economic rights, and the successive licensors have only limited *
20 * liability. *
21 * *
22 * In this respect, the user's attention is drawn to the risks associated *
23 * with loading, using, modifying and/or developing or reproducing the *
24 * software by the user in light of its specific status of free software, *
25 * that may mean that it is complicated to manipulate, and that also *
26 * therefore means that it is reserved for developers and experienced *
27 * professionals having in-depth computer knowledge. Users are therefore *
28 * encouraged to load and test the software's suitability as regards *
29 * their requirements in conditions enabling the security of their *
30 * systems and/or data to be ensured and, more generally, to use and *
31 * operate it in the same conditions as regards security. *
32 * *
33 * The fact that you are presently reading this means that you have had *
34 * knowledge of the CeCILL-B license and that you accept its terms. *
35 ********************************************************************************/
36
37#include "tkunit.h"
38#include "tklog.h"
39#include <cmath>
40
41namespace tkn {
53}
54
55using namespace std;
56using namespace tkn;
57
58std::vector<std::tuple<tkstring, tkunit_manager::units_type, double>> tkunit_manager::funits_properties = {
59 {"Undefined", kUndef_type, 0. }, // undefined
60 {"eV", kEnergy, 1e-3 }, // energy
61 {"keV", kEnergy, 1. },
62 {"MeV", kEnergy, 1e3 },
63 {"GeV", kEnergy, 1e6 },
64 {"TeV", kEnergy, 1e9 },
65 {"as", kTime, 1e-18 }, // time
66 {"fs", kTime, 1e-15 },
67 {"ps", kTime, 1e-12 },
68 {"ns", kTime, 1e-9 },
69 {"us", kTime, 1e-6 },
70 {"ms", kTime, 1e-3 },
71 {"s", kTime, 1. },
72 {"min", kTime, 60. },
73 {"h", kTime, 3600. },
74 {"d", kTime, 86400. },
75 {"y", kTime, 31556926. },
76 {"am", kLenght, 1e-18 }, // lenght
77 {"fm", kLenght, 1e-15 },
78 {"pm", kLenght, 1e-12 },
79 {"nm", kLenght, 1e-9 },
80 {"um", kLenght, 1e-6 },
81 {"mm", kLenght, 1e-3 },
82 {"cm", kLenght, 1e-2 },
83 {"m", kLenght, 1. },
84 {"K", kNoConvUnits, 1. }, // units without conversions: Kelvin
85 {"C", kNoConvUnits, 1. }, // Celcius
86 {"F", kNoConvUnits, 1. }, // Farenheit
87 {"u", kNoConvUnits, 1. }, // atomic mass unit
88 {"", kNoConvUnits, 1. }, // no unit
89 {"%", kNoConvUnits, 1. }, // percentage
90};
91
92tkunit_manager *tkunit_manager::g_unit_manager = nullptr;
93
94tkunit_manager::tkunit_manager() {
95 for(size_t i= 0 ; i<funits_properties.size() ; i++) {
96 funits_map_id_to_string[(units_keys)i] = std::get<0>(funits_properties.at(i));
97 funits_map_string_to_id[std::get<0>(funits_properties.at(i))] = (units_keys)i;
98 funits_map_string_to_id_low[std::get<0>(funits_properties.at(i)).copy().to_lower()] = (units_keys)i;
99 funits_map_conv_coeffs[(units_keys)i] = std::get<2>(funits_properties.at(i));
100 funits_map_types[(units_keys)i] = std::get<1>(funits_properties.at(i));
101 }
102 g_unit_manager = this;
103}
104
106{
107 if ( g_unit_manager == nullptr ) {
108 g_unit_manager = new tkunit_manager();
109 }
110
111 return g_unit_manager;
112}
113
114tkunit::tkunit(double _value, const tkunit_manager::units_keys &_unit) :
115 fUnit_key(_unit),
116 fFactor(gunits->get_factor(fUnit_key)),
117 fValue(_value/fFactor)
118{
119
120}
121
122void tkunit::set_value(const double &_val, const tkstring &_unit)
123{
124 if(_unit.is_empty()) {
125 set_value(_val,fUnit_key);
126 }
127 else {
128 set_value(_val,gunits->get_key(_unit));
129 }
130}
131
132void tkunit::set_value(const double &_val, const tkunit_manager::units_keys &_unit) {
133 fUnit_key = _unit;
134 fFactor = gunits->get_factor(fUnit_key);
135 fValue = _val*fFactor;
136}
137
138bool tkunit::set_unit(const tkstring &_unit) {
139 return set_unit(gunits->get_key(_unit));
140}
141
143 if(_unit == gunits->Undef_unit) return false;
144 // if(gunits->get_type(fUnit_key) == gunits->kNoConvUnits) {
145 // 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;
146 // return false;
147 // }
148 // case where the unit type (energy, time...) does not change => easy mode, juste change the coeff
149 if((gunits->get_type(fUnit_key) == gunits->kUndef_type) || (gunits->get_type(_unit) == gunits->get_type(fUnit_key))) {
150 fUnit_key = _unit;
151 fFactor = gunits->get_factor(fUnit_key);
152 }
153 else {
154 // case Energy to time, or time to Energy
155 if( ((gunits->get_type(fUnit_key) == gunits->kEnergy) && (gunits->get_type(_unit) == gunits->kTime)) ||
156 ((gunits->get_type(fUnit_key) == gunits->kTime) && (gunits->get_type(_unit) == gunits->kEnergy))) {
157 // ev to s
158 // E x tau = hbar
159 // tau = hbar / E = 1.054571818×10−34 (Jxs) / E (keV)
160 // 1J = 6.2415091e+18 eV
161 // tau (s) = (1.054571818e−34*6.2415091e+15 (keVxs) / E (keV)
162 // T (s) = (1.054571818e−34*6.2415091e+15*ln(2) (keVxs) / E (keV)
163 // fFactor = (1.054571818e-34*6.2415091e+15)*ln(2); // ~ 4.5e-19 s
164 fUnit_key = _unit;
165 fValue = (1.054571818e-34*6.2415091e+15)*log(2) / fValue;
166 fFactor = gunits->get_factor(fUnit_key);
167 }
168 else {
169 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;
170 return false;
171 }
172 }
173 return true;
174}
175
176#ifdef HAS_ROOT
177ClassImp(tkunit);
178#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition: tkstring.h:54
bool is_empty() const
Definition: tkstring.h:163
Manages the different units.
Definition: tkunit.h:55
units_keys
units identifiers
Definition: tkunit.h:69
static std::vector< std::tuple< tkstring, units_type, double > > funits_properties
Definition: tkunit.h:75
static tkunit_manager * the_unit_manager()
db_manager is a singleton
Definition: tkunit.cpp:105
A measured value associated to its unit.
Definition: tkunit.h:143
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:122
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition: tkunit.cpp:138
Definition: tklog.cpp:39
tklog & warning_o(tklog &log)
Definition: tklog.h:348
tklog & do_endl(tklog &log)
Definition: tklog.h:235