~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to salome/fsi_coupling/src/FSI_MILIEU/FSI_MILIEU.cxx

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of Code_Saturne, a general-purpose CFD tool.
 
3
 
 
4
  Copyright (C) 1998-2011 EDF S.A.
 
5
 
 
6
  This program is free software; you can redistribute it and/or modify it under
 
7
  the terms of the GNU General Public License as published by the Free Software
 
8
  Foundation; either version 2 of the License, or (at your option) any later
 
9
  version.
 
10
 
 
11
  This program is distributed in the hope that it will be useful, but WITHOUT
 
12
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 
13
  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
14
  details.
 
15
 
 
16
  You should have received a copy of the GNU General Public License along with
 
17
  this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
 
18
  Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
19
*/
 
20
 
 
21
/*----------------------------------------------------------------------------*/
 
22
 
 
23
#include "FSI_MILIEU.hxx"
 
24
#include <string>
 
25
#include <unistd.h>
 
26
 
 
27
#include <Calcium.hxx>
 
28
#include <CalciumException.hxx>
 
29
 
 
30
#include <signal.h>
 
31
#include <SALOME_NamingService.hxx>
 
32
#include <Utils_SALOME_Exception.hxx>
 
33
#include <pthread.h>
 
34
#include <execinfo.h>
 
35
 
 
36
typedef void (*sighandler_t)(int);
 
37
sighandler_t setsig(int sig, sighandler_t handler)
 
38
{
 
39
  struct sigaction context, ocontext;
 
40
  context.sa_handler = handler;
 
41
  sigemptyset(&context.sa_mask);
 
42
  context.sa_flags = 0;
 
43
  if (sigaction(sig, &context, &ocontext) == -1)
 
44
    return SIG_ERR;
 
45
  return ocontext.sa_handler;
 
46
}
 
47
 
 
48
static void AttachDebugger()
 
49
{
 
50
  void *array[20];
 
51
  size_t size=20;
 
52
  char **strings;
 
53
  size_t i;
 
54
  std::string _what;
 
55
  size = backtrace (array, size);
 
56
  strings = backtrace_symbols (array, size);
 
57
  for (i = 0; i < size; i++)
 
58
     _what=_what+strings[i]+ '\n';
 
59
  free (strings);
 
60
 
 
61
  std::cerr << pthread_self() << std::endl;
 
62
  std::cerr << _what << std::endl;
 
63
 
 
64
  if(getenv ("DEBUGGER"))
 
65
    {
 
66
      std::stringstream exec;
 
67
#if 0
 
68
      exec << "$DEBUGGER " << "None " << getpid() << "&";
 
69
#else
 
70
      exec << "$DEBUGGER SALOME_Container " << getpid() << "&";
 
71
#endif
 
72
      std::cerr << exec.str() << std::endl;
 
73
      system(exec.str().c_str());
 
74
      while(1);
 
75
    }
 
76
}
 
77
 
 
78
static void THandler(int theSigId)
 
79
{
 
80
  std::cerr << "SIGSEGV: "  << std::endl;
 
81
  AttachDebugger();
 
82
  //to exit or not to exit
 
83
  _exit(1);
 
84
}
 
85
 
 
86
static void terminateHandler(void)
 
87
{
 
88
  std::cerr << "Terminate: not managed exception !"  << std::endl;
 
89
  AttachDebugger();
 
90
  throw SALOME_Exception("Terminate: not managed exception !");
 
91
}
 
92
 
 
93
static void unexpectedHandler(void)
 
94
{
 
95
  std::cerr << "Unexpected: unexpected exception !"  << std::endl;
 
96
  AttachDebugger();
 
97
  throw SALOME_Exception("Unexpected: unexpected exception !");
 
98
}
 
99
 
 
100
 
 
101
#define  _(A,B)   A##B
 
102
#ifdef _WIN32
 
103
#define F_FUNC(lname,uname) __stdcall uname
 
104
#define F_CALL(lname,uname) uname
 
105
#define STR_PSTR(str)       char *str, int _(Len,str)
 
106
#define STR_PLEN(str)
 
107
#define STR_PTR(str)        str
 
108
#define STR_LEN(str)        _(Len,str)
 
109
#define STR_CPTR(str)        str,strlen(str)
 
110
#define STR_CLEN(str)
 
111
#else
 
112
#define F_FUNC(lname,uname) _(lname,_)        /* Fortran function name */
 
