From 2cafb931f48008b634d15a27513e68aefc96c9e3 Mon Sep 17 00:00:00 2001 From: German <28149841+germa89@users.noreply.github.com> Date: Tue, 21 Dec 2021 16:40:51 +0100 Subject: [PATCH] Improved /TBFT function. (#782) * Improved /TBFT function. It does know upload the experimental data files. * Improved /TBFT function. It does know upload the experimental data files. * Added test case. * Small fix. * Apply suggestions from code review Co-authored-by: Alex Kaszynski * Using get. * Fixing test unit. * Fixing test unit. * Fixing tbft * Added another unit test with raises File not found. * Added another unit test with raises File not found. And fixs. * Added try and finally statements. * Improved unit test. Co-authored-by: Alex Kaszynski --- ansys/mapdl/core/mapdl_grpc.py | 25 +++++++++++++++++++++ tests/test_mapdl.py | 40 +++++++++++++++++++++++++++++++++- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/ansys/mapdl/core/mapdl_grpc.py b/ansys/mapdl/core/mapdl_grpc.py index 5e6c78ceed..911587aa9d 100755 --- a/ansys/mapdl/core/mapdl_grpc.py +++ b/ansys/mapdl/core/mapdl_grpc.py @@ -1043,6 +1043,31 @@ def cdread(self, *args, **kwargs): self.input(fname, **kwargs) + @wraps(_MapdlCore.tbft) + def tbft(self, oper='', id_='', option1='', option2='', option3='', option4='', option5='', option6='', option7='', **kwargs): + """Wraps ``_MapdlCore.tbft``.""" + if oper.lower() == 'eadd': + # Option 2 is a file and option 4 is the directory. + # Option 3 is be extension + option3 = option3.replace('.', '') + fname = option2 if not option3 else option2 + '.' + option3 + filename = os.path.join(option4, fname) + + if self._local: + if not os.path.exists(filename) and filename not in self.list_files(): + raise FileNotFoundError(f"File '{filename}' could not be found.") + else: + if os.path.exists(filename): + self.upload(filename) + option4 = '' # You don't need the directory if you upload it. + elif filename in self.list_files(): + option4 = '' # You don't need the directory if the file is in WDIR + pass + else: + raise FileNotFoundError(f"File '{filename}' could not be found.") + + return super().tbft(oper, id_, option1, option2, option3, option4, option5, option6, option7, **kwargs) + @protect_grpc def input( self, diff --git a/tests/test_mapdl.py b/tests/test_mapdl.py index 1b45b7df97..7cfa911320 100644 --- a/tests/test_mapdl.py +++ b/tests/test_mapdl.py @@ -992,4 +992,42 @@ def test_inquire(mapdl): # **Returning Information About a File to a Parameter** jobname = mapdl.inquire("", 'jobname') assert float(mapdl.inquire("", 'exist', jobname + '.lock')) in [0, 1] - assert float(mapdl.inquire("", 'exist', jobname , 'lock')) in [0, 1] + assert float(mapdl.inquire("", 'exist', jobname, 'lock')) in [0, 1] + + +@pytest.mark.parametrize("option2,option3,option4", [('expdata.dat', '', ''), ('expdata', '.dat', ''), ('expdata', 'dat', 'DIR')]) +def test_tbft(mapdl, option2, option3, option4): + try: + fname = 'expdata.dat' + fpath = os.path.join(os.getcwd(), fname) + + with open(fpath, 'w') as fid: + fid.write("""0.819139E-01 0.82788577E+00 + 0.166709E+00 0.15437247E+01 + 0.253960E+00 0.21686152E+01 + 0.343267E+00 0.27201819E+01 + 0.434257E+00 0.32129833E+0""") + + if option4 == 'DIR': + option4 = os.getcwd() + + mapdl.prep7(mute=True) + mat_id = mapdl.get_value('MAT', 0, 'NUM', 'MAX') + 1 + mapdl.tbft('FADD', mat_id, 'HYPER', 'MOONEY', '3', mute=True) + mapdl.tbft('EADD', mat_id, 'UNIA', option2, option3, option4, mute=True) + + assert fname in mapdl.list_files() + + finally: + try: + os.remove(fname) + except OSError: + pass + + +def test_tbft_not_found(mapdl): + with pytest.raises(FileNotFoundError): + mapdl.prep7(mute=True) + mat_id = mapdl.get_value('MAT', 0, 'NUM', 'MAX') + 1 + mapdl.tbft('FADD', mat_id, 'HYPER', 'MOONEY', '3', mute=True) + mapdl.tbft('EADD', mat_id, 'UNIA', 'non_existing.file', '', '', mute=True)