libhybrid
A library for discretized Hybrid Dynamical Systems
mex_wrapper.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2  * Copyright 2018 - Matteo Ragni, Matteo Cocetti - University of Trento
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
22 
32 #ifdef MATLAB_WRAPPER
33 
34 #include "libhybrid.h"
35 #include "mex.h"
36 
37 #ifndef HYB_MODEL_SOURCE
38 
49 #define HYB_MODEL_SOURCE "model.c"
50 #endif
51 #include HYB_MODEL_SOURCE
52 
53 
70 #define MATLAB_SYSTEM_IDENTIFICATION
71 
72 
73 #if defined(_MSC_VER)
74 #define inline __inline
75 #endif
76 
85 inline void error_message(hyb_errorcode c) {
86  switch(ret) {
87  case HYB_SUCCESS:
88  return;
89  break;
90  case HYB_EMALLOC:
91  mexErrMsgIdAndTxt("LIBHYBRID:Model:MallocError",
92  "Allocation error in the discretization");
93  break;
94  case HYB_NULLPTR:
95  mexErrMsgIdAndTxt("LIBHYBRID:Model:NullPointer",
96  "Null pointer passed to the integrator");
97  break;
98  case HYB_TLIMIT:
99  mexErrMsgIdAndTxt("LIBHYBRID:Model:tLimit",
100  "Reached time horizon for hybrid model");
101  break;
102  case HYB_JLIMIT:
103  mexErrMsgIdAndTxt("LIBHYBRID:Model:jLimit",
104  "Reached jump horizon for hybrid model");
105  break;
106  case HYB_NOJUMP:
107  mexErrMsgIdAndTxt("LIBHYBRID:Model:InvalidSet",
108  "Invalid set conditions provided");
109  break;
110  default:
111  mexErrMsgIdAndTxt("LIBHYBRID:Model:GenericError",
112  "An unknown error was raised");
113  break;
114  }
115 }
116 
117 
129 void mexFunction(int nlhs, mxArray *plhs[],
130  int nrhs, const mxArray *prhs[]) {
131  /* Declaration of input and output arguments. */
132  double *x, *u, **p, *xp, *y, *sim_time;
133  int i, np;
134  size_t nu, nx;
135 
136  if (nrhs < 3) {
137  mexErrMsgIdAndTxt("LIBHYBRID:Model:InvalidSyntax",
138  "At least 4 inputs expected (t, u, x, p).");
139  }
140 
141  np = nrhs - 3;
142 
143  /* Determine number of inputs and states. */
144  nx = mxGetNumberOfElements(prhs[1]); /* Number of states. */
145  nu = mxGetNumberOfElements(prhs[2]); /* Number of inputs. */
146 
147  if (nx != options.x_size) {
148  mexErrMsgIdAndTxt("LIBHYBRID:Model:InvalidSyntax",
149  "The dimension of input 2 should be %d", options.x_size);
150  }
151 
152  /* Obtain double data pointers from mxArrays. */
153  sim_time = mxGetPr(prhs[0]); /* States at time t. */
154  x = mxGetPr(prhs[1]); /* States at time t. */
155  u = mxGetPr(prhs[2]); /* Inputs at time t. */
156 
157  p = mxCalloc(np, sizeof(double*));
158  for (i = 0; i < np; i++)
159  p[i] = mxGetPr(prhs[3+i]); /* Parameter arrays. */
160 
161  /* Create matrix for the return arguments. */
162  plhs[0] = mxCreateDoubleMatrix(nx, 1, mxREAL);
163  plhs[1] = mxCreateDoubleMatrix(options.y_size, 1, mxREAL);
164  xp = mxGetPr(plhs[0]); /* State derivative values. */
165  y = mxGetPr(plhs[1]); /* Output values. */
166 
167  #ifdef MATLAB_SYSTEM_IDENTIFICATION
168  hyb_bool is_step = hyb_true;
169  while (is_step) {
170  hyb_errorcode ret = hyb_main_loop(&options, y, xp, sim_time[0], x, u, (const double**) p);
171  is_step = (xp[1] == x[1] ? hyb_false : hyb_true);
172  error_message(ret);
173  }
174  #else
175  hyb_errorcode ret = hyb_main_loop(&options, y, xp, sim_time[0], x, u, (const double**) p);
176  error_message(ret);
177  #endif
178  mxFree(p);
179 }
180 
181 #endif
hyb_errorcode
Definition: libhybrid.h:258
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
Entry point for MATLAB Api.
Definition: mex_wrapper.c:129
hyb_errorcode hyb_main_loop(hyb_opts *opts, hyb_float *y, hyb_float *xp, hyb_float tau, const hyb_float *x, const hyb_float *u, const hyb_float **p)
Hybrid system main loop.
Definition: libhybrid.c:31
hyb_bool
libhybrid boolean type is actually an enum
Definition: libhybrid.h:73
void error_message(hyb_errorcode c)
handler for errors from hyb_main_loop function
Definition: mex_wrapper.c:85