113
#define F_CALL(lname,uname) _(lname,_)        /* Fortran function call */
 
114
#define STR_PSTR(str)       char *str         /* fortran string arg pointer */
 
115
#define STR_PLEN(str)       , int _(Len,str)  /* fortran string arg length */
 
116
#define STR_PTR(str)        str               /* fortran string pointer */
 
117
#define STR_LEN(str)        _(Len,str)        /* fortran string length */
 
118
#define STR_CPTR(str)        str              /* fortran string calling arg pointer */
 
119
#define STR_CLEN(str)       , strlen(str)     /* fortran string calling arg length */
 
120
#endif
 
121
 
 
122
//DEFS
 
123
#include <runmilieu.h>
 
124
#include <donnees.h>
 
125
//ENDDEF
 
126
 
 
127
#include <calcium.h>
 
128
 
 
129
extern "C" void cp_exit(int err);
 
130
 
 
131
extern "C" void F_FUNC(cpexit,CPEXIT)(int *err)
 
132
{
 
133
  if(*err==-1)
 
134
    _exit(-1);
 
135
  else
 
136
    cp_exit(*err);
 
137
}
 
138
 
 
139
using namespace std;
 
140
 
 
141
//! Constructor for component "FSI_MILIEU" instance
 
142
/*!
 
143
 *
 
144
 */
 
145
FSI_MILIEU_i::FSI_MILIEU_i(CORBA::ORB_ptr orb,
 
146
                     PortableServer::POA_ptr poa,
 
147
                     PortableServer::ObjectId * contId,
 
148
                     const char *instanceName,
 
149
                     const char *interfaceName)
 
150
          : Superv_Component_i(orb, poa, contId, instanceName, interfaceName)
 
151
{
 
152
#if 0
 
153
  setsig(SIGSEGV,&THandler);
 
154
  set_terminate(&terminateHandler);
 
155
  set_unexpected(&unexpectedHandler);
 
156
#endif
 
157
  _thisObj = this ;
 
158
  _id = _poa->activate_object(_thisObj);
 
159
}
 
160
 
 
161
FSI_MILIEU_i::FSI_MILIEU_i(CORBA::ORB_ptr orb,
 
162
                     PortableServer::POA_ptr poa,
 
163
                     Engines::Container_ptr container,
 
164
                     const char *instanceName,
 
165
                     const char *interfaceName)
 
166
          : Superv_Component_i(orb, poa, container, instanceName, interfaceName)
 
167
{
 
168
#if 0
 
169
  setsig(SIGSEGV,&THandler);
 
170
  set_terminate(&terminateHandler);
 
171
  set_unexpected(&unexpectedHandler);
 
172
#endif
 
173
  _thisObj = this ;
 
174
  _id = _poa->activate_object(_thisObj);
 
175
}
 
176
 
 
177
//! Destructor for component "FSI_MILIEU" instance
 
178
FSI_MILIEU_i::~FSI_MILIEU_i()
 
179
{
 
180
}
 
181
 
 
182
void FSI_MILIEU_i::destroy()
 
183
{
 
184
#if 0
 
185
  _remove_ref();
 
186
  if(!CORBA::is_nil(_orb))
 
187
    _orb->shutdown(0);
 
188
#else
 
189
  Engines_Component_i::destroy();
 
190
#endif
 
191
}
 
192
 
 
193
//! Register datastream ports for a component service given its name
 
194
/*!
 
195
 *  \param service_name : service name
 
196
 *  \return true if port registering succeeded, false if not
 
197
 */
 
198
CORBA::Boolean
 
