TkN 2.7
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
35const std::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
70 for(size_t i= 0 ; i<funits_properties.size() ; i++) {
71 funits_map_id_to_string[(units_keys)i] = std::get<0>(funits_properties.at(i));
72 funits_map_string_to_id[std::get<0>(funits_properties.at(i))] = (units_keys)i;
73 funits_map_string_to_id_low[std::get<0>(funits_properties.at(i)).copy().to_lower()] = (units_keys)i;
74 funits_map_conv_coeffs[(units_keys)i] = std::get<2>(funits_properties.at(i));
75 funits_map_types[(units_keys)i] = std::get<1>(funits_properties.at(i));
76 }
77}
78
80{
81 static tkunit_manager *instance = new tkunit_manager();
82 return instance;
83}
84
85tkunit::tkunit(double _value, const tkunit_manager::units_keys &_unit) :
86 fUnit_key(_unit),
87 fFactor(gunits->get_factor(fUnit_key)),
88 fValue(fFactor == 0. ? std::nan("0") : _value*fFactor)
89{
90
91}
92
93void tkunit::set_value(const double &_val, const tkstring &_unit)
94{
95 if(_unit.is_empty()) {
96 set_value(_val,fUnit_key);
97 }
98 else {
99 set_value(_val,gunits->get_key(_unit));
100 }
101}
102
103void tkunit::set_value(const double &_val, const tkunit_manager::units_keys &_unit) {
104 fUnit_key = _unit;
105 fFactor = gunits->get_factor(fUnit_key);
106 fValue = fFactor == 0. ? std::nan("0") : _val*fFactor;
107}
108
109bool tkunit::set_unit(const tkstring &_unit) {
110 return set_unit(gunits->get_key(_unit));
111}
112
114 if(_unit == gunits->Undef_unit) return false;
115 if (_unit == fUnit_key) return true;
116
117 const auto current_type = gunits->get_type(fUnit_key);
118 const auto requested_type = gunits->get_type(_unit);
119
120 // An uninitialised tkunit may acquire any valid unit. Its numeric value
121 // is set separately by the owning tkmeasure.
122 if (current_type == gunits->kUndef_type) {
123 fUnit_key = _unit;
124 fFactor = gunits->get_factor(fUnit_key);
125 return true;
126 }
127
128 // case where the unit type (energy, time...) does not change => easy mode, juste change the coeff
129 if(requested_type == current_type && current_type != gunits->kNoConvUnits) {
130 fUnit_key = _unit;
131 fFactor = gunits->get_factor(fUnit_key);
132 }
133 else {
134 // case Energy to time, or time to Energy
135 if( ((current_type == gunits->kEnergy) && (requested_type == gunits->kTime)) ||
136 ((current_type == gunits->kTime) && (requested_type == gunits->kEnergy))) {
137 if (!std::isfinite(fValue) || fValue <= 0.) {
138 glog << warning_o << " cannot convert a non-positive or non-finite "
139 << gunits->get_name(fUnit_key) << " value to " << gunits->get_name(_unit) << do_endl;
140 return false;
141 }
142 // ev to s
143 // E x tau = hbar
144 // tau = hbar / E = 1.054571818×10−34 (Jxs) / E (keV)
145 // 1J = 6.2415091e+18 eV
146 // tau (s) = (1.054571818e−34*6.2415091e+15 (keVxs) / E (keV)
147 // T (s) = (1.054571818e−34*6.2415091e+15*ln(2) (keVxs) / E (keV)
148 // fFactor = (1.054571818e-34*6.2415091e+15)*ln(2); // ~ 4.5e-19 s
149 fUnit_key = _unit;
150 constexpr double hbar_ln2_keV_s = (1.054571818e-34*6.2415091e+15)*0.69314718055994530942;
151 fValue = hbar_ln2_keV_s / fValue;
152 fFactor = gunits->get_factor(fUnit_key);
153 }
154 else {
155 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;
156 return false;
157 }
158 }
159 return true;
160}
161
162#ifdef HAS_ROOT
163ClassImp(tkunit);
164#endif
std::string with usefull tricks from TString (ROOT) and KVString (KaliVeda) and more....
Definition tkstring.h:33
bool is_empty() const
Definition tkstring.h:145
units_keys
units identifiers
Definition tkunit.h:48
static tkunit_manager * the_unit_manager()
db_manager is a singleton
Definition tkunit.cpp:79
static const std::vector< std::tuple< tkstring, units_type, double > > funits_properties
Definition tkunit.h:54
A measured value associated to its unit.
Definition tkunit.h:144
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:93
bool set_unit(const tkstring &_unit)
set the unit from a string
Definition tkunit.cpp:109
Definition tklog.cpp:16
tklog & warning_o(tklog &log)
Definition tklog.h:325
tklog & do_endl(tklog &log)
Definition tklog.h:212