Antares Simulator
Power System Simulator
xcast.hxx
1 /*
2  * Copyright 2007-2025, RTE (https://www.rte-france.com)
3  * See AUTHORS.txt
4  * SPDX-License-Identifier: MPL-2.0
5  * This file is part of Antares-Simulator,
6  * Adequacy and Performance assessment for interconnected energy networks.
7  *
8  * Antares_Simulator is free software: you can redistribute it and/or modify
9  * it under the terms of the Mozilla Public Licence 2.0 as published by
10  * the Mozilla Foundation, either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * Antares_Simulator is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * Mozilla Public Licence 2.0 for more details.
17  *
18  * You should have received a copy of the Mozilla Public Licence 2.0
19  * along with Antares_Simulator. If not, see <https://opensource.org/license/mpl-2-0/>.
20  */
21 #ifndef __ANTARES_WINDOWS_XCAST_XCAST_HXX__
22 #define __ANTARES_WINDOWS_XCAST_XCAST_HXX__
23 
24 #include <assert.h>
25 #include <wx/statline.h>
26 #include "../../toolbox/components/datagrid/renderer/area/xcast-k.h"
27 #include "../../toolbox/components/datagrid/renderer/area/xcast-coefficients.h"
28 #include "../../toolbox/components/datagrid/renderer/area/xcast-translation.h"
29 #include "../../toolbox/components/datagrid/renderer/area/xcast-conversion.h"
30 #include "../../toolbox/validator.h"
31 #include "../../application/study.h"
32 #include "../../windows/inspector.h"
33 #include <ui/common/component/panel.h>
34 
35 namespace Antares::Window
36 {
37 template<enum Data::TimeSeriesType T>
38 XCast<T>::XCast(wxWindow* parent, Toolbox::InputSelector::Area* notifier):
39  wxPanel(parent, wxID_ANY),
40  pNotifier(notifier),
41  pArea(nullptr)
42 {
43  assert(pNotifier != NULL);
44 
45  // Grids
47 
48  auto* hsizer = new wxBoxSizer(wxVERTICAL);
49  this->SetSizer(hsizer);
50 
51  auto* notebook = new Component::Notebook(this, Component::Notebook::orTop);
52  pNotebook = notebook;
53  notebook->theme(Component::Notebook::themeLight);
54 
56  notebook,
58  pGridCoeffs = grid;
59  pPageGeneral = notebook->add(grid, wxT("coeffs"), wxT("Coefficients"));
60 
62  notebook,
63  new Component::Datagrid::Renderer::XCastK<T>(this, notifier));
64  pPageDailyProfile = notebook->add(grid, wxT("Daily profile"));
65 
67  notebook,
69  pPageDailyProfile = notebook->add(grid, wxT("Translation"));
70 
71  wxSizer* sizer = new wxBoxSizer(wxVERTICAL);
72  hsizer->Add(sizer, 0, wxALL | wxEXPAND);
73  hsizer->Add(notebook, 1, wxALL | wxEXPAND);
74 
75  {
76  auto* pGridSizer = new wxFlexGridSizer(2, 0, 0);
77  wxStaticText* t;
78  wxTextCtrl* edit;
79 
80  // Capacity
81  {
82  t = new wxStaticText(this,
83  wxID_ANY,
84  wxT("Capacity : "),
85  wxDefaultPosition,
86  wxDefaultSize,
87  wxALIGN_RIGHT);
88  wxFont f = t->GetFont();
89  f.SetWeight(wxFONTWEIGHT_BOLD);
90  t->SetFont(f);
91  pGridSizer->Add(t, 0, wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
92 
93  edit = new wxTextCtrl(this,
94  wxID_ANY,
95  wxEmptyString,
96  wxDefaultPosition,
97  wxSize(-1, 22),
98  0,
99  Toolbox::Validator::Numeric());
100  pInstalledCapacity = edit;
101  pGridSizer->Add(edit, 1, wxALL | wxEXPAND, 1);
102  }
103 
104  // Probability distribution
105  t = new wxStaticText(this,
106  wxID_ANY,
107  wxT("Distribution : "),
108  wxDefaultPosition,
109  wxDefaultSize,
110  wxALIGN_RIGHT);
111  pGridSizer->Add(t, 0, wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
112 
113  wxArrayString list;
114  for (uint i = 1; i < Data::XCast::dtMax; ++i)
115  {
116  list.Add(
118  }
119  wxChoice* ch = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 22), list);
120  pDistribution = ch;
121  pGridSizer->Add(ch, 1, wxALL | wxEXPAND, 1);
122 
123  // TS Translation
124  t = new wxStaticText(this,
125  wxID_ANY,
126  wxT("Translation : "),
127  wxDefaultPosition,
128  wxDefaultSize,
129  wxALIGN_RIGHT);
130  pGridSizer->Add(t, 0, wxRIGHT | wxALIGN_RIGHT | wxALIGN_CENTER_VERTICAL);
131 
132  // see XCast::CStringToTSTranslationUse if you modifiy this list
133  list.Clear();
134  list.Add(wxT("Do not use"));
135  list.Add(wxT("Add BEFORE scaling"));
136  list.Add(wxT("Add AFTER scaling"));
137  useTranslation = new wxChoice(this, wxID_ANY, wxDefaultPosition, wxSize(-1, 22), list);
138  pGridSizer->Add(useTranslation, 1, wxALL | wxEXPAND, 1);
139 
140  wxBoxSizer* divider = new wxBoxSizer(wxHORIZONTAL);
141  divider->Add(20, 5);
142  divider->Add(pGridSizer, 0, wxALL | wxEXPAND, 7);
143  divider->Add(
144  new wxStaticLine(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxLI_VERTICAL),
145  0,
146  wxALL | wxEXPAND,
147  5);
148  divider->AddSpacer(15);
149  wxBoxSizer* transferSizer = new wxBoxSizer(wxVERTICAL);
150 
151  useConversion = new wxCheckBox(this, wxID_ANY, wxT(" Use Conversion"));
152  transferSizer->AddSpacer(15);
153  transferSizer->Add(useConversion, 0, wxALL | wxEXPAND);
154 
155  divider->Add(transferSizer, 1, wxALL | wxEXPAND);
156 
157  sizer->Add(divider, 1, wxALL | wxEXPAND);
158 
159  pDistribution->Connect(pDistribution->GetId(),
160  wxEVT_COMMAND_CHOICE_SELECTED,
161  wxCommandEventHandler(XCast<T>::onUpdateDistribution),
162  NULL,
163  this);
164  useTranslation->Connect(useTranslation->GetId(),
165  wxEVT_COMMAND_CHOICE_SELECTED,
166  wxCommandEventHandler(XCast<T>::onUpdateTSTranslationUse),
167  NULL,
168  this);
169  useConversion->Connect(useConversion->GetId(),
170  wxEVT_COMMAND_CHECKBOX_CLICKED,
171  wxCommandEventHandler(XCast<T>::onUpdateConversion),
172  NULL,
173  this);
174  }
175 
176  {
178  wxSizer* ss = new wxBoxSizer(wxVERTICAL);
179 
181  panel,
183  ss->Add(grid, 0, wxALL | wxEXPAND);
184  ss->SetItemMinSize(grid, 100, 150);
185 
186  panel->SetSizer(ss);
187  pPageTranslation = notebook->add(panel, wxT("Conversion"));
188  }
189 
190  // Events
191  OnStudyClosed.connect(this, &XCast<T>::onStudyClosed);
192  if (notifier)
193  {
194  notifier->onAreaChanged.connect(this, &XCast<T>::onAreaChanged);
195  }
196  pInstalledCapacity->Connect(pInstalledCapacity->GetId(),
197  wxEVT_COMMAND_TEXT_UPDATED,
198  wxCommandEventHandler(XCast<T>::onInstalledCapacityChanged),
199  NULL,
200  this);
201 
202  // Select the page by default
203  pPageGeneral->select();
204 }
205 
206 template<enum Data::TimeSeriesType T>
208 {
209  ObserverAncestorType::destroyBoundEvents();
210 }
211 
212 template<enum Data::TimeSeriesType T>
213 void XCast<T>::onUpdateDistribution(wxCommandEvent& evt)
214 {
215  wxChoice* obj = (wxChoice*)evt.GetEventObject();
216  if (obj and pArea)
217  {
218  Data::XCast* xcast = pArea->xcastData<T>();
219  if (!xcast)
220  {
221  return;
222  }
223 
224  YString s;
225  wxStringToString(obj->GetStringSelection(), s);
227  if (d != Data::XCast::dtNone and d != xcast->distribution)
228  {
229  xcast->distribution = d;
230  MarkTheStudyAsModified();
231  Window::Inspector::Refresh();
232  if (pGridCoeffs and pNotebook->selected()
233  and pNotebook->selected()->name() == wxT("coeffs"))
234  {
235  pGridCoeffs->Refresh();
236  }
237  }
238  }
239 }
240 
241 template<enum Data::TimeSeriesType T>
242 void XCast<T>::onUpdateConversion(wxCommandEvent& evt)
243 {
244  if (!pArea)
245  {
246  return;
247  }
248 
249  Data::XCast* xcast = pArea->xcastData<T>();
250  if (!xcast)
251  {
252  return;
253  }
254 
255  if (xcast->useConversion != evt.IsChecked())
256  {
257  xcast->useConversion = evt.IsChecked();
258  MarkTheStudyAsModified();
259  Window::Inspector::Refresh();
260  }
261 }
262 
263 template<enum Data::TimeSeriesType T>
264 void XCast<T>::onUpdateTSTranslationUse(wxCommandEvent& evt)
265 {
266  wxChoice* obj = (wxChoice*)evt.GetEventObject();
267  if (obj and pArea)
268  {
269  Data::XCast* xcast = pArea->xcastData<T>();
270  if (!xcast)
271  {
272  return;
273  }
274 
275  YString s;
276  wxStringToString(obj->GetStringSelection(), s);
277  Data::XCast::TSTranslationUse d = Data::XCast::CStringToTSTranslationUse(s);
278  if (d != xcast->useTranslation)
279  {
280  xcast->useTranslation = d;
281  MarkTheStudyAsModified();
282  Window::Inspector::Refresh();
283  }
284  }
285 }
286 
287 template<enum Data::TimeSeriesType T>
288 inline void XCast<T>::selectDefaultPage()
289 {
290  if (pPageGeneral)
291  {
292  pPageGeneral->select();
293  }
294 }
295 
296 template<enum Data::TimeSeriesType T>
297 void XCast<T>::onStudyClosed()
298 {
299  pArea = nullptr;
300  selectDefaultPage();
301 }
302 
303 template<enum Data::TimeSeriesType T>
304 void XCast<T>::onAreaChanged(Data::Area* area)
305 {
306  using namespace Yuni;
307  // Keeping a pointer to the current area
308  pArea = area;
309 
310  // Updating the installed capacity of the current area
311  if (pArea)
312  {
313  Data::XCast* xcast = pArea->xcastData<T>();
314  if (xcast)
315  {
316  // Capacity
317  pInstalledCapacity->ChangeValue(DoubleToWxString(xcast->capacity));
318 
319  // Distribution
320  int indx = (int)xcast->distribution - 1;
321  if (indx < 0 or indx > 5)
322  {
323  indx = 1;
324  }
325  pDistribution->SetSelection(indx);
326 
327  // Transfert
328  useConversion->SetValue(xcast->useConversion);
329  useTranslation->SetSelection((int)xcast->useTranslation);
330  return;
331  }
332  }
333  pInstalledCapacity->ChangeValue(wxT("0"));
334  pDistribution->SetSelection(1);
335  useConversion->SetValue(false);
336  useTranslation->SetSelection(0);
337 }
338 
339 template<enum Data::TimeSeriesType T>
340 void XCast<T>::onInstalledCapacityChanged(wxCommandEvent& evt)
341 {
342  if (!pArea)
343  {
344  return;
345  }
346  double d;
347  evt.GetString().ToDouble(&d);
348  if (d < 0.)
349  {
350  d = 0.;
351  }
352 
353  Data::XCast* xcastData = pArea->xcastData<T>();
354  if (xcastData and not Yuni::Math::Equals(d, xcastData->capacity))
355  {
356  xcastData->capacity = d;
357  MarkTheStudyAsModified();
358  }
359 }
360 
361 } // namespace Antares::Window
362 
363 #endif // __ANTARES_WINDOWS_XCAST_XCAST_HXX__
A datagrid with virtual values.
Definition: component.h:84
Page * select(bool force=false)
Select the page.
Definition: notebook.cpp:868
Notebook.
Definition: notebook.h:36
void theme(Theme t)
Set the current theme.
Definition: notebook.h:289
Panel implementation.
Definition: panel.h:34
Definition for a single area.
Definition: area.h:51
Definition: xcast.h:35
Distribution distribution
The probability distribution to use.
Definition: xcast.h:182
double capacity
The installed capacity.
Definition: xcast.h:185
TSTranslationUse useTranslation
How to use the timeseries average.
Definition: xcast.h:179
bool useConversion
True to use the transfer function after the generation of the time-series.
Definition: xcast.h:188
Distribution
All available probability distribution.
Definition: xcast.h:61
@ dtMax
The maximum number of distributions.
Definition: xcast.h:75
@ dtNone
None.
Definition: xcast.h:63
static const char * DistributionToCString(Distribution d)
Convert a distribution into its human readable representation.
Definition: xcast.cpp:71
TSTranslationUse
Definition: xcast.h:85
static Distribution StringToDistribution(AnyString str)
Convert a CString into a probability distribution.
Definition: xcast.cpp:92
Visual Component for displaying all available areas (and groups)
Definition: area.h:37
static Yuni::Event< void(Data::Area *)> onAreaChanged
Public event, triggered when the selected area has changed.
Definition: area.h:69
Definition: xcast.h:38
XCast(wxWindow *parent, Toolbox::InputSelector::Area *notifier)
Default constructor.
Definition: xcast.hxx:38
virtual ~XCast()
Destructor.
Definition: xcast.hxx:207