OpenWAM
TDecisor.cpp
1 /*--------------------------------------------------------------------------------*\
2 ==========================|
3  \\ /\ /\ // O pen | OpenWAM: The Open Source 1D Gas-Dynamic Code
4  \\ | X | // W ave |
5  \\ \/_\/ // A ction | CMT-Motores Termicos / Universidad Politecnica Valencia
6  \\/ \// M odel |
7  ----------------------------------------------------------------------------------
8  License
9 
10  This file is part of OpenWAM.
11 
12  OpenWAM is free software: you can redistribute it and/or modify
13  it under the terms of the GNU General Public License as published by
14  the Free Software Foundation, either version 3 of the License, or
15  (at your option) any later version.
16 
17  OpenWAM is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with OpenWAM. If not, see <http://www.gnu.org/licenses/>.
24 
25 
26  \*--------------------------------------------------------------------------------*/
27 
28 //---------------------------------------------------------------------------
29 #pragma hdrstop
30 
31 #include "TDecisor.h"
32 
33 //---------------------------------------------------------------------------
34 
36  TController(nmCtlSwitch, i) {
37  fID = i + 1;
38 }
39 
41 
42 }
43 
44 double TDecisor::Output(double Time) {
45  fInput = FSensor[0]->Output();
46  double OutputLow = fControllerLow->Output(Time);
47  double OutputHigh = fControllerHigh->Output(Time);
48 
49  if(fTargedControlled)
50  fTarget = fControllerTarget->Output(Time);
51 
52  if(fInput < fTarget) {
53  fOutput = OutputLow;
54  } else {
55  fOutput = OutputHigh;
56  }
58 
59  return fOutput;
60 }
61 
62 void TDecisor::LeeController(const char *FileWAM, fpos_t &filepos) {
63  FILE *fich = fopen(FileWAM, "r");
64  fsetpos(fich, &filepos);
65 
66  fscanf(fich, "%lf ", &fTarget);
67  fscanf(fich, "%d ", &fControllerLowID);
68  fscanf(fich, "%d ", &fControllerHighID);
69 
70  int ctrl = 0;
71  fscanf(fich, "%d ", &ctrl);
72  if(ctrl == 0) {
73  fTargedControlled = false;
74  } else {
75  fTargedControlled = true;
76  fControllerTargetID = ctrl;
77  }
78 
79  int tmp = 0;
80  FSensorID.resize(1);
81  fscanf(fich, "%d ", &FSensorID[0]);
82 
83  fgetpos(fich, &filepos);
84  fclose(fich);
85 }
86 
87 void TDecisor::AsignaObjetos(TSensor **Sensor, TController **Controller) {
88  FSensor.push_back(Sensor[FSensorID[0] - 1]);
89 
90  fControllerLow = Controller[fControllerLowID - 1];
91  fControllerHigh = Controller[fControllerHighID - 1];
92  if(fTargedControlled)
93  fControllerTarget = Controller[fControllerTargetID - 1];
94 }
95 
96 void TDecisor::LeeResultadosMedControlador(const char *FileWAM, fpos_t &filepos) {
97  int nvars = 0, var = 0;
98 
99  FILE *fich = fopen(FileWAM, "r");
100  fsetpos(fich, &filepos);
101 
102  fscanf(fich, "%d ", &nvars);
103  for(int i = 0; i < nvars; i++) {
104  fscanf(fich, "%d ", &var);
105  switch(var) {
106  case 0:
107  FResMediosCtrl.Output = true;
108  break;
109  default:
110  std::cout << "Resultados medios en Controlador " << fID << " no implementados " << std::endl;
111  }
112  }
113 
114  fgetpos(fich, &filepos);
115  fclose(fich);
116 }
117 
118 void TDecisor::LeeResultadosInsControlador(const char *FileWAM, fpos_t &filepos) {
119  int nvars = 0, var = 0;
120 
121  FILE *fich = fopen(FileWAM, "r");
122  fsetpos(fich, &filepos);
123 
124  fscanf(fich, "%d ", &nvars);
125  for(int i = 0; i < nvars; i++) {
126  fscanf(fich, "%d ", &var);
127  switch(var) {
128  case 0:
129  FResInstantCtrl.Output = true;
130  break;
131  default:
132  std::cout << "Resultados instantaneos en Controlador " << fID << " no implementados " << std::endl;
133  }
134  }
135 
136  fgetpos(fich, &filepos);
137  fclose(fich);
138 }
139 
140 void TDecisor::CabeceraResultadosMedControlador(stringstream& medoutput) {
141  std::string Label;
142 
143  if(FResMediosCtrl.Output) {
144  Label = "\t" + PutLabel(705) + std::to_string(fID) + PutLabel(901);
145  medoutput << Label.c_str();
146  }
147 
148 }
149 
150 void TDecisor::CabeceraResultadosInsControlador(stringstream& insoutput) {
151  std::string Label;
152 
153  if(FResInstantCtrl.Output) {
154  Label = "\t" + PutLabel(705) + std::to_string(fID) + PutLabel(901);
155  insoutput << Label.c_str();
156  }
157 }
158 
159 void TDecisor::ImprimeResultadosMedControlador(stringstream& medoutput) {
160  std::string Label;
161 
162  if(FResMediosCtrl.Output) {
163  medoutput << "\t" << FResMediosCtrl.OutputMED;
164  }
165 }
166 
167 void TDecisor::ImprimeResultadosInsControlador(stringstream& insoutput) {
168  std::string Label;
169 
170  if(FResInstantCtrl.Output) {
171  insoutput << "\t" << FResInstantCtrl.OutputINS;
172  }
173 }
174 
176  FResMediosCtrl.OutputSUM = 0.;
177  FResMediosCtrl.TiempoSUM = 0.;
178  FResMediosCtrl.Tiempo0 = 0.;
179 }
180 
182  if(FResMediosCtrl.Output) {
183  FResMediosCtrl.OutputMED = FResMediosCtrl.OutputSUM / FResMediosCtrl.TiempoSUM;
184  FResMediosCtrl.OutputSUM = 0.;
185  }
186 }
187 
189  double Delta = Actual - FResMediosCtrl.Tiempo0;
190 
191  if(FResMediosCtrl.Output) {
192  FResMediosCtrl.OutputSUM += fOutput * Delta;
193  }
194  FResMediosCtrl.TiempoSUM += Delta;
195  FResMediosCtrl.Tiempo0 = Actual;
196 }
197 
199  if(FResInstantCtrl.Output)
200  FResInstantCtrl.OutputINS = fOutput;
201 }
202 
203 #pragma package(smart_init)
TDecisor::AsignaObjetos
void AsignaObjetos(TSensor **Sensor, TController **Controller)
Definition: TDecisor.cpp:87
TController
Definition: TController.h:37
TDecisor::CabeceraResultadosInsControlador
void CabeceraResultadosInsControlador(stringstream &insoutput)
Definition: TDecisor.cpp:150
TDecisor::IniciaMedias
void IniciaMedias()
Definition: TDecisor.cpp:175
TController::FSensorID
iVector FSensorID
Array with the ID of the sensor inputs.
Definition: TController.h:43
TController::FResMediosCtrl
stResMediosCtrl FResMediosCtrl
Struct with the average results of the controllers.
Definition: TController.h:49
TDecisor::ResultadosMediosController
void ResultadosMediosController()
Definition: TDecisor.cpp:181
PutLabel
std::string PutLabel(int idx)
Returns an integer.
Definition: labels.cpp:475
TDecisor::Output
double Output(double Time)
Definition: TDecisor.cpp:44
TDecisor::LeeResultadosMedControlador
void LeeResultadosMedControlador(const char *FileWAM, fpos_t &filepos)
Definition: TDecisor.cpp:96
TDecisor::ResultadosInstantController
void ResultadosInstantController()
Definition: TDecisor.cpp:198
TDecisor::LeeController
void LeeController(const char *FileWAM, fpos_t &filepos)
Definition: TDecisor.cpp:62
TDecisor::TDecisor
TDecisor(int i)
Definition: TDecisor.cpp:35
TController::FResInstantCtrl
stResInstantCtrl FResInstantCtrl
Struct with the average results of the controllers.
Definition: TController.h:50
TDecisor::ImprimeResultadosMedControlador
void ImprimeResultadosMedControlador(stringstream &medoutput)
Definition: TDecisor.cpp:159
TSensor
Definition: TSensor.h:42
TDecisor::ImprimeResultadosInsControlador
void ImprimeResultadosInsControlador(stringstream &insoutput)
Definition: TDecisor.cpp:167
TDecisor::LeeResultadosInsControlador
void LeeResultadosInsControlador(const char *FileWAM, fpos_t &filepos)
Definition: TDecisor.cpp:118
TController::Output
virtual double Output(double Time)=0
TDecisor::~TDecisor
~TDecisor()
Definition: TDecisor.cpp:40
TController::FSensor
std::vector< TSensor * > FSensor
Array with the pointers of the sensor inputs.
Definition: TController.h:40
TDecisor::AcumulaResultadosMediosController
void AcumulaResultadosMediosController(double Actual)
Definition: TDecisor.cpp:188
TDecisor::CabeceraResultadosMedControlador
void CabeceraResultadosMedControlador(stringstream &medoutput)
Definition: TDecisor.cpp:140