MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mbc_py.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/libraries/libmbc/mbc_py.cc,v 1.14 2017/01/12 14:43:43 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "mbc_py.h"
33 
34 #include <iostream>
35 #include <cstring>
36 #include <vector>
37 
38 /* reference node global data */
39 
40 uint32_t *mbc_r_k_label;
41 double *mbc_r_x;
42 double *mbc_r_theta;
43 double *mbc_r_r;
45 double *mbc_r_xp;
46 double *mbc_r_omega;
47 double *mbc_r_xpp;
48 double *mbc_r_omegap;
49 uint32_t *mbc_r_d_label;
50 double *mbc_r_f;
51 double *mbc_r_m;
52 
54 uint32_t mbc_r_x_size;
56 uint32_t mbc_r_r_size;
58 uint32_t mbc_r_xp_size;
60 uint32_t mbc_r_xpp_size;
63 uint32_t mbc_r_f_size;
64 uint32_t mbc_r_m_size;
65 
66 /* nodal element global data */
67 
68 uint32_t *mbc_n_k_labels;
69 double *mbc_n_x;
70 double *mbc_n_theta;
71 double *mbc_n_r;
73 double *mbc_n_xp;
74 double *mbc_n_omega;
75 double *mbc_n_xpp;
76 double *mbc_n_omegap;
77 uint32_t *mbc_n_d_labels;
78 double *mbc_n_f;
79 double *mbc_n_m;
80 
82 uint32_t mbc_n_x_size;
84 uint32_t mbc_n_r_size;
86 uint32_t mbc_n_xp_size;
88 uint32_t mbc_n_xpp_size;
91 uint32_t mbc_n_f_size;
92 uint32_t mbc_n_m_size;
93 
94 static std::vector<mbc_nodal_t> n_mbc;
95 
96 int
97 mbc_py_nodal_initialize(const char *const path,
98  const char *const host, unsigned port,
99  int timeout, unsigned verbose, unsigned data_and_next,
100  unsigned refnode, unsigned nodes,
101  unsigned labels, unsigned rot, unsigned accels)
102 {
104  std::memset(&mbc, 0, sizeof(mbc_nodal_t));
105 
107  mbc.mbc.verbose = verbose;
108  mbc.mbc.timeout = timeout;
109 
110  if (mbc.mbc.verbose) {
111  if (path && path[0]) {
112  std::cout << "connecting to path=" << path << std::endl;
113  } else {
114  std::cout << "connecting to host=" << host << ":" << port << std::endl;
115  }
116 
117  if (mbc.mbc.timeout < 0) {
118  std::cout << "timeout=forever" << std::endl;
119  } else {
120  std::cout << "timeout=" << mbc.mbc.timeout << std::endl;
121  }
122  }
123 
124  if (path && path[0]) {
125  if (mbc_unix_init((mbc_t *)&mbc, path)) {
126  return -1;
127  }
128 
129  } else if (host && host[0]) {
130  if (mbc_inet_init((mbc_t *)&mbc, host, port)) {
131  return -1;
132  }
133 
134  } else {
135  return -1;
136  }
137 
138  if (mbc_nodal_init(&mbc, refnode, nodes, labels, rot, accels)) {
139  return -1;
140  }
141 
142  int id = ::n_mbc.size();
143  ::n_mbc.push_back(mbc);
144 
145  return id;
146 }
147 
148 int
150 {
151  if (id >= n_mbc.size()) {
152  return -1;
153  }
154 
155  mbc_nodal_t *mbcp = &::n_mbc[id];
156 
157  if (mbc_nodal_negotiate_request(mbcp)) {
158  return -1;
159  }
160 
161  if (MBC_F_REF_NODE(mbcp)) {
162  if (MBC_F_LABELS(mbcp)) {
163  mbc_r_k_label = &MBC_R_K_LABEL(mbcp);
164  mbc_r_k_label_size = 1;
165 
166  mbc_r_d_label = &MBC_R_D_LABEL(mbcp);
167  mbc_r_d_label_size = 1;
168  }
169 
170  mbc_r_x = MBC_R_X(mbcp);
171  mbc_r_x_size = 3;
172 
173  switch (MBC_F_ROT_REF_NODE(mbcp)) {
174  case MBC_ROT_THETA:
175  mbc_r_theta = MBC_R_THETA(mbcp);
176  mbc_r_theta_size = 3;
177  break;
178 
179  case MBC_ROT_MAT:
180  mbc_r_r = MBC_R_R(mbcp);
181  mbc_r_r_size = 9;
182  break;
183 
184  case MBC_ROT_EULER_123:
187  break;
188 
189  default:
190  std::cerr << "mbc_py_nodal_negotiate: unknown reference node orientation" << std::endl;
191  return -1;
192  }
193 
194  mbc_r_xp = MBC_R_XP(mbcp);
195  mbc_r_xp_size = 3;
196  mbc_r_omega = MBC_R_OMEGA(mbcp);
197  mbc_r_omega_size = 3;
198 
199  if (MBC_F_ACCELS(mbcp)) {
200  mbc_r_xpp = MBC_R_XPP(mbcp);
201  mbc_r_xpp_size = 3;
202  mbc_r_omegap = MBC_R_OMEGAP(mbcp);
203  mbc_r_omegap_size = 3;
204  }
205 
206  mbc_r_f = MBC_R_F(mbcp);
207  mbc_r_f_size = 3;
208  mbc_r_m = MBC_R_M(mbcp);
209  mbc_r_m_size = 3;
210  }
211 
212  if (mbcp->nodes > 0) {
213  if (MBC_F_LABELS(mbcp)) {
215  mbc_n_k_labels_size = mbcp->nodes;
217  mbc_n_d_labels_size = mbcp->nodes;
218  }
219 
220  mbc_n_x = MBC_N_X(mbcp);
221  mbc_n_x_size = 3*mbcp->nodes;
222 
223  switch (MBC_F_ROT(mbcp)) {
224  case MBC_ROT_THETA:
225  mbc_n_theta = MBC_N_THETA(mbcp);
226  mbc_n_theta_size = 3*mbcp->nodes;
227  break;
228 
229  case MBC_ROT_MAT:
230  mbc_n_r = MBC_N_R(mbcp);
231  mbc_n_r_size = 9*mbcp->nodes;
232  break;
233 
234  case MBC_ROT_EULER_123:
236  mbc_n_euler_123_size = 3*mbcp->nodes;
237 
238  case MBC_ROT_NONE:
239  break;
240 
241  default:
242  std::cerr << "mbc_py_nodal_negotiate: unknown node orientation " << MBC_F_ROT(mbcp) << std::endl;
243  return -1;
244  }
245 
246  mbc_n_xp = MBC_N_XP(mbcp);
247  mbc_n_xp_size = 3*mbcp->nodes;
248  mbc_n_omega = MBC_N_OMEGA(mbcp);
249  mbc_n_omega_size = 3*mbcp->nodes;
250 
251  if (MBC_F_ACCELS(mbcp)) {
252  mbc_n_xpp = MBC_N_XPP(mbcp);
253  mbc_n_xpp_size = 3*mbcp->nodes;
254  mbc_n_omegap = MBC_N_OMEGAP(mbcp);
255  mbc_n_omegap_size = 3*mbcp->nodes;
256  }
257 
258  mbc_n_f = MBC_N_F(mbcp);
259  mbc_n_f_size = 3*mbcp->nodes;
260  mbc_n_m = MBC_N_M(mbcp);
261  mbc_n_m_size = 3*mbcp->nodes;
262  }
263 
264  return id;
265 }
266 
267 int
268 mbc_py_nodal_send(unsigned id, int last)
269 {
270  if (id >= n_mbc.size()) {
271  return -1;
272  }
273 
274  return mbc_nodal_put_forces(&::n_mbc[id], last);
275 }
276 
277 int
278 mbc_py_nodal_recv(unsigned id)
279 {
280  if (id >= n_mbc.size()) {
281  return -1;
282  }
283 
284  return mbc_nodal_get_motion(&::n_mbc[id]);
285 }
286 
287 int
289 {
290  if (id >= n_mbc.size()) {
291  return -1;
292  }
293 
294  mbc_nodal_destroy(&::n_mbc[id]);
295 
296  return 0;
297 }
298 
299 /* modal element global data */
300 
301 double *mbc_m_q;
302 double *mbc_m_qp;
303 double *mbc_m_p;
304 
305 uint32_t mbc_m_q_size;
306 uint32_t mbc_m_qp_size;
307 uint32_t mbc_m_p_size;
308 
309 static std::vector<mbc_modal_t> m_mbc;
310 
311 int
312 mbc_py_modal_initialize(const char *const path,
313  const char *const host, unsigned port,
314  int timeout, unsigned verbose, unsigned data_and_next,
315  unsigned refnode, unsigned modes)
316 {
318  std::memset(&mbc, 0, sizeof(mbc_modal_t));
319 
321  mbc.mbc.verbose = verbose;
322  mbc.mbc.timeout = timeout;
323 
324  if (mbc.mbc.verbose) {
325  if (path && path[0]) {
326  std::cout << "connecting to path=" << path << std::endl;
327  } else {
328  std::cout << "connecting to host=" << host << ":" << port << std::endl;
329  }
330 
331  if (mbc.mbc.timeout < 0) {
332  std::cout << "timeout=forever" << std::endl;
333  } else {
334  std::cout << "timeout=" << mbc.mbc.timeout << std::endl;
335  }
336  }
337 
338  if (path && path[0]) {
339  if (mbc_unix_init((mbc_t *)&mbc, path)) {
340  return -1;
341  }
342 
343  } else if (host && host[0]) {
344  if (mbc_inet_init((mbc_t *)&mbc, host, port)) {
345  return -1;
346  }
347 
348  } else {
349  return -1;
350  }
351 
352  if (mbc_modal_init(&mbc, refnode, modes)) {
353  return -1;
354  }
355 
356  int id = m_mbc.size();
357  m_mbc.push_back(mbc);
358 
359  return id;
360 }
361 
362 int
364 {
365  if (id >= m_mbc.size()) {
366  return -1;
367  }
368 
369  mbc_modal_t *mbcp = &::m_mbc[id];
370 
371  if (mbc_modal_negotiate_request(mbcp)) {
372  return -1;
373  }
374 
375  if (MBC_F_REF_NODE(mbcp)) {
376  mbc_r_x = MBC_R_X(mbcp);
377  mbc_r_x_size = 3;
378  mbc_r_r = MBC_R_R(mbcp);
379  mbc_r_r_size = 9;
380 
381  mbc_r_xp = MBC_R_XP(mbcp);
382  mbc_r_xp_size = 3;
383  mbc_r_omega = MBC_R_OMEGA(mbcp);
384  mbc_r_omega_size = 3;
385 
386  mbc_r_f = MBC_R_F(mbcp);
387  mbc_r_f_size = 3;
388  mbc_r_m = MBC_R_M(mbcp);
389  mbc_r_m_size = 3;
390  }
391 
392  if (mbcp->modes > 0) {
393  mbc_m_q = MBC_Q(mbcp);
394  mbc_m_q_size = mbcp->modes;
395  mbc_m_qp = MBC_QP(mbcp);
396  mbc_m_qp_size = mbcp->modes;
397 
398  mbc_m_p = MBC_P(mbcp);
399  mbc_m_p_size = mbcp->modes;
400  }
401 
402  return id;
403 }
404 
405 int
406 mbc_py_modal_send(unsigned id, int last)
407 {
408  if (id >= m_mbc.size()) {
409  return -1;
410  }
411 
412  return mbc_modal_put_forces(&m_mbc[id], last);
413 }
414 
415 int
416 mbc_py_modal_recv(unsigned id)
417 {
418  if (id >= m_mbc.size()) {
419  return -1;
420  }
421 
422  return mbc_modal_get_motion(&m_mbc[id]);
423 }
424 
425 int
427 {
428  if (id >= m_mbc.size()) {
429  return -1;
430  }
431 
432  mbc_modal_destroy(&m_mbc[id]);
433 
434  return 0;
435 }
#define MBC_F_LABELS(mbc)
Definition: mbc.h:241
mbc_t mbc
Definition: mbc.h:335
double * mbc_n_x
Definition: mbc_py.cc:69
uint32_t mbc_r_xp_size
Definition: mbc_py.cc:58
uint32_t mbc_n_f_size
Definition: mbc_py.cc:91
int mbc_nodal_get_motion(mbc_nodal_t *mbc)
Get nodal motion from peer.
double * mbc_r_r
Definition: mbc_py.cc:43
uint32_t * mbc_r_d_label
Definition: mbc_py.cc:49
uint32_t mbc_n_euler_123_size
Definition: mbc_py.cc:85
static int timeout
nodal stuff (partially opaque).
Definition: mbc.h:509
uint32_t mbc_n_xpp_size
Definition: mbc_py.cc:88
int mbc_py_modal_initialize(const char *const path, const char *const host, unsigned port, int timeout, unsigned verbose, unsigned data_and_next, unsigned refnode, unsigned modes)
Definition: mbc_py.cc:312
uint32_t * mbc_n_d_labels
Definition: mbc_py.cc:77
int mbc_py_nodal_send(unsigned id, int last)
Definition: mbc_py.cc:268
#define MBC_N_R(mbc)
Definition: mbc.h:365
#define MBC_N_X(mbc)
Definition: mbc.h:363
uint32_t mbc_r_omega_size
Definition: mbc_py.cc:59
#define MBC_P(mbc)
Definition: mbc.h:518
int mbc_modal_destroy(mbc_modal_t *mbc)
Destroy modal data.
uint32_t mbc_r_f_size
Definition: mbc_py.cc:63
int mbc_nodal_init(mbc_nodal_t *mbc, unsigned refnode, unsigned nodes, unsigned labels, unsigned rot, unsigned accels)
Initialize nodal data.
uint32_t mbc_n_xp_size
Definition: mbc_py.cc:86
#define MBC_N_THETA(mbc)
Definition: mbc.h:364
double * mbc_n_xp
Definition: mbc_py.cc:73
uint32_t mbc_n_omegap_size
Definition: mbc_py.cc:89
double * mbc_n_theta
Definition: mbc_py.cc:70
uint32_t * mbc_r_k_label
Definition: mbc_py.cc:40
#define MBC_N_D_LABELS(mbc)
Definition: mbc.h:371
int mbc_py_modal_send(unsigned id, int last)
Definition: mbc_py.cc:406
uint32_t mbc_r_m_size
Definition: mbc_py.cc:64
int verbose
Definition: mbc.h:128
#define MBC_N_M(mbc)
Definition: mbc.h:373
#define MBC_R_OMEGA(mbc)
Definition: mbc.h:288
int mbc_py_modal_destroy(unsigned id)
Definition: mbc_py.cc:426
double * mbc_n_r
Definition: mbc_py.cc:71
#define MBC_N_XP(mbc)
Definition: mbc.h:367
uint32_t mbc_n_x_size
Definition: mbc_py.cc:82
#define MBC_QP(mbc)
Definition: mbc.h:517
static unsigned rot
#define MBC_F_ACCELS(mbc)
Definition: mbc.h:242
double * mbc_r_xp
Definition: mbc_py.cc:45
uint32_t mbc_r_xpp_size
Definition: mbc_py.cc:60
char data_and_next
Definition: mbc.h:125
double * mbc_n_xpp
Definition: mbc_py.cc:75
double * mbc_r_x
Definition: mbc_py.cc:41
int mbc_py_modal_recv(unsigned id)
Definition: mbc_py.cc:416
double * mbc_n_euler_123
Definition: mbc_py.cc:72
double * mbc_m_q
Definition: mbc_py.cc:301
#define MBC_R_M(mbc)
Definition: mbc.h:293
uint32_t mbc_n_k_labels_size
Definition: mbc_py.cc:81
int mbc_py_nodal_recv(unsigned id)
Definition: mbc_py.cc:278
#define MBC_R_XP(mbc)
Definition: mbc.h:287
#define MBC_N_OMEGAP(mbc)
Definition: mbc.h:370
#define MBC_Q(mbc)
Definition: mbc.h:516
uint32_t mbc_r_theta_size
Definition: mbc_py.cc:55
uint32_t mbc_r_omegap_size
Definition: mbc_py.cc:61
uint32_t * mbc_n_k_labels
Definition: mbc_py.cc:68
static int labels
int mbc_py_modal_negotiate(unsigned id)
Definition: mbc_py.cc:363
#define MBC_R_D_LABEL(mbc)
Definition: mbc.h:291
double * mbc_r_theta
Definition: mbc_py.cc:42
int mbc_modal_negotiate_request(mbc_modal_t *mbc)
Negotiate modal data.
uint32_t mbc_n_omega_size
Definition: mbc_py.cc:87
#define MBC_N_OMEGA(mbc)
Definition: mbc.h:368
#define MBC_R_XPP(mbc)
Definition: mbc.h:289
double * mbc_n_omegap
Definition: mbc_py.cc:76
double * mbc_r_xpp
Definition: mbc_py.cc:47
uint32_t modes
Definition: mbc.h:514
const char * host
Definition: autopilot.c:142
int mbc_py_nodal_initialize(const char *const path, const char *const host, unsigned port, int timeout, unsigned verbose, unsigned data_and_next, unsigned refnode, unsigned nodes, unsigned labels, unsigned rot, unsigned accels)
Definition: mbc_py.cc:97
double * mbc_r_euler_123
Definition: mbc_py.cc:44
double * mbc_m_p
Definition: mbc_py.cc:303
#define MBC_N_F(mbc)
Definition: mbc.h:372
uint32_t mbc_m_q_size
Definition: mbc_py.cc:305
int mbc_modal_init(mbc_modal_t *mbc, int refnode, unsigned modes)
Initialize modal data.
#define MBC_F_ROT(mbc)
Definition: mbc.h:243
uint32_t mbc_m_p_size
Definition: mbc_py.cc:307
Connection data structure (partially opaque)
Definition: mbc.h:103
int timeout
Definition: mbc.h:137
double * mbc_r_omegap
Definition: mbc_py.cc:48
uint32_t mbc_m_qp_size
Definition: mbc_py.cc:306
uint32_t mbc_r_x_size
Definition: mbc_py.cc:54
double * mbc_r_omega
Definition: mbc_py.cc:46
uint32_t mbc_n_r_size
Definition: mbc_py.cc:84
#define MBC_R_OMEGAP(mbc)
Definition: mbc.h:290
static int data_and_next
uint32_t mbc_r_d_label_size
Definition: mbc_py.cc:62
uint32_t mbc_n_theta_size
Definition: mbc_py.cc:83
int mbc_nodal_put_forces(mbc_nodal_t *mbc, int last)
Put forces to peer.
#define MBC_R_K_LABEL(mbc)
Definition: mbc.h:282
static int nodes
static mbc_nodal_t * mbc
#define MBC_R_R(mbc)
Definition: mbc.h:285
#define MBC_N_XPP(mbc)
Definition: mbc.h:369
static int verbose
Definition: ann_tr.c:51
#define MBC_F_REF_NODE(mbc)
Definition: mbc.h:240
int mbc_unix_init(mbc_t *mbc, const char *path)
Initialize communication using "unix" socket.
#define MBC_R_F(mbc)
Definition: mbc.h:292
int mbc_nodal_negotiate_request(mbc_nodal_t *mbc)
Negotiate nodal data.
static std::vector< mbc_nodal_t > n_mbc
Definition: mbc_py.cc:94
uint32_t mbc_r_r_size
Definition: mbc_py.cc:56
uint32_t mbc_r_k_label_size
Definition: mbc_py.cc:53
double * mbc_r_m
Definition: mbc_py.cc:51
int mbc_py_nodal_negotiate(unsigned id)
Definition: mbc_py.cc:149
int mbc_inet_init(mbc_t *mbc, const char *host, short unsigned port)
Initialize communication using "inet" socket.
uint32_t nodes
Definition: mbc.h:339
int mbc_modal_get_motion(mbc_modal_t *mbc)
Get modal motion from peer.
#define MBC_R_X(mbc)
Definition: mbc.h:283
double * mbc_n_f
Definition: mbc_py.cc:78
double * mbc_r_f
Definition: mbc_py.cc:50
Nodal stuff (partially opaque).
Definition: mbc.h:334
static std::vector< mbc_modal_t > m_mbc
Definition: mbc_py.cc:309
uint32_t mbc_n_m_size
Definition: mbc_py.cc:92
uint32_t mbc_n_d_labels_size
Definition: mbc_py.cc:90
double * mbc_n_omega
Definition: mbc_py.cc:74
#define MBC_R_EULER_123(mbc)
Definition: mbc.h:286
#define MBC_F_ROT_REF_NODE(mbc)
Definition: mbc.h:249
unsigned short int port
Definition: autopilot.c:143
int mbc_py_nodal_destroy(unsigned id)
Definition: mbc_py.cc:288
double * mbc_n_m
Definition: mbc_py.cc:79
#define MBC_N_K_LABELS(mbc)
Definition: mbc.h:362
#define MBC_R_THETA(mbc)
Definition: mbc.h:284
int mbc_nodal_destroy(mbc_nodal_t *mbc)
Destroy nodal data.
mbc_t mbc
Definition: mbc.h:510
uint32_t mbc_r_euler_123_size
Definition: mbc_py.cc:57
double * mbc_m_qp
Definition: mbc_py.cc:302
#define MBC_N_EULER_123(mbc)
Definition: mbc.h:366
static int refnode
const char * path
Definition: autopilot.c:141
int mbc_modal_put_forces(mbc_modal_t *mbc, int last)
Put forces to peer.