Power System Platform  2026w10a-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
30//void Divider::DrawSymbol() const
31//{
32// // Plot divider.
33// glLineWidth(2.0);
34// std::vector<wxPoint2DDouble> mSymbol;
35// mSymbol.push_back(m_position + wxPoint2DDouble(-5, 0));
36// mSymbol.push_back(m_position + wxPoint2DDouble(5, 0));
37// glColor4d(0.0, 0.3, 1.0, 1.0);
38// DrawLine(mSymbol, GL_LINES);
39// DrawCircle(m_position + wxPoint2DDouble(0, -3), 2, 10, GL_POLYGON);
40// DrawCircle(m_position + wxPoint2DDouble(0, 3), 2, 10, GL_POLYGON);
41//}
42
43void Divider::DrawDCSymbol(wxGraphicsContext* gc) const
44{
45 // Plot divider.
46 gc->SetPen(wxPen(wxColour(0, 77, 255, 255), 2));
47 gc->SetBrush(*wxTRANSPARENT_BRUSH);
48 wxPoint2DDouble mSymbol[2];
49 mSymbol[0] = m_position + wxPoint2DDouble(-5, 0);
50 mSymbol[1] = m_position + wxPoint2DDouble(5, 0);
51 gc->StrokeLines(2, mSymbol);
52
53 gc->SetPen(*wxTRANSPARENT_PEN);
54 gc->SetBrush(wxBrush(wxColour(0, 77, 255, 255)));
55 DrawDCCircle(m_position + wxPoint2DDouble(0, -3), 2, 10, gc);
56 DrawDCCircle(m_position + wxPoint2DDouble(0, 3), 2, 10, gc);
57}
58
59bool Divider::Solve(double* input, double timeStep)
60{
61 // Get the input vector from connection lines (can't use default (one) input argument)
62 std::vector<double> inputVector;
63 for(auto itN = m_nodeList.begin(), itNEnd = m_nodeList.end(); itN != itNEnd; ++itN) {
64 Node* node = *itN;
65 if(node->GetNodeType() != Node::NodeType::NODE_OUT) {
66 if(!node->IsConnected()) {
67 inputVector.push_back(0.0);
68 } else {
69 for(auto itC = m_childList.begin(), itCEnd = m_childList.end(); itC != itCEnd; ++itC) {
70 ConnectionLine* cLine = static_cast<ConnectionLine*>(*itC);
71 auto nodeList = cLine->GetNodeList();
72 for(auto itCN = nodeList.begin(), itCNEnd = nodeList.end(); itCN != itCNEnd; ++itCN) {
73 Node* childNode = *itCN;
74 if(childNode == node) {
75 inputVector.push_back(cLine->GetValue());
76 break;
77 }
78 }
79 }
80 }
81 }
82 }
83 if(inputVector.size() != 2) return false;
84
85 // If the denominator is zero, set the output a big number (1e15).
86 if(std::abs(inputVector[1]) < 1e-15) {
87 m_output = 1e15;
88 } else {
89 m_output = inputVector[0] / inputVector[1];
90 }
91
92 return true;
93}
94
96{
97 Divider* copy = new Divider(m_elementID);
98 *copy = *this;
99 return copy;
100}
101
102rapidxml::xml_node<>* Divider::SaveElement(rapidxml::xml_document<>& doc, rapidxml::xml_node<>* elementListNode)
103{
104 auto elementNode = XMLParser::AppendNode(doc, elementListNode, "Divider");
105 XMLParser::SetNodeAttribute(doc, elementNode, "ID", m_elementID);
106
107 SaveCADProperties(doc, elementNode);
108 SaveControlNodes(doc, elementNode);
109
110 return elementNode;
111}
112
113bool Divider::OpenElement(rapidxml::xml_node<>* elementNode)
114{
115 if(!OpenCADProperties(elementNode)) return false;
116 if(!OpenControlNodes(elementNode)) return false;
117
118 StartMove(m_position);
119 UpdatePoints();
120
121 return true;
122}
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:95
Base class of all elements of the program. This class is responsible for manage graphical and his dat...
Definition Element.h:112
virtual void DrawDCCircle(wxPoint2DDouble position, double radius, int numSegments, wxGraphicsContext *gc) const
Draw a circle using device context.
Definition Element.cpp:177
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...