1 /*
  2  * PressureUnit.js - Unit conversions for pressure
  3  *
  4  * Copyright © 2021-2022 JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 /*
 21 !depends
 22 Measurement.js
 23 */
 24 
 25 var Measurement = require("./Measurement.js");
 26 
 27 /**
 28  * @class
 29  * Create a new pressure measurement instance.
 30  *
 31  * @constructor
 32  * @extends Measurement
 33  * @param options {{unit:string,amount:number|string|undefined}} Options controlling
 34  * the construction of this instance
 35  */
 36 var PressureUnit = function (options) {
 37     this.unit = "pascal";
 38     this.amount = 0;
 39 
 40     this.ratios = PressureUnit.ratios;
 41     this.aliases = PressureUnit.aliases;
 42     this.aliasesLower = PressureUnit.aliasesLower;
 43     this.systems = PressureUnit.systems;
 44 
 45     this.parent(options);
 46 };
 47 
 48 PressureUnit.prototype = new Measurement();
 49 PressureUnit.prototype.parent = Measurement;
 50 PressureUnit.prototype.constructor = PressureUnit;
 51 
 52 // https://en.wikipedia.org/wiki/Pressure
 53 PressureUnit.ratios = {
 54     /*                              index mPa           Pa            hPa           mbar          kPa          bar            MPa            GPa            atm          torr       lbs/in2            */
 55     "millipascal":                 [ 1,   1,            1e-3,         1e-5,         1e-5,         1e-6,        1e-8,          1e-9,          1e-12,         9.8692e-9,   7.5006e-6, 1.45038e-7         ],
 56     "pascal":                      [ 2,   1000,         1,            1e-2,         1e-2,         1e-3,        1e-5,          1e-6,          1e-9,          9.8692e-6,   7.5006e-3, 1.45038e-4         ],
 57     "hectopascal":                 [ 3,   1e5,          1e2,          1,            1,            0.1,         1e-3,          1e-4,          1e-7,          9.8692e-4,   7.5006e-1, 1.45038e-2         ],
 58     "millibar":                    [ 4,   1e5,          1e2,          1,            1,            0.1,         1e-3,          1e-4,          1e-7,          9.8692e-4,   7.5006e-1, 1.45038e-2         ],
 59     "kilopascal":                  [ 5,   1e6,          1e3,          1e2,          1e2,          1,           1e-2,          1e-3,          1e-6,          9.8692e-3,   7.5006,    1.45038e-1         ],
 60     "bar":                         [ 6,   1e8,          1e5,          1e3,          1e3,          100,         1,             0.1,           1e-4,          9.8692e-1,   7.5006e2,  14.5038            ],
 61     "megapascal":                  [ 7,   1e9,          1e6,          1e4,          1e4,          1e3,         10,            1,             1e-3,          9.8692,      7.5006e3,  145.038            ],
 62     "gigapascal":                  [ 8,   1e12,         1e9,          1e7,          1e7,          1e6,         1e4,           1e3,           1,             9.8692e3,    7.5006e6,  1.45038e5          ],
 63     "atmosphere":                  [ 9,   1.01325e8,    1.01325e5,    1.01325e3,    1.01325e3,    1.01325e2,   1.01325,       1.01325e-1,    1.01325e-4,    1,           760,       14.6959487755142   ],
 64     "torr":                        [ 10,  1.333224e5,   1.333224e2,   1.333224,     1.333224,     1.333224e-1, 1.333224e-3,   1.333224e-4,   1.333224e-2,   1.315789e-3, 1,         1.9336775e-2       ],
 65     "pound-force-per-square-inch": [ 11,  6.89475729e6, 6.89475729e3, 6.89475729e1, 6.89475729e1, 6.89475729,  6.89475729e-2, 6.89475729e-3, 6.89475729e-6, 6.8046e-2,   51.7149,   1                  ],
 66 };
 67 
 68 /**
 69  * Return the type of this measurement. Examples are "mass",
 70  * "length", "speed", etc. Measurements can only be converted
 71  * to measurements of the same type.<p>
 72  *
 73  * The type of the units is determined automatically from the
 74  * units. For example, the unit "grams" is type "mass". Use the
 75  * static call {@link Measurement.getAvailableUnits}
 76  * to find out what units this version of ilib supports.
 77  *
 78  * @return {string} the name of the type of this measurement
 79  */
 80 PressureUnit.prototype.getMeasure = function() {
 81     return "pressure";
 82 };
 83 
 84 /**
 85  * Return a new instance of this type of measurement.
 86  *
 87  * @param {Object} params parameters to the constructor
 88  * @return {Measurement} a measurement subclass instance
 89  */
 90 PressureUnit.prototype.newUnit = function(params) {
 91     return new PressureUnit(params);
 92 };
 93 
 94 PressureUnit.systems = {
 95     "metric": [
 96         "millipascal",
 97         "pascal",
 98         "hectopascal",
 99         "millibar",
100         "kilopascal",
101         "bar",
102         "megapascal",
103         "gigapascal"
104     ],
105     "uscustomary": [
106         "torr",
107 //        "inch-of-mercury",
108 //        "foot-sea-water",
109         "pound-force-per-square-inch",
110         "atmosphere"
111     ],
112     "imperial": [
113 //        "centimeter-of-water",
114         "torr",
115 //        "millimeter-of-mercury",
116 //        "inch-of-mercury",
117 //        "foot-sea-water",
118         "pound-force-per-square-inch",
119 //        "meter-sea-water",
120         "atmosphere"
121     ],
122     "conversions": {
123         "metric": {
124             "uscustomary": {
125                 "millipascal": "torr",
126                 "pascal": "torr",
127                 "hectopascal": "torr",
128                 "millibar": "torr",
129                 "kilopascal": "inch-of-mercury",
130                 "bar": "pound-force-per-square-inch",
131                 "megapascal": "pound-force-per-square-inch",
132                 "gigapascal": "atmosphere"
133             },
134             "imperial": {
135                 "millipascal": "torr",
136                 "pascal": "torr",
137                 "hectopascal": "torr",
138                 "millibar": "torr",
139                 "kilopascal": "inch-of-mercury",
140                 "bar": "pound-force-per-square-inch",
141                 "megapascal": "pound-force-per-square-inch",
142                 "gigapascal": "atmosphere"
143             }
144         },
145         "uscustomary": {
146             "metric": {
147                 "torr": "pascal",
148 //                "inch-of-mercury": "kilopascal",
149 //                "foot-sea-water": "meter-sea-water",
150                 "pound-force-per-square-inch": "bar",
151                 "atmosphere": "bar"
152             },
153             "imperial": {
154 //                "centimeter-of-water": "torr",
155 //                "millimeter-of-mercury": "inch-of-mercury",
156 //               "meter-sea-water": "atmosphere"
157             }
158         },
159         "imperial": {
160             "metric": {
161 //                "centimeter-of-water": "pascal",
162                 "torr": "pascal",
163 //                "millimeter-of-mercury": "millibar",
164 //                "inch-of-mercury": "kilopascal",
165 //                "foot-sea-water": "meter-sea-water",
166                 "pound-force-per-square-inch": "bar",
167 //                "meter-sea-water": "foot-sea-water",
168                 "atmosphere": "bar"
169             }
170         }
171     }
172 };
173 
174 PressureUnit.aliases = {
175     "Pa": "pascal",
176     "㎩": "pascal",
177     "pascals": "pascal",
178     "hPa": "hectopascal",
179     "㍱": "hectopascal",
180     "mbar": "millibar",
181     "kPa": "kilopascal",
182     "㎪": "kilopascal",
183     "MPa": "megapascal",
184     "㎫": "megapascal",
185     "GPa": "gigapascal",
186     "㎬": "gigapascal",
187     "t": "torr",
188     "psi": "pound-force-per-square-inch",
189     "pounds per square inch": "pound-force-per-square-inch",
190     "pound per square inch": "pound-force-per-square-inch",
191     "pound force per square inch": "pound-force-per-square-inch",
192     "pounds per sq. in.": "pound-force-per-square-inch",
193     "pound per sq. in.": "pound-force-per-square-inch",
194     "lbs per square inch": "pound-force-per-square-inch",
195     "lbf per square inch": "pound-force-per-square-inch",
196     "lbs per sq. in.": "pound-force-per-square-inch",
197     "lbf per sq. in.": "pound-force-per-square-inch",
198     "pounds/square inch": "pound-force-per-square-inch",
199     "pounds/sq. in.": "pound-force-per-square-inch",
200     "lbs/square inch": "pound-force-per-square-inch",
201     "lbf/square inch": "pound-force-per-square-inch",
202     "lbs/sq. in.": "pound-force-per-square-inch",
203     "lbf/sq. in.": "pound-force-per-square-inch",
204     "pounds/inch²": "pound-force-per-square-inch",
205     "pounds/in²": "pound-force-per-square-inch",
206     "lbs/inch²": "pound-force-per-square-inch",
207     "lbf/inch²": "pound-force-per-square-inch",
208     "lbs/in²": "pound-force-per-square-inch",
209     "lbf/in²": "pound-force-per-square-inch",
210     "atmos": "atmosphere",
211     "atm": "atmosphere",
212     "mmHg": "millimeter-of-mercury",
213     "inHg": "inch-of-mercury",
214     "cmw": "centimeter-of-water",
215     "fsw": "foot-sea-water",
216     "msw": "meter-sea-water",
217 };
218 
219 PressureUnit.aliasesLower = {};
220 for (var a in PressureUnit.aliases) {
221     PressureUnit.aliasesLower[a.toLowerCase()] = PressureUnit.aliases[a];
222 }
223 
224 /**
225  * Convert a pressure to another measure.
226  * @static
227  * @param to {string} unit to convert to
228  * @param from {string} unit to convert from
229  * @param pressure {number} amount to be convert
230  * @returns {number|undefined} the converted amount
231  */
232 PressureUnit.convert = function(to, from, pressure) {
233     from = Measurement.getUnitIdCaseInsensitive(PressureUnit, from) || from;
234     to = Measurement.getUnitIdCaseInsensitive(PressureUnit, to) || to;
235     var fromRow = PressureUnit.ratios[from];
236     var toRow = PressureUnit.ratios[to];
237     if (typeof(from) === 'undefined' || typeof(to) === 'undefined') {
238         return undefined;
239     }
240     return pressure * fromRow[toRow[0]];
241 };
242 
243 /**
244  * @private
245  * @static
246  */
247 PressureUnit.getMeasures = function () {
248     return Object.keys(PressureUnit.ratios);
249 };
250 
251 //register with the factory method
252 Measurement._constructors["pressure"] = PressureUnit;
253 
254 module.exports = PressureUnit;
255