OpenWAM
TMatlab.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 "TMatlab.h"
32 
33 //---------------------------------------------------------------------------
34 //---------------------------------------------------------------------------
35 
36 TMatlab::TMatlab(int Inputs, int Outputs) {
37  FInput = NULL;
38  FOutput = NULL;
39 
40  FNInputs = Inputs;
41  FNOutputs = Outputs;
42 
43 //VECTORES mxArray
44  FInputMatlab = NULL;
45  FOutputMatlab = NULL;
46 
47 //DIMENSIONADO VECTORES DE COMUNICACION
48 
49  FInput = new double[FNInputs];
50  FOutput = new double[FNOutputs];
51 
52 //VersMatlab=nmMatlab53;
53  VersMatlab = nmMatlab65;
54 
55 }
56 
57 //---------------------------------------------------------------------------
58 //---------------------------------------------------------------------------
59 
60 TMatlab::~TMatlab() {
61  if(FInput != NULL)
62  delete[] FInput;
63 
64  if(FOutput != NULL)
65  delete[] FOutput;
66 }
67 //---------------------------------------------------------------------------
68 //---------------------------------------------------------------------------
69 
70 void TMatlab::IniciaECU() {
71  try {
72 
73  int i = 0;
74 
75  char daux[140];
76  /* Inicializacion del acceso a MATLAB*/
77 
78  for(i = 0; i < 140; i++) {
79  daux[i] = '\0';
80  }
81 
82  /* Open a new instance of MATLAB */
83  int retstatus = 0;
84  ep = engOpenSingleUse("c:\matlab53\bin\matlab.exe", NULL, &retstatus);
85 
86  /* The pointer may not be for a valid instance. Check. */
87  int result = engEvalString(ep, "disp('foo')");
88 
89  if(result > 0) {
90  /* It failed. */
91  printf("\nERROR: no se puede abrir una nueva sesion de MATLAB !");
92  engClose(ep);
93  exit(0);
94  }
95 
96  /* crean los mxarray para comunicar con MATLAB*/
97  FInputMatlab = mxCreateDoubleMatrix(1, FNInputs, mxREAL);
98  FOutputMatlab = mxCreateDoubleMatrix(1, FNOutputs, mxREAL);
99 
100  engEvalString(ep, "FInputMatlab=zeros(1,FNInputs);");
101  engEvalString(ep, "FOutputMatlab=zeros(1,FNOutputs);");
102 
103  strcat(daux, "addpath '");
104  i = 9;
105  while(Fdirtrab[i - 9] != '\n') {
106  daux[i] = Fdirtrab[i - 9];
107  ++i;
108  }
109  daux[i] = '\'';
110 
111  engEvalString(ep, daux);
112 
113  engEvalString(ep, Fworkspace);
114 
115  /* Fin de la inicializacion del acceso a MATLAB*/
116 
117  } catch(exception &N) {
118  std::cout << "ERROR: IniciaECU" << std::endl;
119  std::cout << "Tipo de error: " << N.what() << std::endl;
120  throw Exception(N.what());
121  }
122 }
123 
124 //---------------------------------------------------------------------------
125 //---------------------------------------------------------------------------
126 
127 void TMatlab::FinalizaECU()
128 
129 {
130  try {
131  engClose(ep);
132  } catch(exception &N) {
133  std::cout << "ERROR: FinalizaECU" << std::endl;
134  std::cout << "Tipo de error: " << N.what() << std::endl;
135  throw Exception(N.what());
136  }
137 }
138 
139 //---------------------------------------------------------------------------
140 //---------------------------------------------------------------------------
141 
142 void TMatlab::CalculaECU() {
143  try {
144 
145  memcpy(mxGetPr(FInputMatlab), FInput, 2 * sizeof(double));
146  engPutVariable(ep, "FInputMatlab", FInputMatlab);
147  engEvalString(ep, Fficheme);
148 
149  FOutputMatlab = engGetVariable(ep, "FOutputMatlab");
150  memcpy(FOutput, mxGetPr(FOutputMatlab), 3 * sizeof(double));
151  mxDestroyArray(FOutputMatlab);
152 
153  } catch(exception &N) {
154  std::cout << "ERROR: CalculaECU" << std::endl;
155  std::cout << "Tipo de error: " << N.what() << std::endl;
156  throw Exception(N.what());
157  }
158 }
159 
160 //---------------------------------------------------------------------------
161 //---------------------------------------------------------------------------
162 
163 void TMatlab::PutInput(int i, double valor) {
164  try {
165  if(i < FNInputs) {
166  FInput[i] = valor;
167  } else {
168  std::cout << "WARNING: El valor de la entrada a matlab se sale de rango" << std::endl;
169  std::cout << " Revisa el acceso a matlab" << std::endl;
170  }
171  } catch(exception &N) {
172  std::cout << "ERROR: PutInput" << std::endl;
173  std::cout << "Tipo de error: " << N.what() << std::endl;
174  throw Exception(N.what());
175  }
176 }
177 
178 //---------------------------------------------------------------------------
179 //---------------------------------------------------------------------------
180 
181 double TMatlab::GetOutput(int i) {
182  try {
183  if(i < FNOutputs) {
184  return FOutput[i];
185  } else {
186  std::cout << "WARNING: El valor de la salida de matlab se sale de rango" << std::endl;
187  std::cout << " Revisa el acceso a matlab" << std::endl;
188  return 0.;
189  }
190  } catch(exception &N) {
191  std::cout << "ERROR: GetOutput" << std::endl;
192  std::cout << "Tipo de error: " << N.what() << std::endl;
193  throw Exception(N.what());
194  }
195 }
196 
197 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 
200 void TMatlab::LeeFicherosECU(FILE *fich) {
201  try {
202  fgets(Fdirtrab, 256, fich); //Lee la ruta, maximo 256 Characters
203  fscanf(fich, "%s", &Fworkspace);
204  fscanf(fich, "%s", &Fficheme);
205  } catch(exception &N) {
206  std::cout << "ERROR: LeeFicherosECU" << std::endl;
207  std::cout << "Tipo de error: " << N.what() << std::endl;
208  throw Exception(N.what());
209  }
210 }
211 
212 //---------------------------------------------------------------------------
213 //---------------------------------------------------------------------------
214 
215 #pragma package(smart_init)
216 
Exception
Custom exception class.
Definition: Exception.hpp:39