MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
extforce.cc
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/mbdyn/base/extforce.cc,v 1.51 2017/08/03 11:49:02 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 2007-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 "mbconfig.h" /* This goes first in every *.c,*.cc file */
33 
34 #include "dataman.h"
35 #include "extforce.h"
36 #include "extedge.h"
37 #include "except.h"
38 #include "solver.h"
39 
40 #include <fstream>
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <cstdlib>
45 #include <unistd.h>
46 #include <cerrno>
47 
48 /* ExtFileHandlerBase - begin */
49 
51  std::streamsize Precision)
52 : Precision(Precision), SleepTime(SleepTime), bOK(true)
53 {
54  NO_OP;
55 }
56 
58 {
59  NO_OP;
60 }
61 
64 {
65  return NEGOTIATE_NO;
66 }
67 
68 /* NOTE: getting here, in general, should be considered Bad (TM)
69  * however, right now, it is used to distinguish whether communication
70  * will occur on a iostream or a file descriptor */
71 std::ostream *
73 {
74  return 0;
75 }
76 
77 std::istream *
79 {
80  return 0;
81 }
82 
83 /* NOTE: getting here Is Bad (TM) */
84 int
86 {
88  // return -1;
89 }
90 
91 int
93 {
95  // return 0;
96 }
97 
98 int
100 {
102  // return -1;
103 }
104 
105 int
107 {
109  // return 0;
110 }
111 
112 /* ExtFileHandlerBase - end */
113 
114 /* ExtFileHandler - begin */
115 
117  bool bRemoveIn,
118  std::string& fout,
119  bool bNoClobberOut,
120  mbsleep_t SleepTime,
121  std::streamsize Precision)
122 : ExtFileHandlerBase(SleepTime, Precision),
123 fin(fin), fout(fout), tmpout(fout + ".tmp"),
124 bRemoveIn(bRemoveIn), bNoClobberOut(bNoClobberOut)
125 {
126  NO_OP;
127 }
128 
130 {
131  NO_OP;
132 }
133 
134 bool
136 {
137  return true;
138 }
139 
140 void
142 {
143  NO_OP;
144 }
145 
146 void
148 {
149  NO_OP;
150 }
151 
152 bool
154 {
155  if (bNoClobberOut) {
156  bool bKeepGoing(true);
157 
158  for (int cnt = 0; bKeepGoing; cnt++) {
159  struct stat s;
160 
161  if (stat(fout.c_str(), &s) != 0) {
162  int save_errno = errno;
163 
164  switch (save_errno) {
165  case ENOENT:
166  bKeepGoing = false;
167  break;
168 
169  default:
170  silent_cerr("unable to stat output file "
171  "\"" << fout.c_str() << "\": "
172  << strerror(save_errno)
173  << std::endl);
175  }
176 
177  } else {
179  outf.setstate(std::ios_base::badbit);
180  return false;
181  }
182 
183  if (SleepTime > 0) {
184  silent_cout("output file "
185  "\"" << fout.c_str() << "\" "
186  "still present, "
187  "try #" << cnt << "; "
188  "sleeping " << SleepTime << " s"
189  << std::endl);
190  mbsleep(&SleepTime);
191 
192  } else {
193  silent_cout("output file "
194  "\"" << fout.c_str() << "\" "
195  "still present, "
196  "try #" << cnt
197  << std::endl);
198  }
199  }
200  }
201  }
202 
203  outf.open(tmpout.c_str());
204 
205  if (!outf) {
206  silent_cerr("unable to open file \"" << fout.c_str() << "\""
207  << std::endl);
209  }
210 
211  if (Precision != 0) {
212  outf.precision(Precision);
213  }
214  outf.setf(std::ios::scientific);
215  return outf.good();
216 }
217 
218 void
220 {
221  outf.close();
222  if (rename(tmpout.c_str(), fout.c_str()) != 0) {
223  int save_errno = errno;
224  silent_cerr("ExtFileHandler: unable to rename output file "
225  "\"" << tmpout.c_str() << "\" "
226  "into \"" << fout.c_str() << "\" "
227  "(" << save_errno << ": " << strerror(save_errno) << ")"
228  << std::endl);
230  }
231 }
232 
233 bool
235 {
236  inf.open(fin.c_str());
237 
238  for (int cnt = 0; !inf; cnt++) {
239  silent_cout("input file \"" << fin.c_str() << "\" missing, "
240  "try #" << cnt << "; "
241  "sleeping " << SleepTime << " s" << std::endl);
242 
244  inf.setstate(std::ios_base::badbit);
245  return (bOK = false);
246  }
247 
248  mbsleep(&SleepTime);
249  inf.clear();
250  inf.open(fin.c_str());
251  }
252 
253  return inf.good();
254 }
255 
256 bool
258 {
259  inf.close();
260 
261  if (bRemoveIn) {
262  if (unlink(fin.c_str()) != 0) {
263  int save_errno = errno;
264 
265  switch (save_errno) {
266  case ENOENT:
267  break;
268 
269  default:
270  silent_cerr("unable to delete input file "
271  "\"" << fin.c_str() << "\": "
272  << strerror(save_errno) << std::endl);
274  }
275  }
276  }
277 
278  // NOTE: allow MBDyn to decide when converged
279  return true;
280 }
281 
282 std::ostream *
284 {
285  return &outf;
286 }
287 
288 std::istream *
290 {
291  return &inf;
292 }
293 
294 /* ExtFileHandler - end */
295 
296 /* ExtSocketHandler - begin */
297 
298 #ifdef USE_SOCKET
299 
300 ESCmd
301 ExtSocketHandler::u2cmd(unsigned u) const
302 {
303  switch (u) {
304  case ES_REGULAR_DATA:
305  case ES_GOTO_NEXT_STEP:
306  case ES_ABORT:
308  return ESCmd(u);
309  }
310 
311  return ES_UNKNOWN;
312 }
313 
314 static const char *ESCmd2str[] = {
315  "0",
316  "1",
317  "REGULAR_DATA", // 2
318  "3",
319  "GOTO_NEXT_STEP", // 4
320  "ABORT", // 5
321  "REGULAR_DATA_AND_GOTO_NEXT_STEP", // 6
322  0
323 };
324 
325 const char *
327 {
328  if (cmd == ES_UNKNOWN) {
329  return "UNKNOWN";
330  }
331 
332  return ESCmd2str[cmd];
333 }
334 
335 ExtSocketHandler::ExtSocketHandler(UseSocket *pUS, mbsleep_t SleepTime,
336  int recv_flags, int send_flags)
337 : ExtFileHandlerBase(SleepTime, 0),
338 pUS(pUS), recv_flags(recv_flags), send_flags(send_flags),
339 bReadForces(true), bLastReadForce(false)
340 {
341  NO_OP;
342 }
343 
344 ExtSocketHandler::~ExtSocketHandler(void)
345 {
346  if (pUS->GetSock() >= 0) {
347  uint8_t u = ES_ABORT;
348 
349  // ignore result
350  (void)send(pUS->GetSock(), (void *)&u, sizeof(u), send_flags);
351  }
352  SAFEDELETE(pUS);
353 }
354 
356 ExtSocketHandler::NegotiateRequest(void) const
357 {
358  if (pUS->Create()) {
360 
361  } else {
363  }
364 }
365 
366 bool
367 ExtSocketHandler::Prepare_pre(void)
368 {
369  uint8_t u;
370  ssize_t rc;
371 
372  switch (NegotiateRequest()) {
374  u = ES_NEGOTIATION;
375 
376  rc = send(pUS->GetSock(), (void *)&u, sizeof(u), send_flags);
377  if (rc == -1) {
378  int save_errno = errno;
379  silent_cerr("ExtSocketHandler: negotiation request send() failed "
380  "(" << save_errno << ": " << strerror(save_errno) << ")"
381  << std::endl);
382  return (bOK = false);
383 
384  } else if (rc != sizeof(u)) {
385  silent_cerr("ExtSocketHandler: negotiation request send() failed "
386  "(sent " << rc << " bytes "
387  "instead of " << sizeof(u) << ")"
388  << std::endl);
389  return (bOK = false);
390  }
391  break;
392 
394  rc = recv(pUS->GetSock(), (void *)&u, sizeof(u), recv_flags);
395  if (rc == -1) {
396  return (bOK = false);
397 
398  } else if (rc != sizeof(u)) {
399  return (bOK = false);
400  }
401 
402  if (u != ES_NEGOTIATION) {
403  silent_cerr("ExtSocketHandler: negotiation request recv() failed"
404  << std::endl);
405  return (bOK = false);
406  }
407  break;
408 
409  default:
410  ASSERT(0);
411  }
412 
413  return true;
414 }
415 
416 void
417 ExtSocketHandler::Prepare_post(bool ok)
418 {
419  if (NegotiateRequest()) {
420  uint8_t u = ok ? ES_OK : ES_ABORT;
421 
422  ssize_t rc = send(pUS->GetSock(), (void *)&u, sizeof(u),
423  send_flags);
424  if (rc == -1) {
425  int save_errno = errno;
426  silent_cerr("ExtSocketHandler: negotiation response send() failed "
427  "(" << save_errno << ": " << strerror(save_errno) << ")"
428  << std::endl);
430 
431  } else if (rc != sizeof(u)) {
432  silent_cerr("ExtSocketHandler: negotiation response send() failed "
433  "(sent " << rc << " bytes "
434  "instead of " << sizeof(u) << ")"
435  << std::endl);
437  }
438 
439  } else {
440  uint8_t u;
441  ssize_t rc = recv(pUS->GetSock(), (void *)&u, sizeof(u),
442  recv_flags);
443  if (rc == -1) {
444  int save_errno = errno;
445  silent_cerr("ExtSocketHandler: negotiation response recv() failed "
446  "(" << save_errno << ": " << strerror(save_errno) << ")"
447  << std::endl);
449 
450  } else if (rc != sizeof(u)) {
451  silent_cerr("ExtSocketHandler: negotiation response recv() failed "
452  "(received " << rc << " bytes "
453  "instead of " << sizeof(u) << ")"
454  << std::endl);
456 
457  }
458 
459  if (u != ES_OK) {
461  }
462  }
463 }
464 
465 void
466 ExtSocketHandler::AfterPredict(void)
467 {
468  bLastReadForce = false;
469  bReadForces = true;
470 }
471 
472 bool
473 ExtSocketHandler::Send_pre(SendWhen when)
474 {
475  if (!bReadForces || !bOK) {
476  return false;
477  }
478 
479  uint8_t u;
480  if (when == SEND_AFTER_CONVERGENCE) {
482  } else {
483  u = ES_REGULAR_DATA;
484  }
485  ssize_t rc = send(pUS->GetSock(), (void *)&u, sizeof(u), send_flags);
486  if (rc == -1) {
487  int save_errno = errno;
488  silent_cerr("ExtSocketHandler: send() failed "
489  "(" << save_errno << ": " << strerror(save_errno) << ")"
490  << std::endl);
492  return (bOK = false);
493 
494  } else if (rc != sizeof(u)) {
495  silent_cerr("ExtSocketHandler: send() failed "
496  "(sent " << rc << " bytes "
497  "instead of " << sizeof(u) << ")"
498  << std::endl);
500  return (bOK = false);
501  }
502 
503  return true;
504 }
505 
506 void
507 ExtSocketHandler::Send_post(SendWhen when)
508 {
509 #if 0
510  if (when == SEND_AFTER_CONVERGENCE) {
511  bReadForces = true;
512  }
513 #endif
514  NO_OP;
515 }
516 
517 bool
518 ExtSocketHandler::Recv_pre(void)
519 {
520  if (!bReadForces) {
521  return false;
522  }
523 
524  uint8_t u = 0;
525 
526  if (SleepTime != 0) {
527  for ( ; ; ) {
528  ssize_t rc;
529 
530  rc = recv(pUS->GetSock(), (void *)&u, sizeof(u),
531  recv_flags | MSG_DONTWAIT);
532  if (rc != -1) {
533  break;
534  }
535 
536  int save_errno = errno;
537 
538  if (errno != EAGAIN) {
539  silent_cerr("ExtSocketHandler: "
540  "recv() failed (" << save_errno << ": "
541  << strerror(save_errno) << ")"
542  << std::endl);
544  return (bOK = false);
545  }
546 
548  return (bOK = false);
549  }
550 
551  mbsleep(&SleepTime);
552  }
553 
554  } else {
555  // wait until the status code is returned
556  ssize_t rc = recv(pUS->GetSock(), (void *)&u, sizeof(u), recv_flags);
557  if (rc == -1) {
558  int save_errno = errno;
559 
560  if (errno != EAGAIN) {
561  silent_cerr("ExtSocketHandler: "
562  "recv() failed (" << save_errno << ": "
563  << strerror(save_errno) << ")"
564  << std::endl);
566  return (bOK = false);
567  }
568 
569  } else if (rc != sizeof(u)) {
570  silent_cerr("ExtSocketHandler: "
571  "recv()=" << rc << " (expected " << sizeof(u) << ")"
572  << std::endl);
574  return (bOK = false);
575  }
576  }
577 
578  // TODO: act upon status code value
579  switch (u2cmd(u)) {
580  case ES_UNKNOWN:
581  silent_cerr("ExtSocketHandler: "
582  "received unknown code (" << unsigned(u) << ")"
583  << std::endl);
585  return (bOK = false);
586 
587  case ES_REGULAR_DATA:
588  break;
589 
590  case ES_GOTO_NEXT_STEP:
591  // peer is done; do not read forces, keep using old
592  bReadForces = false;
593  return false;
594 
595  case ES_ABORT:
596  silent_cout("ExtSocketHandler: peer requested end of simulation"
597  << std::endl);
599  bReadForces = false;
600  return false;
601 
603  // peer is done; read forces for the last time
604  bLastReadForce = true;
605  break;
606 
607  default:
609  }
610 
611  return true;
612 }
613 
614 bool
615 ExtSocketHandler::Recv_post(void)
616 {
617  if (bLastReadForce) {
618  bReadForces = false;
619  }
620 
621  return !bReadForces;
622 }
623 
624 int
625 ExtSocketHandler::GetOutFileDes(void)
626 {
627  return pUS->GetSock();
628 }
629 
630 int
631 ExtSocketHandler::GetSendFlags(void) const
632 {
633  return send_flags;
634 }
635 
636 int
637 ExtSocketHandler::GetInFileDes(void)
638 {
639  return pUS->GetSock();
640 }
641 
642 int
643 ExtSocketHandler::GetRecvFlags(void) const
644 {
645  return recv_flags;
646 }
647 
648 #endif // USE_SOCKET
649 
650 /* ExtSocketHandler - end */
651 
652 /* ExtFileHandlerEDGE moved to extedge.h, extedge.cc */
653 
654 /* ExtForce - begin */
655 
656 /*
657  * Communication patterns:
658 
659  - loose coupling:
660  - receive forces at first residual assembly
661  - send motion after convergence
662 
663  - tight coupling:
664  - send motion after predict
665  - receive forces at each residual assembly
666  - send motion at each update after solution
667 
668  - almost-tight coupling:
669  - send motion after predict
670  - receive forces every some residual assembly
671  - send motion every some update after solution
672  - send motion after convergence (to make sure
673  peer gets the last solution)
674  */
675 
676 /* Costruttore */
677 ExtForce::ExtForce(unsigned int uL,
678  DataManager *pDM,
679  ExtFileHandlerBase *pEFH,
680  bool bSendAfterPredict,
681  int iCoupling,
682  flag fOut)
683 : Elem(uL, fOut),
684 Force(uL, fOut),
685 c(iCoupling > COUPLING_LOOSE ? pDM : NULL),
686 pEFH(pEFH),
687 bSendAfterPredict(bSendAfterPredict),
688 iCoupling(iCoupling),
689 iCouplingCounter(0),
690 bFirstSend(true),
691 bFirstRecv(true)
692 {
693  NO_OP;
694 }
695 
697 {
698  if (pEFH) {
699  SAFEDELETE(pEFH);
700  }
701 }
702 
703 void
707 {
708  bFirstSend = true;
709  bFirstRecv = true;
710 
711  bool ok = false;
712  if (pEFH->Prepare_pre()) {
713  ok = Prepare(pEFH);
714  }
715  pEFH->Prepare_post(ok);
716  if (!ok) {
718  }
719 
720  if (iCoupling < COUPLING_TIGHT) {
722  }
723 }
724 
725 void
727  const VectorHandler& XPrimeCurr)
728 {
729  /* If running tight coupling, send kinematics every iteration */
730  /* NOTE: tight coupling may need relaxation */
732  || (iCoupling == COUPLING_LOOSE && bSendAfterPredict))
733  {
735  if (iCoupling == COUPLING_LOOSE) {
737  } else if (bFirstSend) {
739  } else {
741  }
742 
743  Send(when);
744  }
745 }
746 
747 /*
748  * Elaborazione stato interno dopo la convergenza
749  */
750 void
752 {
753  /* After prediction, mark next residual as first */
754  iCouplingCounter = 0;
755  bFirstSend = true;
756  bFirstRecv = true;
757 
758  pEFH->AfterPredict();
759 
760  /* needed to send predicted data */
761  if (bSendAfterPredict) {
762  Update(X, XP);
763  }
764 }
765 
766 /*
767  * Elaborazione stato interno dopo la convergenza
768  */
769 void
771  const VectorHandler& XP)
772 {
773  /* If not running tight coupling, send kinematics only at convergence */
776 #if 0
777  if (bRemoveIn) {
778  Unlink();
779  }
780 #endif
781  }
782 }
783 
784 /*
785  * Send output to peer
786  */
787 void
789 {
790  if (pEFH->Send_pre(when)) {
791  Send(pEFH, when);
792  bFirstSend = false;
793  }
794  pEFH->Send_post(when);
795 }
796 
797 void
799 {
801  || ((iCoupling == COUPLING_LOOSE || iCoupling == COUPLING_STAGGERED) && bFirstRecv))
802  {
803  if (pEFH->Recv_pre()) {
804  Recv(pEFH);
805  bFirstRecv = false;
806  }
807 
808  if (pEFH->Recv_post()) {
809  // TODO: need to handle requests for end of simulation
811  }
812  }
813 
814  if (iCoupling == COUPLING_NONE) {
816  }
817 }
818 
819 void
820 ExtForce::InitialWorkSpaceDim(integer* piNumRows, integer* piNumCols) const
821 {
822  *piNumRows = 0;
823  *piNumCols = 0;
824 }
825 
826 /* Contributo allo jacobiano durante l'assemblaggio iniziale */
829  const VectorHandler& XCurr)
830 {
831  WorkMat.SetNullMatrix();
832  return WorkMat;
833 }
834 
835 /* Contributo al residuo durante l'assemblaggio iniziale */
838  const VectorHandler& XCurr)
839 {
840  WorkVec.ResizeReset(0);
841  return WorkVec;
842 }
843 
844 void
846  MBDynParser& HP,
847  unsigned int uLabel,
848  mbsleep_t& SleepTime,
849  std::streamsize& Precision)
850 {
851  mbsleep_t MinSleepTime = SleepTime;
852  if (HP.IsKeyWord("sleep" "time")) {
853  SleepTime = HP.GetTimeout(SleepTime);
854  if (SleepTime < MinSleepTime ) {
855  silent_cerr("ExtForce(" << uLabel << "): "
856  "invalid sleep time " << SleepTime
857  << " less than " << MinSleepTime
858  << " at line " << HP.GetLineData()
859  << std::endl);
861  }
862  }
863 
864  Precision = 6;
865  if (HP.IsKeyWord("precision")) {
866  if (!HP.IsKeyWord("default")) {
867  Precision = HP.GetInt();
868  if (Precision < 0) {
869  silent_cerr("ExtForce(" << uLabel << "): "
870  "invalid precision value "
871  "\"" << Precision << "\""
872  " at line " << HP.GetLineData()
873  << std::endl);
875  }
876  }
877  }
878 }
879 
880 
881 static ExtFileHandlerBase *
883  MBDynParser& HP,
884  unsigned int uLabel)
885 {
886 #ifdef USE_SOCKET
887  ExtFileHandlerBase *pEFH = 0;
888 
889  bool bGotCreate(false);
890  bool bCreate(false);
891  unsigned short int port = (unsigned short int)-1;
892  std::string host;
893  std::string path;
894 
895  if (HP.IsKeyWord("create")) {
896  bGotCreate = true;
897  if (!HP.GetYesNo(bCreate)) {
898  silent_cerr("ExtSocketHandler"
899  "(" << uLabel << "): "
900  "\"create\" must be either \"yes\" or \"no\" "
901  "at line " << HP.GetLineData()
902  << std::endl);
904  }
905  }
906 
907  if (HP.IsKeyWord("local") || HP.IsKeyWord("path")) {
908  const char *m = HP.GetFileName();
909 
910  if (m == 0) {
911  silent_cerr("ExtSocketHandler"
912  "(" << uLabel << "): "
913  "unable to read local path "
914  "at line " << HP.GetLineData()
915  << std::endl);
917  }
918 
919  path = m;
920  }
921 
922  if (HP.IsKeyWord("port")) {
923  if (!path.empty()) {
924  silent_cerr("ExtSocketHandler"
925  "(" << uLabel << "): "
926  "cannot specify port "
927  "for a local socket "
928  "at line " << HP.GetLineData()
929  << std::endl);
931  }
932 
933  int p = HP.GetInt();
934  /* Da sistemare da qui */
935 #ifdef IPPORT_USERRESERVED
936  if (p <= IPPORT_USERRESERVED) {
937  silent_cerr("ExtSocketHandler"
938  "(" << uLabel << "): "
939  "cannot listen on reserved port "
940  << port << ": less than "
941  "IPPORT_USERRESERVED=" << IPPORT_USERRESERVED
942  << " at line " << HP.GetLineData()
943  << std::endl);
945  }
946  /* if #undef'd, don't bother checking;
947  * the OS will do it for us */
948 #endif /* IPPORT_USERRESERVED */
949 
950  port = p;
951  }
952 
953  if (HP.IsKeyWord("host")) {
954  if (!path.empty()) {
955  silent_cerr("ExtSocketHandler"
956  "(" << uLabel << "): "
957  "cannot specify host for a local socket "
958  "at line " << HP.GetLineData()
959  << std::endl);
961  }
962 
963  const char *h;
964 
965  h = HP.GetStringWithDelims();
966  if (h == 0) {
967  silent_cerr("ExtSocketHandler"
968  "(" << uLabel << "): "
969  "unable to read host "
970  "at line " << HP.GetLineData()
971  << std::endl);
973  }
974 
975  host = h;
976 
977  } else if (path.empty() && !bCreate) {
978  silent_cerr("ExtSocketHandler"
979  "(" << uLabel << "): "
980  "host undefined "
981  "at line " << HP.GetLineData()
982  << std::endl);
984  }
985 
986  int socket_type = SOCK_STREAM;
987  if (HP.IsKeyWord("socket" "type")) {
988  if (HP.IsKeyWord("udp")) {
989  socket_type = SOCK_DGRAM;
990 
991  if (!bGotCreate) {
992  bCreate = true;
993  }
994 
995  } else if (!HP.IsKeyWord("tcp")) {
996  silent_cerr("ExtSocketHandler(" << uLabel << "\"): "
997  "invalid socket type "
998  "at line " << HP.GetLineData() << std::endl);
1000  }
1001  }
1002 
1003  if ((socket_type == SOCK_DGRAM) && !bCreate) {
1004  silent_cerr("ExtSocketHandler(" << uLabel << "\"): "
1005  "socket type=udp incompatible with create=no "
1006  "at line " << HP.GetLineData() << std::endl);
1008  }
1009 
1010  // we want to block until the whole chunk is received
1011  int recv_flags = 0;
1012  int send_flags = 0;
1013 #ifdef MSG_WAITALL
1014  recv_flags |= MSG_WAITALL;
1015 #endif // MSG_WAITALL
1016 
1017  while (HP.IsArg()) {
1018  if (HP.IsKeyWord("signal")) {
1019 #ifdef MSG_NOSIGNAL
1020  recv_flags &= ~MSG_NOSIGNAL;
1021  send_flags &= ~MSG_NOSIGNAL;
1022 #else // ! MSG_NOSIGNAL
1023  silent_cout("ExtSocketHandler"
1024  "(" << uLabel << "): "
1025  "MSG_NOSIGNAL not defined (ignored) "
1026  "at line " << HP.GetLineData()
1027  << std::endl);
1028 #endif // ! MSG_NOSIGNAL
1029 
1030  // not honored by recv(2)
1031  } else if (HP.IsKeyWord("no" "signal")) {
1032 #ifdef MSG_NOSIGNAL
1033  recv_flags |= MSG_NOSIGNAL;
1034  send_flags |= MSG_NOSIGNAL;
1035 #else // ! MSG_NOSIGNAL
1036  silent_cout("ExtSocketHandler"
1037  "(" << uLabel << "): "
1038  "MSG_NOSIGNAL not defined (ignored) "
1039  "at line " << HP.GetLineData()
1040  << std::endl);
1041 #endif // ! MSG_NOSIGNAL
1042 
1043  } else {
1044  break;
1045  }
1046  }
1047 
1048  UseSocket *pUS = 0;
1049  if (path.empty()) {
1050  if (port == (unsigned short int)(-1)) {
1051  silent_cerr("ExtSocketHandler"
1052  "(" << uLabel << "): "
1053  "port missing"
1054  << std::endl);
1056  }
1057  SAFENEWWITHCONSTRUCTOR(pUS, UseInetSocket, UseInetSocket(host, port, socket_type, bCreate));
1058 
1059  } else {
1060  SAFENEWWITHCONSTRUCTOR(pUS, UseLocalSocket, UseLocalSocket(path, socket_type, bCreate));
1061  }
1062 
1063  if (bCreate) {
1064  pDM->RegisterSocketUser(pUS);
1065 
1066  } else {
1067  pUS->Connect();
1068  }
1069 
1070  mbsleep_t SleepTime = mbsleep_init(0);
1071  std::streamsize Precision = 0;
1072  ReadExtFileParams(pDM, HP, uLabel, SleepTime, Precision);
1073  // NOTE: so far, precision is ignored
1074 
1075  SAFENEWWITHCONSTRUCTOR(pEFH, ExtSocketHandler,
1076  ExtSocketHandler(pUS, SleepTime, recv_flags, send_flags));
1077 
1078  return pEFH;
1079 #else // ! USE_SOCKET
1080  silent_cerr("ExtSocketHandler not supported" << std::endl);
1082 #endif // ! USE_SOCKET
1083 }
1084 
1085 
1086 static ExtFileHandlerBase *
1088  MBDynParser& HP,
1089  unsigned int uLabel)
1090 {
1091  if (HP.IsKeyWord("EDGE")) {
1092  return ReadExtFileHandlerEDGE(pDM, HP, uLabel);
1093 
1094  } else if (HP.IsKeyWord("socket")) {
1095  return ReadExtSocketHandler(pDM, HP, uLabel);
1096  }
1097 
1098  ExtFileHandlerBase *pEFH = 0;
1099 
1100  // default
1101  const char *s = HP.GetFileName();
1102  if (s == 0) {
1103  silent_cerr("ExtForce(" << uLabel << "): "
1104  "unable to get input file name "
1105  "at line " << HP.GetLineData() << std::endl);
1107  }
1108  std::string fin = s;
1109 
1110  bool bUnlinkIn = false;
1111  if (HP.IsKeyWord("unlink")) {
1112  bUnlinkIn = true;
1113  }
1114 
1115  s = HP.GetFileName();
1116  if (s == 0) {
1117  silent_cerr("ExtForce(" << uLabel << "): "
1118  "unable to get output file name "
1119  "at line " << HP.GetLineData() << std::endl);
1121  }
1122  std::string fout = s;
1123 
1124  bool bNoClobberOut = false;
1125  if (HP.IsKeyWord("no" "clobber")) {
1126  bNoClobberOut = true;
1127  }
1128 
1129  mbsleep_t SleepTime = mbsleep_init(1);
1130  std::streamsize Precision = 0;
1131  ReadExtFileParams(pDM, HP, uLabel, SleepTime, Precision);
1132 
1134  ExtFileHandler(fin, bUnlinkIn, fout, bNoClobberOut,
1135  SleepTime, Precision));
1136 
1137  return pEFH;
1138 }
1139 
1140 void
1142  MBDynParser& HP,
1143  unsigned int uLabel,
1144  ExtFileHandlerBase*& pEFH,
1145  bool& bSendAfterPredict,
1146  int& iCoupling)
1147 {
1148  pEFH = ReadExtFileHandler(pDM, HP, uLabel);
1149 
1150  iCoupling = ExtForce::COUPLING_LOOSE;
1151  if (HP.IsKeyWord("coupling")) {
1152  if (HP.IsKeyWord("none")) {
1153  iCoupling = ExtForce::COUPLING_NONE;
1154 
1155  } else if (HP.IsKeyWord("staggered")) {
1156  iCoupling = ExtForce::COUPLING_STAGGERED;
1157 
1158  } else if (HP.IsKeyWord("loose")) {
1159  iCoupling = ExtForce::COUPLING_LOOSE;
1160 
1161  } else if (HP.IsKeyWord("tight")) {
1162  iCoupling = ExtForce::COUPLING_TIGHT;
1163 
1164  } else {
1165  iCoupling = HP.GetInt();
1166  if (iCoupling < -1) {
1167  silent_cerr("ExtForce(" << uLabel << "): "
1168  "invalid coupling value "
1169  "\"" << iCoupling << "\""
1170  " at line " << HP.GetLineData()
1171  << std::endl);
1173  }
1174  }
1175  }
1176 
1177  if (iCoupling >= ExtForce::COUPLING_LOOSE) {
1178  bSendAfterPredict = true;
1179  if (HP.IsKeyWord("send" "after" "predict")) {
1180  if (!HP.GetYesNo(bSendAfterPredict)) {
1181  silent_cerr("ExtForce(" << uLabel << "): "
1182  "invalud \"send after predict\" value "
1183  "at line " << HP.GetLineData()
1184  << std::endl);
1186  }
1187  }
1188 
1189  } else {
1190  bSendAfterPredict = false;
1191  }
1192 }
ESCmd
Legal commands.
Definition: mbc.h:49
virtual bool Send_pre(SendWhen when)
Definition: extforce.cc:153
Definition: mbc.h:58
virtual std::ostream * GetOutStream(void)
Definition: extforce.cc:283
virtual SubVectorHandler & InitialAssRes(SubVectorHandler &WorkVec, const VectorHandler &XCurr)
Definition: extforce.cc:837
virtual std::istream * GetInStream(void)
Definition: extforce.cc:78
long int flag
Definition: mbdyn.h:43
virtual void AfterPredict(void)=0
#define MBDYN_EXCEPT_ARGS
Definition: except.h:63
virtual int GetInFileDes(void)
Definition: extforce.cc:99
virtual int GetOutFileDes(void)
Definition: extforce.cc:85
virtual void ResizeReset(integer)
Definition: vh.cc:55
void Recv(void)
Definition: extforce.cc:798
virtual integer GetInt(integer iDefval=0)
Definition: parser.cc:1050
virtual void Prepare_post(bool ok)
Definition: extforce.cc:141
virtual ~ExtForce(void)
Definition: extforce.cc:696
virtual bool Recv_post(void)
Definition: extforce.cc:257
virtual ~ExtFileHandlerBase(void)
Definition: extforce.cc:57
bool bSendAfterPredict
Definition: extforce.h:196
int mbsleep(const mbsleep_t *t)
Definition: mbsleep.c:90
unsigned long mbsleep_t
Definition: mbsleep.h:51
ExtFileHandlerBase(mbsleep_t SleepTime, std::streamsize Precision)
Definition: extforce.cc:50
virtual const char * GetFileName(enum Delims Del=DEFAULTDELIM)
Definition: parsinc.cc:673
Definition: force.h:46
std::string fin
Definition: extforce.h:114
Converged c
Definition: extforce.h:192
virtual void Set(Converged::State s)
Definition: converged.cc:62
ExtFileHandler(std::string &fin, bool bRemoveIn, std::string &fout, bool bNoClobberOut, mbsleep_t SleepTime, std::streamsize Precision)
Definition: extforce.cc:116
#define NO_OP
Definition: myassert.h:74
std::vector< Hint * > Hints
Definition: simentity.h:89
virtual bool Recv_pre(void)=0
int iCouplingCounter
Definition: extforce.h:215
virtual std::istream * GetInStream(void)
Definition: extforce.cc:289
ExtForce(unsigned int uL, DataManager *pDM, ExtFileHandlerBase *pEFH, bool bSendAfterPredict, int iCoupling, flag fOut)
Definition: extforce.cc:677
int mbdyn_stop_at_end_of_iteration(void)
Definition: solver.cc:172
virtual Negotiate NegotiateRequest(void) const
Definition: extforce.cc:63
static ExtFileHandlerBase * ReadExtSocketHandler(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
Definition: extforce.cc:882
virtual ~ExtFileHandler(void)
Definition: extforce.cc:129
virtual bool Prepare_pre(void)
Definition: extforce.cc:135
virtual bool Prepare(ExtFileHandlerBase *pEFH)=0
void SetNullMatrix(void)
Definition: submat.h:1159
ExtFileHandlerBase * ReadExtFileHandlerEDGE(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
Definition: extedge.cc:757
Definition: mbdyn.h:76
virtual bool IsKeyWord(const char *sKeyWord)
Definition: parser.cc:910
virtual bool Send_pre(SendWhen when)=0
Definition: mbc.h:50
const char * host
Definition: autopilot.c:142
void Send(ExtFileHandlerBase::SendWhen when)
Definition: extforce.cc:788
virtual int GetRecvFlags(void) const
Definition: extforce.cc:106
ExtFileHandlerBase * pEFH
Definition: extforce.h:193
virtual void SetValue(DataManager *pDM, VectorHandler &X, VectorHandler &XP, SimulationEntity::Hints *h=0)
Definition: extforce.cc:704
std::ifstream inf
Definition: extforce.h:117
virtual const char * GetStringWithDelims(enum Delims Del=DEFAULTDELIM, bool escape=true)
Definition: parser.cc:1228
Definition: mbdyn.h:77
mbsleep_t mbsleep_init(long t)
Definition: mbsleep.c:38
bool bNoClobberOut
Definition: extforce.h:115
std::streamsize Precision
Definition: extforce.h:75
void mbdyn_set_stop_at_end_of_iteration(void)
Definition: solver.cc:192
static const char * cmd2str(int cmd)
Definition: mbc.h:55
static ExtFileHandlerBase * ReadExtFileHandler(DataManager *pDM, MBDynParser &HP, unsigned int uLabel)
Definition: extforce.cc:1087
bool bFirstSend
Definition: extforce.h:218
virtual mbsleep_t GetTimeout(const mbsleep_t &DefVal)
Definition: parser.cc:1062
#define ASSERT(expression)
Definition: colamd.c:977
std::string tmpout
Definition: extforce.h:114
std::ofstream outf
Definition: extforce.h:118
virtual std::ostream * GetOutStream(void)
Definition: extforce.cc:72
virtual bool Recv_post(void)=0
#define SAFENEWWITHCONSTRUCTOR(pnt, item, constructor)
Definition: mynewmem.h:698
#define MSG_NOSIGNAL
Definition: sock.h:69
virtual void Send_post(SendWhen when)
Definition: extforce.cc:219
virtual bool Prepare_pre(void)=0
static std::stack< cleanup * > c
Definition: cleanup.cc:59
virtual void InitialWorkSpaceDim(integer *piNumRows, integer *piNumCols) const
Definition: extforce.cc:820
virtual bool GetYesNo(bool &bRet)
Definition: parser.cc:1022
virtual bool IsArg(void)
Definition: parser.cc:807
Definition: elem.h:75
bool bFirstRecv
Definition: extforce.h:219
virtual void Send_post(SendWhen when)=0
bool bRemoveIn
Definition: extforce.h:115
virtual bool Recv_pre(void)
Definition: extforce.cc:234
virtual void Prepare_post(bool ok)=0
virtual int GetSendFlags(void) const
Definition: extforce.cc:92
#define IPPORT_USERRESERVED
Definition: sock.h:66
std::string fout
Definition: extforce.h:114
unsigned short int port
Definition: autopilot.c:143
virtual void Update(const VectorHandler &XCurr, const VectorHandler &XPrimeCurr)
Definition: extforce.cc:726
mbsleep_t SleepTime
Definition: extforce.h:76
virtual void AfterPredict(void)
Definition: extforce.cc:147
int iCoupling
Definition: extforce.h:212
long int integer
Definition: colamd.c:51
virtual HighParser::ErrOut GetLineData(void) const
Definition: parsinc.cc:697
virtual VariableSubMatrixHandler & InitialAssJac(VariableSubMatrixHandler &WorkMat, const VectorHandler &XCurr)
Definition: extforce.cc:828
void ReadExtForce(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, ExtFileHandlerBase *&pEFH, bool &bSendAfterPredict, int &iCoupling)
Definition: extforce.cc:1141
void mbdyn_set_stop_at_end_of_time_step(void)
Definition: solver.cc:200
const char * path
Definition: autopilot.c:141
#define SAFEDELETE(pnt)
Definition: mynewmem.h:710
void ReadExtFileParams(DataManager *pDM, MBDynParser &HP, unsigned int uLabel, mbsleep_t &SleepTime, std::streamsize &Precision)
Definition: extforce.cc:845
virtual void AfterConvergence(const VectorHandler &X, const VectorHandler &XP)
Definition: extforce.cc:770
virtual void AfterPredict(VectorHandler &X, VectorHandler &XP)
Definition: extforce.cc:751