AMO-Tools-Suite  v.0.9.0
Set of tools for calculating energy efficiency in industrial equipment
All Classes Namespaces Files Functions Variables Enumerations Friends Macros Pages
standalone.h
1 #ifndef AMO_TOOLS_SUITE_STANDALONE_H
2 #define AMO_TOOLS_SUITE_STANDALONE_H
3 
4 #include <nan.h>
5 #include <node.h>
6 #include "calculator/util/CHP.h"
7 #include "calculator/util/CompressedAir.h"
8 #include <string>
9 #include <array>
10 #include <vector>
11 #include <exception>
12 #include <iostream>
13 
14 
15 using namespace Nan;
16 using namespace v8;
17 
18 Local<Object> inp;
19 Local<Object> r;
20 
21 double Get(std::string const & nm) {
22  Local<String> getName = Nan::New<String>(nm).ToLocalChecked();
23  v8::Isolate *isolate = v8::Isolate::GetCurrent();
24  v8::Local<v8::Context> context = isolate->GetCurrentContext();
25  Local<Value> rObj = Nan::To<Object>(inp).ToLocalChecked()->Get(context, getName).ToLocalChecked();
26  if (rObj->IsUndefined()) {
27  ThrowTypeError(std::string("Get method in standalone.h: " + nm + " not present in object").c_str());
28  }
29  return Nan::To<double>(rObj).FromJust();
30 }
31 
32 std::vector<double> GetVector(const std::string &key, Local<Object> obj)
33 {
34  v8::Isolate *isolate = v8::Isolate::GetCurrent();
35  v8::Local<v8::Context> context = isolate->GetCurrentContext();
36 
37  Local<Value> arrayTmp = Nan::Get(Nan::To<v8::Object>(obj).ToLocalChecked(), Nan::New<String>(key).ToLocalChecked()).ToLocalChecked();
38  Local<Array> jsArray = v8::Local<v8::Array>::Cast(arrayTmp);
39  std::vector<double> array;
40 
41  for (unsigned int i = 0; i < jsArray->Length(); i++)
42  {
43  v8::Local<v8::Value> jsElement = jsArray->Get(context, i).ToLocalChecked();
44  double val = Nan::To<double>(jsElement).FromJust();
45  array.push_back(val);
46  }
47  return array;
48 }
49 
50 void SetR(std::string const & nm, double n) {
51  Local<String> getName = Nan::New<String>(nm).ToLocalChecked();
52  Local<Number> getNum = Nan::New<Number>(n);
53  Nan::Set(r, getName, getNum);
54 }
55 
56 NAN_METHOD(CHPcalculator) {
57  inp = Nan::To<Object>(info[0]).ToLocalChecked();
58  r = Nan::New<Object>();
59 
60  const double annualOperatingHours = Get("annualOperatingHours");
61  const double annualElectricityConsumption = Get("annualElectricityConsumption");
62  const double annualThermalDemand = Get("annualThermalDemand");
63  const double boilerThermalFuelCosts = Get("boilerThermalFuelCosts");
64  const double avgElectricityCosts = Get("avgElectricityCosts");
65  const double boilerThermalFuelCostsCHPcase = Get("boilerThermalFuelCostsCHPcase");
66  const double CHPfuelCosts = Get("CHPfuelCosts");
67  const double percentAvgkWhElectricCostAvoidedOrStandbyRate = Get("percentAvgkWhElectricCostAvoidedOrStandbyRate");
68  const double displacedThermalEfficiency = Get("displacedThermalEfficiency");
69  const double chpAvailability = Get("chpAvailability");
70  const double thermalUtilization = Get("thermalUtilization");
71 
72  unsigned val = static_cast<unsigned>(Get("option"));
73  CHP::Option option = static_cast<CHP::Option>(val);
74 
75  auto const chp = CHP(annualOperatingHours, annualElectricityConsumption, annualThermalDemand,
76  boilerThermalFuelCosts, avgElectricityCosts, option, boilerThermalFuelCostsCHPcase,
77  CHPfuelCosts, percentAvgkWhElectricCostAvoidedOrStandbyRate, displacedThermalEfficiency,
78  chpAvailability, thermalUtilization);
79 
80  auto const & costInfo = chp.getCostInfo();
81 
82  SetR("annualOperationSavings", costInfo.at("annualOperationSavings"));
83  SetR("totalInstalledCostsPayback", costInfo.at("totalInstalledCostsPayback"));
84  SetR("simplePayback", costInfo.at("simplePayback"));
85  SetR("fuelCosts", costInfo.at("fuelCosts"));
86  SetR("thermalCredit", costInfo.at("thermalCredit"));
87  SetR("incrementalOandM", costInfo.at("incrementalOandM"));
88  SetR("totalOperatingCosts", costInfo.at("totalOperatingCosts"));
89  info.GetReturnValue().Set(r);
90 }
91 
92 NAN_METHOD(usableAirCapacity) {
93  inp = Nan::To<Object>(info[0]).ToLocalChecked();
94  const double tankSize = Get("tankSize");
95  const double airPressureIn = Get("airPressureIn");
96  const double airPressureOut = Get("airPressureOut");
97  info.GetReturnValue().Set(ReceiverTank::calculateUsableCapacity(tankSize, airPressureIn, airPressureOut));
98 }
99 
100 NAN_METHOD(pneumaticAirRequirement) {
101  inp = Nan::To<Object>(info[0]).ToLocalChecked();
102  r = Nan::New<Object>();
103 
104  int val = Get("pistonType");
105  auto const pistonType = (!val) ? PneumaticAirRequirement::PistonType::SingleActing : PneumaticAirRequirement::PistonType::DoubleActing;
106  const double cylinderDiameter = Get("cylinderDiameter");
107  const double cylinderStroke = Get("cylinderStroke");
108  const double airPressure = Get("airPressure");
109  const double cyclesPerMinute = Get("cyclesPerMinute");
110 
111 
112  PneumaticAirRequirement airRequirement;
113  if (pistonType == PneumaticAirRequirement::PistonType::SingleActing) {
114  airRequirement = PneumaticAirRequirement(pistonType, cylinderDiameter, cylinderStroke, airPressure, cyclesPerMinute);
115  } else {
116  const double pistonRodDiameter = Get("pistonRodDiameter");
117  airRequirement = PneumaticAirRequirement(pistonType, cylinderDiameter, cylinderStroke, pistonRodDiameter, airPressure, cyclesPerMinute);
118  }
119 
120  auto const output = airRequirement.calculate();
121  SetR("airRequirementPneumaticCylinder", output.airRequirementPneumaticCylinder);
122  SetR("volumeAirIntakePiston", output.volumeAirIntakePiston);
123  SetR("compressionRatio", output.compressionRatio);
124  info.GetReturnValue().Set(r);
125 }
126 
127 NAN_METHOD(receiverTank) {
128  inp = Nan::To<Object>(info[0]).ToLocalChecked();
129 
130  ReceiverTank tank;
131  ReceiverTank::Method method = static_cast<ReceiverTank::Method>(static_cast<unsigned>(Get("method")));
132 
133 
134  if (method == ReceiverTank::Method::General) {
135 
136  const double airDemand = Get("airDemand");
137  const double allowablePressureDrop = Get("allowablePressureDrop");
138  const double atmosphericPressure = Get("atmosphericPressure");
139 
140  tank = {ReceiverTank::Method::General, airDemand, allowablePressureDrop, atmosphericPressure};
141 
142  } else if (method == ReceiverTank::Method::DedicatedStorage) {
143 
144  const double finalTankPressure = Get("finalTankPressure");
145  const double initialTankPressure = Get("initialTankPressure");
146  const double lengthOfDemand = Get("lengthOfDemand");
147  const double airFlowRequirement = Get("airFlowRequirement");
148  const double atmosphericPressure = Get("atmosphericPressure");
149 
150  tank = {ReceiverTank::Method::DedicatedStorage, lengthOfDemand, airFlowRequirement, atmosphericPressure, initialTankPressure, finalTankPressure};
151 
152  } else if (method == ReceiverTank::Method::BridgingCompressorReactionDelay) {
153 
154  const double speedOfAir = Get("speedOfAir");
155  const double distanceToCompressorRoom = Get("distanceToCompressorRoom");
156  const double atmosphericPressure = Get("atmosphericPressure");
157  const double airDemand = Get("airDemand");
158  const double allowablePressureDrop = Get("allowablePressureDrop");
159 
160  tank = {ReceiverTank::Method::BridgingCompressorReactionDelay, distanceToCompressorRoom, speedOfAir, atmosphericPressure, airDemand, allowablePressureDrop};
161 
162  } else {
163  const double lengthOfDemand = Get("lengthOfDemand");
164  const double airFlowRequirement = Get("airFlowRequirement");
165  const double atmosphericPressure = Get("atmosphericPressure");
166  const double initialTankPressure = Get("initialTankPressure");
167  const double finalTankPressure = Get("finalTankPressure");
168  const double meteredControl = Get("meteredControl");
169 
170  tank = {ReceiverTank::Method::MeteredStorage, lengthOfDemand, airFlowRequirement, atmosphericPressure, initialTankPressure, finalTankPressure, meteredControl};
171  }
172  Local<Number> size = Nan::New(tank.calculateSize());
173  info.GetReturnValue().Set(size);
174 }
175 
176 NAN_METHOD(operatingCost) {
177  inp = Nan::To<Object>(info[0]).ToLocalChecked();
178  r = Nan::New<Object>();
179 
180  const double motorBhp = Get("motorBhp");
181  const double bhpUnloaded = Get("bhpUnloaded");
182  const double annualOperatingHours = Get("annualOperatingHours");
183  const double runTimeLoaded = Get("runTimeLoaded");
184  const double efficiencyLoaded = Get("efficiencyLoaded");
185  const double efficiencyUnloaded = Get("efficiencyUnloaded");
186  const double costOfElectricity = Get("costOfElectricity");
187 
188  auto output = Compressor::OperatingCost(motorBhp, bhpUnloaded, annualOperatingHours,
189  runTimeLoaded, efficiencyLoaded, efficiencyUnloaded,
190  costOfElectricity).calculate();
191 
192  SetR("runTimeUnloaded", output.runTimeUnloaded);
193  SetR("costForLoaded", output.costForLoaded);
194  SetR("costForUnloaded", output.costForUnloaded);
195  SetR("totalAnnualCost", output.totalAnnualCost);
196  info.GetReturnValue().Set(r);
197 }
198 
199 NAN_METHOD(airSystemCapacity) {
200  inp = Nan::To<Object>(info[0]).ToLocalChecked();
201  r = Nan::New<Object>();
202 
203  std::vector<double> receiverCapacitiesGallons = GetVector("receiverCapacities", inp);
204  const double oneHalf = Get("oneHalf");
205  const double threeFourths = Get("threeFourths");
206  const double one = Get("one");
207  const double oneAndOneFourth = Get("oneAndOneFourth");
208  const double oneAndOneHalf = Get("oneAndOneHalf");
209  const double two = Get("two");
210  const double twoAndOneHalf = Get("twoAndOneHalf");
211  const double three = Get("three");
212  const double threeAndOneHalf = Get("threeAndOneHalf");
213  const double four = Get("four");
214  const double five = Get("five");
215  const double six = Get("six");
216  const double eight = Get("eight");
217  const double ten = Get("ten");
218  const double twelve = Get("twelve");
219  const double fourteen = Get("fourteen");
220  const double sixteen = Get("sixteen");
221  const double eighteen = Get("eighteen");
222  const double twenty = Get("twenty");
223  const double twentyFour = Get("twentyFour");
224 
226  (
227  {
228  oneHalf, threeFourths, one, oneAndOneFourth,
229  oneAndOneHalf, two, twoAndOneHalf, three,
230  threeAndOneHalf, four, five, six, eight, ten, twelve, fourteen,
231  sixteen, eighteen, twenty, twentyFour
232  },
233  std::move(receiverCapacitiesGallons)
234  ).calculate();
235 
236  SetR("totalPipeVolume", output.totalPipeVolume);
237  SetR("totalReceiverVolume", output.totalReceiverVol);
238  SetR("totalCapacityOfCompressedAirSystem", output.totalCapacityOfCompressedAirSystem);
239 
240  v8::Local<v8::Array> receiverCapacities = Nan::New<v8::Array>(output.receiverCapacities.size());
241  for (std::size_t i = 0; i < output.receiverCapacities.size(); i++) {
242  Nan::Set(receiverCapacities, i, Nan::New<Number>(output.receiverCapacities[i]));
243  }
244  Nan::Set(r, Nan::New<String>("receiverCapacities").ToLocalChecked(), receiverCapacities);
245 
246  SetR("oneHalf", output.pipeLengths.oneHalf);
247  SetR("threeFourths", output.pipeLengths.threeFourths);
248  SetR("one", output.pipeLengths.one);
249  SetR("oneAndOneFourth", output.pipeLengths.oneAndOneFourth);
250  SetR("oneAndOneHalf", output.pipeLengths.oneAndOneHalf);
251  SetR("two", output.pipeLengths.two);
252  SetR("twoAndOneHalf", output.pipeLengths.twoAndOneHalf);
253  SetR("three", output.pipeLengths.three);
254  SetR("threeAndOneHalf", output.pipeLengths.threeAndOneHalf);
255  SetR("four", output.pipeLengths.four);
256  SetR("five", output.pipeLengths.five);
257  SetR("six", output.pipeLengths.six);
258  SetR("eight", output.pipeLengths.eight);
259  SetR("ten", output.pipeLengths.ten);
260  SetR("twelve", output.pipeLengths.twelve);
261  SetR("fourteen", output.pipeLengths.fourteen);
262  SetR("sixteen", output.pipeLengths.sixteen);
263  SetR("eighteen", output.pipeLengths.eighteen);
264  SetR("twenty", output.pipeLengths.twenty);
265  SetR("twentyFour", output.pipeLengths.twentyFour);
266  info.GetReturnValue().Set(r);
267 }
268 
269 NAN_METHOD(airVelocity) {
270  inp = Nan::To<Object>(info[0]).ToLocalChecked();
271  r = Nan::New<Object>();
272 
273  const double airFlow = Get("airFlow");
274  const double pipePressure = Get("pipePressure");
275  const double atmosphericPressure = Get("atmosphericPressure");
276 
277  Compressor::PipeData output = Compressor::AirVelocity(airFlow, pipePressure, atmosphericPressure).calculate();
278 
279  SetR("oneHalf", output.oneHalf);
280  SetR("threeFourths", output.threeFourths);
281  SetR("one", output.one);
282  SetR("oneAndOneFourth", output.oneAndOneFourth);
283  SetR("oneAndOneHalf", output.oneAndOneHalf);
284  SetR("two", output.two);
285  SetR("twoAndOneHalf", output.twoAndOneHalf);
286  SetR("three", output.three);
287  SetR("threeAndOneHalf", output.threeAndOneHalf);
288  SetR("four", output.four);
289  SetR("five", output.five);
290  SetR("six", output.six);
291  SetR("eight", output.eight);
292  SetR("ten", output.ten);
293  SetR("twelve", output.twelve);
294  SetR("fourteen", output.fourteen);
295  SetR("sixteen", output.sixteen);
296  SetR("eighteen", output.eighteen);
297  SetR("twenty", output.twenty);
298  SetR("twentyFour", output.twentyFour);
299 
300  info.GetReturnValue().Set(r);
301 }
302 
303 NAN_METHOD(pipeSizing) {
304  inp = Nan::To<Object>(info[0]).ToLocalChecked();
305  r = Nan::New<Object>();
306 
307  const double airFlow = Get("airFlow");
308  const double airlinePressure = Get("airlinePressure");
309  const double designVelocity = Get("designVelocity");
310  const double atmosphericPressure = Get("atmosphericPressure");
311 
312  auto output = Compressor::PipeSizing(airFlow, airlinePressure, designVelocity, atmosphericPressure).calculate();
313 
314  SetR("crossSectionalArea", output.crossSectionalArea);
315  SetR("pipeDiameter", output.pipeDiameter);
316 
317  info.GetReturnValue().Set(r);
318 }
319 
320 NAN_METHOD(pneumaticValve) {
321  inp = Nan::To<Object>(info[0]).ToLocalChecked();
322  r = Nan::New<Object>();
323  v8::Isolate *isolate = v8::Isolate::GetCurrent();
324  v8::Local<v8::Context> context = isolate->GetCurrentContext();
325 
326  Local<Value> flowRate = inp->Get(context, Nan::New<String>("flowRate").ToLocalChecked()).ToLocalChecked();
327  const double inletPressure = Get("inletPressure");
328  const double outletPressure = Get("outletPressure");
329  if (flowRate->IsUndefined()) {
330  double output = Compressor::PneumaticValve(inletPressure, outletPressure).calculate();
331  SetR("flowRate", output);
332  } else {
333  const double flowRateInput = Get("flowRate");
334  double output = Compressor::PneumaticValve(inletPressure, outletPressure, flowRateInput).calculate();
335  SetR("flowCoefficient", output);
336  }
337 
338  info.GetReturnValue().Set(r);
339 }
340 
341 NAN_METHOD(bagMethod) {
342  inp = Nan::To<Object>(info[0]).ToLocalChecked();
343  r = Nan::New<Object>();
344 
345  const double operatingTime = Get("operatingTime");
346  const double bagFillTime = Get("bagFillTime");
347  const double heightOfBag = Get("heightOfBag");
348  const double diameterOfBag = Get("diameterOfBag");
349  const double numberOfUnits = Get("numberOfUnits");
350 
351  auto output = BagMethod(operatingTime, bagFillTime, heightOfBag, diameterOfBag, numberOfUnits).calculate();
352  SetR("flowRate", output.flowRate);
353  SetR("annualConsumption", output.annualConsumption);
354 
355  info.GetReturnValue().Set(r);
356 }
357 // TODO: Add estimate, orifice, and decibels methods in the future
358 
359 #endif //AMO_TOOLS_SUITE_STANDALONE_H
double calculateSize()
static double calculateUsableCapacity(const double tankSize, const double airPressureIn, const double airPressureOut)
Definition: CHP.h:16
Option
Definition: CHP.h:35
Output calculate()