diff options
Diffstat (limited to 'Project')
-rw-r--r-- | Project/ControlEditor.cpp | 67 | ||||
-rw-r--r-- | Project/ControlElementSolver.cpp | 62 | ||||
-rw-r--r-- | Project/ControlElementSolver.h | 4 | ||||
-rw-r--r-- | Project/Project.mk | 2 |
4 files changed, 90 insertions, 45 deletions
diff --git a/Project/ControlEditor.cpp b/Project/ControlEditor.cpp index ad2db78..ac778de 100644 --- a/Project/ControlEditor.cpp +++ b/Project/ControlEditor.cpp @@ -623,41 +623,46 @@ void ControlEditor::OnKeyDown(wxKeyEvent& event) double integrationError = 1e-5; double simTime = 10.0; double printStep = 1e-2; - + double pulsePer = 1.0; - ControlElementSolver solver(this, timeStep, integrationError, true, 0.0); - - double currentTime = 0.0; - double printTime = 0.0; - double pulseTime = 0.0; - std::vector<double> time; - std::vector<double> solution; - std::vector<double> inputV; - while(currentTime <= simTime) { - double input = 0.0; - if(pulseTime >= pulsePer * 2.0) pulseTime = 0.0; - if(pulseTime >= pulsePer) input = 1.0; - - solver.SolveNextStep(input); - if(printTime >= printStep) { - time.push_back(currentTime); - solution.push_back(solver.GetLastSolution()); - inputV.push_back(input); - printTime = 0.0; + ControlElementSolver solver(this, timeStep, integrationError, false, 0.0); + if(solver.IsOK()) { + double currentTime = 0.0; + double printTime = 0.0; + double pulseTime = 0.0; + std::vector<double> time; + std::vector<double> solution; + std::vector<double> inputV; + while(currentTime <= simTime) { + double input = 0.0; + if(pulseTime >= pulsePer * 2.0) pulseTime = 0.0; + if(pulseTime >= pulsePer) input = 1.0; + + solver.SolveNextStep(input); + if(printTime >= printStep) { + time.push_back(currentTime); + solution.push_back(solver.GetLastSolution()); + inputV.push_back(input); + printTime = 0.0; + } + printTime += timeStep; + currentTime += timeStep; + pulseTime += timeStep; } - printTime += timeStep; - currentTime += timeStep; - pulseTime += timeStep; + std::vector<ElementPlotData> epdList; + ElementPlotData curve1Data(_("TESTES"), ElementPlotData::CT_BUS); + curve1Data.AddData(inputV, _("Entrada")); + curve1Data.AddData(solution, _("Saida")); + epdList.push_back(curve1Data); + + ChartView* cView = new ChartView(this, epdList, time); + cView->Show(); + } else { + wxMessageDialog msgDialog(this, _("it was not possible to solve the control system"), + _("Error"), wxOK | wxCENTRE | wxICON_ERROR); + msgDialog.ShowModal(); } - std::vector<ElementPlotData> epdList; - ElementPlotData curve1Data(_("TESTES"), ElementPlotData::CT_BUS); - curve1Data.AddData(inputV, _("Entrada")); - curve1Data.AddData(solution, _("Saida")); - epdList.push_back(curve1Data); - - ChartView* cView = new ChartView(this, epdList, time); - cView->Show(); /* std::vector<double> time, sinC, cosC, tgC; diff --git a/Project/ControlElementSolver.cpp b/Project/ControlElementSolver.cpp index a1aee27..d48e39f 100644 --- a/Project/ControlElementSolver.cpp +++ b/Project/ControlElementSolver.cpp @@ -51,17 +51,22 @@ ControlElementSolver::ControlElementSolver(ControlEditor* controlEditor, failMessage = _("There is no output in the control system."); } + m_timeStep = timeStep; + m_integrationError = integrationError; + if(!InitializeValues(input, startAllZero)) { + fail = true; + failMessage = _("It was not possible to initialize the control system."); + } + if(fail) { wxMessageDialog msgDialog(controlEditor, failMessage, _("Error"), wxOK | wxCENTRE | wxICON_ERROR); msgDialog.ShowModal(); + } else { + m_isOK = true; } - - m_timeStep = timeStep; - m_integrationError = integrationError; - InitializeValues(input, startAllZero); } -void ControlElementSolver::InitializeValues(double input, bool startAllZero) +bool ControlElementSolver::InitializeValues(double input, bool startAllZero) { // Reset Elements values auto elementList = m_ctrlContainer->GetControlElementsList(); @@ -88,10 +93,43 @@ void ControlElementSolver::InitializeValues(double input, bool startAllZero) } if(!startAllZero) { + // double origTimeStep = m_timeStep; // Calculate the steady-state results according to the input. - double minError = 1e-6; - + double minError = 1e-7 * m_timeStep; + int minIteration = 20; + int maxIteration = 100 / m_timeStep; + //double timeStep = 0.1; // Initial value is the maximum step value. + /* + // Get the minimum time step + for(auto it = tfList.begin(), itEnd = tfList.end(); it != itEnd; ++it) { + TransferFunction* tf = *it; + std::vector<double> num = tf->GetNumerator(); + std::vector<double> den = tf->GetDenominator(); + for(unsigned int i = 0; i < num.size(); ++i) { + if(num[i] < timeStep) timeStep = num[i]; + } + for(unsigned int i = 0; i < den.size(); ++i) { + if(den[i] < timeStep) timeStep = den[i]; + } + } + m_timeStep = timeStep;*/ + + double prevSol = 0.0; + double currentSol = 1.0; + int numIt = 0; + while(std::abs(prevSol - currentSol) > minError || numIt <= minIteration) { + prevSol = currentSol; + SolveNextStep(input); + currentSol = GetLastSolution(); + numIt++; + if(numIt >= maxIteration) return false; + } + wxMessageBox(wxString::Format("%d\n%e\n%f", numIt, minError,currentSol)); + // m_timeStep = origTimeStep; + m_solutions.clear(); } + + return true; } void ControlElementSolver::SolveNextStep(double input) @@ -130,9 +168,9 @@ void ControlElementSolver::SolveNextStep(double input) ConnectionLine* currentLine = firstConn; while(currentLine) { - //ConnectionLine* lastLine = currentLine; + // ConnectionLine* lastLine = currentLine; currentLine = SolveNextElement(currentLine); - //if(!currentLine) m_solutions.push_back(lastLine->GetValue()); + // if(!currentLine) m_solutions.push_back(lastLine->GetValue()); } bool haveUnsolvedElement = true; @@ -159,13 +197,13 @@ void ControlElementSolver::SolveNextStep(double input) if(haveUnsolvedElement) break; } } - + // Set the control system step output. if(m_outputControl->GetChildList().size() == 1) { ConnectionLine* cLine = static_cast<ConnectionLine*>(m_outputControl->GetChildList()[0]); m_solutions.push_back(cLine->GetValue()); - } - else m_solutions.push_back(0.0); + } else + m_solutions.push_back(0.0); } void ControlElementSolver::FillAllConnectedChildren(ConnectionLine* parent) diff --git a/Project/ControlElementSolver.h b/Project/ControlElementSolver.h index e51c40d..6d0ad3f 100644 --- a/Project/ControlElementSolver.h +++ b/Project/ControlElementSolver.h @@ -27,10 +27,11 @@ class ControlElementSolver bool startAllZero = false, double input = 0.0); ~ControlElementSolver() {} - virtual void InitializeValues(double input, bool startAllZero); + virtual bool InitializeValues(double input, bool startAllZero); virtual void SolveNextStep(double input); virtual std::vector<double> GetSolutions() { return m_solutions; } virtual double GetLastSolution() { return m_solutions[m_solutions.size() - 1]; } + virtual bool IsOK() const { return m_isOK; } protected: void FillAllConnectedChildren(ConnectionLine* parent); ConnectionLine* SolveNextElement(ConnectionLine* currentLine); @@ -39,6 +40,7 @@ class ControlElementSolver double m_timeStep; double m_integrationError; std::vector<double> m_solutions; + bool m_isOK = false; IOControl* m_inputControl = NULL; IOControl* m_outputControl = NULL; diff --git a/Project/Project.mk b/Project/Project.mk index cbf6e8f..e472ddd 100644 --- a/Project/Project.mk +++ b/Project/Project.mk @@ -13,7 +13,7 @@ CurrentFileName := CurrentFilePath := CurrentFileFullPath := User :=NDSE-69 -Date :=10/05/2017 +Date :=11/05/2017 CodeLitePath :="C:/Program Files/CodeLite" LinkerName :=C:/TDM-GCC-64/bin/g++.exe SharedObjectLinkerName :=C:/TDM-GCC-64/bin/g++.exe -shared -fPIC |