forked from NHERI-SimCenter/SimCenterBackendApplications
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUnits.cpp
More file actions
269 lines (197 loc) · 7.21 KB
/
Copy pathUnits.cpp
File metadata and controls
269 lines (197 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include "common/Units.h"
namespace Units
{
double GetLengthFactor(UnitSystem& fromUnitSystem, UnitSystem& toUnitSystem)
{
if(LengthUnit::Unknown == fromUnitSystem.lengthUnit)
{
std::cerr << "Unknown length unit!!!" << std::endl;
return 1.0;
}
if(LengthUnit::Unknown == toUnitSystem.lengthUnit)
{
std::cerr << "Unknown length unit!!!" << std::endl;
return 1.0;
}
if(fromUnitSystem.lengthUnit == toUnitSystem.lengthUnit)
return 1.0;
//Converting from fromUnitSystem to Millimeter
double toMillimeter = 1.0;
if(LengthUnit::Meter == fromUnitSystem.lengthUnit)
toMillimeter = 1000.0;
else if(LengthUnit::Centimeter == fromUnitSystem.lengthUnit)
toMillimeter = 10.0;
else if(LengthUnit::Millimeter == fromUnitSystem.lengthUnit)
toMillimeter = 1.0;
else if(LengthUnit::Inch == fromUnitSystem.lengthUnit)
toMillimeter = 25.4;
else if(LengthUnit::Foot == fromUnitSystem.lengthUnit)
toMillimeter = 304.8;
//Converting from Millimeter to toUnitSystem
double fromMillimeter = 1.0;
if(LengthUnit::Meter == toUnitSystem.lengthUnit)
fromMillimeter = 0.001;
else if(LengthUnit::Centimeter == toUnitSystem.lengthUnit)
fromMillimeter = 0.1;
else if(LengthUnit::Millimeter == toUnitSystem.lengthUnit)
fromMillimeter = 1.0;
else if(LengthUnit::Inch == toUnitSystem.lengthUnit)
fromMillimeter = 1.0/25.4;
else if(LengthUnit::Foot == toUnitSystem.lengthUnit)
fromMillimeter = 1.0/304.8;
return toMillimeter * fromMillimeter;
}
double GetTimeFactor(UnitSystem& fromUnitSystem, UnitSystem& toUnitsystem)
{
if(TimeUnit::Unknown == fromUnitSystem.timeUnit)
{
std::cerr << "Unknown time unit!!!" << std::endl;
return 1.0;
}
if(TimeUnit::Unknown == toUnitsystem.timeUnit)
{
std::cerr << "Unknown time unit!!!" << std::endl;
return 1.0;
}
if(fromUnitSystem.timeUnit == toUnitsystem.timeUnit)
return 1.0;
//converting to second
double toSecond = 1.0;
if(TimeUnit::Hour == fromUnitSystem.timeUnit)
toSecond = 3600.0;
else if(TimeUnit::Minute == fromUnitSystem.timeUnit)
toSecond = 60.0;
//converting from second
double fromSecond = 1.0;
if(TimeUnit::Hour == toUnitsystem.timeUnit)
fromSecond = 1.0/3600.0;
else if(TimeUnit::Minute == toUnitsystem.timeUnit)
fromSecond = 1.0/60.0;
return toSecond * fromSecond;
}
double GetForceFactor(const UnitSystem &fromUnitSystem,
const UnitSystem &toUnitSystem) {
// Unknown force unit
if (fromUnitSystem.forceUnit == ForceUnit::Unknown) {
std::cerr << "from: Unknown force unit!!!" << std::endl;
return 1.0;
}
if (toUnitSystem.forceUnit == ForceUnit::Unknown) {
std::cerr << "to: Unknown force unit!!!" << std::endl;
return 1.0;
}
// Force units equal
if (fromUnitSystem.forceUnit == toUnitSystem.forceUnit) {
return 1.0;
}
// Converting from fromUnitSystem to newtons
double toNewtons = 1.0;
if (ForceUnit::Kilonewton == fromUnitSystem.forceUnit)
toNewtons = 1000.0;
else if (ForceUnit::Pounds == fromUnitSystem.forceUnit)
toNewtons = 4.4482216;
else if (ForceUnit::Kips == fromUnitSystem.forceUnit)
toNewtons = 4448.2216;
// Converting from newtons to toUnitSystem
double fromNewtons = 1.0;
if (ForceUnit::Kilonewton == toUnitSystem.forceUnit)
fromNewtons = 1.0 / 1000.0;
else if (ForceUnit::Pounds == toUnitSystem.forceUnit)
fromNewtons = 1.0 / 4.4482216;
else if (ForceUnit::Kips == toUnitSystem.forceUnit)
fromNewtons = 1.0 / 4448.2216;
return toNewtons * fromNewtons;
}
double GetAccelerationFactor(UnitSystem &fromUnitSystem,
UnitSystem &toUnitSystem) {
double timeFactor = GetTimeFactor(fromUnitSystem, toUnitSystem);
double lengthFactor = GetLengthFactor(fromUnitSystem, toUnitSystem);
return lengthFactor / (timeFactor * timeFactor);
}
LengthUnit ParseLengthUnit(const char* lengthUnit)
{
std::string lengthUnitString(lengthUnit);
std::transform(lengthUnitString.begin(), lengthUnitString.end(), lengthUnitString.begin(), ::tolower);
static std::map<std::string, LengthUnit> lengthUnitMap
{
{"m", LengthUnit::Meter},
{"meter", LengthUnit::Meter},
{"meters", LengthUnit::Meter},
{"cm", LengthUnit::Centimeter},
{"centimeter", LengthUnit::Centimeter},
{"centimeters", LengthUnit::Centimeter},
{"mm", LengthUnit::Millimeter},
{"millimeter", LengthUnit::Millimeter},
{"millimeters", LengthUnit::Millimeter},
{"in", LengthUnit::Inch},
{"inch", LengthUnit::Inch},
{"inches", LengthUnit::Inch},
{"ft", LengthUnit::Foot},
{"foot", LengthUnit::Foot},
{"feet", LengthUnit::Foot}
};
if(lengthUnitMap.end() != lengthUnitMap.find(lengthUnitString))
return lengthUnitMap[lengthUnitString];
std::cerr << "Failed to parse length unit: " << lengthUnitString << "!!!" << std::endl;
return LengthUnit::Unknown;
}
ForceUnit ParseForceUnit(const char *forceUnit) {
std::string forceUnitString(forceUnit);
std::transform(forceUnitString.begin(), forceUnitString.end(),
forceUnitString.begin(), ::tolower);
static std::map<std::string, ForceUnit> forceUnitMap
{
{"pounds", ForceUnit::Pounds},
{"lbs", ForceUnit::Pounds},
{"lb", ForceUnit::Pounds},
{"k", ForceUnit::Kips},
{"kips", ForceUnit::Kips},
{"newton", ForceUnit::Newton},
{"newtons", ForceUnit::Newton},
{"n", ForceUnit::Newton},
{"kn", ForceUnit::Kilonewton},
{"kilonewton", ForceUnit::Kilonewton},
{"kilonewtons", ForceUnit::Kilonewton}
};
if (forceUnitMap.end() != forceUnitMap.find(forceUnitString))
return forceUnitMap[forceUnitString];
std::cerr << "Failed to parse force unit: " << forceUnitString << "!!!"
<< std::endl;
return ForceUnit::Unknown;
}
TimeUnit ParseTimeUnit(const char* timeUnit)
{
std::string timeUnitString(timeUnit);
std::transform(timeUnitString.begin(), timeUnitString.end(), timeUnitString.begin(), ::tolower);
static std::map<std::string, TimeUnit> timeUnitMap
{
{"s", TimeUnit::Second},
{"sec", TimeUnit::Second},
{"second", TimeUnit::Second},
{"seconds", TimeUnit::Second},
{"min", TimeUnit::Minute},
{"minute", TimeUnit::Minute},
{"minutes", TimeUnit::Minute},
{"hr", TimeUnit::Hour},
{"hour", TimeUnit::Hour},
{"hours", TimeUnit::Hour},
};
if(timeUnitMap.end() != timeUnitMap.find(timeUnitString))
return timeUnitMap[timeUnitString];
std::cerr << "Failed to parse time unit: " << timeUnitString << "!!!" << std::endl;
return TimeUnit::Unknown;
}
double GetGravity(UnitSystem &unitSystem)
{
double gravity = 9.80665; //in SI Units (m/sec^2)
UnitSystem SIUnits;
SIUnits.lengthUnit = LengthUnit::Meter;
SIUnits.timeUnit = TimeUnit::Second;
double conversionFactor = GetAccelerationFactor(SIUnits, unitSystem);
return gravity * conversionFactor;
}
}