Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: supporting ´´to_dataframe()´´ for some bc list commands #3412

Merged
merged 5 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/changelog.d/3412.miscellaneous.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
to_dataframe() support for some bc list commands
129 changes: 117 additions & 12 deletions src/ansys/mapdl/core/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@
REG_FLOAT_INT = re.compile(
r"[+-]?[0-9]*[.]?[0-9]*[Ee]?[+-]?[0-9]+|\s[0-9]+\s"
) # match number groups
BC_REGREP = re.compile(
r"^\s*([0-9]+)\s*([A-Za-z]+)\s*([0-9]*[.]?[0-9]+)\s+([0-9]*[.]?[0-9]+)"
)
BC_REGREP = re.compile(r"^\s*([0-9]+)\s*([A-Za-z]+)((?:\s+[0-9]*[.]?[0-9]+)+)$")


MSG_NOT_PANDAS = """'Pandas' is not installed or could not be found.
Hence this command is not applicable.
Expand Down Expand Up @@ -105,7 +104,34 @@
"SWLI",
]

CMD_BC_LISTING = ["FLIS", "DLIS"]
CMD_BC_LISTING = [
"DKLI",
"DLLI",
"DALI",
"DLIS",
"FKLI",
"FLIS",
"SFLL",
# "SFAL", Define two integers before label (regex)
# "SFLI", Use two lines to define each BC in the list
# "SFEL", Use two lines to define each BC in the list
"BFKL",
"BFLL",
"BFAL",
]

COLNAMES_BC_LISTING = {
"DKLI": ["KEYPOINT", "LABEL", "REAL", "IMAG", "EXP KEY"],
"DLLI": ["LINE", "LABEL", "REAL", "IMAG", "NAREA"],
"DALI": ["AREA", "LABEL", "REAL", "IMAG"],
"DLIS": ["NODE", "LABEL", "REAL", "IMAG"],
"FKLI": ["KEYPOINT", "LABEL", "REAL", "IMAG"],
"FLIS": ["NODE", "LABEL", "REAL", "IMAG"],
"SFLL": ["LINE", "LABEL", "VALI", "VALJ", "VAL2I", "VAL2J"],
"BFKL": ["KEYPOINT", "LABEL", "VALUE"],
"BFLL": ["LINE", "LABEL", "VALUE"],
"BFAL": ["AREA", "LABEL", "VALUE"],
}

CMD_ENTITY_LISTING = [
"NLIS",
Expand Down Expand Up @@ -793,15 +819,92 @@

"""

def bc_colnames(self):
"""Get the column names based on bc list command"""

bc_type = {
"BODY FORCES": "BF",
"SURFACE LOAD": "SF",
"POINT LOAD": "F",
"FORCES": "F",
"CONSTRAINTS": "D",
}

entity = {
"KEYPOINT": "K",
"LINE": "L",
"AREA": "A",
"NODE": "",
"ELEMENT": "E",
}

title = self._get_body()[0]

_bcType = [i for i in bc_type.keys() if i in title]
_entity = [i for i in entity.keys() if i in title]

if _bcType and _entity:

key_bc = bc_type[_bcType[0]] + entity[_entity[0]] + "LIST"
key_bc = key_bc[:4]

if key_bc in COLNAMES_BC_LISTING.keys():

_cols = COLNAMES_BC_LISTING[key_bc]

# Check num columns in data
ldata = []
for line in self.splitlines():
line = line.strip()
# exclude any line containing characters [A-Z] except for E
if line:
items = BC_REGREP.findall(line)
if items:
ldata = list(items[0][:2]) + items[0][2].split()
break

if ldata:
if len(_cols) > len(ldata):
_cols = _cols[: len(ldata)]

return _cols

return None

Check warning on line 872 in src/ansys/mapdl/core/commands.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/commands.py#L872

Added line #L872 was not covered by tests

def get_columns(self):
"""Get the column names for the dataframe.

Returns
-------
List of strings

"""
if self._columns_names:
return self._columns_names

Check warning on line 883 in src/ansys/mapdl/core/commands.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/commands.py#L883

Added line #L883 was not covered by tests

bc_colnames = self.bc_colnames()

if bc_colnames:
return bc_colnames

body = self._get_body()

Check warning on line 890 in src/ansys/mapdl/core/commands.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/commands.py#L890

Added line #L890 was not covered by tests

pairs = list(self._get_data_group_indexes(body))
try:
return body[pairs[0][0]].split()
except:
return None

Check warning on line 896 in src/ansys/mapdl/core/commands.py

View check run for this annotation

Codecov / codecov/patch

src/ansys/mapdl/core/commands.py#L892-L896

Added lines #L892 - L896 were not covered by tests

def _parse_table(self):
"""Parse tabular command output."""
parsed_lines = []
for line in self.splitlines():
line = line.strip()
# exclude any line containing characters [A-Z] except for E
if line:
items = BC_REGREP.findall(line)
if items:
parsed_lines.append(list(items[0]))
parsed_lines.append(list(items[0][:2]) + items[0][2].split())

return parsed_lines

Expand Down Expand Up @@ -838,17 +941,19 @@

"""
df = super().to_dataframe(data=self.to_list())
if "NODE" in df.columns:
df["NODE"] = df["NODE"].astype(np.int32, copy=False)

primitives = ["KEYPOINT", "LINE", "AREA", "VOLUME", "NODE", "ELEMENT"]

float_col = ["REAL", "IMAG", "VALUE", "VALI", "VALJ"]

for i in df.columns.intersection(primitives):
df[i] = df[i].astype(np.int32, copy=False)

if "LABEL" in df.columns:
df["LABEL"] = df["LABEL"].astype(str, copy=False)

if "REAL" in df.columns:
df["REAL"] = df["REAL"].astype(np.float64, copy=False)

if "IMAG" in df.columns:
df["IMAG"] = df["IMAG"].astype(np.float64, copy=False)
for i in df.columns.intersection(float_col):
df[i] = df[i].astype(np.float64, copy=False)

return df

Expand Down
Loading
Loading