199
FSI_MILIEU_i::init_service(const char * service_name) {
 
200
  CORBA::Boolean rtn = false;
 
201
  string s_name(service_name);
 
202
 
 
203
  if (s_name == "inter_run")
 
204
    {
 
205
      try
 
206
        {
 
207
          //initialization CALCIUM ports IN
 
208
          create_calcium_port(this,(char *)"ALMAXI",(char *)"CALCIUM_double",(char *)"IN",(char *)"I");
 
209
          create_calcium_port(this,(char *)"DTSAT",(char *)"CALCIUM_double",(char *)"IN",(char *)"I");
 
210
          create_calcium_port(this,(char *)"DTAST",(char *)"CALCIUM_double",(char *)"IN",(char *)"I");
 
211
          create_calcium_port(this,(char *)"DEPAST",(char *)"CALCIUM_double",(char *)"IN",(char *)"I");
 
212
          create_calcium_port(this,(char *)"VITAST",(char *)"CALCIUM_double",(char *)"IN",(char *)"I");
 
213
          create_calcium_port(this,(char *)"FORSAT",(char *)"CALCIUM_double",(char *)"IN",(char *)"I");
 
214
          create_calcium_port(this,(char *)"DONGEO",(char *)"CALCIUM_integer",(char *)"IN",(char *)"I");
 
215
          create_calcium_port(this,(char *)"ICV",(char *)"CALCIUM_integer",(char *)"IN",(char *)"I");
 
216
          //initialization CALCIUM ports OUT
 
217
          create_calcium_port(this,(char *)"DTCALC",(char *)"CALCIUM_double",(char *)"OUT",(char *)"I");
 
218
          create_calcium_port(this,(char *)"EPSILO",(char *)"CALCIUM_double",(char *)"OUT",(char *)"I");
 
219
          create_calcium_port(this,(char *)"TTINIT",(char *)"CALCIUM_double",(char *)"OUT",(char *)"I");
 
220
          create_calcium_port(this,(char *)"PDTREF",(char *)"CALCIUM_double",(char *)"OUT",(char *)"I");
 
221
          create_calcium_port(this,(char *)"DEPSAT",(char *)"CALCIUM_double",(char *)"OUT",(char *)"I");
 
222
          create_calcium_port(this,(char *)"FORAST",(char *)"CALCIUM_double",(char *)"OUT",(char *)"I");
 
223
          create_calcium_port(this,(char *)"NBPDTM",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
224
          create_calcium_port(this,(char *)"NBSSIT",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
225
          create_calcium_port(this,(char *)"ISYNCP",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
226
          create_calcium_port(this,(char *)"NTCHRO",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
227
          create_calcium_port(this,(char *)"ICVEXT",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
228
          create_calcium_port(this,(char *)"ICVAST",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
229
          create_calcium_port(this,(char *)"NB_DYN",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
230
          create_calcium_port(this,(char *)"NB_FOR",(char *)"CALCIUM_integer",(char *)"OUT",(char *)"I");
 
231
        }
 
232
      catch(const PortAlreadyDefined& ex)
 
233
        {
 
234
          std::cerr << "FSI_MILIEU: " << ex.what() << std::endl;
 
235
          //Ports already created : we use them
 
236
        }
 
237
      catch ( ... )
 
238
        {
 
239
          std::cerr << "FSI_MILIEU: unknown exception" << std::endl;
 
240
        }
 
241
      rtn = true;
 
242
    }
 
243
 
 
244
  return rtn;
 
245
}
 
246
 
 
247
 
 
248
void FSI_MILIEU_i::inter_run(CORBA::Long NBPDTM,CORBA::Long NBSSIT,CORBA::Long ISYNCP,CORBA::Long NTCHR,CORBA::Double DTREF,CORBA::Double TTINIT,CORBA::Double EPSILO)
 
249
{
 
250
  beginService("FSI_MILIEU_i::inter_run");
 
251
  Superv_Component_i * component = dynamic_cast<Superv_Component_i*>(this);
 
252
  //char       nom_instance[INSTANCE_LEN];
 
253
  //int info = cp_cd(component,nom_instance);
 
254
  try
 
255
    {
 
256
//BODY
 
257
inter_cs_ast_set_nbpdtm(NBPDTM);
 
258
inter_cs_ast_set_nbssit(NBSSIT);
 
259
inter_cs_ast_set_isyncp(ISYNCP);
 
260
inter_cs_ast_set_ntchr(NTCHR);
 
261
inter_cs_ast_set_dtref(DTREF);
 
262
inter_cs_ast_set_ttinit(TTINIT);
 
263
inter_cs_ast_set_epsilo(EPSILO);
 
264
runmilieu(component);
 
265
//ENDBODY
 
266
      //cp_fin(component,CP_ARRET);
 
267
    }
 
268
  catch ( const CalciumException & ex)
 
269
    {
 
270
      std::cerr << ex.what() << std::endl;
 
271
      //cp_fin(component,CP_ARRET);
 
272
      SALOME::ExceptionStruct es;
 
273
      es.text=CORBA::string_dup(ex.what());
 
274
      es.type=SALOME::INTERNAL_ERROR;
 
275
      throw SALOME::SALOME_Exception(es);
 
276
    }
 
277
  catch ( const SALOME_Exception & ex)
 
278
    {
 
279
      //cp_fin(component,CP_ARRET);
 
280
      SALOME::ExceptionStruct es;
 
281
      es.text=CORBA::string_dup(ex.what());
 
282
      es.type=SALOME::INTERNAL_ERROR;
 
283
      throw SALOME::SALOME_Exception(es);
 
284
    }
 
285
  catch ( const SALOME::SALOME_Exception & ex)
 
286
    {
 
287
      //cp_fin(component,CP_ARRET);
 
288
      throw;
 
289
    }
 
290
  catch ( const std::exception& ex)
 
291
    {
 
292
      //std::cerr << typeid(ex).name() << std::endl;
 
293
      SALOME::ExceptionStruct es;
 
294
      es.text=CORBA::string_dup(ex.what());
 
295
      es.type=SALOME::INTERNAL_ERROR;
 
296
      throw SALOME::SALOME_Exception(es);
 
297
    }
 
298
  catch (...)
 
299
    {
 
300
      std::cerr << "unknown exception" << std::endl;
 
301
#if 0
 
302
      _exit(-1);
 
303
#endif
 
304
      //cp_fin(component,CP_ARRET);
 
305
      SALOME::ExceptionStruct es;
 
306
      es.text=CORBA::string_dup(" unknown exception");
 
307
      es.type=SALOME::INTERNAL_ERROR;
 
308
      throw SALOME::SALOME_Exception(es);
 
309
    }
 
310
  endService("FSI_MILIEU_i::inter_run");
 
311
}
 
312
 
 
313
 
 
314
 
 
315
extern "C"
 
316
{
 
317
  PortableServer::ObjectId * FSI_MILIEUEngine_factory( CORBA::ORB_ptr orb,
 
318
                                                    PortableServer::POA_ptr poa,
 
319
                                                    PortableServer::ObjectId * contId,
 
320
                                                    const char *instanceName,
 
321
                                                    const char *interfaceName)
 
322
  {
 
323
    MESSAGE("PortableServer::ObjectId * FSI_MILIEUEngine_factory()");
 
324
    FSI_MILIEU_i * myEngine = new FSI_MILIEU_i(orb, poa, contId, instanceName, interfaceName);
 
325
    return myEngine->getId() ;
 
326
  }
 
327
  void yacsinit()
 
328
  {
 
329
    int argc=0;
 
330
    char *argv=0;
 
331
    CORBA::ORB_var orb = CORBA::ORB_init( argc , &argv ) ;
 
332
    PortableServer::POAManager_var pman;
 
333
    CORBA::Object_var obj;
 
334
    try
 
335
      {
 
336
        SALOME_NamingService * salomens = new SALOME_NamingService(orb);
 
337
        obj = orb->resolve_initial_references("RootPOA");
 
338
        PortableServer::POA_var  poa = PortableServer::POA::_narrow(obj);
 
339
        PortableServer::POAManager_var pman = poa->the_POAManager();
 
340
        std::string containerName(getenv("SALOME_CONTAINERNAME"));
 
341
        std::string instanceName(getenv("SALOME_INSTANCE"));
 
342
        obj=orb->string_to_object(getenv("SALOME_CONTAINER"));
 
343
        Engines::Container_var container = Engines::Container::_narrow(obj);
 
344
        FSI_MILIEU_i * myEngine = new FSI_MILIEU_i(orb, poa, container, instanceName.c_str(), "FSI_MILIEU");
 
345
        pman->activate();
 
346
        obj=myEngine->POA_FSI_ORB::FSI_MILIEU::_this();
 
347
        Engines::EngineComponent_var component = Engines::EngineComponent::_narrow(obj);
 
348
        string component_registerName = containerName + "/" + instanceName;
 
349
        salomens->Register(component,component_registerName.c_str());
 
350
        orb->run();
 
351
        orb->destroy();
 
352
      }
 
353
    catch(CORBA::Exception&)
 
354
      {
 
355
        std::cerr << "Caught CORBA::Exception."<< std::endl;
 
356
      }
 
357
    catch(std::exception& exc)
 
358
      {
 
359
        std::cerr << "Caught std::exception - "<<exc.what() << std::endl;
 
360
      }
 
361
    catch(...)
 
362
      {
 
363
        std::cerr << "Caught unknown exception." << std::endl;
 
364
      }
 
365
  }
 
366
 
 
367
  void F_FUNC(yacsinit,YACSINIT)()
 
368
  {
 
369
    yacsinit();
 
370
  }
 
371
}