Building Controls Virtual Test Bed
cclient.c
Go to the documentation of this file.
1 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <math.h>
32 #include "utilSocket.h"
33 
36 int main(int argc, char *argv[]){
38  // Declare variables for the socket communication
39  // File name used to get the port number
40  const char *const simCfgFilNam = "socket.cfg";
41  // Client error flag
42  const int cliErrFla = -1;
43  // Client stop flag
44  const int stoFla = 1;
45  // Flags to exchange the status of the simulation program
46  // and of the middleware.
47  int flaWri = 0;
48  int flaRea = 0;
49  // Number of variables to be exchanged
50  const int nDblWri = 2;
51  int nDblRea;
52  // Number of rooms
53  int nRoo =2;
54  // Arrays that contain the variables to be exchanged
55  double dblValWri[2];
56  double dblValRea[2];
57  int i, iSte, sockfd, retVal;
58  // set simulation time step
59  double delTim;
60  double endTim = 6.*3600;
61 
63  // Declare variables of the room model
64  double simTimWri = 0;
65  double simTimRea = 0;
66  double TIni = 10;
67  double tau = 2*3600;
68  double Q0Hea = 100;
69  double UA = Q0Hea / 20;
70  double TOut = 5;
71  double C[] = {tau*UA, 2*tau*UA};
72  double TRoo[] = {TIni, TIni};
73 
74  double y[] = {0, 0};
75  int nSte;
77  if (argc <= 1) {
78  printf("Usage: %s simulation_timestep_in_seconds\n", argv[0]);
79  return(1);
80  }
81  delTim = atof(argv[1]);
82  fprintf(stderr,"Simulation model has time step %8.5g\n", delTim);
83  fprintf(stderr,"Simulation model has end time %8.5g\n", endTim);
84 
85  if (delTim <= 0){
86  printf("Error: End time must be bigger than zero.\n");
87  printf("Usage: %s simulation_timestep_in_seconds end_time_in_seconds\n", argv[0]);
88  exit(1);
89  }
90 
91  //nSte = (int)nearbyint(endTim/delTim);
92  nSte = (int)floor((endTim/delTim) + 0.5); // added by TNouidui. nearbyint not defined on Windows.
93  if (abs(nSte*delTim-endTim) > 1E-10*endTim){
94  printf("Error: End time divided by time step must be an integer.\n");
95  printf(" Number of time steps is %d.\n", nSte);
96  printf("Usage: %s simulation_timestep_in_seconds end_time_in_seconds\n", argv[0]);
97  exit(1);
98  }
99 
101  // Establish the client socket
102  sockfd = establishclientsocket(simCfgFilNam);
103  if (sockfd < 0){
104  fprintf(stderr,"Error: Failed to obtain socket file descriptor. sockfd=%d.\n", sockfd);
105  exit((sockfd)+100);
106  }
107 
109  // Simulation loop
110  for(iSte=0; iSte < nSte+1; iSte++){
111  // Set simulation time
112  simTimWri = (double)iSte*delTim; // set simulation time
114  // assign values to be exchanged
115  for(i=0; i < nDblWri; i++)
116  dblValWri[i]=TRoo[i];
117 
119  // Exchange values
120  retVal = exchangedoubleswithsocket(&sockfd, &flaWri, &flaRea,
121  &nDblWri, &nDblRea,
122  &simTimWri, dblValWri,
123  &simTimRea, dblValRea);
125  // Check flags
126  if (retVal < 0){
127  sendclientmessage(&sockfd, &cliErrFla);
128  printf("Simulator received value %d when reading from socket. Exit simulation.\n", retVal);
129  closeipc(&sockfd);
130  exit((retVal)+100);
131  }
132 
133  if (flaRea == 1){
134  printf("Simulator received end of simulation signal from server. Exit simulation.\n");
135  closeipc(&sockfd);
136  exit(0);
137  }
138 
139  if (flaRea != 0){
140  printf("Simulator received flag = %d from server. Exit simulation.\n", flaRea);
141  closeipc(&sockfd);
142  exit(1);
143  }
145  // Check for the correct number of double values
146  if (nDblRea != 2){
147  printf("Simulator received nDblRea = %d from server, but expected 2. Exit simulation.\n",
148  nDblRea);
149  closeipc(&sockfd);
150  exit(1);
151  }
153  // No flags found that require the simulation to terminate.
154  // Assign exchanged variables
155  for(i=0; i < nRoo; i++)
156  y[i] = dblValRea[i];
157 
159  // Having obtained y_k, we compute the new state x_k+1 = f(y_k)
160  // This is the actual simulation of the client.
161  for(i=0; i < nRoo; i++)
162  TRoo[i] = TRoo[i] + delTim/C[i] * ( UA * (TOut-TRoo[i] ) + Q0Hea * y[i] );
163  } // end of simulation loop
165  // Close socket at end of simulation
166  sendclientmessage(&sockfd, &stoFla);
167  closeipc(&sockfd);
168  exit(0);
169 }
170 
function sockfd
int exchangedoubleswithsocket(const int *sockfd, const int *flaWri, int *flaRea, const int *nDblWri, int *nDblRea, double *simTimWri, double dblValWri[], double *simTimRea, double dblValRea[])
Exchanges data with the socket.
for i
Definition: compile.m:69
int main(int argc, char *argv[])
Main function.
Definition: cclient.c:36
int establishclientsocket(const char *const docname)
Establishes a connection to the socket.
function retVal
Definition: closeIPC.m:1
fprintf([' ', 'Trying to send client error from MATLAB to the BCVTB.'])
int sendclientmessage(const int *sockfd, const int *flaWri)
Writes a message flag to the socket stream.
end Get return values from pointers flaRea
int closeipc(int *sockfd)
Closes the inter process communication socket.