Skip to content

Commit 3a30f4d

Browse files
authored
Fix/ use cdread inside input (#671)
* Added swtich to modify the input function input file. * Updated test case to reflect CDB format properly instead of input format. * Passing also CDREAD options as kwargs * Modifying the CDREAD test cases to include the new type of CDB. * Fixing style.
1 parent a8bc8d1 commit 3a30f4d

File tree

2 files changed

+67
-18
lines changed

2 files changed

+67
-18
lines changed

ansys/mapdl/core/mapdl_grpc.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,9 @@ def cdread(self, *args, **kwargs):
10091009

10101010
kwargs.setdefault("verbose", False)
10111011
kwargs.setdefault("progress_bar", False)
1012+
kwargs.setdefault("orig_cmd", 'CDREAD')
1013+
kwargs.setdefault("cd_read_option", option.upper())
1014+
10121015
self.input(fname, **kwargs)
10131016

10141017
@protect_grpc
@@ -1019,6 +1022,7 @@ def input(
10191022
progress_bar=False,
10201023
time_step_stream=None,
10211024
chunk_size=512,
1025+
orig_cmd='/INP',
10221026
**kwargs,
10231027
):
10241028
"""Stream a local input file to a remote mapdl instance.
@@ -1045,6 +1049,12 @@ def input(
10451049
These defaults will be ignored if ``time_step_stream`` is
10461050
manually set.
10471051
1052+
orig_cmd : str
1053+
Original command. There are some cases, were input is
1054+
used to send the file to the grpc server but then we want
1055+
to run something different than ``/INPUT``, for example
1056+
``CDREAD``.
1057+
10481058
Returns
10491059
-------
10501060
str
@@ -1133,7 +1143,14 @@ def input(
11331143
# file.
11341144
tmp_name = "_input_tmp_.inp"
11351145
tmp_out = "_input_tmp_.out"
1136-
tmp_dat = f"/OUT,{tmp_out}\n/INP,'{filename}'\n"
1146+
if 'CDRE' in orig_cmd.upper():
1147+
# Using CDREAD
1148+
option = kwargs.get("cd_read_option", 'COMB')
1149+
tmp_dat = f"/OUT,{tmp_out}\n{orig_cmd},'{option}','{filename}'\n"
1150+
else:
1151+
# Using default INPUT
1152+
tmp_dat = f"/OUT,{tmp_out}\n{orig_cmd},'{filename}'\n"
1153+
11371154
if self._local:
11381155
local_path = self.directory
11391156
with open(os.path.join(local_path, tmp_name), "w") as f:

tests/test_mapdl.py

Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,20 @@
4747
# Many of the commands could be deleted, but for the sake of good
4848
# testing we are going to leave them.
4949

50-
CDB_FILE = """/COM,ANSYS RELEASE 2021 R2 BUILD 21.2
50+
CDB_FILE = """ S 1
51+
/COM,ANSYS RELEASE 12.1BETAUP20090531 10:26:32 06/01/2009 S 2
52+
/NOPR S 3
53+
/PREP7 S 4
54+
/TITLE, S 5
55+
1H,,1H;,,18Hdisc_pad_model.cdb, G 1
56+
5HANSYS,22H 12.1BETA UP20090531,,,,,,,1.0,,,,,13H000601.102632, G 2
57+
1.0000E-04,,,,9,,; G 3
58+
S 29G 3D 0P 0 T 1
59+
:CDWRITE ! START OF CDWRITE DATA
60+
/COM,ANSYS RELEASE 2021 R2 BUILD 21.2
5161
/PREP7
5262
/NOPR
53-
/TITLE,'CDREAD and CDWRITE tests
63+
/TITLE,'CDREAD and CDWRITE tests'
5464
*IF,_CDRDOFF,EQ,1,THEN !if solid model was read in
5565
_CDRDOFF= !reset flag, numoffs already performed
5666
*ELSE !offset database for the following FE model
@@ -103,6 +113,25 @@ def asserting_cdread_cdwrite_tests(mapdl):
103113
return 'asdf1234' in mapdl.parameters['T_PAR']
104114

105115

116+
def warns_in_cdread_error_log(mapdl):
117+
"""Check for specific warns in the error log associated with using /INPUT with CDB files
118+
instead of CDREAD command."""
119+
error_files = [each for each in os.listdir(mapdl.directory) if each.endswith('.err')]
120+
121+
# "S 1", "1 H" and "5 H Ansys" are character at the end of lines in the CDB_FILE variable.
122+
# They are allowed in the CDREAD command, but it gives warnings in the /INPUT command.
123+
warn_cdread_1 = "S1 is not a recognized"
124+
warn_cdread_2 = "1H is not a recognized"
125+
warn_cdread_3 = "5HANSYS is not a recognized"
126+
127+
warns = []
128+
for each in error_files:
129+
with open(os.path.join(mapdl.directory, each)) as fid:
130+
error_log = ''.join(fid.readlines())
131+
warns.append((warn_cdread_1 in error_log) or (warn_cdread_2 in error_log) or (warn_cdread_3 in error_log))
132+
return any(warns)
133+
134+
106135
@pytest.fixture(scope="function")
107136
def make_block(mapdl, cleared):
108137
mapdl.block(0, 1, 0, 1, 0, 1)
@@ -696,15 +725,15 @@ def test_title(mapdl, cleared):
696725

697726

698727
def test_cdread(mapdl, cleared):
699-
random_letters = mapdl.directory.split('/')[0][-3:0]
728+
random_letters = random_string(4)
700729

701-
mapdl.run(f"parmtest='{random_letters}'")
730+
mapdl.run(f"PARMTEST='{random_letters}'")
702731
mapdl.cdwrite('all', 'model2', 'cdb')
703732

704733
mapdl.clear()
705734
mapdl.cdread("db", 'model2', 'cdb')
706735

707-
assert random_letters == mapdl.parameters['parmtest']
736+
assert random_letters in mapdl.parameters['PARMTEST']
708737

709738

710739
# CDREAD tests are actually a good way to test 'input' command.
@@ -738,35 +767,38 @@ def test_cdread_in_python_directory(mapdl, cleared, tmpdir):
738767
# the archive from the current working directory.
739768
old_cwd = os.getcwd()
740769
try:
770+
# We are not checking yet if the file is read correctly, just if the file
771+
# can be read.
741772
os.chdir(tmpdir)
742-
mapdl.cdread('db', 'model', 'cdb')
743-
assert asserting_cdread_cdwrite_tests(mapdl)
773+
mapdl.cdread('COMB', 'model', 'cdb') # 'COMB' is needed since we use the CDB with the strange line endings.
774+
assert asserting_cdread_cdwrite_tests(mapdl) and not warns_in_cdread_error_log(mapdl)
744775

745776
clearing_cdread_cdwrite_tests(mapdl)
746-
mapdl.cdread('db', 'model.cdb')
747-
assert asserting_cdread_cdwrite_tests(mapdl)
777+
mapdl.cdread('COMB', 'model.cdb')
778+
assert asserting_cdread_cdwrite_tests(mapdl) and not warns_in_cdread_error_log(mapdl)
748779

749780
clearing_cdread_cdwrite_tests(mapdl)
750-
mapdl.cdread('db', 'model')
751-
assert asserting_cdread_cdwrite_tests(mapdl)
781+
mapdl.cdread('COMB', 'model')
782+
assert asserting_cdread_cdwrite_tests(mapdl) and not warns_in_cdread_error_log(mapdl)
783+
752784
finally:
753785
# always change back to the previous directory
754786
os.chdir(old_cwd)
755787

756788
clearing_cdread_cdwrite_tests(mapdl)
757789
fullpath = str(tmpdir.join('model.cdb'))
758-
mapdl.cdread('db', fullpath)
759-
assert asserting_cdread_cdwrite_tests(mapdl)
790+
mapdl.cdread('COMB', fullpath)
791+
assert asserting_cdread_cdwrite_tests(mapdl) and not warns_in_cdread_error_log(mapdl)
760792

761793
clearing_cdread_cdwrite_tests(mapdl)
762794
fullpath = str(tmpdir.join('model'))
763-
mapdl.cdread('db', fullpath, 'cdb')
764-
assert asserting_cdread_cdwrite_tests(mapdl)
795+
mapdl.cdread('COMB', fullpath, 'cdb')
796+
assert asserting_cdread_cdwrite_tests(mapdl) and not warns_in_cdread_error_log(mapdl)
765797

766798
clearing_cdread_cdwrite_tests(mapdl)
767799
fullpath = str(tmpdir.join('model'))
768-
mapdl.cdread('db', fullpath)
769-
assert asserting_cdread_cdwrite_tests(mapdl)
800+
mapdl.cdread('COMB', fullpath)
801+
assert asserting_cdread_cdwrite_tests(mapdl) and not warns_in_cdread_error_log(mapdl)
770802

771803

772804
def test_cdread_in_apdl_directory(mapdl, cleared):

0 commit comments

Comments
 (0)