Power System Platform  2026w34a-beta
Loading...
Searching...
No Matches
BusForm.cpp
1/*
2 * Copyright (C) 2017 Thales Lima Oliveira <thales@ufu.br>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include <cmath>
19#include <wx/numformatter.h>
20
21#include "BusForm.h"
23#include "../utils/Path.h"
24#include "BusFormArcFlashProtection.h"
26
27namespace
28{
29 wxString FormatArcFlashNumber(double value)
30 {
31 return wxNumberFormatter::ToString(
32 value, 12,
33 wxNumberFormatter::Style_None |
34 wxNumberFormatter::Style_NoTrailingZeroes);
35 }
36
37 wxString FormatArcFlashInput(double value)
38 {
39 return value == 0.0 ? wxString() : FormatArcFlashNumber(value);
40 }
41}
42
43BusForm::BusForm(wxWindow* parent, Bus* bus, wxWindow* workspace) : BusFormBase(parent), m_bus(bus), m_parent(parent), m_workspace(workspace)
44{
45 m_choiceFaultType->SetString(0, _("Three-phase"));
46 m_choiceFaultType->SetString(1, _("Line-to-line"));
47 m_choiceFaultType->SetString(2, _("Double line-to-ground"));
48 m_choiceFaultType->SetString(3, _("Line-to-ground"));
49
50 // Arcflash
51 struct ElectrodeConfiguration
52 {
53 wxString code;
54 wxString image;
55 };
56
57 const std::vector<ElectrodeConfiguration> configurations = {
58 {.code = _("VCB - Vertical, Metal Box"), .image = "VCB.png"},
59 {.code = _("VCBB - Vertical, Insulating Barrier"), .image = "VCBB.png"},
60 {.code = _("HCB - Horizontal, Metal Box"), .image = "HCB.png"},
61 {.code = _("VOA - Vertical, Open Air"), .image = "VOA.png"},
62 {.code = _("HOA - Horizontal, Open Air"), .image = "HOA.png"}
63 };
64
65 const wxSize size = FromDIP(wxSize(32, 32));
66 const wxString imgPath = Paths::GetDataPath() + "/images/arcflash/";
67
68 for (const auto& config : configurations)
69 {
70 wxImage image(imgPath + config.image, wxBITMAP_TYPE_PNG);
71 if (!image.IsOk()) {
72 wxMessageBox(_("Could not load the electrode configuration image:\n") + imgPath + config.image, _("Arc Flash"), wxOK | wxICON_ERROR, this);
73 continue;
74 }
75
76 image.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
77 m_bmpComboBoxConfig->Append(config.code, wxBitmap(image));
78 }
79
80 m_electrodePreview = new BitmapPopup(this);
81 m_electrodePreview->Hide();
82
83 m_textCtrlName->SetValue(bus->GetElectricalData().name);
84 m_textCtrlNomVoltage->SetValue(bus->StringFromDouble(bus->GetElectricalData().nominalVoltage));
85
86 if (bus->GetElectricalData().nominalVoltageUnit == ElectricalUnit::UNIT_V)
87 m_choiceNomVoltage->SetSelection(0);
88 else
89 m_choiceNomVoltage->SetSelection(1);
90
91 m_checkBoxCtrlVoltage->SetValue(bus->GetElectricalData().isVoltageControlled);
92 m_textCtrlCtrlVoltage->SetValue(bus->StringFromDouble(bus->GetElectricalData().controlledVoltage));
93 m_choiceCtrlVoltage->SetSelection(bus->GetElectricalData().controlledVoltageUnitChoice);
94 m_checkBoxSlackBus->SetValue(bus->GetElectricalData().slackBus);
95
96 m_checkBoxFault->SetValue(bus->GetElectricalData().hasFault);
97 switch (bus->GetElectricalData().faultType) {
99 m_choiceFaultType->SetSelection(0);
100 } break;
102 m_choiceFaultType->SetSelection(1);
103 } break;
105 m_choiceFaultType->SetSelection(2);
106 } break;
108 m_choiceFaultType->SetSelection(3);
109 } break;
110 default:
111 break;
112 }
113 switch (bus->GetElectricalData().faultLocation) {
115 m_choiceFaultPlace->SetSelection(0);
116 } break;
118 m_choiceFaultPlace->SetSelection(1);
119 } break;
121 m_choiceFaultPlace->SetSelection(2);
122 } break;
123 default:
124 break;
125 }
126 m_textCtrlFaultResistance->SetValue(bus->StringFromDouble(bus->GetElectricalData().faultResistance));
127 m_textCtrlFaultReactance->SetValue(bus->StringFromDouble(bus->GetElectricalData().faultReactance));
128
129 m_checkBoxPlotData->SetValue(bus->GetElectricalData().plotBus);
130 m_checkBoxStabFault->SetValue(bus->GetElectricalData().stabHasFault);
131 m_textCtrlStabFaultTime->SetValue(bus->StringFromDouble(bus->GetElectricalData().stabFaultTime));
132 m_textCtrlStabFaultLength->SetValue(bus->StringFromDouble(bus->GetElectricalData().stabFaultLength));
133 m_textCtrlStabFaultResistance->SetValue(bus->StringFromDouble(bus->GetElectricalData().stabFaultResistance));
134 m_textCtrlStabFaultReactance->SetValue(bus->StringFromDouble(bus->GetElectricalData().stabFaultReactance));
135
136 EnableCtrlVoltageFields(bus->GetElectricalData().isVoltageControlled);
137 EnableFaultFields(bus->GetElectricalData().hasFault);
138 EnableStabFaultFields(bus->GetElectricalData().stabHasFault);
139
140 m_checkBoxPlotPQData->SetValue(bus->GetElectricalData().plotPQData);
141
142 LoadArcFlashData(m_bus->GetElectricalDataRef().arcFlash);
143
144 m_staticBitmapWarning->Hide();
145
146 GetSizer()->Layout();
147 GetSizer()->Fit(this);
148 SetSize(GetBestSize());
149}
150
151BusForm::~BusForm() {}
152void BusForm::OnButtonCancelClick(wxCommandEvent& event) { EndModal(wxID_CANCEL); }
153void BusForm::OnButtonOKClick(wxCommandEvent& event)
154{
155 BusElectricalData data = m_bus->GetElectricalData();
156 data.name = m_textCtrlName->GetValue();
157 if (!m_bus->DoubleFromString(m_parent, m_textCtrlNomVoltage->GetValue(), data.nominalVoltage,
158 _("Value entered incorrectly in the field \"Rated voltage\".")))
159 return;
160 data.nominalVoltageUnit = m_choiceNomVoltage->GetSelection() == 0 ? ElectricalUnit::UNIT_V : ElectricalUnit::UNIT_kV;
161 data.isVoltageControlled = m_checkBoxCtrlVoltage->GetValue();
162 if (data.isVoltageControlled) {
163 if (!m_bus->DoubleFromString(m_parent, m_textCtrlCtrlVoltage->GetValue(), data.controlledVoltage,
164 _("Value entered incorrectly in the field \"Controlled voltage\".")))
165 return;
166 data.controlledVoltageUnitChoice = m_choiceCtrlVoltage->GetSelection();
167 }
168 data.slackBus = m_checkBoxSlackBus->GetValue();
169
170 data.hasFault = m_checkBoxFault->GetValue();
171 switch (m_choiceFaultType->GetSelection()) {
172 case 0: {
173 data.faultType = FaultData::FAULT_THREEPHASE;
174 } break;
175 case 1: {
176 data.faultType = FaultData::FAULT_2LINE;
177 } break;
178 case 2: {
179 data.faultType = FaultData::FAULT_2LINE_GROUND;
180 } break;
181 case 3: {
182 data.faultType = FaultData::FAULT_LINE_GROUND;
183 } break;
184 }
185
186 switch (m_choiceFaultPlace->GetSelection()) {
187 case 0: {
188 data.faultLocation = FaultData::FAULT_LINE_A;
189 } break;
190 case 1: {
191 data.faultLocation = FaultData::FAULT_LINE_B;
192 } break;
193 case 2: {
194 data.faultLocation = FaultData::FAULT_LINE_C;
195 } break;
196 }
197
198 if (!m_bus->DoubleFromString(m_parent, m_textCtrlFaultResistance->GetValue(), data.faultResistance,
199 _("Value entered incorrectly in the field \"Fault resistance\".")))
200 return;
201
202 if (!m_bus->DoubleFromString(m_parent, m_textCtrlFaultReactance->GetValue(), data.faultReactance,
203 _("Value entered incorrectly in the field \"Fault reactance\".")))
204 return;
205
206 data.plotBus = m_checkBoxPlotData->GetValue();
207 data.stabHasFault = m_checkBoxStabFault->GetValue();
208
209 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultTime->GetValue(), data.stabFaultTime,
210 _("Value entered incorrectly in the field \"Time\".")))
211 return;
212
213 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultLength->GetValue(), data.stabFaultLength,
214 _("Value entered incorrectly in the field \"Fault lenght\".")))
215 return;
216
217 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultResistance->GetValue(), data.stabFaultResistance,
218 _("Value entered incorrectly in the field \"Fault resistence (stability)\".")))
219 return;
220
221 if (!m_bus->DoubleFromString(m_parent, m_textCtrlStabFaultReactance->GetValue(), data.stabFaultReactance,
222 _("Value entered incorrectly in the field \"Fault reactance (stability)\".")))
223 return;
224
225 data.plotPQData = m_checkBoxPlotPQData->GetValue();
226
227 m_bus->SetElectricalData(data);
228
229 if (data.stabHasFault)
230 m_bus->SetDynamicEvent(true);
231 else
232 m_bus->SetDynamicEvent(false);
233
234 auto& arcFlash = m_bus->GetElectricalDataRef().arcFlash;
235
236 ArcFlashData afData = arcFlash;
237
238 if (!ValidateAndGetArcFlashData(afData))
239 return;
240
241 arcFlash = std::move(afData);
242
243 EndModal(wxID_OK);
244}
245
246void BusForm::OnNominalVoltageChoice(wxCommandEvent& event) { UpdateChoiceBoxes(); }
247void BusForm::OnFaultTypeChoice(wxCommandEvent& event) { UpdateChoiceBoxes(); }
248void BusForm::OnControlledVoltageClick(wxCommandEvent& event)
249{
250 EnableCtrlVoltageFields(m_checkBoxCtrlVoltage->GetValue());
251}
252void BusForm::OnInsertFaultClick(wxCommandEvent& event) { EnableFaultFields(m_checkBoxFault->GetValue()); }
253void BusForm::OnInsertStabFaultClick(wxCommandEvent& event) { EnableStabFaultFields(m_checkBoxStabFault->GetValue()); }
254void BusForm::EnableCtrlVoltageFields(bool enable)
255{
256 m_textCtrlCtrlVoltage->Enable(enable);
257 m_choiceCtrlVoltage->Enable(enable);
258
259 UpdateChoiceBoxes();
260}
261
262void BusForm::EnableFaultFields(bool enable)
263{
264 m_choiceFaultType->Enable(enable);
265 m_choiceFaultPlace->Enable(enable);
266 m_textCtrlFaultReactance->Enable(enable);
267 m_textCtrlFaultResistance->Enable(enable);
268 m_staticTextPU_1->Enable(enable);
269 m_staticTextPU_2->Enable(enable);
270
271 UpdateChoiceBoxes();
272}
273
274void BusForm::EnableStabFaultFields(bool enable)
275{
276 m_textCtrlStabFaultTime->Enable(enable);
277 m_textCtrlStabFaultLength->Enable(enable);
278 m_staticTextS_1->Enable(enable);
279 m_staticTextS_2->Enable(enable);
280 m_textCtrlStabFaultReactance->Enable(enable);
281 m_textCtrlStabFaultResistance->Enable(enable);
282 m_staticTextPU_3->Enable(enable);
283 m_staticTextPU_4->Enable(enable);
284}
285
286void BusForm::UpdateChoiceBoxes()
287{
288 switch (m_choiceFaultType->GetSelection()) {
289 case 0: // three-phase
290 {
291 m_choiceFaultPlace->Enable(false);
292 } break;
293 case 1: // line-to-line
294 case 2: // double line-to-line
295 {
296 if (m_checkBoxFault->GetValue()) m_choiceFaultPlace->Enable(true);
297 m_choiceFaultPlace->SetString(0, _("Lines AB"));
298 m_choiceFaultPlace->SetString(1, _("Lines BC"));
299 m_choiceFaultPlace->SetString(2, _("Lines CA"));
300 } break;
301 case 3: // line-to-ground
302 {
303 if (m_checkBoxFault->GetValue()) m_choiceFaultPlace->Enable(true);
304 m_choiceFaultPlace->SetString(0, _("Line A"));
305 m_choiceFaultPlace->SetString(1, _("Line B"));
306 m_choiceFaultPlace->SetString(2, _("Line C"));
307 } break;
308 default:
309 break;
310 }
311 switch (m_choiceNomVoltage->GetSelection()) {
312 case 0: {
313 m_choiceCtrlVoltage->SetString(1, _("V"));
314 } break;
315 case 1: {
316 m_choiceCtrlVoltage->SetString(1, _("kV"));
317 } break;
318 default:
319 break;
320 }
321}
322
323bool BusForm::ValidateAndGetArcFlashData(ArcFlashData& data)
324{
325 ArcFlashData result = data;
326
327 auto showError = [&](const wxString& message) {
328 wxMessageBox(message, _("Invalid data"),
329 wxOK | wxICON_WARNING, m_parent);
330 return false;
331 };
332
333 auto readPositiveValue = [&](wxTextCtrl* control, double& value,
334 const wxString& field) {
335 if (!m_bus->DoubleFromString(
336 m_parent, control->GetValue(), value,
337 wxString::Format(
338 _("Value entered incorrectly in the field \"%s.\""),
339 field)))
340 return false;
341
342 if (!std::isfinite(value) || value <= 0.0)
343 return showError(wxString::Format(
344 _("The field \"%s\" must be greater than zero."),
345 field));
346
347 return true;
348 };
349
350 // Electrode configuration.
351 const int configSelection = m_bmpComboBoxConfig->GetSelection();
352
353 if (configSelection < 0 ||
354 configSelection >= static_cast<int>(m_bmpComboBoxConfig->GetCount()) ||
355 configSelection > static_cast<int>(ArcFlashElectrodeConfig::HOA))
356 return showError(_("Select a valid electrode configuration."));
357
358 result.electrodeConfig =
359 static_cast<ArcFlashElectrodeConfig>(configSelection);
360
361 // Common input parameters.
362 if (!readPositiveValue(m_textCtrlGap, result.conductorGap,
363 _("Electrode gap (mm)")))
364 return false;
365
366 if (!readPositiveValue(m_textCtrlWorkDist, result.workingDistance,
367 _("Working distance (mm)")))
368 return false;
369
370 // Enclosure dimensions are required only for enclosed configurations.
371 if (IsEnclosedConfig(result.electrodeConfig)) {
372 if (!readPositiveValue(m_textCtrlHeight, result.enclosureHeight,
373 _("Height (mm)")))
374 return false;
375
376 if (!readPositiveValue(m_textCtrlWidth, result.enclosureWidth,
377 _("Width (mm)")))
378 return false;
379
380 if (!readPositiveValue(m_textCtrlGap525558, result.enclosureDepth,
381 _("Depth (mm)")))
382 return false;
383 }
384 else {
385 result.enclosureHeight = 0.0;
386 result.enclosureWidth = 0.0;
387 result.enclosureDepth = 0.0;
388 }
389
390 // Invalidate calculated results only if relevant inputs have changed.
391 const bool inputsChanged =
392 result.electrodeConfig != data.electrodeConfig ||
393 result.conductorGap != data.conductorGap ||
394 result.workingDistance != data.workingDistance ||
395 (IsEnclosedConfig(result.electrodeConfig) &&
396 (result.enclosureHeight != data.enclosureHeight ||
397 result.enclosureWidth != data.enclosureWidth ||
398 result.enclosureDepth != data.enclosureDepth));
399
400 if (inputsChanged) {
401 result.calculated = false;
402 result.iarc_kA = 0.0;
403 result.iarcMin_kA = 0.0;
404 result.incidentEnergy_Jcm2 = 0.0;
405 result.incidentEnergy_calcm2 = 0.0;
406 result.arcFlashBoundary_mm = 0.0;
407 result.usedReducedCurrent = false;
408 result.warnings.clear();
409 }
410
411 // Preserve data.enabled: there is no corresponding control in the
412 // interface shown.
413 data = std::move(result);
414
415 if (inputsChanged) {
416 m_staticTextEnergy->SetLabel(_("E = -- J/cm²"));
417 m_staticTextAFB->SetLabel(_("AFB = -- mm"));
418 m_staticBitmapWarning->Hide();
419 m_panelArcFlash->Layout();
420 }
421
422 return true;
423}
424
425void BusForm::LoadArcFlashData(const ArcFlashData& data)
426{
427 // Electrode configuration.
428 const int configSelection = static_cast<int>(data.electrodeConfig);
429
430 if (configSelection >= 0 &&
431 configSelection < static_cast<int>(m_bmpComboBoxConfig->GetCount()))
432 m_bmpComboBoxConfig->SetSelection(configSelection);
433 else if (m_bmpComboBoxConfig->GetCount() > 0)
434 m_bmpComboBoxConfig->SetSelection(0);
435
436 // Input parameters.
437 m_textCtrlGap->ChangeValue(FormatArcFlashInput(data.conductorGap));
438 m_textCtrlWorkDist->ChangeValue(FormatArcFlashInput(data.workingDistance));
439 m_textCtrlHeight->ChangeValue(FormatArcFlashInput(data.enclosureHeight));
440 m_textCtrlWidth->ChangeValue(FormatArcFlashInput(data.enclosureWidth));
441 m_textCtrlGap525558->ChangeValue(FormatArcFlashInput(data.enclosureDepth));
442
443 UpdateArcFlashEnclosureFields();
444
445 // Calculated results.
446 if (data.calculated) {
447 m_staticTextEnergy->SetLabel(wxString::Format(
448 _("E = %s J/cm²"),
449 FormatArcFlashNumber(data.incidentEnergy_Jcm2)));
450
451 m_staticTextAFB->SetLabel(wxString::Format(
452 _("AFB = %s mm"),
453 FormatArcFlashNumber(data.arcFlashBoundary_mm)));
454 }
455 else {
456 m_staticTextEnergy->SetLabel(_("E = -- J/cm²"));
457 m_staticTextAFB->SetLabel(_("AFB = -- mm"));
458 }
459
460 // Display the warning icon and its tooltip when applicable.
461 wxString warningText;
462
463 if (data.calculated) {
464 for (const auto& warning : data.warnings) {
465 if (!warningText.IsEmpty())
466 warningText += "\n";
467
468 warningText += warning;
469 }
470
471 if (data.usedReducedCurrent) {
472 if (!warningText.IsEmpty())
473 warningText += "\n";
474
475 warningText += _("Reduced-current calculation was used.");
476 }
477 }
478
479 m_staticBitmapWarning->SetToolTip(warningText);
480 m_staticBitmapWarning->Show(!warningText.IsEmpty());
481
482 m_panelArcFlash->Layout();
483 m_panelArcFlash->Refresh();
484}
485
486void BusForm::UpdateArcFlashEnclosureFields()
487{
488 const int selection = m_bmpComboBoxConfig->GetSelection();
489
490 const bool isValidSelection =
491 selection >= 0 &&
492 selection <= static_cast<int>(ArcFlashElectrodeConfig::HOA);
493
494 const bool isEnclosed = isValidSelection &&
495 IsEnclosedConfig(
496 static_cast<ArcFlashElectrodeConfig>(selection));
497
498 m_staticTextHeight->Enable(isEnclosed);
499 m_textCtrlHeight->Enable(isEnclosed);
500
501 m_staticTextWidth->Enable(isEnclosed);
502 m_textCtrlWidth->Enable(isEnclosed);
503
504 m_staticTextDepth->Enable(isEnclosed);
505 m_textCtrlGap525558->Enable(isEnclosed);
506}
507
508void BusForm::OnMouseEnterElectrodeConfig(wxMouseEvent& event)
509{
510 wxString configStr = "";
511 if (m_bmpComboBoxConfig->GetSelection() == -1)
512 {
513 event.Skip();
514 return;
515 }
516 else
517 {
518 switch (m_bmpComboBoxConfig->GetSelection())
519 {
520 case 0:
521 configStr = wxT("VCB");
522 break;
523 case 1:
524 configStr = wxT("VCBB");
525 break;
526 case 2:
527 configStr = wxT("HCB");
528 break;
529 case 3:
530 configStr = wxT("VOA");
531 break;
532 case 4:
533 configStr = wxT("HOA");
534 break;
535 }
536 }
537 const wxString imgPath = Paths::GetDataPath() + "/images/arcflash/" + configStr + ".png";
538 wxImage image(imgPath, wxBITMAP_TYPE_PNG);
539 const wxSize size = FromDIP(wxSize(300, 300));
540 image.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH);
541 m_electrodePreview->SetBitmap(wxBitmap(image));
542
543 m_electrodePreview->Position(wxGetMousePosition() + wxPoint(10, 10), wxSize(0, 0));
544
545 m_electrodePreview->Show();
546 m_electrodePreview->Raise();
547
548 event.Skip();
549}
550
551void BusForm::OnMouseLeaveElectrodeConfig(wxMouseEvent& event)
552{
553 m_electrodePreview->Hide();
554 event.Skip();
555}
556
557void BusForm::OnMouseMotionElectrodeConfig(wxMouseEvent& event)
558{
559 const wxPoint mouse = wxGetMousePosition();
560 const wxSize popupSize = m_electrodePreview->GetSize();
561
562 wxPoint pos = mouse + FromDIP(wxPoint(10, 10));
563
564 const wxRect screen = wxGetClientDisplayRect();
565
566 if (pos.x + popupSize.x > screen.GetRight())
567 pos.x = mouse.x - popupSize.x - FromDIP(10);
568
569 if (pos.y + popupSize.y > screen.GetBottom())
570 pos.y = mouse.y - popupSize.y - FromDIP(10);
571
572 m_electrodePreview->SetPosition(pos);
573}
574
575BitmapPopup::BitmapPopup(wxWindow* parent) : wxPopupWindow(parent, wxBORDER_SIMPLE)
576{
577 m_bitmap = new wxStaticBitmap(this, wxID_ANY, wxNullBitmap);
578}
579
580void BitmapPopup::SetBitmap(const wxBitmap& bitmap)
581{
582 m_bitmap->SetBitmap(bitmap);
583 m_bitmap->SetSize(bitmap.GetSize());
584 SetSize(bitmap.GetSize());
585}
586void BusForm::OnnConfigProtectionButtonClick(wxCommandEvent& event)
587{
588 double basePower = 100e6; // Default base power is 100 MVA
589 if (auto* workspace = dynamic_cast<Workspace*>(m_workspace))
590 {
591 basePower = m_bus->GetValueFromUnit(
592 workspace->GetProperties()->GetSimulationPropertiesData().basePower,
593 workspace->GetProperties()->GetSimulationPropertiesData().basePowerUnit);
594 }
595
596 BusFormArcFlashProtection protectionConfig(this, m_bus, basePower);
597 if (protectionConfig.ShowModal() == wxID_OK)
598 {
599 // Handle the protection configuration here
600 }
601}
602void BusForm::OnCBArcFlashConfigSelected(wxCommandEvent& event)
603{
604 UpdateArcFlashEnclosureFields();
605}
@ FAULT_LINE_GROUND
@ FAULT_2LINE_GROUND
Node for power elements. All others power elements are connected through this.
Definition Bus.h:135
static wxString StringFromDouble(double value, int minDecimal=1, int maxDecimals=13)
Convert a double value to string.
Definition Element.cpp:548