def __init__(self, factory, ansys_input_directory): self.input_data = factory.input_data self.directory = factory.directory self.output_directory = factory.output_directory self.ansys_input_directory = ansys_input_directory os.chdir(self.directory) with open('aaS_MapdlID.txt', 'r') as f: aasMapdlKey = f.read() self.mapdl = CORBA.ORB_init().string_to_object(aasMapdlKey)
def _launch(self, start_parm, verbose): corba_key = launch_corba(verbose=verbose, **start_parm) orb = CORBA.ORB_init() self._server = orb.string_to_object(corba_key) # verify you can connect to MAPDL try: self._server.getComponentName() except: raise MapdlRuntimeError('Unable to connect to APDL server') # must set to non-interactive in linux if os.name == 'posix': self.batch() self._log.debug('Connected to ANSYS using CORBA interface with key %s', corba_key) # separate logger for broadcast file if self._log_broadcast: self._broadcast_logger = self._start_broadcast_logger()
def open_corba(self, nproc, timeout, additional_switches): """ Open a connection to ANSYS via a CORBA interface """ self.log.info('Connecting to ANSYS via CORBA') # command must include "aas" flag to start MAPDL server command = '"%s" -j %s -aas -i tmp.inp -o out.txt -b -np %d %s' % ( self.exec_file, self._jobname, nproc, additional_switches) # add run location to command self.log.debug('Spawning shell process with: "%s"' % command) self.log.debug('At "%s"' % self.path) old_path = os.getcwd() os.chdir(self.path) self.process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) os.chdir(old_path) # listen for broadcast file self.log.debug('Waiting for valid key in %s' % self.broadcast_file) telapsed = 0 tstart = time.time() while telapsed < timeout: try: if os.path.isfile(self.broadcast_file): with open(self.broadcast_file, 'r') as f: text = f.read() if 'visited:collaborativecosolverunitior' in text: self.log.debug('Initialized ANSYS') break time.sleep(0.1) telapsed = time.time() - tstart except KeyboardInterrupt: raise KeyboardInterrupt # exit if timed out if telapsed > timeout: err_msg = 'Unable to start ANSYS within %.1f seconds' % timeout self.log.error(err_msg) raise TimeoutError(err_msg) # open server keyfile = os.path.join(self.path, 'aaS_MapdlId.txt') with open(keyfile) as f: key = f.read() # attempt to import corba try: from ansys_corba import CORBA except: pip_cmd = 'pip install ansys_corba' raise ImportError('Missing ansys_corba.\n' + 'This feature does not support MAC OS.\n' +\ 'Otherwise, please install with "%s"' % pip_cmd) orb = CORBA.ORB_init() self.mapdl = orb.string_to_object(key) # quick test try: self.mapdl.getComponentName() except: raise Exception('Unable to connect to APDL server') self.using_corba = True self.log.debug('Connected to ANSYS using CORBA interface') self.log.debug('Key %s' % key)
def _launch(self): """Open a connection to ANSYS via a CORBA interface""" # Using stored parameters so launch command can be run from a # cached state (when launching the GUI) self._log.info('Connecting to ANSYS via CORBA') # create a dummy input file for getting NON-INTERACTIVE without # running /BATCH tmp_inp = os.path.join(self.path, 'tmp.inp') with open(tmp_inp, 'w') as f: f.write('FINISH') # command must include "aas" flag to start MAPDL server command = '"%s" -aas -j %s -b -i tmp.inp -o out.txt -np %d %s' % ( self._exec_file, self._jobname, self._nproc, self._additional_switches) # remove the broadcast file if it exists as the key will be # output here when ansys server is available if os.path.isfile(self._broadcast_file): os.remove(self._broadcast_file) # add run location to command self._log.debug('Spawning shell process with: "%s"', command) self._log.debug('At "%s"', self.path) # after v19, this is the only way this will work... if os.name == 'nt': command = 'START /B "MAPDL" %s' % command # set stdout if self._log.level < 20: # < INFO self._process = subprocess.Popen(command, shell=True, cwd=self.path) else: self._process = subprocess.Popen(command, shell=True, cwd=self.path, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # listen for broadcast file self._log.debug('Waiting for valid key in %s', self._broadcast_file) telapsed = 0 tstart = time.time() while telapsed < self._start_timeout: try: if os.path.isfile(self._broadcast_file): with open(self._broadcast_file, 'r') as f: text = f.read() if 'visited:collaborativecosolverunitior' in text: self._log.debug('Initialized ANSYS') break time.sleep(0.1) telapsed = time.time() - tstart except KeyboardInterrupt: raise KeyboardInterrupt # exit if timed out if telapsed > self._start_timeout: err_msg = 'Unable to start ANSYS within %.1f seconds' % self._start_timeout self._log.error(err_msg) raise TimeoutError(err_msg) # open server keyfile = os.path.join(self.path, 'aaS_MapdlId.txt') with open(keyfile) as f: key = f.read() orb = CORBA.ORB_init() self._server = orb.string_to_object(key) # set to non-interactive # if os.name != 'nt': # text = self._server.executeCommandToString('/BATCH') try: self._server.getComponentName() except: raise RuntimeError('Unable to connect to APDL server') self._log.debug('Connected to ANSYS using CORBA interface with key %s', key) # separate logger for broadcast file if self._log_broadcast: self._broadcast_logger = self._start_broadcast_logger()