Files
laatzen/Common/Metrology/Measurements/MeasurementResults.cs
T

294 lines
12 KiB
C#

using System;
using Xylem.Common.Metrology.Measurements.Consts;
namespace Xylem.Common.Metrology.Measurements
{
/// <summary>
/// Calculate the measurements based on time, start- and stop-volume and overflow.
/// </summary>
public struct MeasurementResults
{
/// <inheritdoc cref="MeasurementResultsStates"/>
public readonly MeasurementResultsStates State;
/// <summary>
/// Channel being needed for later analysis of channel number
/// as feedback to the caller
/// </summary>
public readonly Int32 Channel;
/// <summary>
/// Accumulated overflow volume in cubic-meters,
/// the DUT internal range can overflow due to
/// the limitation of the number or the display resolution.
/// </summary>
public readonly Double AccuDutOverflowVolumeCm;
/// <summary>
/// Accumulated overflow time in seconds,
/// the number-range can cause an timer overflow.
/// </summary>
public readonly Double AccuDutOverflowTimeS;
/// <summary>
/// DUT start record
/// </summary>
public readonly IMeasurementRecord DutStartRecord;
/// <summary>
/// DUT end record
/// </summary>
public readonly IMeasurementRecord DutStopRecord;
/// <summary>
/// Volume = (End Volume + overflow) - Start Volume in cubic meters,
/// real DUT measurement uncorrected
/// </summary>
public readonly Double DutVolumeCm;
/// <summary>
/// DUT measurement corrected if REF time is given
/// </summary>
public readonly Double CorrectedDutVolumeCm;
/// <summary>
/// REF volume in cubic meters
/// </summary>
public readonly Double? RefVolumeCm;
/// <summary>
/// Time = (End time + overflow) - Start time in seconds,
/// real DUT measurement uncorrected
/// </summary>
public readonly Double DutTimeS;
/// <summary>
/// REF time in seconds
/// </summary>
public readonly Double? RefTimeS;
/// <summary>
/// Flow rate in cubic meters per hour based on DUT
/// </summary>
public readonly Double? DutFlowRateCmPh;
/// <summary>
/// Flow rate in cubic meters per hour based on REF
/// </summary>
public readonly Double? RefFlowRateCmPh;
/// <summary>
/// Scale factor between REF volume and DUT volume
/// this will use the <see cref="CorrectedDutVolumeCm"/>
/// /// </summary>
public readonly Double? ScaleFactorRefToDut;
/// <summary>
/// Deviation between DUT volume and REF volume relative,
/// this will use the <see cref="CorrectedDutVolumeCm"/>
/// </summary>
public readonly Double? DeviationDutToRefRel;
/// <summary>
/// Deviation between DUT volume and REF volume in percent,
/// this will use the <see cref="CorrectedDutVolumeCm"/>
/// </summary>
public readonly Double? DeviationDutToRefPer;
/// <summary>
/// Calculation of measurement results, overflow will be taken into account:
/// - DutVolumeCm in cubic meters:
/// DUT volume measurement uncorrected, direct measurement from the DUT,
/// - CorrectedDutVolumeCm in cubic meters:
/// equals the uncorrected DutVolumeCm if REF time is unknown,
/// else it is the volume calculated from the DUT flow-rate with the REF time,
/// - DutTimeS in seconds:
/// DUT time measurement,
/// - DutFlowRateCmPh in cubic meters per hour:
/// based on DUT volume and DUT time uncorrected,
/// - RefFlowRateCmPh in cubic meters per hour:
/// REF volume and REF time being used,
/// - ScaleFactorRefToDut unit less:
/// calculated with absolute and corrected DUT volume and REF volume
/// needed for calibration, is higher than one if DUT volume is less than the REF volume,
/// can be directly used to calculate the new scale by multiplication with the current scale,
/// - DeviationDutToRefRel unit less:
/// calculated with absolute and corrected DUT volume and REF volume
/// deviation needed for verification relative of DUT to REF,
/// - DeviationDutToRefPer in percent:
/// calculated with absolute and corrected DUT volume and REF volume
/// deviation needed for verification of DUT to REF in percent.
/// </summary>
/// <history date="07-Nov-2018" author="Thomas Wiedebusch">
/// - Scale factor calculation added,
/// - Deviation calculation added.
/// </history>
/// <history date="03-Nov-2018" author="Thomas Wiedebusch">
/// - Flow rate calculation added,
/// - Slot added.
/// </history>
/// <history date="19-Dec-2018" author="Thomas Wiedebusch">
/// - RefVolumeCm, RefTimeSS and absVolumeQm added,
/// - Slot removed.
/// </history>
/// <history date="22-Jan-2019" author="Thomas Wiedebusch">
/// - FlowRateQmPh split to DutFlowRateCmPh and RefFlowRateCmPh,
/// - CorrectedDutVolumeCm added,
/// - input channel compared with start and end record channels.
/// </history>
/// <history date="27-Feb-2019" author="Thomas Wiedebusch">
/// - State implemented.
/// </history>
/// <history date="01-Mar-2019" author="Thomas Wiedebusch">
/// - Check and states for DUT time zero, DUT volume zero, over flow volume below zero,
/// start and end record time below zero.
/// </history>
/// <history date="04-Mar-2019" author="Thomas Wiedebusch">
/// - Supported static start/stop with DUT- and REF-volume, this is time independent but the
/// DUT- and over-flow-times shouldn't be negative or zero.
/// - Meter renamed to DUT (Device under Test).
/// </history>
/// <param name="dutStartRecord">Start of measurement record set </param>
/// <param name="dutStopRecord">End of measurement record set </param>
/// <param name="accuDutOverflowVolumeCm">accumulated overflow volume, will add to result volume</param>
/// <param name="accuDutOverflowTimeS">accumulated overflow time, will add to result time</param>
/// <param name="refTimeS">measured REF time</param>
/// <param name="refVolumeCm">REF volume</param>
/// <param name="requiredChannel">physical channel of measurement path (0 for main measurement)</param>
public MeasurementResults(IMeasurementRecord dutStartRecord, IMeasurementRecord dutStopRecord,
Double accuDutOverflowVolumeCm = 0.0, Double accuDutOverflowTimeS = 0.0, Double? refVolumeCm = null,
Double? refTimeS = null, Int32 requiredChannel = 0)
{
//backup input
DutStartRecord = dutStartRecord;
DutStopRecord = dutStopRecord;
AccuDutOverflowVolumeCm = accuDutOverflowVolumeCm;
AccuDutOverflowTimeS = accuDutOverflowTimeS;
RefVolumeCm = refVolumeCm;
RefTimeS = refTimeS;
Channel = requiredChannel;
//initialize
State = MeasurementResultsStates.Okay;
DutVolumeCm = 0.0;
DutTimeS = 0;
CorrectedDutVolumeCm = 0.0;
DeviationDutToRefPer = null;
DeviationDutToRefRel = null;
ScaleFactorRefToDut = null;
DutFlowRateCmPh = null;
RefFlowRateCmPh = null;
//safety issue for channel input, has to be identical
if (requiredChannel != DutStartRecord.GetChannel() || requiredChannel != DutStopRecord.GetChannel())
{
State = MeasurementResultsStates.WrongChannel;
return;
}
//build the measured volume
DutVolumeCm = (DutStopRecord.GetVolumeCm() + accuDutOverflowVolumeCm) - DutStartRecord.GetVolumeCm();
//assume a correct measurement for static start/stop procedure, in case of flying start/stop
//this is going to be overwritten if REF time is set
CorrectedDutVolumeCm = DutVolumeCm;
//Flow-direction can be negative because of hardware assembly of sensors
var absDutVolumeCm = Math.Abs(CorrectedDutVolumeCm);
//calculate the measurement time
DutTimeS = (DutStopRecord.GetTimeS() + accuDutOverflowTimeS) - DutStartRecord.GetTimeS();
//a negative overflow time is impossible due to the physics
if (accuDutOverflowTimeS < 0.0)
{
State = MeasurementResultsStates.InvalidOverFlowTime;
return;
}
//a negative time is impossible due to the physics
if ( DutTimeS < 0.0 || DutStopRecord.GetTimeS() < 0.0 || DutStartRecord.GetTimeS() < 0.0 )
{
State = MeasurementResultsStates.InvalidDutTime;
return;
}
//the <= will be used to avoid floating point comparison with == 0.0 warning message,
//if the DUT time is zero, there might be an error on filling the record
if (Math.Abs(DutTimeS) <= 0.0)
{
State = MeasurementResultsStates.ZeroDutTime;
return;
}
//DUT flow-rate in cubic meters per second
var dutFlowRateCmPs = DutVolumeCm / DutTimeS;
//finally the DUT flow-rate in cubic meters per hour uncorrected
DutFlowRateCmPh = dutFlowRateCmPs * 3600;
//the <= will be used to avoid floating point comparison with == 0.0 warning message,
if (absDutVolumeCm <= 0.0)
{
State = MeasurementResultsStates.ZeroDutVolume;
return;
}
//TODO ref volume can not be null anymore
//for static start/stop the scale factor can be calculated
//further calculation cannot be executed if REF volume is unknown
if (!RefVolumeCm.HasValue)
{
return;
}
//Flow-direction can be negative because of hardware assembly of sensors
var absRefVolumeCm = Math.Abs(RefVolumeCm.Value);
//the <= will be used to avoid floating point comparison with == 0.0 warning message
if (absRefVolumeCm <= 0.0)
{
State = MeasurementResultsStates.ZeroRefVolume;
return;
}
//calculate scale for static start/stop
ScaleFactorRefToDut = absRefVolumeCm / absDutVolumeCm;
DeviationDutToRefRel = 0.0;
DeviationDutToRefPer = 0.0;
//return with status okay if nothing else to calculate
if (!refTimeS.HasValue)
{
return;
}
//the <= will be used to avoid floating point comparison with == 0.0 warning message,
if (Math.Abs(refTimeS.Value) <= 0.0)
{
State = MeasurementResultsStates.ZeroRefTime;
return;
}
//a negative time is impossible due to the physics
if (refTimeS.Value < 0.0)
{
State = MeasurementResultsStates.InvalidRefTime;
return;
}
//for flying start/stop usage of REF time is required to correct the DUT volume
//take the ultra precise REF time to recalculate the DutVolumeCm via the constant flow rate
CorrectedDutVolumeCm = dutFlowRateCmPs * refTimeS.Value;
//Flow-direction can be negative because of hardware assembly of sensors, recalculate
//the corrected abs value
absDutVolumeCm = Math.Abs(CorrectedDutVolumeCm);
//calculate the REF flow rate
RefFlowRateCmPh = RefVolumeCm.Value / refTimeS.Value * 3600;
//final calculation of scale and deviation
ScaleFactorRefToDut = absRefVolumeCm / absDutVolumeCm;
DeviationDutToRefRel = (absDutVolumeCm - absRefVolumeCm) / absRefVolumeCm;
DeviationDutToRefPer = DeviationDutToRefRel * 100;
}
}
}