# -*- coding: utf-8 -*-

#-------------------------------------------------------------------------------

# This file is part of Code_Saturne, a general-purpose CFD tool.
#
# Copyright (C) 1998-2013 EDF S.A.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301, USA.

#-------------------------------------------------------------------------------

import sys,traceback,os
import string
import FSI_ORB__POA
import calcium
import dsccalcium
import SALOME
import linecache

# Trick so that one doesn't have to set the PYTHONPATH variable
cspath = '@pkgpythondir@'
if os.path.isdir(cspath) and not cspath in sys.path:
    sys.path.insert(0, cspath)

import ConfigParser
from cs_package import package

config = ConfigParser.ConfigParser()
pkg = package()
config.read(pkg.get_configfiles())

# Code_Aster installation directory
if not config.has_option('install', 'aster'):
  sys.stderr.write("Cannot locate Code_Aster installation.\n")
  sys.exit(1)
else:
  aster_dir = config.get('install', 'aster')

# Code_Aster Python library files
aster_path = os.path.join(aster_dir, 'bibpyt')

sys.path.insert(0, aster_path)

from Execution.E_SUPERV import SUPERV


try:
  import numpy
except:
  numpy=None

#DEFS

#ENDDEF

class ExecutionError(Exception):
  '''General exception during execution'''

class FSI_ASTER(FSI_ORB__POA.FSI_ASTER,dsccalcium.PyDSCComponent,SUPERV):
  '''
     To be identified as a SALOME component this Python class
     must have the same name as the component, inherit omniorb
     class FSI_ORB__POA.FSI_ASTER and DSC class dsccalcium.PyDSCComponent
     that implements DSC API.
  '''
  def __init__ ( self, orb, poa, contID, containerName, instanceName, interfaceName ):
    self.init=0
    dsccalcium.PyDSCComponent.__init__(self, orb, poa,contID,containerName,instanceName,interfaceName)

  def init_service(self,service):
    if service == "op0117":
       #initialization CALCIUM ports IN
       calcium.create_calcium_port(self.proxy,"NB_FOR","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"NB_DYN","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"COONOD","CALCIUM_double","IN","I")
       calcium.create_calcium_port(self.proxy,"COOFAC","CALCIUM_double","IN","I")
       calcium.create_calcium_port(self.proxy,"COLNOD","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"COLFAC","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"FORAST","CALCIUM_double","IN","I")
       calcium.create_calcium_port(self.proxy,"NBPDTM","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"NBSSIT","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"EPSILO","CALCIUM_double","IN","I")
       calcium.create_calcium_port(self.proxy,"ICVAST","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"ISYNCP","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"NTCHRO","CALCIUM_integer","IN","I")
       calcium.create_calcium_port(self.proxy,"TTINIT","CALCIUM_double","IN","I")
       calcium.create_calcium_port(self.proxy,"PDTREF","CALCIUM_double","IN","I")
       calcium.create_calcium_port(self.proxy,"DTCALC","CALCIUM_double","IN","I")
       #initialization CALCIUM ports OUT
       calcium.create_calcium_port(self.proxy,"DEPAST","CALCIUM_double","OUT","I")
       calcium.create_calcium_port(self.proxy,"VITAST","CALCIUM_double","OUT","I")
       calcium.create_calcium_port(self.proxy,"DTAST","CALCIUM_double","OUT","I")
       return True
    return False

  def interpstring(self,text,args):
    try:
      self.jdc.g_context.update(args)
      CONTEXT.set_current_step(self.jdc)
      linecache.cache['<string>']=0,None,string.split(text,'\n'),'<string>'
      exec text in self.jdc.const_context,self.jdc.g_context
      CONTEXT.unset_current_step()
    except EOFError:
      CONTEXT.unset_current_step()
    except:
      CONTEXT.unset_current_step()
      raise


  def op0117(self,jdc):
    self.beginService("FSI_ASTER.op0117")
    try:
      args={"jdc":jdc,"component":self.proxy.ptr()}
      if not args.has_key("jdc"):
        fcomm=open("jdc",'r')
        jdc=fcomm.read()
        fcomm.close()
        #args["jdc"]=jdc
      if not self.init:
        self.init=1
        fcomm=open("fort.1",'w')
        fcomm.write(jdc)
        fcomm.close()
        ier=self.main(args)
        if ier != 0:
          raise ExecutionError("Error in initial execution")
      else:
        self.interpstring(jdc,args)

      self.endService("FSI_ASTER.op0117")
      j=self.jdc
      return
    except:
      exc_typ,exc_val,exc_fr=sys.exc_info()
      l=traceback.format_exception(exc_typ,exc_val,exc_fr)
      self.endService("FSI_ASTER.op0117")
      sys.stdout.flush()
      sys.stderr.flush()
      raise SALOME.SALOME_Exception(SALOME.ExceptionStruct(SALOME.BAD_PARAM,"".join(l),"FSI_ASTER.py",0))


  def destroy(self):
     self._orb.shutdown(0)
