MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
mbc.h
Go to the documentation of this file.
1 /*
2  * MBDyn (C) is a multibody analysis code.
3  * http://www.mbdyn.org
4  *
5  * Copyright (C) 1996-2017
6  *
7  * Pierangelo Masarati <masarati@aero.polimi.it>
8  * Paolo Mantegazza <mantegazza@aero.polimi.it>
9  *
10  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
11  * via La Masa, 34 - 20156 Milano, Italy
12  * http://www.aero.polimi.it
13  *
14  * Changing this copyright notice is forbidden.
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation (version 2 of the License).
19  *
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29  */
30 
31 /*
32  * NOTE: this is intentionally configuration-independent.
33  */
34 
35 #ifndef MBC_H
36 #define MBC_H
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif /* __cplusplus */
41 
42 #include <stdint.h>
43 
49 enum ESCmd {
50  ES_UNKNOWN = -1,
51  /* 1 intentionally unused */
53  /* 3 intentionally unused */
55  ES_ABORT = 5,
58  ES_OK = 8,
59 
61 };
62 
64 enum MBCType {
65  MBC_MODAL = 0x0001U,
66  MBC_NODAL = 0x0002U,
68 
69  MBC_REF_NODE = 0x0004U,
70 
71  MBC_ACCELS = 0x0008U,
72 
73  MBC_LABELS = 0x0010U,
74 
76  MBC_ROT_THETA = 0x0100U,
78  MBC_ROT_MAT = 0x0200U,
80  MBC_ROT_EULER_123 = 0x0400U,
81 
82  /* add more? */
83 
86  MBC_ROT_NONE = 0x0000U,
87 
89 
96 
98 
100 };
101 
103 typedef struct {
105  int sock;
106 
108  unsigned sock_flags;
109 
112 
115 
117  uint8_t cmd;
118 
126 
128  int verbose;
129 
137  int timeout;
138 } mbc_t;
139 
141 /* validate command
142  *
143  * command needs to be set in mbc->cmd
144  */
145 extern int
147 
149 /* get command from peer
150  *
151  * command is stored in mbc->cmd
152  */
153 extern int
155 
157 /* put command to peer
158  *
159  * command needs to be set in mbc->cmd
160  */
161 extern int
163 
176 extern int
177 mbc_inet_init(mbc_t *mbc, const char *host, short unsigned port);
178 
190 extern int
191 mbc_unix_init(mbc_t *mbc, const char *path);
192 
233 typedef struct {
234  uint32_t flags;
235 #define MBC_F(mbc) ((mbc)->mbcr.flags)
236 #define MBC_F_GET(mbc, f) (MBC_F(mbc) & (f))
237 #define MBC_F_SET(mbc, f) (MBC_F(mbc) |= (f))
238 #define MBC_F_RESET(mbc, f) (MBC_F(mbc) &= ~(f))
239 
240 #define MBC_F_REF_NODE(mbc) MBC_F_GET(mbc, MBC_REF_NODE)
241 #define MBC_F_LABELS(mbc) MBC_F_GET(mbc, MBC_LABELS)
242 #define MBC_F_ACCELS(mbc) MBC_F_GET(mbc, MBC_ACCELS)
243 #define MBC_F_ROT(mbc) MBC_F_GET(mbc, MBC_ROT_MASK)
244 #define MBC_F_ROT_THETA(mbc) MBC_F_GET(mbc, MBC_ROT_THETA)
245 #define MBC_F_ROT_MAT(mbc) MBC_F_GET(mbc, MBC_ROT_MAT)
246 #define MBC_F_ROT_EULER_123(mbc) MBC_F_GET(mbc, MBC_ROT_EULER_123)
247 #define MBC_U_REF_NODE_ROT_2_ROT(u) (((u) & MBC_REF_NODE_ROT_MASK) >> 4)
248 #define MBC_U_ROT_2_REF_NODE_ROT(u) (((u) & MBC_ROT_MASK) << 4)
249 #define MBC_F_ROT_REF_NODE(mbc) MBC_U_REF_NODE_ROT_2_ROT(MBC_F_GET((mbc), MBC_REF_NODE_ROT_MASK))
250 #define MBC_F_REF_NODE_ROT(mbc) MBC_F_GET(mbc, MBC_REF_NODE_ROT_MASK)
251 
252 #define MBC_F_SET_REF_NODE(mbc) MBC_F_SET(mbc, MBC_REF_NODE)
253 #define MBC_F_SET_LABELS(mbc) MBC_F_SET(mbc, MBC_LABELS)
254 #define MBC_F_SET_ACCELS(mbc) MBC_F_SET(mbc, MBC_ACCELS)
255 #define MBC_F_SET_ROT_THETA(mbc) MBC_F_SET(mbc, MBC_ROT_THETA)
256 #define MBC_F_SET_ROT_MAT(mbc) MBC_F_SET(mbc, MBC_ROT_MAT)
257 #define MBC_F_SET_ROT_EULER_123(mbc) MBC_F_SET(mbc, MBC_ROT_EULER_123)
258 
259  /* reference node data */
260  union {
261  /* NOTE: wasting an uint32_t for each uint32_t to make sure alignment is on double */
262  char char_r_ptr[(2 + 2) * sizeof(uint32_t) + (3 + 9 + 3 + 3 + 3 + 3 + 3 + 3) * sizeof(double)];
263  uint32_t uint32_t_r_ptr[(2 + 2) + (3 + 9 + 3 + 3 + 3 + 3 + 3 + 3) * sizeof(double) / sizeof(uint32_t)];
264  double double_r_ptr[(2 + 2) * sizeof(uint32_t) / sizeof(double) + (3 + 9 + 3 + 3 + 3 + 3 + 3 + 3) * sizeof(double)];
265  } r_ptr;
266 
267  uint32_t k_size;
268  int32_t r_k_label;
269  int32_t r_k_x;
270  int32_t r_k_theta;
271  int32_t r_k_r;
272  int32_t r_k_euler_123;
273  int32_t r_k_xp;
274  int32_t r_k_omega;
275  int32_t r_k_xpp;
276  int32_t r_k_omegap;
277  uint32_t d_size;
278  int32_t r_d_label;
279  int32_t r_d_f;
280  int32_t r_d_m;
281 #define MBC_R_PTR(mbc, type, off) ((off) < 0 ? NULL : ((type *)&((mbc)->mbcr.r_ptr.type ## _r_ptr[(off)])))
282 #define MBC_R_K_LABEL(mbc) (MBC_R_PTR((mbc), uint32_t, (mbc)->mbcr.r_k_label)[0])
283 #define MBC_R_X(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_x))
284 #define MBC_R_THETA(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_theta))
285 #define MBC_R_R(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_r))
286 #define MBC_R_EULER_123(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_euler_123))
287 #define MBC_R_XP(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_xp))
288 #define MBC_R_OMEGA(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_omega))
289 #define MBC_R_XPP(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_xpp))
290 #define MBC_R_OMEGAP(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_k_omegap))
291 #define MBC_R_D_LABEL(mbc) (MBC_R_PTR((mbc), uint32_t, (mbc)->mbcr.r_d_label)[0])
292 #define MBC_R_F(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_d_f))
293 #define MBC_R_M(mbc) (MBC_R_PTR((mbc), double, (mbc)->mbcr.r_d_m))
294 #define MBC_R_KINEMATICS_SIZE(mbc) ((mbc)->mbcr.k_size)
295 #define MBC_R_DYNAMICS_SIZE(mbc) ((mbc)->mbcr.d_size)
296 #define MBC_R_SIZE(mbc) (MBC_R_KINEMATICS_SIZE(mbc) + MBC_R_DYNAMICS_SIZE(mbc))
297 #define MBC_R_KINEMATICS(mbc) ((void *)(mbc)->mbcr.r_ptr.char_r_ptr)
298 #define MBC_R_DYNAMICS(mbc) ((void *)(MBC_R_KINEMATICS(mbc) + MBC_R_KINEMATICS_SIZE(mbc)))
299 } mbc_rigid_t;
300 
334 typedef struct {
337 
338  /* nodal data */
339  uint32_t nodes;
340  uint32_t k_size;
341 
342  char *n_ptr;
343  /* n_k_labels optional */
344  uint32_t *n_k_labels;
345  double *n_k_x;
346  /* n_k_theta, n_k_r, n_k_euler_123 mutually exclusive */
347  double *n_k_theta;
348  double *n_k_r;
349  double *n_k_euler_123;
350  double *n_k_xp;
351  double *n_k_omega;
352  /* n_k_xpp, n_k_omegap optional */
353  double *n_k_xpp;
354  double *n_k_omegap;
355 
356  uint32_t d_size;
357  /* n_d_labels optional */
358  uint32_t *n_d_labels;
359  double *n_d_f;
360  double *n_d_m;
361 
362 #define MBC_N_K_LABELS(mbc) ((mbc)->n_k_labels)
363 #define MBC_N_X(mbc) ((mbc)->n_k_x)
364 #define MBC_N_THETA(mbc) ((mbc)->n_k_theta)
365 #define MBC_N_R(mbc) ((mbc)->n_k_r)
366 #define MBC_N_EULER_123(mbc) ((mbc)->n_k_euler_123)
367 #define MBC_N_XP(mbc) ((mbc)->n_k_xp)
368 #define MBC_N_OMEGA(mbc) ((mbc)->n_k_omega)
369 #define MBC_N_XPP(mbc) ((mbc)->n_k_xpp)
370 #define MBC_N_OMEGAP(mbc) ((mbc)->n_k_omegap)
371 #define MBC_N_D_LABELS(mbc) ((mbc)->n_d_labels)
372 #define MBC_N_F(mbc) ((mbc)->n_d_f)
373 #define MBC_N_M(mbc) ((mbc)->n_d_m)
374 #define MBC_N_KINEMATICS_SIZE(mbc) ((mbc)->k_size)
375 #define MBC_N_DYNAMICS_SIZE(mbc) ((mbc)->d_size)
376 #define MBC_N_KINEMATICS(mbc) ((void *)(mbc)->n_ptr)
377 #define MBC_N_DYNAMICS(mbc) ((void *)(MBC_N_KINEMATICS(mbc) + MBC_N_KINEMATICS_SIZE(mbc)))
378 #define MBC_N_SIZE(mbc) (MBC_N_KINEMATICS_SIZE(mbc) + MBC_N_DYNAMICS_SIZE(mbc))
379 } mbc_nodal_t;
380 
405 extern int
406 mbc_nodal_init(mbc_nodal_t *mbc, unsigned refnode, unsigned nodes,
407  unsigned labels, unsigned rot, unsigned accels);
408 
417 extern int
419 
434 /*
435  * the protocol consists in:
436  *
437  * - negotiation request:
438  * - the negotiation request tag (ES_NEGOTIATION, uint8_t)
439  * - a bunch of flags (uint32_t), containing:
440  * - the type of interface (MBC_MODAL or MBC_NODAL)
441  * - whether a reference node is defined, MBC_REF_NODE OR-ed to previous
442  * - more...
443  * - the number of nodes (uint32_t)
444  *
445  * - negotiation response:
446  * - the negotiation response tag (ES_OK or ES_ABORT, uint8_t)
447  */
448 extern int
450 
452 /*
453  * companion of above, provided for completeness; not used
454  */
455 extern int
457 
468 extern int
470 
487 extern int
489 
490 
491 
509 typedef struct {
512 
513  /* modal data */
514  uint32_t modes;
515  double *m;
516 #define MBC_Q(mbc) (&(mbc)->m[0])
517 #define MBC_QP(mbc) (&(mbc)->m[(mbc)->modes])
518 #define MBC_P(mbc) (&(mbc)->m[2*(mbc)->modes])
519 #define MBC_M_KINEMATICS(mbc) MBC_Q((mbc))
520 #define MBC_M_DYNAMICS(mbc) MBC_P((mbc))
521 #define MBC_M_KINEMATICS_SIZE(mbc) (2*(mbc)->modes*sizeof(double))
522 #define MBC_M_DYNAMICS_SIZE(mbc) ((mbc)->modes*sizeof(double))
523 #define MBC_M_SIZE(mbc) (3*(mbc)->modes*sizeof(double))
524 } mbc_modal_t;
525 
540 extern int
541 mbc_modal_init(mbc_modal_t *mbc, int refnode, unsigned modes);
542 
551 extern int
553 
568 /*
569  * the protocol consists in:
570  *
571  * - negotiation request:
572  * - the negotiation request tag (ES_NEGOTIATION, uint8_t)
573  * - a bunch of flags (uint32_t), containing:
574  * - the type of interface (MBC_MODAL or MBC_NODAL)
575  * - whether a reference node is defined, MBC_REF_NODE OR-ed to previous
576  * - more...
577  * - the number of modes (uint32_t)
578  *
579  * - negotiation response:
580  * - the negotiation response tag (ES_OK or ES_ABORT, uint8_t)
581  */
582 extern int
584 
589 extern int
591 
602 extern int
604 
620 extern int
622 
623 #ifdef __cplusplus
624 }
625 #endif /* __cplusplus */
626 
627 #endif /* MBC_H */
ESCmd
Legal commands.
Definition: mbc.h:49
int mbc_nodal_negotiate_response(mbc_nodal_t *mbc)
Unused.
mbc_t mbc
Definition: mbc.h:335
Definition: mbc.h:58
char * n_ptr
Definition: mbc.h:342
int mbc_nodal_get_motion(mbc_nodal_t *mbc)
Get nodal motion from peer.
double * n_k_r
Definition: mbc.h:348
nodal stuff (partially opaque).
Definition: mbc.h:509
double * n_d_m
Definition: mbc.h:360
int32_t r_d_label
Definition: mbc.h:278
int32_t r_d_m
Definition: mbc.h:280
mbc_rigid_t mbcr
Definition: mbc.h:336
double * n_k_xpp
Definition: mbc.h:353
int mbc_modal_destroy(mbc_modal_t *mbc)
Destroy modal data.
int32_t r_k_r
Definition: mbc.h:271
unsigned sock_flags
Definition: mbc.h:108
int mbc_nodal_init(mbc_nodal_t *mbc, unsigned refnode, unsigned nodes, unsigned labels, unsigned rot, unsigned accels)
Initialize nodal data.
int32_t r_k_euler_123
Definition: mbc.h:272
Definition: mbc.h:65
int32_t r_d_f
Definition: mbc.h:279
mbc_rigid_t mbcr
Definition: mbc.h:511
int mbc_modal_negotiate_response(mbc_modal_t *mbc)
Unused.
int verbose
Definition: mbc.h:128
int32_t r_k_xpp
Definition: mbc.h:275
double * n_k_euler_123
Definition: mbc.h:349
double * n_k_omegap
Definition: mbc.h:354
uint32_t * n_k_labels
Definition: mbc.h:344
int32_t r_k_label
Definition: mbc.h:268
static unsigned rot
Definition: mbc.h:71
double * n_k_omega
Definition: mbc.h:351
char data_and_next
Definition: mbc.h:125
int32_t r_k_omega
Definition: mbc.h:274
Definition: mbc.h:99
int send_flags
Definition: mbc.h:114
int sock
Definition: mbc.h:105
static int labels
uint32_t * n_d_labels
Definition: mbc.h:358
int mbc_modal_negotiate_request(mbc_modal_t *mbc)
Negotiate modal data.
Definition: mbc.h:66
uint32_t d_size
Definition: mbc.h:277
int32_t r_k_xp
Definition: mbc.h:273
int32_t r_k_x
Definition: mbc.h:269
int recv_flags
Definition: mbc.h:111
Definition: mbc.h:50
uint32_t modes
Definition: mbc.h:514
const char * host
Definition: autopilot.c:142
int mbc_modal_init(mbc_modal_t *mbc, int refnode, unsigned modes)
Initialize modal data.
int mbc_put_cmd(mbc_t *mbc)
Opaque.
Connection data structure (partially opaque)
Definition: mbc.h:103
int timeout
Definition: mbc.h:137
Definition: mbc.h:55
uint32_t d_size
Definition: mbc.h:356
int32_t r_k_omegap
Definition: mbc.h:276
double * n_d_f
Definition: mbc.h:359
uint32_t k_size
Definition: mbc.h:340
int mbc_nodal_put_forces(mbc_nodal_t *mbc, int last)
Put forces to peer.
int mbc_get_cmd(mbc_t *mbc)
Opaque.
static int nodes
static mbc_nodal_t * mbc
uint32_t flags
Definition: mbc.h:234
double * n_k_xp
Definition: mbc.h:350
int mbc_unix_init(mbc_t *mbc, const char *path)
Initialize communication using "unix" socket.
int mbc_check_cmd(mbc_t *mbc)
Opaque.
int mbc_nodal_negotiate_request(mbc_nodal_t *mbc)
Negotiate nodal data.
double * m
Definition: mbc.h:515
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
Definition: mbc.h:60
MBCType
Parameters used to control the communication type and fields.
Definition: mbc.h:64
int mbc_modal_get_motion(mbc_modal_t *mbc)
Get modal motion from peer.
double * n_k_x
Definition: mbc.h:345
Nodal stuff (partially opaque).
Definition: mbc.h:334
int32_t r_k_theta
Definition: mbc.h:270
Reference node (AKA "rigid") stuff (partially opaque).
Definition: mbc.h:233
unsigned short int port
Definition: autopilot.c:143
uint32_t k_size
Definition: mbc.h:267
Definition: mbc.h:73
uint8_t cmd
Definition: mbc.h:117
int mbc_nodal_destroy(mbc_nodal_t *mbc)
Destroy nodal data.
double * n_k_theta
Definition: mbc.h:347
mbc_t mbc
Definition: mbc.h:510
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.