Improve electric utils code.
[e-mobility-charging-stations-simulator.git] / src / utils / ElectricUtils.ts
1 /**
2 * Rationale: https://wiki.piment-noir.org/doku.php/en:cs:modelling_multi-phased_electrical_system_interconnexion
3 */
4
5 /**
6 * Targeted to AC related values calculation.
7 */
8 export class ACElectricUtils {
9 static amperageTotal(nbOfPhases: number, Iph: number): number {
10 return nbOfPhases * Iph;
11 }
12
13 static powerPerPhase(V: number, Iph: number, cosPhi = 1): number {
14 const powerPerPhase = V * Iph * cosPhi;
15 if (cosPhi === 1) {
16 return powerPerPhase;
17 }
18 return Math.round(powerPerPhase);
19 }
20
21 static powerTotal(nbOfPhases: number, V: number, Iph: number, cosPhi = 1): number {
22 return nbOfPhases * ACElectricUtils.powerPerPhase(V, Iph, cosPhi);
23 }
24
25 static amperageTotalFromPower(P: number, V: number, cosPhi = 1): number {
26 const amperage = P / (V * cosPhi);
27 if (cosPhi === 1 && P % V === 0) {
28 return amperage;
29 }
30 return Math.round(amperage);
31 }
32
33 static amperagePerPhaseFromPower(nbOfPhases: number, P: number, V: number, cosPhi = 1): number {
34 const amperage = ACElectricUtils.amperageTotalFromPower(P, V, cosPhi);
35 const amperagePerPhase = amperage / nbOfPhases;
36 if (amperage % nbOfPhases === 0) {
37 return amperagePerPhase;
38 }
39 return Math.round(amperagePerPhase);
40 }
41 }
42
43 /**
44 * Targeted to DC related values calculation.
45 */
46 export class DCElectricUtils {
47 static power(V: number, I: number): number {
48 return V * I;
49 }
50
51 static amperage(P: number, V: number): number {
52 const amperage = P / V;
53 if (P % V === 0) {
54 return amperage;
55 }
56 return Math.round(amperage);
57 }
58 }