Skip to content

Commit 16b97b4

Browse files
authored
Fix off_screen testing open_gui (#264)
* remove off_screen arg * running AATT prior to surface meshing for display * added surface cache * fixed open_gui * version bump to 0.43.2
1 parent f6f5401 commit 16b97b4

File tree

9 files changed

+193
-176
lines changed

9 files changed

+193
-176
lines changed

pyansys/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# major, minor, patch
2-
version_info = 0, 43, 1
2+
version_info = 0, 43, 2
33

44
# Nice string for the version
55
__version__ = '.'.join(map(str, version_info))

pyansys/launcher.py

Lines changed: 4 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
"""Module for launching MAPDL locally."""
22
from glob import glob
3-
import time
4-
import subprocess
53
import re
64
import warnings
75
import os
86
import appdirs
97
import tempfile
108
import socket
11-
import pexpect
129

1310
from pyansys.misc import is_float, random_string
1411
from pyansys.errors import LockFileException, VersionError
@@ -209,114 +206,6 @@ def check_lock_file(path, jobname, override):
209206
'running at "%s"' % path)
210207

211208

212-
213-
def launch_corba(exec_file=None, run_location=None, jobname=None, nproc=None,
214-
additional_switches='', start_timeout=60):
215-
"""Start MAPDL in AAS mode
216-
217-
218-
Notes
219-
-----
220-
The CORBA interface is likely to fail on computers with multiple
221-
network adapters. The ANSYS RPC isn't smart enough to determine
222-
the right adapter and will likely try to communicate on the wrong
223-
IP.
224-
"""
225-
# Using stored parameters so launch command can be run from a
226-
# cached state (when launching the GUI)
227-
228-
# can't run /BATCH in windows, so we trick it using "-b" and
229-
# provide a dummy input file
230-
if os.name == 'nt':
231-
# must be a random filename to avoid conflicts with other
232-
# potential instances
233-
tmp_file = '%s.inp' % random_string(10)
234-
with open(os.path.join(run_location, tmp_file), 'w') as f:
235-
f.write('FINISH')
236-
additional_switches += ' -b -i %s -o out.txt' % tmp_file
237-
238-
# command must include "aas" flag to start MAPDL server
239-
command = '"%s" -aas -j %s -np %d %s' % (exec_file,
240-
jobname,
241-
nproc,
242-
additional_switches)
243-
244-
# if os.name == 'nt': # required after v190
245-
# command = 'START /B "MAPDL" %s' % command
246-
247-
# remove any broadcast files
248-
broadcast_file = os.path.join(run_location, 'mapdl_broadcasts.txt')
249-
if os.path.isfile(broadcast_file):
250-
os.remove(broadcast_file)
251-
252-
# windows 7 won't run this
253-
# if os.name == 'nt' and platform.release() == '7':
254-
# cwd = os.getcwd()
255-
# os.chdir(run_location)
256-
# os.system('START /B "MAPDL" %s' % command)
257-
# os.chdir(cwd)
258-
# else:
259-
subprocess.Popen(command, shell=True,
260-
cwd=run_location,
261-
stdin=subprocess.DEVNULL,
262-
stdout=subprocess.DEVNULL,
263-
stderr=subprocess.DEVNULL)
264-
265-
# listen for broadcast file
266-
telapsed = 0
267-
tstart = time.time()
268-
started_rpc = False
269-
while telapsed < start_timeout and not started_rpc:
270-
try:
271-
if os.path.isfile(broadcast_file):
272-
broadcast = open(broadcast_file).read()
273-
# see if connection to RPC has been made
274-
rpc_txt = 'visited:collaborativecosolverunitior-set:'
275-
started_rpc = rpc_txt in broadcast
276-
277-
time.sleep(0.1)
278-
telapsed = time.time() - tstart
279-
280-
except KeyboardInterrupt:
281-
raise KeyboardInterrupt
282-
283-
# exit if timed out
284-
if not started_rpc:
285-
err_str = 'Unable to start ANSYS within %.1f seconds' % start_timeout
286-
if os.path.isfile(broadcast_file):
287-
broadcast = open(broadcast_file).read()
288-
err_str += '\n\nLast broadcast:\n%s' % broadcast
289-
raise TimeoutError(err_str)
290-
291-
# return CORBA key
292-
keyfile = os.path.join(run_location, 'aaS_MapdlId.txt')
293-
return open(keyfile).read()
294-
295-
296-
def launch_pexpect(exec_file=None, run_location=None, jobname=None, nproc=None,
297-
additional_switches='', start_timeout=60):
298-
"""Launch MAPDL as a pexpect process.
299-
300-
Limited to only a linux instance
301-
"""
302-
command = '%s -j %s -np %d %s' % (exec_file, jobname, nproc,
303-
additional_switches)
304-
process = pexpect.spawn(command, cwd=run_location)
305-
process.delaybeforesend = None
306-
307-
try:
308-
index = process.expect(['BEGIN:', 'CONTINUE'],
309-
timeout=start_timeout)
310-
except: # capture failure
311-
raise RuntimeError(process.before.decode('utf-8'))
312-
313-
if index: # received ... press enter to continue
314-
process.sendline('')
315-
process.expect('BEGIN:', timeout=start_timeout)
316-
317-
return process
318-
319-
320209
def launch_mapdl(exec_file=None, run_location=None, mode=None, jobname='file',
321210
nproc=2, override=False, loglevel='INFO',
322211
additional_switches='', start_timeout=120,
@@ -632,16 +521,12 @@ def launch_mapdl(exec_file=None, run_location=None, mode=None, jobname='file',
632521

633522
if mode == 'console':
634523
from pyansys.mapdl_console import MapdlConsole
635-
process = launch_pexpect(**start_parm)
636-
return MapdlConsole(process, loglevel=loglevel,
637-
log_apdl=log_apdl, **start_parm)
524+
return MapdlConsole(loglevel=loglevel, log_apdl=log_apdl, **start_parm)
638525
elif mode == 'corba':
639526
from pyansys.mapdl_corba import MapdlCorba
640-
corba_key = launch_corba(**start_parm)
641-
return MapdlCorba(corba_key, loglevel=loglevel,
642-
log_apdl=log_apdl,
643-
log_broadcast=kwargs.get('log_broadcast', False),
644-
**start_parm)
527+
return MapdlCorba(loglevel=loglevel, log_apdl=log_apdl,
528+
log_broadcast=kwargs.get('log_broadcast',
529+
False), **start_parm)
645530
else:
646531
raise ValueError('Invalid mode %s' % mode)
647532

@@ -663,12 +548,6 @@ def check_mode(mode, version):
663548
if mode == 'corba':
664549
if version < 170:
665550
raise VersionError('CORBA AAS mode requires MAPDL v17.0 or newer.')
666-
# elif version > 20.1 and os.name == 'nt':
667-
# raise ValueError('CORBA AAS mode on Windows requires MAPDL 2020R1'
668-
# ' or earlier.')
669-
# elif version >= 20.0 and os.name == 'posix':
670-
# raise ValueError('CORBA AAS mode on Linux requires MAPDL 2019R3'
671-
# ' or earlier.')
672551

673552
elif mode == 'console' and is_win:
674553
raise ValueError('Console mode requires Linux')

pyansys/mapdl.py

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from pyansys.element_commands import element_commands
2020
from pyansys.errors import MapdlRuntimeError, MapdlInvalidRoutineError
2121
from pyansys.plotting import general_plotter
22-
22+
from pyansys.launcher import get_ansys_path
2323

2424
MATPLOTLIB_LOADED = True
2525
try:
@@ -459,22 +459,24 @@ def open_gui(self, include_result=True): # pragma: no cover
459459
if os.path.isfile(tmp_database):
460460
os.remove(tmp_database)
461461

462-
# get the state, close, and finish
462+
# cache result file, version, and routine before closing
463+
resultfile = self._result_file
464+
version = self.version
463465
prior_processor = self.parameters.routine
466+
467+
# get the close, and finish
464468
self.finish()
465469
self.save(tmp_database)
466-
# self.exit(close_log=False)
467470
self.exit()
468471

469472
# copy result file to temp directory
470473
if include_result:
471-
resultfile = os.path.join(self.path, '%s.rst' % self.jobname)
472474
if os.path.isfile(resultfile):
473475
tmp_resultfile = os.path.join(save_path, '%s.rst' % name)
474476
copyfile(resultfile, tmp_resultfile)
475477

476478
# write temporary input file
477-
start_file = os.path.join(save_path, 'start%s.ans' % self.version)
479+
start_file = os.path.join(save_path, 'start%s.ans' % version)
478480
with open(start_file, 'w') as f:
479481
f.write('RESUME\n')
480482

@@ -486,22 +488,25 @@ def open_gui(self, include_result=True): # pragma: no cover
486488
# issue system command to run ansys in GUI mode
487489
cwd = os.getcwd()
488490
os.chdir(save_path)
489-
os.system('cd "%s" && "%s" -g -j %s' % (save_path,
490-
self._start_parm['exec_file'],
491-
name))
491+
exec_file = self._start_parm.get('exec_file',
492+
get_ansys_path(allow_input=False))
493+
os.system('cd "%s" && "%s" -g -j %s' % (save_path, exec_file, name))
492494
os.chdir(cwd)
493495

494496
# must remove the start file when finished
495497
os.remove(start_file)
496498
os.remove(other_start_file)
497499

498-
# reload database when finished
499-
# self._launch() # TODO
500+
# reattach to a new session and reload database
501+
self._launch(self._start_parm)
500502
self.resume(tmp_database)
501503
if prior_processor is not None:
502504
if 'BEGIN' not in prior_processor:
503505
self.run('/%s' % prior_processor)
504506

507+
def _launch(self, *args, **kwargs): # pragma: no cover
508+
raise NotImplementedError('Implemented by child class')
509+
505510
def _close_apdl_log(self):
506511
"""Closes the APDL log"""
507512
if self._apdl_log is not None:
@@ -1047,6 +1052,20 @@ def result(self):
10471052
if not self._local:
10481053
raise RuntimeError('Binary interface only available when result is local.')
10491054

1055+
result_path = self._result_file
1056+
if not os.path.isfile(result_path):
1057+
raise FileNotFoundError('No results found at %s' % result_path)
1058+
return pyansys.read_binary(result_path)
1059+
1060+
@property
1061+
def _result_file(self):
1062+
"""Path of the result file
1063+
1064+
Notes
1065+
-----
1066+
There may be multiple result files at this location (if not
1067+
combining results)
1068+
"""
10501069
try:
10511070
result_path = self.inquire('RSTFILE')
10521071
except RuntimeError:
@@ -1056,12 +1075,7 @@ def result(self):
10561075
result_path = os.path.join(self.path, '%s.rst' % self._jobname)
10571076
elif not os.path.dirname(result_path):
10581077
result_path = os.path.join(self.path, '%s.rst' % result_path)
1059-
1060-
# there may be multiple result files at this location (if not
1061-
# combining results)
1062-
if not os.path.isfile(result_path):
1063-
raise FileNotFoundError('No results found at %s' % result_path)
1064-
return pyansys.read_binary(result_path)
1078+
return result_path
10651079

10661080
def _get(self, *args, **kwargs):
10671081
"""Simply use the default get method"""
@@ -1332,7 +1346,7 @@ def read_float_from_inline_function(self, function_str):
13321346
13331347
Returns
13341348
-------
1335-
float
1349+
value : float
13361350
Value returned by inline function.
13371351
13381352
Examples

pyansys/mapdl_console.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
Used when launching Mapdl via pexpect on Linux when <= 17.0
44
"""
5+
import pexpect
56
import time
67
import re
78

@@ -41,26 +42,50 @@
4142
ignored = re.compile(r'[\s\S]+'.join(['WARNING', 'command', 'ignored']))
4243

4344

45+
def launch_pexpect(exec_file=None, run_location=None, jobname=None, nproc=None,
46+
additional_switches='', start_timeout=60):
47+
"""Launch MAPDL as a pexpect process.
48+
49+
Limited to only a linux instance
50+
"""
51+
command = '%s -j %s -np %d %s' % (exec_file, jobname, nproc,
52+
additional_switches)
53+
process = pexpect.spawn(command, cwd=run_location)
54+
process.delaybeforesend = None
55+
56+
try:
57+
index = process.expect(['BEGIN:', 'CONTINUE'],
58+
timeout=start_timeout)
59+
except: # capture failure
60+
raise RuntimeError(process.before.decode('utf-8'))
61+
62+
if index: # received ... press enter to continue
63+
process.sendline('')
64+
process.expect('BEGIN:', timeout=start_timeout)
65+
66+
return process
67+
68+
4469
class MapdlConsole(_MapdlCore):
4570
"""Control interaction with an ANSYS shell instance.
4671
4772
Only works on Linux.
48-
49-
Parameters
50-
----------
51-
process : pexpect
52-
5373
"""
5474

55-
def __init__(self, process, loglevel='INFO', log_apdl='w',
56-
use_vtk=True, **start_parm):
75+
def __init__(self, loglevel='INFO', log_apdl='w', use_vtk=True,
76+
**start_parm):
5777
"""Opens an ANSYS process using pexpect"""
5878
self._auto_continue = True
5979
self._continue_on_error = False
60-
self._process = process
80+
self._process = None
81+
self._launch(start_parm)
6182
super().__init__(loglevel=loglevel, use_vtk=use_vtk, log_apdl=log_apdl,
6283
**start_parm)
6384

85+
def _launch(self, start_parm):
86+
"""Connect to MAPDL process using pexpect"""
87+
self._process = launch_pexpect(**start_parm)
88+
6489
def _run(self, command):
6590
"""Sends command and returns ANSYS's response"""
6691
self._reset_cache()

0 commit comments

Comments
 (0)