Skip to content

Commit 2ff3e06

Browse files
committed
Converter star slash improvements (#1478)
* Adding only_commands option to converter. * Adding unit tests * fixing trailing commas issue * Removing unnecessary copy * Fixing unit tests. Changing variables names. * Another improvement * Changing variables name. * Changing variable name to lower caps. * Update tests/test_convert.py
1 parent 4932798 commit 2ff3e06

File tree

2 files changed

+126
-42
lines changed

2 files changed

+126
-42
lines changed

src/ansys/mapdl/core/convert.py

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def convert_script(
8484
cleanup_output=True,
8585
header=True,
8686
print_com=True,
87+
only_commands=False,
8788
):
8889
"""Converts an ANSYS input file to a python PyMAPDL script.
8990
@@ -144,6 +145,12 @@ def convert_script(
144145
Print command ``/COM`` arguments to python console.
145146
Defaults to ``True``.
146147
148+
only_commands : bool, optional
149+
If ``True``, it converts only the commands, meaning that header
150+
(``header=False``), imports (``add_imports=False``),
151+
and exit commands are NOT included (``auto_exit=False``).
152+
Overrides ``header``, ``add_imports`` and ``auto_exit``.
153+
147154
Returns
148155
-------
149156
list
@@ -190,6 +197,7 @@ def convert_script(
190197
cleanup_output=cleanup_output,
191198
header=header,
192199
print_com=print_com,
200+
only_commands=only_commands,
193201
)
194202

195203
translator.save(filename_out)
@@ -210,6 +218,7 @@ def convert_apdl_block(
210218
cleanup_output=True,
211219
header=True,
212220
print_com=True,
221+
only_commands=False,
213222
):
214223
"""Converts an ANSYS input string to a python PyMAPDL string.
215224
@@ -273,6 +282,12 @@ def convert_apdl_block(
273282
Print command ``/COM`` arguments to python console.
274283
Defaults to ``True``.
275284
285+
only_commands : bool, optional
286+
If ``True``, it converts only the commands, meaning that header
287+
(``header=False``), imports (``add_imports=False``),
288+
and exit commands are NOT included (``auto_exit=False``).
289+
Overrides ``header``, ``add_imports`` and ``auto_exit``.
290+
276291
Returns
277292
-------
278293
list
@@ -308,6 +323,7 @@ def convert_apdl_block(
308323
cleanup_output=cleanup_output,
309324
header=header,
310325
print_com=print_com,
326+
only_commands=only_commands,
311327
)
312328

313329
if isinstance(apdl_strings, str):
@@ -329,8 +345,14 @@ def _convert(
329345
cleanup_output=True,
330346
header=True,
331347
print_com=True,
348+
only_commands=False,
332349
):
333350

351+
if only_commands:
352+
auto_exit = False
353+
add_imports = False
354+
header = False
355+
334356
translator = FileTranslator(
335357
loglevel,
336358
line_ending,
@@ -586,36 +608,40 @@ def translate_line(self, line):
586608
# Cleaning ending empty arguments.
587609
# Because of an extra comma added to toffst command when generating ds.dat.
588610
line_ = line.split(",")[::-1] # inverting order
611+
589612
for ind, each in enumerate(line_):
590-
if each:
613+
if each.strip(): # strip to remove spaces in empty arguments
591614
break
592-
else:
593-
line_.pop(ind)
594-
line = ",".join(line_[::-1])
615+
616+
line = ",".join(line_[ind:][::-1])
595617

596618
# remove trailing comma
597619
line = line[:-1] if line[-1] == "," else line
620+
line_upper = line.upper()
598621

599-
cmd_ = line.split(",")[0].upper()
622+
cmd_caps = line.split(",")[0].upper()
623+
cmd_caps_short = cmd_caps[:4]
600624

601-
if cmd_[:4] in ["SOLV", "LSSO"] and self._comment_solve:
625+
items = line.split(",")
626+
627+
if cmd_caps_short in ["SOLV", "LSSO"] and self._comment_solve:
602628
self.store_command(
603629
"com", ["The following line has been commented due to `comment_solve`:"]
604630
)
605631
self.store_command("com", [line])
606632
return
607633

608-
if cmd_[:4] == "/COM":
634+
if cmd_caps_short == "/COM":
609635
# It is a comment
610636
self.store_command("com", [line[5:]])
611637
return
612638

613-
if cmd_ == "*DO":
639+
if cmd_caps == "*DO":
614640
self.start_non_interactive()
615641
self.store_run_command(line)
616642
return
617643

618-
if cmd_ in ["*ENDDO", "*ENDIF"]:
644+
if cmd_caps in ["*ENDDO", "*ENDIF"]:
619645
self.store_run_command(line)
620646
self.end_non_interactive()
621647
return
@@ -631,13 +657,13 @@ def translate_line(self, line):
631657
self.end_non_interactive()
632658
return
633659

634-
if cmd_ == "/VERIFY":
660+
if cmd_caps == "/VERIFY":
635661
self.store_run_command("FINISH")
636662
self.store_run_command(line)
637663
self.store_run_command("/PREP7")
638664
return
639665

640-
if line[:4].upper() == "*REP":
666+
if cmd_caps_short == "*REP":
641667
if not self.non_interactive:
642668
prev_cmd = self.lines.pop(-1)
643669
self.start_non_interactive()
@@ -651,33 +677,40 @@ def translate_line(self, line):
651677
self.end_non_interactive()
652678
return
653679

654-
if line[:4].upper() in COMMANDS_TO_NOT_BE_CONVERTED:
680+
if cmd_caps_short in COMMANDS_TO_NOT_BE_CONVERTED:
655681
self.store_run_command(line)
656682
return
657683

658-
if line[:4].upper() == "/TIT": # /TITLE
684+
if cmd_caps_short == "/TIT": # /TITLE
659685
parameters = line.split(",")[1:]
660686
return self.store_command("title", ["".join(parameters).strip()])
661687

662-
if line[:4].upper() == "*GET":
688+
if cmd_caps_short == "*GET":
663689
if self.non_interactive: # gives error
664690
self.store_run_command(line)
665691
return
666692
else:
667693
parameters = line.split(",")[1:]
668694
return self.store_command("get", parameters)
669695

670-
if line[:4].upper() == "/NOP":
696+
if cmd_caps_short == "/NOP":
671697
self.comment = (
672698
"It is not recommended to use '/NOPR' in a normal PyMAPDL session."
673699
)
674700
self.store_under_scored_run_command(line)
675701
return
676702

677-
if line[:6].upper() == "/PREP7":
703+
if cmd_caps_short == "*CRE": # creating a function
704+
if self.macros_as_functions:
705+
self.start_function(items[1].strip())
706+
return
707+
else:
708+
self.start_non_interactive()
709+
710+
if cmd_caps == "/PREP7":
678711
return self.store_command("prep7", [])
679712

680-
if "*END" in line:
713+
if "*END" in line_upper:
681714
if self.macros_as_functions:
682715
self.store_empty_line()
683716
self.store_empty_line()
@@ -693,7 +726,7 @@ def translate_line(self, line):
693726
return
694727

695728
# check for if statement
696-
if line[:3].upper() == "*IF" or "*IF" in line.upper():
729+
if cmd_caps[:3] == "*IF" or "*IF" in line_upper:
697730
self.start_non_interactive()
698731
self.store_run_command(line)
699732
return
@@ -712,16 +745,14 @@ def translate_line(self, line):
712745
): # To escape cmds that require (XX) but they are not in block
713746
self.end_non_interactive()
714747
return
715-
elif line[:4] == "*USE" and self.macros_as_functions:
716-
items = line.split(",")
748+
elif cmd_caps_short == "*USE" and self.macros_as_functions:
717749
func_name = items[1].strip()
718750
if func_name in self._functions:
719751
args = ", ".join(items[2:])
720752
self.lines.append(f"{func_name}({args})")
721753
return
722754

723755
# check if a line is setting a variable
724-
items = line.split(",")
725756
if "=" in items[0]: # line sets a variable:
726757
self.store_run_command(line)
727758
return
@@ -732,54 +763,67 @@ def translate_line(self, line):
732763
self.store_empty_line()
733764
return
734765

735-
if line == "-1" or line == "END PREAD": # End of block commands
766+
if line == "-1" or line_upper == "END PREAD": # End of block commands
736767
self.store_run_command(line)
737768
self._in_block = False
738769
self.end_non_interactive()
739770
return
740771

741772
# check valid command
742-
if command not in self._valid_commands:
743-
cmd = line[:4].upper()
744-
if cmd == "*CRE": # creating a function
745-
if self.macros_as_functions:
746-
self.start_function(items[1].strip())
747-
return
748-
else:
749-
self.start_non_interactive()
750-
751-
elif cmd in self._non_interactive_commands:
752-
if cmd in self._block_commands:
773+
if (
774+
self._pymapdl_command(command) not in self._valid_commands
775+
or cmd_caps_short in self._non_interactive_commands
776+
):
777+
if cmd_caps_short in self._non_interactive_commands:
778+
if cmd_caps_short in self._block_commands:
753779
self._in_block = True
754780
self._block_count = 0
755781
self._block_count_target = 0
756782

757-
elif cmd in self._enum_block_commands:
783+
elif cmd_caps_short in self._enum_block_commands:
758784
self._in_block = True
759785
self._block_count = 0
760-
if cmd == "CMBL": # In cmblock
786+
if cmd_caps_short == "CMBL": # In cmblock
761787
# CMBLOCK,Cname,Entity,NUMITEMS,,,,,KOPT
762788
numitems = int(line.split(",")[3])
763789
_block_count_target = (
764790
numitems // 8 + 1 if numitems % 8 != 0 else numitems // 8
765791
)
766792
self._block_count_target = (
767793
_block_count_target + 2
768-
) # because the cmd line and option line.
794+
) # because the cmd_caps_short line and option line.
769795

770-
self._block_current_cmd = cmd
796+
self._block_current_cmd = cmd_caps_short
771797
self.start_non_interactive()
772798

773-
if self._in_block and cmd not in self._non_interactive_commands:
799+
if self._in_block and cmd_caps_short not in self._non_interactive_commands:
774800
self.store_run_command(original_line)
775801
else:
776802
self.store_run_command(line)
777803

778804
elif self.use_function_names:
805+
if command[0] == "/":
806+
slash_command = f"slash{command[1:]}"
807+
if slash_command in dir(Commands):
808+
command = slash_command
809+
else:
810+
command = command[1:]
811+
elif command[0] == "*":
812+
star_command = f"star{command[1:]}"
813+
if star_command in dir(Commands):
814+
command = star_command
815+
else:
816+
command = command[1:]
817+
779818
self.store_command(command, parameters)
780819
else:
781820
self.store_run_command(line)
782821

822+
def _pymapdl_command(self, command):
823+
if command[0] in ["/", "*"]:
824+
command = command[1:]
825+
return command
826+
783827
def start_function(self, func_name):
784828
self._functions.append(func_name)
785829
self.store_empty_line()

tests/test_convert.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,11 @@ def test_comment_solve():
244244

245245

246246
def test_macro_to_function():
247-
assert "def SLV(" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
248-
assert "SLV()" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
249-
assert "\n\n\ndef SLV" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
250-
assert "\n\n\nSLV" in convert_apdl_block(APDL_MACRO, macros_as_functions=True)
247+
output = convert_apdl_block(APDL_MACRO, macros_as_functions=True)
248+
assert "def SLV(" in output
249+
assert "SLV()" in output
250+
assert "\n\n\ndef SLV" in output
251+
assert "\n\n\nSLV" in output
251252

252253

253254
def test_out():
@@ -361,3 +362,42 @@ def test_print_com_in_converter():
361362
assert "print_com=True" in convert_apdl_block("/prep7\nN,,,,") # Default
362363
assert "print_com=True" in convert_apdl_block("/prep7\nN,,,,", print_com=True)
363364
assert "print_com=True" not in convert_apdl_block("/prep7\nN,,,,", print_com=False)
365+
366+
367+
def test_only_commands():
368+
output = convert_apdl_block(
369+
"/view,1,1,1",
370+
only_commands=True,
371+
add_imports=True,
372+
auto_exit=True,
373+
header="asdf",
374+
)
375+
assert "mapdl.view(1, 1, 1)" in output
376+
assert "launch_mapdl" not in output
377+
assert "import" not in output
378+
assert "mapdl.exit" not in output
379+
380+
381+
@pytest.mark.parametrize(
382+
"parameters",
383+
[
384+
["/view,1,1,1", "mapdl.view(1, 1, 1)"],
385+
["/view,1,,1,1", 'mapdl.view(1, "", 1, 1)'],
386+
["/view,1,,1, ,1", 'mapdl.view(1, "", 1, "", 1)'],
387+
["*get,1,1,1", "mapdl.get(1, 1, 1)"],
388+
["*get,1,asdf,,1,qwert", 'mapdl.get(1, "asdf", "", 1, "qwert")'],
389+
["*get,1,asdf,,1,qwert", 'mapdl.get(1, "asdf", "", 1, "qwert")'],
390+
["vget,1,,'asdf',", 'mapdl.vget(1, "", "asdf")'],
391+
["*vget,1,,'asdf',", 'mapdl.starvget(1, "", "asdf")'],
392+
["*vget,1,,'asdf',,,,,", 'mapdl.starvget(1, "", "asdf")'],
393+
[
394+
"*vget,1,,,,,,,'asdf',,,,,",
395+
'mapdl.starvget(1, "", "", "", "", "", "", "asdf")',
396+
],
397+
["solve", "mapdl.solve()"],
398+
["/solu", "mapdl.slashsolu()"],
399+
["solu", "mapdl.solu()"],
400+
],
401+
)
402+
def test_convert_star_slash(parameters):
403+
assert convert_apdl_block(parameters[0], only_commands=True) == parameters[1]

0 commit comments

Comments
 (0)