boon-metrology 1.0
C-based Applications for Smart Grid
Loading...
Searching...
No Matches
boon_metrology.hpp
1#pragma ONCE
2
3#include "boon_metrology.h"
4#include "nanoutil.h"
5#include "cring_buffer.h"
6#include <cstdint>
7#include <vector>
8#include <cstddef>
9#include <mutex>
10#include <random>
11
12#define MAX_ERROR_STRING 256
13
14class CBoonNano;
15
16#ifdef __cplusplus
17
18#define MS_NOT_CONFIGURED 0
19#define MS_IS_CONFIGURED 1
20#define MS_TRAINING_IN_PROGRESS 2
21#define MS_READY_TO_USE 3
22
23#define NUM_USE_CASE 4
24#define MAX_USE_CASE_NAME_LENGTH 50
25#define MAX_MODEL_STATUS_STRING_LENGTH 2500
26#define NUM_DHC_RANDOM_SAMPLE 1000
27
28// Turn a C struct into a class
29class CMetrologySample : public MetrologySample {
30public:
31 // init this class from a struct coming from the C code
32 CMetrologySample();
33 explicit CMetrologySample(const MetrologySample& StructForInit);
34 CMetrologySample& operator=(const CMetrologySample& rhs);
35};
36
37class CMetrologyUseCase {
38public:
39 CMetrologyUseCase(const char* UseCaseName);
40 ~CMetrologyUseCase();
41 const char* GetErrorMessage() { return m_ErrorStringBuffer; };
42
43protected:
44 void SetErrorMessage(const char* fmt, ...);
45 bool IsConfigured() { return m_ModelStatus >= MS_IS_CONFIGURED; }
46 bool IsReadyToUse() { return m_ModelStatus == MS_READY_TO_USE; }
47 bool TrainingInProgress() { return m_ModelStatus == MS_TRAINING_IN_PROGRESS; }
48 float GetTrainingProgress();
49 virtual const char* GetStatus();
50
51 virtual bool Configure(uint64_t NumTrainingSamples);
52 virtual bool Serialize(const char* VarPath, const char* ThisVarName, struct archive* ar, bool IsLoading);
53 virtual bool PostData(const CMetrologySample& MS);
54
55 virtual void Reset();
56
57 char m_ErrorStringBuffer[MAX_ERROR_STRING];
58 char m_UseCaseName[MAX_USE_CASE_NAME_LENGTH];
59 char m_ModelStatusString[MAX_MODEL_STATUS_STRING_LENGTH];
60 int m_ModelStatus; // MS_NOT_CONFIGURED, MS_TRAINING_IN_PROGRESS, MS_READY_TO_USE
61 uint64_t m_PostedSampleCount; // the number of samples that have been posted to this use case
62 uint64_t m_NumTrainingSamples; // the number of samples required for training
63};
64
65//Power Quality Monitoring Use Case
66class CPowerQualityMonitoring : public CMetrologyUseCase {
67public:
68 CPowerQualityMonitoring();
69 ~CPowerQualityMonitoring();
70 bool Configure(struct PowerQualitySettings* PQS);
71 bool PostData(const CMetrologySample& MS) override;
72 bool Serialize(const char* VarPath, const char* ThisVarName, struct archive* ar, bool IsLoading) override;
73 const char* GetStatus() override;
74 const char* GetDiagnostics(uint16_t ResultsList);
75 bool GetPowerQualityScore(float *QualityScore);
76
77protected:
78 void Reset() override;
79
80 CBoonNano *m_Nano;
81 int m_MostRecentID;
82};
83
84//PV Disaggregation Use Case
85class CPVDisaggregation : public CMetrologyUseCase {
86public:
87 CPVDisaggregation();
88 ~CPVDisaggregation();
89 bool Configure(struct PVDisaggregationSettings* PVDS);
90 bool PostData(const CMetrologySample& MS) override;
91 bool Serialize(const char* VarPath, const char* ThisVarName, struct archive* ar, bool IsLoading) override;
92 const char* GetStatus() override;
93 const char* GetDiagnostics(uint16_t ResultsList);
94
95 bool ResetEstimate();
96 float GetPVEstimate();
97 const char *GetDisaggregation();
98
99protected:
100 void Reset() override;
101 void ComputeDerivativeStatistics();
102 void InitializeStatistics();
103 bool m_isReset;
104
105 CRB::CRingBuffer<float> m_Derivatives;
106 float m_LastValue;
107 float m_Mean, m_StdDev, m_ZValueThresh, m_RunningSum;
108 uint64_t m_SamplesSinceReset;
109};
110
111#define NUM_LOADS 2
112
113class CStateChange {
114public:
115 CStateChange();
116 ~CStateChange();
117 CStateChange& operator=(const CStateChange& rhs);
118
119 // state variables
120 bool m_LoadIsOn;
121 char m_LastChangeTOD[MAX_TIMESTAMP_LENGTH];
122 float m_ActivePower;
123
124protected:
125 void Reset();
126};
127
128//PV Disaggregation Use Case
129class CLoadDisaggregation : public CMetrologyUseCase {
130public:
131 CLoadDisaggregation();
132 ~CLoadDisaggregation();
133 bool Configure(struct LoadDisaggregationSettings* LDS);
134 bool PostData(const CMetrologySample& MS, struct LoadChange *LC);
135 bool Serialize(const char* VarPath, const char* ThisVarName, struct archive* ar, bool IsLoading) override;
136 const char* GetStatus() override;
137 const char* GetDiagnostics(uint16_t ResultsList);
138 const char *GetDisaggregation();
139
140protected:
141 void Reset() override;
142 void GenerateKernel(uint16_t WindowSize);
143 float ConvolveRecentDiffs();
144 bool GetCenterRingBufferPattern(std::vector<float> & Pattern);
145
146 CBoonNano *m_Nano;
147 CRB::CRingBuffer<CMetrologySample> m_RecentMetrologySamples;
148 std::vector<float> m_DetectedPattern;
149 std::vector<float> m_Kernel;
150 bool m_AwaitingThreshCrossing;
151 float m_MaxDetectedConvolution;
152
153 // state variables
154 std::vector<CStateChange> m_StateChange;
155 LoadDisaggregationSettings m_LDS;
156};
157
158struct CapacityData {
159 float P;
160 float Q;
161 float V;
162};
163
164struct LeastSquares {
165 double a;
166 double b;
167 double c;
168};
169
170struct DataStatistics {
171 double Mean;
172 double Min;
173 double Max;
174};
175
176class CDynamicHostingCapacity : public CMetrologyUseCase {
177public:
178 CDynamicHostingCapacity();
179 ~CDynamicHostingCapacity();
180
181 bool Configure(struct DynamicHostingCapacitySettings* DHS);
182 bool PostData(const CMetrologySample& MS) override;
183 bool Serialize(const char* VarPath, const char* ThisVarName, struct archive* ar, bool IsLoading) override;
184 const char* GetStatus() override;
185 const char* GetDiagnostics(uint16_t ResultsList);
186
187 bool GetVoltageEstimatePQ(float DeltaP, float DeltaQ, float *DeltaVEstimate);
188 bool GetVoltageEstimateP(float DeltaP, float *DeltaVEstimate);
189
190protected:
191 bool PushBack(float P, float Q, float V);
192 void Reset() override;
193 uint16_t GetRandomGeometric();
194 bool GenerateDeltaStatsValues(bool isLinear);
195
196 // random sample buffer
197 CRB::CRingBuffer<CapacityData> m_RandomSampleOne;
198 CRB::CRingBuffer<CapacityData> m_RandomSampleTwo;
199
200 uint16_t m_geometric_rand_space_one;
201 uint16_t m_geometric_rand_space_two;
202 uint16_t m_CountOne;
203 uint16_t m_CountTwo;
204
205 std::mt19937 m_Gen;
206 std::uniform_real_distribution<float> m_Distribution;
207
208 // results
209 LeastSquares m_ModelCoefficients;
210 std::vector<DataStatistics> m_Stats;
211};
212
213// Define a structure to for libarchive callbacks
214struct ArchiveBufferStatus {
215 uint8_t* Data;
216 size_t Size;
217};
218
219
220class BoonMetrology {
221public:
222 BoonMetrology(uint16_t SamplesPerSecond);
223 ~BoonMetrology();
224
225 bool ConfigureLoadDisaggregation(struct LoadDisaggregationSettings* LDS);
226 bool ConfigurePVDisaggregation(struct PVDisaggregationSettings* PVDS);
227 bool ConfigureDynamicHostingCapacity(struct DynamicHostingCapacitySettings* DHS);
228 bool ConfigurePowerQualityMonitoring(struct PowerQualitySettings* PQS);
229 void CopyBoonMetrologyConfiguration(struct BoonMetrologyConfiguration* BMC);
230
231 bool IncrementalSerialize(const char* DirPath, void* ReadOrWriteCallback, struct IncrementalArchiveFileStatus* AFS,
232 size_t TransferBufferSize, void* UserData, bool IsLoading);
233 bool SerializeBuffer(uint8_t **Buffer, size_t *BufferLength, bool IsLoading);
234
235 bool PostMetrologySample(const MetrologySample* MS, struct LoadChange* LC);
236
237 void ResetPVDisaggregationEstimate();
238 bool GetPVDisaggregationEstimate(float *PVEstimate);
239
240 const char *GetDisaggregation();
241
242 bool GetPowerQualityScore(float *QualityScore);
243
244 bool GetVoltageEstimatePQ(float DeltaP, float DeltaQ, float *DeltaVEstimate);
245 bool GetVoltageEstimateP(float DeltaP, float *DeltaVEstimate);
246
247 const char* GetStatus();
248 const char* GetStatusPowerQualityMonitoring();
249 const char* GetStatusDynamicHostingCapacity();
250 const char* GetStatusPVDisaggregation();
251 const char* GetStatusLoadDisaggregation();
252
253 const char* GetDiagnostics(uint16_t ResultsList);
254
255 float GetVersion();
256
257 const char *GetErrorMessage();
258
259protected:
260 bool Serialize(struct archive* ar, bool IsLoading);
261 void Reset();
262
263 std::mutex m_Mutex;
264
265 void SetErrorMessage(const char* fmt, ...);
266 void SetDefaultMetrologyConfiguration();
267 struct BoonMetrologyConfiguration m_MetrologyConfiguration;
268 char m_ErrorStringBuffer[MAX_ERROR_STRING];
269 char m_ModelStatusString[MAX_MODEL_STATUS_STRING_LENGTH * NUM_USE_CASE];
270
271 uint16_t m_SamplesPerSecond;
272 CRB::CRingBuffer<CMetrologySample> m_RecentMetrologySamples;
273 CPVDisaggregation* m_PVDisaggregation;
274 CLoadDisaggregation* m_LoadDisaggregation;
275 CPowerQualityMonitoring* m_PQM;
276 CDynamicHostingCapacity* m_DHC;
277};
278
279#endif
Initialize values not being used to 0.
Definition boon_metrology.h:218