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 #include "mbconfig.h"           /* This goes first in every *.c,*.cc file */
32 
33 #ifdef USE_SOCKET
34 
35 #include <stdlib.h>
36 #include <assert.h>
37 #include <cstring>
38 #include "mbcxx.h"
39 
40 MBCBase::Rot
GetRot(void) const41 MBCBase::GetRot(void) const
42 {
43 	assert(GetStatus() >= INITIALIZED);
44 	return MBCBase::Rot(MBC_F_ROT(GetRefNodePtr()));
45 }
46 
47 bool
bRefNode(void) const48 MBCBase::bRefNode(void) const
49 {
50 	assert(GetStatus() >= INITIALIZED);
51 	return MBC_F_REF_NODE(GetRefNodePtr());
52 }
53 
54 MBCBase::Rot
GetRefNodeRot(void) const55 MBCBase::GetRefNodeRot(void) const {
56 	assert(GetStatus() >= INITIALIZED);
57 	return MBCBase::Rot(MBC_F_ROT_REF_NODE(GetRefNodePtr()));
58 }
59 
60 bool
bAccelerations(void) const61 MBCBase::bAccelerations(void) const
62 {
63 	assert(GetStatus() >= INITIALIZED);
64 	return MBC_F_ACCELS(GetRefNodePtr());
65 }
66 
67 bool
bLabels(void) const68 MBCBase::bLabels(void) const
69 {
70 	assert(GetStatus() >= INITIALIZED);
71 	return MBC_F_LABELS(GetRefNodePtr());
72 }
73 
74 void
SetTimeout(int t)75 MBCBase::SetTimeout(int t)
76 {
77 	GetBasePtr()->timeout = t;
78 }
79 
80 void
SetVerbose(bool bv)81 MBCBase::SetVerbose(bool bv)
82 {
83 	GetBasePtr()->verbose = bv;
84 }
85 
86 void
SetDataAndNext(bool bd)87 MBCBase::SetDataAndNext(bool bd)
88 {
89 	GetBasePtr()->data_and_next = bd;
90 }
91 
92 bool
bVerbose(void) const93 MBCBase::bVerbose(void) const
94 {
95 	return GetBasePtr()->verbose;
96 }
97 
98 bool
bDataAndNext(void) const99 MBCBase::bDataAndNext(void) const
100 {
101 	return GetBasePtr()->data_and_next;
102 }
103 
104 MBCBase::Status
GetStatus(void) const105 MBCBase::GetStatus(void) const
106 {
107 	return m_status;
108 }
109 
110 void
SetStatus(MBCBase::Status s)111 MBCBase::SetStatus(MBCBase::Status s)
112 {
113 	m_status = s;
114 }
115 
MBCBase(void)116 MBCBase::MBCBase(void)
117 : m_status(NOT_READY)
118 {
119 }
120 
~MBCBase(void)121 MBCBase::~MBCBase(void)
122 {
123 }
124 
125 int
Init(const char * const path)126 MBCBase::Init(const char *const path)
127 {
128 	if (GetStatus() != INITIALIZED) return -1;
129 	int rc = mbc_unix_init(GetBasePtr(), path);
130 	if (rc == 0) SetStatus(SOCKET_READY);
131 	return rc;
132 }
133 
134 int
Init(const char * const host,short unsigned port)135 MBCBase::Init(const char *const host, short unsigned port)
136 {
137 	if (GetStatus() != INITIALIZED) return -1;
138 	int rc = mbc_inet_init(GetBasePtr(), host, port);
139 	if (rc == 0) SetStatus(SOCKET_READY);
140 	return rc;
141 }
142 
143 int
GetCmd(void) const144 MBCBase::GetCmd(void) const
145 {
146 	return GetBasePtr()->cmd;
147 }
148 
149 uint32_t
GetRefNodeKinematicsLabel(void) const150 MBCBase::GetRefNodeKinematicsLabel(void) const
151 {
152 	assert(GetStatus() == READY);
153 	return MBC_R_K_LABEL(GetRefNodePtr());
154 }
155 
156 uint32_t
KinematicsLabel(void) const157 MBCBase::KinematicsLabel(void) const
158 {
159 	assert(GetStatus() == READY);
160 	assert(bLabels());
161 	return MBC_R_K_LABEL(GetRefNodePtr());
162 }
163 
164 const double *const
GetRefNodeX(void) const165 MBCBase::GetRefNodeX(void) const
166 {
167 	assert(GetStatus() == READY);
168 	assert(bRefNode());
169 	return MBC_R_X(GetRefNodePtr());
170 }
171 
172 const double *const
GetRefNodeR(void) const173 MBCBase::GetRefNodeR(void) const
174 {
175 	assert(GetStatus() == READY);
176 	assert(bRefNode());
177 	assert(GetRefNodeRot() == MAT);
178 	return MBC_R_R(GetRefNodePtr());
179 }
180 
181 const double *const
GetRefNodeTheta(void) const182 MBCBase::GetRefNodeTheta(void) const
183 {
184 	assert(GetStatus() == READY);
185 	assert(bRefNode());
186 	assert(GetRefNodeRot() == THETA);
187 	return MBC_R_THETA(GetRefNodePtr());
188 }
189 
190 const double *const
GetRefNodeEuler123(void) const191 MBCBase::GetRefNodeEuler123(void) const
192 {
193 	assert(GetStatus() == READY);
194 	assert(bRefNode());
195 	assert(GetRefNodeRot() == EULER_123);
196 	return MBC_R_EULER_123(GetRefNodePtr());
197 }
198 
199 const double *const
GetRefNodeXP(void) const200 MBCBase::GetRefNodeXP(void) const
201 {
202 	assert(GetStatus() == READY);
203 	assert(bRefNode());
204 	return MBC_R_XP(GetRefNodePtr());
205 }
206 
207 const double *const
GetRefNodeOmega(void) const208 MBCBase::GetRefNodeOmega(void) const
209 {
210 	assert(GetStatus() == READY);
211 	assert(bRefNode());
212 	assert(GetRefNodeRot() != NONE);
213 	return MBC_R_OMEGA(GetRefNodePtr());
214 }
215 
216 const double *const
GetRefNodeXPP(void) const217 MBCBase::GetRefNodeXPP(void) const
218 {
219 	assert(GetStatus() == READY);
220 	assert(bRefNode());
221 	assert(bAccelerations());
222 	return MBC_R_XPP(GetRefNodePtr());
223 }
224 
225 const double *const
GetRefNodeOmegaP(void) const226 MBCBase::GetRefNodeOmegaP(void) const
227 {
228 	assert(GetStatus() == READY);
229 	assert(bRefNode());
230 	assert(GetRefNodeRot() != NONE);
231 	assert(bAccelerations());
232 	return MBC_R_OMEGAP(GetRefNodePtr());
233 }
234 
235 const double&
X(uint8_t idx) const236 MBCBase::X(uint8_t idx) const
237 {
238 	assert(GetStatus() == READY);
239 	assert(bRefNode());
240 	if (idx < 1 || idx > 3) throw;
241 	return (MBC_R_X(GetRefNodePtr()))[idx - 1];
242 }
243 
244 const double&
R(uint8_t ir,uint8_t ic) const245 MBCBase::R(uint8_t ir, uint8_t ic) const
246 {
247 	assert(GetStatus() == READY);
248 	assert(bRefNode());
249 	assert(GetRefNodeRot() == MAT);
250 	if (ir < 1 || ir > 3 || ic < 1 || ic > 3) throw;
251 	return (MBC_R_R(GetRefNodePtr()))[3*(ic - 1) + ir - 1];
252 }
253 
254 const double&
Theta(uint8_t idx) const255 MBCBase::Theta(uint8_t idx) const
256 {
257 	assert(GetStatus() == READY);
258 	assert(bRefNode());
259 	assert(GetRefNodeRot() == THETA);
260 	if (idx < 1 || idx > 3) throw;
261 	return (MBC_R_THETA(GetRefNodePtr()))[idx - 1];
262 }
263 
264 const double&
Euler123(uint8_t idx) const265 MBCBase::Euler123(uint8_t idx) const
266 {
267 	assert(GetStatus() == READY);
268 	assert(bRefNode());
269 	assert(GetRefNodeRot() == EULER_123);
270 	if (idx < 1 || idx > 3) throw;
271 	return (MBC_R_EULER_123(GetRefNodePtr()))[idx - 1];
272 }
273 
274 const double&
XP(uint8_t idx) const275 MBCBase::XP(uint8_t idx) const
276 {
277 	assert(GetStatus() == READY);
278 	assert(bRefNode());
279 	if (idx < 1 || idx > 3) throw;
280 	return (MBC_R_XP(GetRefNodePtr()))[idx - 1];
281 }
282 
283 const double&
Omega(uint8_t idx) const284 MBCBase::Omega(uint8_t idx) const
285 {
286 	assert(GetStatus() == READY);
287 	assert(bRefNode());
288 	assert(GetRefNodeRot() != NONE);
289 	if (idx < 1 || idx > 3) throw;
290 	return (MBC_R_OMEGA(GetRefNodePtr()))[idx - 1];
291 }
292 
293 const double&
XPP(uint8_t idx) const294 MBCBase::XPP(uint8_t idx) const
295 {
296 	assert(GetStatus() == READY);
297 	assert(bRefNode());
298 	assert(bAccelerations());
299 	if (idx < 1 || idx > 3) throw;
300 	return (MBC_R_XPP(GetRefNodePtr()))[idx - 1];
301 }
302 
303 const double&
OmegaP(uint8_t idx) const304 MBCBase::OmegaP(uint8_t idx) const
305 {
306 	assert(GetStatus() == READY);
307 	assert(bRefNode());
308 	assert(GetRefNodeRot() != NONE);
309 	assert(bAccelerations());
310 	if (idx < 1 || idx > 3) throw;
311 	return (MBC_R_OMEGAP(GetRefNodePtr()))[idx - 1];
312 }
313 
314 uint32_t
GetRefNodeDynamicsLabel(void) const315 MBCBase::GetRefNodeDynamicsLabel(void) const
316 {
317 	assert(GetStatus() == READY);
318 	assert(bRefNode());
319 	return MBC_R_D_LABEL(GetRefNodePtr());
320 }
321 
322 const uint32_t&
DynamicsLabel(void) const323 MBCBase::DynamicsLabel(void) const
324 {
325 	assert(GetStatus() == READY);
326 	assert(bRefNode());
327 	return MBC_R_D_LABEL(GetRefNodePtr());
328 }
329 
330 uint32_t&
DynamicsLabel(void)331 MBCBase::DynamicsLabel(void)
332 {
333 	assert(GetStatus() == READY);
334 	assert(bRefNode());
335 	return MBC_R_D_LABEL(GetRefNodePtr());
336 }
337 
338 const double *
GetRefNodeF(void) const339 MBCBase::GetRefNodeF(void) const
340 {
341 	assert(GetStatus() == READY);
342 	assert(bRefNode());
343 	return MBC_R_F(GetRefNodePtr());
344 }
345 
346 const double *
GetRefNodeM(void) const347 MBCBase::GetRefNodeM(void) const
348 {
349 	assert(GetStatus() == READY);
350 	assert(bRefNode());
351 	assert(GetRefNodeRot() != NONE);
352 	return MBC_R_M(GetRefNodePtr());
353 }
354 
355 const double&
F(uint8_t idx) const356 MBCBase::F(uint8_t idx) const
357 {
358 	assert(GetStatus() == READY);
359 	assert(bRefNode());
360 	if (idx < 1 || idx > 3) throw;
361 	return (MBC_R_F(GetRefNodePtr()))[idx - 1];
362 }
363 
364 double&
F(uint8_t idx)365 MBCBase::F(uint8_t idx)
366 {
367 	assert(GetStatus() == READY);
368 	assert(bRefNode());
369 	if (idx < 1 || idx > 3) throw;
370 	return (MBC_R_F(GetRefNodePtr()))[idx - 1];
371 }
372 
373 const double&
M(uint8_t idx) const374 MBCBase::M(uint8_t idx) const
375 {
376 	assert(GetStatus() == READY);
377 	assert(bRefNode());
378 	assert(GetRefNodeRot() != NONE);
379 	if (idx < 1 || idx > 3) throw;
380 	return (MBC_R_M(GetRefNodePtr()))[idx - 1];
381 }
382 
383 double&
M(uint8_t idx)384 MBCBase::M(uint8_t idx)
385 {
386 	assert(GetStatus() == READY);
387 	assert(bRefNode());
388 	assert(GetRefNodeRot() != NONE);
389 	if (idx < 1 || idx > 3) throw;
390 	return (MBC_R_M(GetRefNodePtr()))[idx - 1];
391 }
392 
393 mbc_t *
GetBasePtr(void) const394 MBCNodal::GetBasePtr(void) const
395 {
396 	return (mbc_t*)&mbc;
397 }
398 
399 mbc_refnode_stub_t *
GetRefNodePtr(void) const400 MBCNodal::GetRefNodePtr(void) const
401 {
402 	return (mbc_refnode_stub_t *)&mbc;
403 }
404 
MBCNodal(void)405 MBCNodal::MBCNodal(void)
406 {
407 	memset(&mbc, 0, sizeof(mbc));
408 }
409 
MBCNodal(MBCBase::Rot refnode_rot,unsigned nodes,bool labels,MBCBase::Rot rot,bool accels)410 MBCNodal::MBCNodal(MBCBase::Rot refnode_rot, unsigned nodes,
411        	bool labels, MBCBase::Rot rot, bool accels)
412 {
413 	if (Initialize(refnode_rot, nodes, labels, rot, accels)) {
414 		throw;
415 	}
416 }
417 
~MBCNodal(void)418 MBCNodal::~MBCNodal(void)
419 {
420 	Close();
421 }
422 
423 MBCBase::Type
GetType(void) const424 MBCNodal::GetType(void) const
425 {
426 	return NODAL;
427 }
428 
429 int
Initialize(MBCBase::Rot refnode_rot,unsigned nodes,bool labels,MBCBase::Rot rot,bool accels)430 MBCNodal::Initialize(MBCBase::Rot refnode_rot, unsigned nodes,
431        	bool labels, MBCBase::Rot rot, bool accels)
432 {
433 	if (GetStatus() != NOT_READY) return -1;
434 	memset(&mbc, 0, sizeof(mbc));
435 	int rc = mbc_nodal_init(&mbc, refnode_rot, nodes, labels,
436 		unsigned(rot) | MBC_U_ROT_2_REF_NODE_ROT(unsigned(refnode_rot)),
437 		accels);
438 	if (rc == 0) const_cast<MBCNodal *>(this)->SetStatus(INITIALIZED);
439 	return rc;
440 }
441 
442 int
Negotiate(void) const443 MBCNodal::Negotiate(void) const
444 {
445 	if (GetStatus() != SOCKET_READY) return -1;
446 	int rc = mbc_nodal_negotiate_request(&mbc);
447 	if (rc == 0) const_cast<MBCNodal *>(this)->SetStatus(READY);
448 	return rc;
449 }
450 
451 int
PutForces(bool bConverged) const452 MBCNodal::PutForces(bool bConverged) const
453 {
454 	if (GetStatus() != READY) return -1;
455 	return mbc_nodal_put_forces(&mbc, bConverged);
456 }
457 
458 int
GetMotion(void) const459 MBCNodal::GetMotion(void) const
460 {
461 	if (GetStatus() != READY) return -1;
462 	return mbc_nodal_get_motion(&mbc);
463 }
464 
465 int
Close(void) const466 MBCNodal::Close(void) const
467 {
468 	int rc = -1;
469 	if (GetStatus() == READY) {
470 		rc = mbc_nodal_destroy(&mbc);
471 		const_cast<MBCNodal *>(this)->SetStatus(CLOSED);
472 	}
473 	return rc;
474 }
475 
476 uint32_t
KinematicsLabel(void) const477 MBCNodal::KinematicsLabel(void) const
478 {
479 	return MBCBase::KinematicsLabel();
480 }
481 
482 const double&
X(uint8_t idx) const483 MBCNodal::X(uint8_t idx) const
484 {
485 	return MBCBase::X(idx);
486 }
487 
488 const double&
R(uint8_t ir,uint8_t ic) const489 MBCNodal::R(uint8_t ir, uint8_t ic) const
490 {
491 	return MBCBase::R(ir, ic);
492 }
493 
494 const double&
Theta(uint8_t idx) const495 MBCNodal::Theta(uint8_t idx) const
496 {
497 	return MBCBase::Theta(idx);
498 }
499 
500 const double&
Euler123(uint8_t idx) const501 MBCNodal::Euler123(uint8_t idx) const
502 {
503 	return MBCBase::Euler123(idx);
504 }
505 
506 const double&
XP(uint8_t idx) const507 MBCNodal::XP(uint8_t idx) const
508 {
509 	return MBCBase::XP(idx);
510 }
511 
512 const double&
Omega(uint8_t idx) const513 MBCNodal::Omega(uint8_t idx) const
514 {
515 	return MBCBase::Omega(idx);
516 }
517 
518 const double&
XPP(uint8_t idx) const519 MBCNodal::XPP(uint8_t idx) const
520 {
521 	return MBCBase::XPP(idx);
522 }
523 
524 const double&
OmegaP(uint8_t idx) const525 MBCNodal::OmegaP(uint8_t idx) const
526 {
527 	return MBCBase::OmegaP(idx);
528 }
529 
530 const uint32_t&
DynamicsLabel(void) const531 MBCNodal::DynamicsLabel(void) const
532 {
533 	return MBCBase::DynamicsLabel();
534 }
535 
536 uint32_t&
DynamicsLabel(void)537 MBCNodal::DynamicsLabel(void)
538 {
539 	return MBCBase::DynamicsLabel();
540 }
541 
542 const double&
F(uint8_t idx) const543 MBCNodal::F(uint8_t idx) const
544 {
545 	return MBCBase::F(idx);
546 }
547 
548 double&
F(uint8_t idx)549 MBCNodal::F(uint8_t idx)
550 {
551 	return MBCBase::F(idx);
552 }
553 
554 const double&
M(uint8_t idx) const555 MBCNodal::M(uint8_t idx) const
556 {
557 	return MBCBase::M(idx);
558 }
559 
560 double&
M(uint8_t idx)561 MBCNodal::M(uint8_t idx)
562 {
563 	return MBCBase::M(idx);
564 }
565 
566 uint32_t
GetNodes(void) const567 MBCNodal::GetNodes(void) const
568 {
569 	assert(GetStatus() >= INITIALIZED);
570 	return mbc.nodes;
571 }
572 
573 uint32_t *
GetKinematicsLabel(void) const574 MBCNodal::GetKinematicsLabel(void) const
575 {
576 	assert(GetStatus() == READY);
577 	assert(bLabels());
578 	return MBC_N_K_LABELS(&mbc);
579 }
580 
581 const double *const
GetX(void) const582 MBCNodal::GetX(void) const
583 {
584 	assert(GetStatus() == READY);
585 	return MBC_N_X(&mbc);
586 }
587 
588 const double *const
GetR(void) const589 MBCNodal::GetR(void) const
590 {
591 	assert(GetStatus() == READY);
592 	assert(GetRot() == MAT);
593 	return MBC_N_R(&mbc);
594 }
595 
596 const double *const
GetTheta(void) const597 MBCNodal::GetTheta(void) const
598 {
599 	assert(GetStatus() == READY);
600 	assert(GetRot() == THETA);
601 	return MBC_N_THETA(&mbc);
602 }
603 
604 const double *const
GetEuler123(void) const605 MBCNodal::GetEuler123(void) const
606 {
607 	assert(GetStatus() == READY);
608 	assert(GetRot() == EULER_123);
609 	return MBC_N_EULER_123(&mbc);
610 }
611 
612 const double *const
GetXP(void) const613 MBCNodal::GetXP(void) const
614 {
615 	assert(GetStatus() == READY);
616 	return MBC_N_XP(&mbc);
617 }
618 
619 const double *const
GetOmega(void) const620 MBCNodal::GetOmega(void) const
621 {
622 	assert(GetStatus() == READY);
623 	assert(GetRot() != NONE);
624 	return MBC_N_OMEGA(&mbc);
625 }
626 
627 const double *const
GetXPP(void) const628 MBCNodal::GetXPP(void) const
629 {
630 	assert(GetStatus() == READY);
631 	assert(bAccelerations());
632 	return MBC_N_XPP(&mbc);
633 }
634 
635 const double *const
GetOmegaP(void) const636 MBCNodal::GetOmegaP(void) const
637 {
638 	assert(GetStatus() == READY);
639 	assert(GetRot() != NONE);
640 	assert(bAccelerations());
641 	return MBC_N_OMEGAP(&mbc);
642 }
643 
644 const double&
X(uint32_t n,uint8_t idx) const645 MBCNodal::X(uint32_t n, uint8_t idx) const
646 {
647 	assert(GetStatus() == READY);
648 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
649 	return (MBC_N_X(&mbc))[3*(n - 1) + idx - 1];
650 }
651 
652 const double&
R(uint32_t n,uint8_t ir,uint8_t ic) const653 MBCNodal::R(uint32_t n, uint8_t ir, uint8_t ic) const
654 {
655 	assert(GetStatus() == READY);
656 	assert(GetRot() == MAT);
657 	if (ir < 1 || ir > 3 || ic < 1 || ic > 3 || n < 1 || n > GetNodes()) throw;
658 	return (MBC_N_R(&mbc))[9*(n - 1) + 3*(ic - 1) + ir - 1];
659 }
660 
661 const double&
Theta(uint32_t n,uint8_t idx) const662 MBCNodal::Theta(uint32_t n, uint8_t idx) const
663 {
664 	assert(GetStatus() == READY);
665 	assert(GetRot() == THETA);
666 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
667 	return (MBC_N_THETA(&mbc))[3*(n - 1) + idx - 1];
668 }
669 
670 const double&
Euler123(uint32_t n,uint8_t idx) const671 MBCNodal::Euler123(uint32_t n, uint8_t idx) const
672 {
673 	assert(GetStatus() == READY);
674 	assert(GetRot() == EULER_123);
675 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
676 	return (MBC_N_EULER_123(&mbc))[3*(n - 1) + idx - 1];
677 }
678 
679 const double&
XP(uint32_t n,uint8_t idx) const680 MBCNodal::XP(uint32_t n, uint8_t idx) const
681 {
682 	assert(GetStatus() == READY);
683 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
684 	return (MBC_N_XP(&mbc))[3*(n - 1) + idx - 1];
685 }
686 
687 const double&
Omega(uint32_t n,uint8_t idx) const688 MBCNodal::Omega(uint32_t n, uint8_t idx) const
689 {
690 	assert(GetStatus() == READY);
691 	assert(GetRot() != NONE);
692 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
693 	return (MBC_N_OMEGA(&mbc))[3*(n - 1) + idx - 1];
694 }
695 
696 const double&
XPP(uint32_t n,uint8_t idx) const697 MBCNodal::XPP(uint32_t n, uint8_t idx) const
698 {
699 	assert(GetStatus() == READY);
700 	assert(bAccelerations());
701 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
702 	return (MBC_N_XPP(&mbc))[3*(n - 1) + idx - 1];
703 }
704 
705 const double&
OmegaP(uint32_t n,uint8_t idx) const706 MBCNodal::OmegaP(uint32_t n, uint8_t idx) const
707 {
708 	assert(GetStatus() == READY);
709 	assert(GetRot() != NONE);
710 	assert(bAccelerations());
711 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
712 	return (MBC_N_OMEGAP(&mbc))[3*(n - 1) + idx - 1];
713 }
714 
715 uint32_t
GetKinematicsLabel(uint32_t n) const716 MBCNodal::GetKinematicsLabel(uint32_t n) const
717 {
718 	assert(GetStatus() == READY);
719 	assert(bLabels());
720 	if (n < 1 || n > GetNodes()) throw;
721 	return (MBC_N_K_LABELS(&mbc))[n - 1];
722 }
723 
724 uint32_t
KinematicsLabel(uint32_t n) const725 MBCNodal::KinematicsLabel(uint32_t n) const
726 {
727 	assert(GetStatus() == READY);
728 	assert(bLabels());
729 	if (n < 1 || n > GetNodes()) throw;
730 	return (MBC_N_K_LABELS(&mbc))[n - 1];
731 }
732 
733 const double *const
GetX(uint32_t n) const734 MBCNodal::GetX(uint32_t n) const
735 {
736 	assert(GetStatus() == READY);
737 	return &(MBC_N_X(&mbc)[3*(n - 1)]);
738 }
739 
740 const double *const
GetR(uint32_t n) const741 MBCNodal::GetR(uint32_t n) const
742 {
743 	assert(GetStatus() == READY);
744 	assert(GetRot() == MAT);
745 	return &(MBC_N_R(&mbc)[9*(n - 1)]);
746 }
747 
748 const double *const
GetTheta(uint32_t n) const749 MBCNodal::GetTheta(uint32_t n) const
750 {
751 	assert(GetStatus() == READY);
752 	assert(GetRot() == THETA);
753 	return &(MBC_N_THETA(&mbc)[3*(n - 1)]);
754 }
755 
756 const double *const
GetEuler123(uint32_t n) const757 MBCNodal::GetEuler123(uint32_t n) const
758 {
759 	assert(GetStatus() == READY);
760 	assert(GetRot() == EULER_123);
761 	return &(MBC_N_EULER_123(&mbc)[3*(n - 1)]);
762 }
763 
764 const double *const
GetXP(uint32_t n) const765 MBCNodal::GetXP(uint32_t n) const
766 {
767 	assert(GetStatus() == READY);
768 	return &(MBC_N_XP(&mbc)[3*(n - 1)]);
769 }
770 
771 const double *const
GetOmega(uint32_t n) const772 MBCNodal::GetOmega(uint32_t n) const
773 {
774 	assert(GetStatus() == READY);
775 	assert(GetRot() != NONE);
776 	return &(MBC_N_OMEGA(&mbc)[3*(n - 1)]);
777 }
778 
779 const double *const
GetXPP(uint32_t n) const780 MBCNodal::GetXPP(uint32_t n) const
781 {
782 	assert(GetStatus() == READY);
783 	assert(bAccelerations());
784 	return &(MBC_N_XPP(&mbc)[3*(n - 1)]);
785 }
786 
787 const double *const
GetOmegaP(uint32_t n) const788 MBCNodal::GetOmegaP(uint32_t n) const
789 {
790 	assert(GetStatus() == READY);
791 	assert(GetRot() != NONE);
792 	assert(bAccelerations());
793 	return &(MBC_N_OMEGAP(&mbc)[3*(n - 1)]);
794 }
795 
796 uint32_t *
GetDynamicsLabel(void) const797 MBCNodal::GetDynamicsLabel(void) const
798 {
799 	assert(GetStatus() == READY);
800 	assert(bLabels());
801 	return MBC_N_D_LABELS(&mbc);
802 }
803 
804 const uint32_t&
DynamicsLabel(uint32_t n) const805 MBCNodal::DynamicsLabel(uint32_t n) const
806 {
807 	assert(GetStatus() == READY);
808 	assert(bLabels());
809 	if (n < 1 || n > GetNodes()) throw;
810 	return (MBC_N_D_LABELS(&mbc))[n - 1];
811 }
812 
813 uint32_t&
DynamicsLabel(uint32_t n)814 MBCNodal::DynamicsLabel(uint32_t n)
815 {
816 	assert(GetStatus() == READY);
817 	assert(bLabels());
818 	if (n < 1 || n > GetNodes()) throw;
819 	return (MBC_N_D_LABELS(&mbc))[n - 1];
820 }
821 
822 const double *
GetF(void) const823 MBCNodal::GetF(void) const
824 {
825 	assert(GetStatus() == READY);
826 	return MBC_N_F(&mbc);
827 }
828 
829 const double *
GetM(void) const830 MBCNodal::GetM(void) const
831 {
832 	assert(GetStatus() == READY);
833 	assert(GetRot() != NONE);
834 	return MBC_N_M(&mbc);
835 }
836 
837 const double *
GetF(uint32_t n) const838 MBCNodal::GetF(uint32_t n) const
839 {
840 	assert(GetStatus() == READY);
841 	if (n < 1 || n > GetNodes()) throw;
842 	return &(MBC_N_F(&mbc))[3*(n - 1)];
843 }
844 
845 const double *
GetM(uint32_t n) const846 MBCNodal::GetM(uint32_t n) const
847 {
848 	assert(GetStatus() == READY);
849 	assert(GetRot() != NONE);
850 	if (n < 1 || n > GetNodes()) throw;
851 	return &(MBC_N_M(&mbc))[3*(n - 1)];
852 }
853 
854 const double&
F(uint32_t n,uint8_t idx) const855 MBCNodal::F(uint32_t n, uint8_t idx) const
856 {
857 	assert(GetStatus() == READY);
858 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
859 	return (MBC_N_F(&mbc))[3*(n - 1) + idx - 1];
860 }
861 
862 double&
F(uint32_t n,uint8_t idx)863 MBCNodal::F(uint32_t n, uint8_t idx)
864 {
865 	assert(GetStatus() == READY);
866 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
867 	return (MBC_N_F(&mbc))[3*(n - 1) + idx - 1];
868 }
869 
870 const double&
M(uint32_t n,uint8_t idx) const871 MBCNodal::M(uint32_t n, uint8_t idx) const
872 {
873 	assert(GetStatus() == READY);
874 	assert(GetRot() != NONE);
875 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
876 	return (MBC_N_M(&mbc))[3*(n - 1) + idx - 1];
877 }
878 
879 double&
M(uint32_t n,uint8_t idx)880 MBCNodal::M(uint32_t n, uint8_t idx)
881 {
882 	assert(GetStatus() == READY);
883 	assert(GetRot() != NONE);
884 	if (idx < 1 || idx > 3 || n < 1 || n > GetNodes()) throw;
885 	return (MBC_N_M(&mbc))[3*(n - 1) + idx - 1];
886 }
887 
888 mbc_t *
GetBasePtr(void) const889 MBCModal::GetBasePtr(void) const
890 {
891 	return (mbc_t*)&mbc;
892 }
893 
894 mbc_refnode_stub_t *
GetRefNodePtr(void) const895 MBCModal::GetRefNodePtr(void) const
896 {
897 	return (mbc_refnode_stub_t *)&mbc;
898 }
899 
MBCModal(void)900 MBCModal::MBCModal(void)
901 {
902 	memset(&mbc, 0, sizeof(mbc));
903 }
904 
MBCModal(MBCBase::Rot refnode_rot,unsigned modes)905 MBCModal::MBCModal(MBCBase::Rot refnode_rot, unsigned modes)
906 {
907 	if (Initialize(refnode_rot, modes)) {
908 		throw;
909 	}
910 }
911 
~MBCModal(void)912 MBCModal::~MBCModal(void)
913 {
914 	Close();
915 }
916 
917 MBCBase::Type
GetType(void) const918 MBCModal::GetType(void) const
919 {
920 	return MODAL;
921 }
922 
923 int
Initialize(MBCBase::Rot refnode_rot,unsigned modes)924 MBCModal::Initialize(MBCBase::Rot refnode_rot, unsigned modes)
925 {
926 	if (GetStatus() != NOT_READY) return -1;
927 	memset(&mbc, 0, sizeof(mbc));
928 	int rc = mbc_modal_init(&mbc, refnode_rot, modes);
929 	if (rc == 0) const_cast<MBCModal *>(this)->SetStatus(INITIALIZED);
930 	return rc;
931 }
932 
933 int
Negotiate(void) const934 MBCModal::Negotiate(void) const
935 {
936 	if (GetStatus() != SOCKET_READY) return -1;
937 	int rc = mbc_modal_negotiate_request(&mbc);
938 	if (rc == 0) const_cast<MBCModal *>(this)->SetStatus(READY);
939 	return rc;
940 }
941 
942 int
PutForces(bool bConverged) const943 MBCModal::PutForces(bool bConverged) const
944 {
945 	if (GetStatus() != READY) return -1;
946 	return mbc_modal_put_forces(&mbc, bConverged);
947 }
948 
949 int
GetMotion(void) const950 MBCModal::GetMotion(void) const
951 {
952 	if (GetStatus() != READY) return -1;
953 	return mbc_modal_get_motion(&mbc);
954 }
955 
956 int
Close(void) const957 MBCModal::Close(void) const
958 {
959 	int rc = -1;
960 	if (GetStatus() == READY) {
961 		rc = mbc_modal_destroy(&mbc);
962 		const_cast<MBCModal *>(this)->SetStatus(CLOSED);
963 	}
964 	return rc;
965 }
966 
967 uint32_t
KinematicsLabel(void) const968 MBCModal::KinematicsLabel(void) const
969 {
970 	return MBCBase::KinematicsLabel();
971 }
972 
973 const double&
X(uint8_t idx) const974 MBCModal::X(uint8_t idx) const
975 {
976 	return MBCBase::X(idx);
977 }
978 
979 const double&
R(uint8_t ir,uint8_t ic) const980 MBCModal::R(uint8_t ir, uint8_t ic) const
981 {
982 	return MBCBase::R(ir, ic);
983 }
984 
985 const double&
Theta(uint8_t idx) const986 MBCModal::Theta(uint8_t idx) const
987 {
988 	return MBCBase::Theta(idx);
989 }
990 
991 const double&
Euler123(uint8_t idx) const992 MBCModal::Euler123(uint8_t idx) const
993 {
994 	return MBCBase::Euler123(idx);
995 }
996 
997 const double&
XP(uint8_t idx) const998 MBCModal::XP(uint8_t idx) const
999 {
1000 	return MBCBase::XP(idx);
1001 }
1002 
1003 const double&
Omega(uint8_t idx) const1004 MBCModal::Omega(uint8_t idx) const
1005 {
1006 	return MBCBase::Omega(idx);
1007 }
1008 
1009 const double&
XPP(uint8_t idx) const1010 MBCModal::XPP(uint8_t idx) const
1011 {
1012 	return MBCBase::XPP(idx);
1013 }
1014 
1015 const double&
OmegaP(uint8_t idx) const1016 MBCModal::OmegaP(uint8_t idx) const
1017 {
1018 	return MBCBase::OmegaP(idx);
1019 }
1020 
1021 const uint32_t&
DynamicsLabel(void) const1022 MBCModal::DynamicsLabel(void) const
1023 {
1024 	return MBCBase::DynamicsLabel();
1025 }
1026 
1027 uint32_t&
DynamicsLabel(void)1028 MBCModal::DynamicsLabel(void)
1029 {
1030 	return MBCBase::DynamicsLabel();
1031 }
1032 
1033 const double&
F(uint8_t idx) const1034 MBCModal::F(uint8_t idx) const
1035 {
1036 	return MBCBase::F(idx);
1037 }
1038 
1039 double&
F(uint8_t idx)1040 MBCModal::F(uint8_t idx)
1041 {
1042 	return MBCBase::F(idx);
1043 }
1044 
1045 const double&
M(uint8_t idx) const1046 MBCModal::M(uint8_t idx) const
1047 {
1048 	return MBCBase::M(idx);
1049 }
1050 
1051 double&
M(uint8_t idx)1052 MBCModal::M(uint8_t idx)
1053 {
1054 	return MBCBase::M(idx);
1055 }
1056 
1057 uint32_t
GetModes(void) const1058 MBCModal::GetModes(void) const
1059 {
1060 	assert(GetStatus() == READY);
1061 	return mbc.modes;
1062 }
1063 
1064 const double *const
GetQ(void) const1065 MBCModal::GetQ(void) const
1066 {
1067 	assert(GetStatus() == READY);
1068 	return MBC_Q(&mbc);
1069 }
1070 
1071 const double *const
GetQP(void) const1072 MBCModal::GetQP(void) const
1073 {
1074 	assert(GetStatus() == READY);
1075 	return MBC_QP(&mbc);
1076 }
1077 
1078 const double&
Q(uint32_t m) const1079 MBCModal::Q(uint32_t m) const
1080 {
1081 	assert(GetStatus() == READY);
1082 	if (m < 1 || m > GetModes()) throw;
1083 	return (MBC_Q(&mbc))[m - 1];
1084 }
1085 
1086 const double&
QP(uint32_t m) const1087 MBCModal::QP(uint32_t m) const
1088 {
1089 	assert(GetStatus() == READY);
1090 	if (m < 1 || m > GetModes()) throw;
1091 	return (MBC_QP(&mbc))[m - 1];
1092 }
1093 
1094 const double *
GetP(void) const1095 MBCModal::GetP(void) const
1096 {
1097 	assert(GetStatus() == READY);
1098 	return MBC_P(&mbc);
1099 }
1100 
1101 const double&
P(uint32_t m) const1102 MBCModal::P(uint32_t m) const
1103 {
1104 	assert(GetStatus() == READY);
1105 	if (m < 1 || m > GetModes()) throw;
1106 	return (MBC_P(&mbc))[m - 1];
1107 }
1108 
1109 double&
P(uint32_t m)1110 MBCModal::P(uint32_t m)
1111 {
1112 	assert(GetStatus() == READY);
1113 	if (m < 1 || m > GetModes()) throw;
1114 	return (MBC_P(&mbc))[m - 1];
1115 }
1116 
1117 #endif // USE_SOCKET
1118