Power System Platform  2026w23a-beta
Loading...
Searching...
No Matches
Divider.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 "ConnectionLine.h"
19#include "Divider.h"
20#include <wx/pen.h>
21#include <wx/brush.h>
22
23Divider::Divider(int id) : MathOperation(id) {}
24Divider::~Divider()
25{
26 for (auto& node : m_nodeList) if (node) delete node;
27 m_nodeList.clear();
28}
29
30void Divider::DrawDCSymbol(GUIColour* guiColour, wxGraphicsContext* gc) const
31{
32 // Plot divider.
33 gc->SetPen(wxPen(guiColour->bus, 2));
34 gc->SetBrush(*wxTRANSPARENT_BRUSH);
35 wxPoint2DDouble mSymbol[2];
36 mSymbol[0] = m_position + wxPoint2DDouble(-5, 0);
37 mSymbol[1] = m_position + wxPoint2DDouble(5, 0);
38 gc->StrokeLines(2, mSymbol);
39
40 gc->SetPen(*wxTRANSPARENT_PEN);
41 gc->SetBrush(wxBrush(guiColour->bus));
42 DrawDCCircle(m_position + wxPoint2DDouble(0, -3), 2, 10, gc);
43 DrawDCCircle(m_position + wxPoint2DDouble(0, 3), 2, 10, gc);
44}
45
46bool Divider::Solve(double* input, double timeStep)
47{
48 // Get the input vector from connection lines (can't use default (one) input argument)
49 std::vector<double> inputVector;
50 for(auto itN = m_nodeList.begin(), itNEnd = m_nodeList.end(); itN != itNEnd; ++itN) {
51 Node* node = *itN;
52 if(node->GetNodeType() != Node::NodeType::NODE_OUT) {
53 if(!node->IsConnected()) {
54 inputVector.push_back(0.0);
55 } else {
56 for(auto itC = m_childList.begin(), itCEnd = m_childList.end(); itC != itCEnd; ++itC) {
57 ConnectionLine* cLine = static_cast<ConnectionLine*>(*itC);
58 auto nodeList = cLine->GetNodeList();
59 for(auto itCN = nodeList.begin(), itCNEnd = nodeList.end(); itCN != itCNEnd; ++itCN) {
60 Node* childNode = *itCN;
61 if(childNode == node) {
62 inputVector.push_back(cLine->GetValue());
63 break;
64 }
65 }
66 }
67 }
68 }
69 }
70 if(inputVector.size() != 2) return false;
71
72 // If the denominator is zero, set the output a big number (1e15).
73 if(std::abs(inputVector[1]) < 1e-15) {
74 m_output = 1e15;
75 } else {
76 m_output = inputVector[0] / inputVector[1];
77 }
78
79 return true;
80}
81
83{
84 Divider* copy = new Divider(*this);
85 return copy;
86}
87
88rapidxml::xml_node<>* Divider::SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode)
89{
90 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "Divider");
91 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
92
93 SaveCADProperties(doc, elementNode);
94 SaveControlNodes(doc, elementNode);
95
96 return elementNode;
97}
98
99bool Divider::OpenElement(rapidxml::xml_node<>* elementNode)
100{
101 if(!OpenCADProperties(elementNode)) return false;
102 if(!OpenControlNodes(elementNode)) return false;
103
104 StartMove(m_position);
105 UpdatePoints();
106
107 return true;
108}
Connection between two control elements or other connection line and an element.
virtual void StartMove(wxPoint2DDouble position)
Update the element attributes related to the movement.
Control element that divides two inputs.
Definition Divider.h:33
virtual Element * GetCopy()
Get a the element copy.
Definition Divider.cpp:82
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:114
virtual void DrawDCCircle(wxPoint2DDouble position, double radius, int numSegments, wxGraphicsContext *gc) const
Draw a circle using device context.
Definition Element.cpp:173
Abstract class that define the general behavior of math operation control block.
Node of a control element. This class manages the user interaction with the connection and control el...