Skip to content

Commit 4ae1d4b

Browse files
koubaaMohamed Koubaapyansys-ci-bot
authored
fix: Contact mpp fix (#744)
Co-authored-by: Mohamed Koubaa <[email protected]> Co-authored-by: pyansys-ci-bot <[email protected]>
1 parent 39974e3 commit 4ae1d4b

File tree

14 files changed

+96
-63
lines changed

14 files changed

+96
-63
lines changed

doc/changelog/744.documentation.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fix: Contact mpp fix

src/ansys/dyna/core/lib/card.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,31 @@ def _convert_fields_to_long_format(self) -> typing.List[Field]:
5656
return fields
5757

5858
def read(self, buf: typing.TextIO, parameter_set: ParameterSet = None) -> bool:
59-
if not self._is_active():
59+
if not self.active:
6060
return False
6161
line, to_exit = read_line(buf)
6262
if to_exit:
6363
return True
6464
self._load(line, parameter_set)
6565
return False
6666

67+
def _get_format_spec(self, fields):
68+
format_spec = []
69+
for field in fields:
70+
if field._is_flag():
71+
field_type = field._value
72+
else:
73+
field_type = field.type
74+
format_spec.append((field.offset, field.width, field_type))
75+
return format_spec
76+
6777
def _load(self, data_line: str, parameter_set: ParameterSet) -> None:
6878
"""loads the card data from a list of strings"""
6979
fields = self._fields
7080
if self.format == format_type.long:
7181
fields = self._convert_fields_to_long_format()
72-
format = [(field.offset, field.width, field.type) for field in fields]
73-
values = load_dataline(format, data_line, parameter_set)
82+
format_spec = self._get_format_spec(fields)
83+
values = load_dataline(format_spec, data_line, parameter_set)
7484
num_fields = len(fields)
7585
for field_index in range(num_fields):
7686
self._fields[field_index].value = values[field_index]
@@ -85,15 +95,16 @@ def write(
8595
format = self._format_type
8696

8797
def _write(buf: typing.TextIO):
88-
if self._is_active():
98+
if self.active:
8999
if comment:
90100
write_comment_line(buf, self._fields, format)
91101
buf.write("\n")
92102
write_fields(buf, self._fields, None, format)
93103

94104
return write_or_return(buf, _write)
95105

96-
def _is_active(self) -> bool:
106+
@property
107+
def active(self) -> bool:
97108
if self._active_func == None:
98109
return True
99110
return True if self._active_func() else False

src/ansys/dyna/core/lib/card_interface.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ def __subclasshook__(cls, subclass):
3737
and callable(subclass.write)
3838
and hasattr(subclass, "read")
3939
and callable(subclass.read)
40+
and hasattr(subclass, "active")
41+
and callable(subclass.active)
4042
)
4143

4244
@abc.abstractmethod
@@ -65,3 +67,9 @@ def format(self) -> format_type:
6567
def format(self, value: format_type) -> None:
6668
"""Set the card format type."""
6769
raise NotImplementedError
70+
71+
@property
72+
@abc.abstractmethod
73+
def active(self) -> bool:
74+
"""Return whether the card is active."""
75+
raise NotImplementedError

src/ansys/dyna/core/lib/card_set.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _items(self, value: typing.List[Cards]) -> None:
7070
def _initialize(self):
7171
if self._initialized:
7272
return
73-
if self._bounded and self._active:
73+
if self._bounded and self.active:
7474
self._initialize_data(self._length_func())
7575
self._initialized = True
7676

@@ -82,7 +82,7 @@ def option_specs(self):
8282
return self._option_specs
8383

8484
@property
85-
def _active(self) -> bool:
85+
def active(self) -> bool:
8686
if self._active_func == None:
8787
return True
8888
return self._active_func()

src/ansys/dyna/core/lib/cards.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""Base class for cards and I/O."""
2424

2525
import typing
26+
import warnings
2627

2728
from ansys.dyna.core.lib.card_interface import CardInterface
2829
from ansys.dyna.core.lib.card_writer import write_cards
@@ -188,12 +189,28 @@ def _try_read_options_with_no_title(self, buf: typing.TextIO) -> None:
188189
if not any_options_read:
189190
buf.seek(pos)
190191

192+
def _read_card(self, card: CardInterface, buf: typing.TextIO, parameters: ParameterSet) -> bool:
193+
pos = buf.tell()
194+
with warnings.catch_warnings(record=True) as w:
195+
card.read(buf, parameters)
196+
caught = list(w)
197+
198+
# the card is not active after reading it. THat means we should *not* read it. Rewinding back to the buffer
199+
# start position. In this case any warnings caused by reading the card can be ignored.
200+
if not card.active:
201+
buf.seek(pos)
202+
else:
203+
# emit warnings caught while reading the card
204+
for caught_warning in caught:
205+
warnings.warn(caught_warning.message)
206+
return True
207+
191208
def _read_data(self, buf: typing.TextIO, parameters: ParameterSet) -> None:
192209
for card in self._get_pre_option_cards():
193-
card.read(buf, parameters)
210+
self._read_card(card, buf, parameters)
194211
for card in self._get_non_option_cards():
195-
card.read(buf, parameters)
212+
self._read_card(card, buf, parameters)
196213
for card in self._get_post_option_cards():
197-
card.read(buf, parameters)
214+
self._read_card(card, buf, parameters)
198215

199216
self._try_read_options_with_no_title(buf)

src/ansys/dyna/core/lib/cards_/special/include_card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def write(
7373
format = self._format_type
7474

7575
def _write(buf: typing.TextIO):
76-
if self._is_active():
76+
if self.active:
7777
if comment:
7878
write_comment_line(buf, self._fields, format)
7979
buf.write("\n")

src/ansys/dyna/core/lib/field.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import dataclasses
2525
import typing
2626

27-
import pandas as pd
28-
2927
from ansys.dyna.core.lib.config import use_lspp_defaults
3028

3129

@@ -52,8 +50,15 @@ def __init__(self, name: str, type: type, offset: int, width: int, /, value: typ
5250
else:
5351
self._value = kwargs.get(name, value) if use_lspp_defaults() else None
5452

53+
def _is_flag(self) -> bool:
54+
return type(self._value) == Flag
55+
5556
def __repr__(self) -> str:
56-
return f"Field({self.name}, {self.type}, {self.offset}, {self.width}, {self.value})"
57+
if self._is_flag():
58+
valuestr = f"Flag({self.value})"
59+
else:
60+
valuestr = self.value
61+
return f"Field({self.name}, {self.type}, {self.offset}, {self.width}, {valuestr})"
5762

5863
@property
5964
def name(self) -> str:
@@ -89,28 +94,20 @@ def width(self, value: int) -> None:
8994

9095
@property
9196
def value(self) -> typing.Any:
92-
if type(self._value) == Flag:
97+
if self._is_flag():
9398
return self._value.value
9499
return self._value
95100

96-
def _truthy_value(self, value) -> bool:
97-
if pd.isna(value):
98-
return False
99-
return True if value else False
100-
101-
def _no_value_flag(self, value) -> bool:
102-
return self._truthy_value(value) and type(value) is Flag
103-
104101
@value.setter
105102
def value(self, value: typing.Any) -> None:
106-
if type(self._value) == Flag:
103+
if self._is_flag():
107104
self._value.value = value
108105
else:
109106
self._value = value
110107

111108
def io_info(self) -> typing.Tuple[str, typing.Type]:
112109
"""Return the value and type used for io."""
113-
if type(self._value) == Flag:
110+
if self._is_flag():
114111
if self._value.value:
115112
return self._value.true_value, str
116113
else:

src/ansys/dyna/core/lib/series_card.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ def __get_null_value(self):
171171
return math.nan
172172
return None
173173

174-
def _is_active(self) -> bool:
174+
@property
175+
def active(self) -> bool:
175176
if self._active_func == None:
176177
return True
177178
return self._active_func()
@@ -299,7 +300,7 @@ def write(
299300
if format == None:
300301
format = self._format_type
301302
output = ""
302-
if self._is_active():
303+
if self.active:
303304
lines = [row for row in self._get_lines(format, comment) if row]
304305
output = "\n".join(lines)
305306
if buf == None:

src/ansys/dyna/core/lib/table_card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ def bounded(self) -> bool:
267267
return self._bounded
268268

269269
def _num_rows(self) -> int:
270-
if not self._is_active():
270+
if not self.active:
271271
return 0
272272
return self._length_func()
273273

src/ansys/dyna/core/lib/table_card_group.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ def write(
136136
buf: typing.Optional[typing.TextIO] = None,
137137
comment: typing.Optional[bool] = True,
138138
) -> str:
139-
if self._is_active():
139+
if self.active:
140140
self._initialize()
141141
self._propagate()
142142

@@ -190,7 +190,8 @@ def _get_index_of_given_card(self, overall_index: int) -> int:
190190
index into the card given by _get_index_of_which_card"""
191191
return math.floor(overall_index / len(self._get_active_cards()))
192192

193-
def _is_active(self) -> bool:
193+
@property
194+
def active(self) -> bool:
194195
if self._active_func == None:
195196
return True
196197
return self._active_func()
@@ -214,7 +215,7 @@ def bounded(self) -> bool:
214215
return self._bounded
215216

216217
def _num_rows(self) -> int:
217-
if not self._is_active():
218+
if not self.active:
218219
return 0
219220
num_active_cards = len(self._get_active_cards())
220221
return self._length_func() * num_active_cards

0 commit comments

Comments
 (0)