Source: functions_power.js

"use strict";

/**
* AC power across an impedance due to current flowing, W.
 * @param {number} resistance resistance of the circuit in Ohm.
 * @param {number} reactance reactance of the circuit in Ohm.
 * @param {number} current the current flowing in the circuit, A.
 * @param {number} powerFactor power factor of the current (set =1 for d.c.).
* @return Power, W.
* @customfunction
*/
function powerAC(resistance, reactance, current, powerFactor) {
    var json = httpGetCall("electrical/power/" + resistance + "/" + reactance + "/" + current + "/" + powerFactor);
    var data = JSON.parse(json);
    return data.m_real;
}


/**
* AC VA (volt-amps) across an impedance due to current flowing, W.
 * @param {number} resistance resistance of the circuit in Ohm.
 * @param {number} reactance reactance of the circuit in Ohm.
 * @param {number} current the current flowing in the circuit, A.
 * @param {number} powerFactor power factor of the current (set =1 for d.c.).
* @return Power, W.
* @customfunction
*/
function powerACVA(resistance, reactance, current, powerFactor) {
    var json = httpGetCall("electrical/power/" + resistance + "/" + reactance + "/" + current + "/" + powerFactor);
    var data = JSON.parse(json);
    return data.m_magnitude;
}

/**
* AC reactive power across an impedance due to current flowing, W.
 * @param {number} resistance resistance of the circuit in Ohm.
 * @param {number} reactance reactance of the circuit in Ohm.
 * @param {number} current the current flowing in the circuit, A.
 * @param {number} powerFactor power factor of the current (set =1 for d.c.).
* @return Power, W.
* @customfunction
*/
function powerACVAr(resistance, reactance, current, powerFactor) {
    var json = httpGetCall("electrical/power/" + resistance + "/" + reactance + "/" + current + "/" + powerFactor);
    var data = JSON.parse(json);
    return data.m_imaginary;
}

/**
* Complex power across an impedance due to current flowing, W.
 * @param {complex} impedance impedance of the circuit in Ohm.
 * @param {complex} current the current flowing in the circuit, A.
* @return Power, W.
* @customfunction
*/
function powerCmplx(impedance, current) {
    var json = httpGetCall("electrical/power/" + complexRe(impedance) + "/" + complexIm(impedance) + "/" + complexMag(current) + "/" + complexPF(current));
    var data = JSON.parse(json);
    return cmplx(data.m_real, data.m_imaginary);
}

/**
* Complex power give by VxI* (I* is the conjugate of I), W.
* Additionally, power is calculated depending on phases (d.c., 1-ph or 3-ph)
 * @param {complex} voltage voltage of the circuit, cartesian coordinate, V.
 * @param {complex} current current of the circuit, cartesian coordinates, A.
 * @param {number} phase number of phases to consider, see =phases().
* @return Power, complex form (W + jVAr).
* @customfunction
*/
function powerComplex(voltage, current, phase) {
    var json = httpGetCall("electrical/power/" + complexMag(voltage) + "/" + complexPhase(voltage) + "/" + phase + "/" + complexMag(current) + "/" + complexPF(current) + "/" + 1);
    var data = JSON.parse(json);
    return cmplx(data.power_out.m_real, data.power_out.m_imaginary);
}



/**
* DC power across a resistance due to current flowing, W.
 * @param {number} resistance resistance of the circuit in Ohm.
 * @param {number} current the current flowing in the circuit, A.
* @return Power, W.
* @customfunction
*/
function powerDC(resistance, current) {
    var json = httpGetCall("electrical/power/" + resistance + "/" + 0 + "/" + current + "/" + 1);
    var data = JSON.parse(json);
    return data.m_real;
}