1 /*
  2  * ForceUnit.js - Unit conversions for force
  3  *
  4  * Copyright © 2018-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 force 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 ForceUnit = function (options) {
 37     this.unit = "newton";
 38     this.amount = 0;
 39 
 40     this.ratios = ForceUnit.ratios;
 41     this.aliases = ForceUnit.aliases;
 42     this.aliasesLower = ForceUnit.aliasesLower;
 43     this.systems = ForceUnit.systems;
 44 
 45     this.parent(options);
 46 };
 47 
 48 ForceUnit.prototype = new Measurement();
 49 ForceUnit.prototype.parent = Measurement;
 50 ForceUnit.prototype.constructor = ForceUnit;
 51 
 52 // https://en.wikipedia.org/wiki/Force
 53 ForceUnit.ratios = {
 54     /*                        index dyne       mN          N           kN         MN         GN         kilogram-force pound-force poundal     */
 55     "dyne":                  [ 1,   1,         1e-2,       1e-5,       1e-8,      1e-11,     1e-14,     1.01972e-6,    2.24809e-6, 7.23301e-5   ],
 56     "millinewton":           [ 2,   100,       1,          1e-3,       1e-6,      1e-9,      1e-12,     1.01972e-4,    2.24809e-4, 7.23301e-3   ],
 57     "newton":                [ 3,   1e5,       1000,       1,          1e-3,      1e-6,      1e-9,      0.101972,      0.224809,   7.23301      ],
 58     "kilonewton":            [ 4,   1e8,       1e6,        1000,       1,         1e-3,      1e-6,      101.972,       224.809,    7.23301e3    ],
 59     "meganewton":            [ 5,   1e11,      1e9,        1e6,        1000,      1,         1e-3,      1.01972e5,     2.24809e5,  7.23301e6    ],
 60     "giganewton":            [ 6,   1e14,      1e12,       1e9,        1e6,       1000,      1,         1.01972e8,     2.24809e8,  7.23301e9    ],
 61     "kilogram-force":        [ 7,   9.80665e5, 9.80665e-3, 9.80665,    9.80665e3, 9.80665e6, 9.80665e9, 1,             2.20462,    70.9316      ],
 62     "pound-force":           [ 8,   4.44822e5, 4.44822e-3, 4.44822,    4.44822e3, 4.44822e6, 4.44822e9, 0.453592,      1,          32.174       ],
 63     "poundal":               [ 9,   1.38255e4, 1.38255e-4, 1.38255e-1, 1.38255e2, 1.38255e5, 1.38255e8, 0.0140981,     0.031081,   1            ]
 64 };
 65 
 66 /**
 67  * Return the type of this measurement. Examples are "mass",
 68  * "length", "speed", etc. Measurements can only be converted
 69  * to measurements of the same type.<p>
 70  *
 71  * The type of the units is determined automatically from the
 72  * units. For example, the unit "grams" is type "mass". Use the
 73  * static call {@link Measurement.getAvailableUnits}
 74  * to find out what units this version of ilib supports.
 75  *
 76  * @return {string} the name of the type of this measurement
 77  */
 78 ForceUnit.prototype.getMeasure = function() {
 79     return "force";
 80 };
 81 
 82 /**
 83  * Return a new instance of this type of measurement.
 84  *
 85  * @param {Object} params parameters to the constructor
 86  * @return {Measurement} a measurement subclass instance
 87  */
 88 ForceUnit.prototype.newUnit = function(params) {
 89     return new ForceUnit(params);
 90 };
 91 
 92 ForceUnit.systems = {
 93     "metric": [
 94         "dyne",
 95         "millinewton",
 96         "newton",
 97         "kilonewton",
 98         "meganewton",
 99         "giganewton"
100     ],
101     "uscustomary": [
102         "poundal",
103         "pound-force"
104     ],
105     "imperial": [
106         "poundal",
107         "pound-force"
108     ],
109     "conversions": {
110         "metric": {
111             "uscustomary": {
112                 "millinewton": "pound-force",
113                 "newton": "pound-force",
114                 "kilonewton": "pound-force",
115                 "meganewton": "pound-force",
116                 "giganewton": "pound-force",
117                 "dyne": "pound-force",
118                 "kilogram-force": "pound-force"
119             },
120             "imperial": {
121                 "millinewton": "pound-force",
122                 "newton": "pound-force",
123                 "kilonewton": "pound-force",
124                 "meganewton": "pound-force",
125                 "giganewton": "pound-force",
126                 "dyne": "pound-force",
127                 "kilogram-force": "pound-force"
128             }
129         },
130         "uscustomary": {
131             "metric": {
132                 "poundal": "newton",
133                 "pound-force": "newton"
134             }
135         },
136         "imperial": {
137             "uscustomary": {
138                 "poundal": "newton",
139                 "pound-force": "newton"
140             }
141         }
142     }
143 };
144 
145 ForceUnit.aliases = {
146     "N": "newton",
147     "newtons": "newton",
148     "milli newtons": "millinewton",
149     "milli newton": "millinewton",
150     "milli-newton": "millinewton",
151     "mN": "millinewton",
152     "kilo newtons": "kilonewton",
153     "kilo newton": "kilonewton",
154     "kilo-newton": "kilonewton",
155     "kN": "kilonewton",
156     "mega newtons": "meganewton",
157     "mega newton": "meganewton",
158     "mega-newton": "meganewton",
159     "MN": "meganewton",
160     "giga newtons": "giganewton",
161     "giga newton": "giganewton",
162     "giga-newton": "giganewton",
163     "giga-newton": "giganewton",
164     "GN": "giganewton",
165     "dynes": "dyne",
166     "dyn": "dyne",
167     "kilopond": "kilogram-force",
168     "kiloponds": "kilogram-force",
169     "kilogram force": "kilogram-force",
170     "kilograms force": "kilogram-force",
171     "kilograms-force": "kilogram-force",
172     "kp": "kilogram-force",
173     "poundals": "poundal",
174     "pdl": "poundal",
175     "pound force": "pound-force",
176     "pounds force": "pound-force",
177     "lb force": "pound-force",
178     "pounds force": "pound-force",
179     "lbs force": "pound-force",
180     "lbf": "pound-force"
181 };
182 
183 ForceUnit.aliasesLower = {};
184 for (var a in ForceUnit.aliases) {
185     ForceUnit.aliasesLower[a.toLowerCase()] = ForceUnit.aliases[a];
186 }
187 
188 /**
189  * Convert a force to another measure.
190  * @static
191  * @param to {string} unit to convert to
192  * @param from {string} unit to convert from
193  * @param force {number} amount to be convert
194  * @returns {number|undefined} the converted amount
195  */
196 ForceUnit.convert = function(to, from, force) {
197     from = Measurement.getUnitIdCaseInsensitive(ForceUnit, from) || from;
198     to = Measurement.getUnitIdCaseInsensitive(ForceUnit, to) || to;
199     var fromRow = ForceUnit.ratios[from];
200     var toRow = ForceUnit.ratios[to];
201     if (typeof(from) === 'undefined' || typeof(to) === 'undefined') {
202         return undefined;
203     }
204     return force * fromRow[toRow[0]];
205 };
206 
207 /**
208  * @private
209  * @static
210  */
211 ForceUnit.getMeasures = function () {
212     return Object.keys(ForceUnit.ratios);
213 };
214 
215 //register with the factory method
216 Measurement._constructors["force"] = ForceUnit;
217 
218 module.exports = ForceUnit;
219