Skip to content

Commit 4cd8c11

Browse files
authored
Merge pull request #1200 from jguarato/dev/print-table
Add format_table methods
2 parents 67503f3 + 15cf185 commit 4cd8c11

File tree

4 files changed

+156
-42
lines changed

4 files changed

+156
-42
lines changed

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ xlrd
77
pint>=0.18
88
methodtools
99
numba
10-
ccp-performance
10+
ccp-performance
11+
prettytable

ross/bearing_seal_element.py

Lines changed: 107 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import toml
99
import warnings
1010
from inspect import signature
11+
from prettytable import PrettyTable
1112
from numpy.polynomial import Polynomial
1213
from plotly import graph_objects as go
1314
from scipy import interpolate as interpolate
@@ -259,6 +260,19 @@ def _process_coefficient(self, coefficient):
259260

260261
return coefficient, interpolated
261262

263+
def _get_coefficient_list(self, ignore_mass=False):
264+
"""List with all bearing coefficients as strings"""
265+
coefficients = [
266+
attr.replace("_interpolated", "")
267+
for attr in self.__dict__.keys()
268+
if "_interpolated" in attr
269+
]
270+
271+
if ignore_mass:
272+
coefficients = [coeff for coeff in coefficients if "m" not in coeff]
273+
274+
return coefficients
275+
262276
def plot(
263277
self,
264278
coefficients=None,
@@ -357,6 +371,97 @@ def plot(
357371

358372
return fig
359373

374+
@check_units
375+
def format_table(
376+
self,
377+
frequency=None,
378+
coefficients=None,
379+
frequency_units="rad/s",
380+
stiffness_units="N/m",
381+
damping_units="N*s/m",
382+
mass_units="kg",
383+
):
384+
"""Return frequency vs coefficients in table format.
385+
386+
Parameters
387+
----------
388+
frequency : array, pint.Quantity, optional
389+
Array with frequencies (rad/s).
390+
Default is 5 values from min to max frequency.
391+
coefficients : list, str, optional
392+
List or str with the coefficients to include.
393+
Defaults is a list of stiffness and damping coefficients.
394+
frequency_units : str, optional
395+
Frequency units.
396+
Default is rad/s.
397+
stiffness_units : str, optional
398+
Stiffness units.
399+
Default is N/m.
400+
damping_units : str, optional
401+
Damping units.
402+
Default is N*s/m.
403+
mass_units : str, optional
404+
Mass units.
405+
Default is kg.
406+
407+
Returns
408+
-------
409+
table : PrettyTable object
410+
Table object with bearing coefficients to be printed.
411+
412+
Example
413+
-------
414+
>>> bearing = bearing_example()
415+
>>> table = bearing.format_table(
416+
... frequency=[0, 50, 100, 150, 200],
417+
... coefficients=['kxx', 'kxy', 'cxx', 'cxy']
418+
... )
419+
>>> print(table)
420+
+-------------------+-----------+-----------+-------------+-------------+
421+
| Frequency [rad/s] | kxx [N/m] | kxy [N/m] | cxx [N*s/m] | cxy [N*s/m] |
422+
+-------------------+-----------+-----------+-------------+-------------+
423+
| 0.0 | 1000000.0 | 0.0 | 200.0 | 0.0 |
424+
| 50.0 | 1000000.0 | 0.0 | 200.0 | 0.0 |
425+
| 100.0 | 1000000.0 | 0.0 | 200.0 | 0.0 |
426+
| 150.0 | 1000000.0 | 0.0 | 200.0 | 0.0 |
427+
| 200.0 | 1000000.0 | 0.0 | 200.0 | 0.0 |
428+
+-------------------+-----------+-----------+-------------+-------------+
429+
"""
430+
if isinstance(coefficients, str):
431+
coefficients = [coefficients]
432+
elif coefficients is None:
433+
coefficients = self._get_coefficient_list(ignore_mass=True)
434+
435+
default_units = {"k": "N/m", "c": "N*s/m", "m": "kg"}
436+
y_units = {"k": stiffness_units, "c": damping_units, "m": mass_units}
437+
438+
if frequency is None:
439+
frequency = np.linspace(min(self.frequency), max(self.frequency), 5)
440+
frequency_range = Q_(frequency, "rad/s").to(frequency_units).m
441+
442+
headers = [f"Frequency [{frequency_units}]"]
443+
data = [frequency_range]
444+
445+
table = PrettyTable()
446+
447+
for coeff in coefficients:
448+
headers.append(f"{coeff} [{default_units[coeff[0]]}]")
449+
columns = (
450+
Q_(
451+
getattr(self, f"{coeff}_interpolated")(frequency),
452+
default_units[coeff[0]],
453+
)
454+
.to(y_units[coeff[0]])
455+
.m
456+
)
457+
data.append(columns)
458+
459+
table.field_names = headers
460+
for row in np.array(data).T:
461+
table.add_row(row.round(5))
462+
463+
return table
464+
360465
def __repr__(self):
361466
"""Return a string representation of a bearing element.
362467
@@ -412,11 +517,7 @@ def __eq__(self, other):
412517
self.__dict__.keys()
413518
)
414519

415-
coefficients = {
416-
attr.replace("_interpolated", "")
417-
for attr in self.__dict__.keys()
418-
if "_interpolated" in attr
419-
}
520+
coefficients = set(self._get_coefficient_list())
420521

421522
compared_attributes = list(coefficients.union(init_args))
422523
compared_attributes.sort()
@@ -449,11 +550,7 @@ def save(self, file):
449550
self.__dict__.keys()
450551
)
451552

452-
coefficients = {
453-
attr.replace("_interpolated", "")
454-
for attr in self.__dict__.keys()
455-
if "_interpolated" in attr
456-
}
553+
coefficients = set(self._get_coefficient_list())
457554

458555
args = list(coefficients.union(init_args))
459556
args.sort()

ross/results.py

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import copy
77
import inspect
88
from abc import ABC
9+
from prettytable import PrettyTable
910
from collections.abc import Iterable
1011
from warnings import warn
1112

@@ -1520,7 +1521,7 @@ class CriticalSpeedResults(Results):
15201521
_wn : array
15211522
Undamped critical speeds array.
15221523
_wd : array
1523-
Undamped critical speeds array.
1524+
Damped critical speeds array.
15241525
log_dec : array
15251526
Logarithmic decrement for each critical speed.
15261527
damping_ratio : array
@@ -1781,7 +1782,6 @@ def whirl_values(self):
17811782
def data_mode(
17821783
self,
17831784
mode=None,
1784-
length_units="m",
17851785
frequency_units="rad/s",
17861786
damping_parameter="log_dec",
17871787
):
@@ -1791,9 +1791,9 @@ def data_mode(
17911791
----------
17921792
mode : int
17931793
The n'th vibration mode
1794-
length_units : str, optional
1795-
length units.
1796-
Default is 'm'.
1794+
frequency_units : str, optional
1795+
Frequency units.
1796+
Default is "rad/s".
17971797
damping_parameter : str, optional
17981798
Define which value to show for damping. We can use "log_dec" or "damping_ratio".
17991799
Default is "log_dec".
@@ -1825,6 +1825,44 @@ def data_mode(
18251825

18261826
return df
18271827

1828+
def format_table(
1829+
self,
1830+
frequency_units="rad/s",
1831+
):
1832+
"""Return natural frequencies and damping ratios of modes in table format
1833+
1834+
Parameters
1835+
----------
1836+
frequency_units : str, optional
1837+
Frequency units.
1838+
Default is "rad/s".
1839+
1840+
Returns
1841+
-------
1842+
table : PrettyTable object
1843+
Table object with natural frequencies and damping ratios of modes.
1844+
"""
1845+
1846+
wn = Q_(self.wn, "rad/s").to(frequency_units).m
1847+
wd = Q_(self.wd, "rad/s").to(frequency_units).m
1848+
damping_ratio = self.damping_ratio
1849+
log_dec = self.log_dec
1850+
1851+
headers = [
1852+
"Mode",
1853+
f"Undamped natural frequencies [{frequency_units}]",
1854+
f"Damped natural frequencies [{frequency_units}]",
1855+
"Damping ratio",
1856+
"Logarithmic decrement",
1857+
]
1858+
1859+
table = PrettyTable()
1860+
table.field_names = headers
1861+
for row in zip(range(len(wn)), wn, wd, damping_ratio, log_dec):
1862+
table.add_row(row)
1863+
1864+
return table
1865+
18281866
def plot_mode_3d(
18291867
self,
18301868
mode=None,
@@ -1883,7 +1921,7 @@ def plot_mode_3d(
18831921
if fig is None:
18841922
fig = go.Figure()
18851923

1886-
df = self.data_mode(mode, length_units, frequency_units, damping_parameter)
1924+
df = self.data_mode(mode, frequency_units, damping_parameter)
18871925

18881926
damping_name = df["damping_name"].values[0]
18891927
damping_value = df["damping_value"].values[0]
@@ -1962,7 +2000,6 @@ def plot_mode_2d(
19622000
orientation="major",
19632001
frequency_type="wd",
19642002
title=None,
1965-
length_units="m",
19662003
frequency_units="rad/s",
19672004
damping_parameter="log_dec",
19682005
**kwargs,
@@ -1986,9 +2023,6 @@ def plot_mode_2d(
19862023
A brief title to the mode shape plot, it will be displayed above other
19872024
relevant data in the plot area. It does not modify the figure layout from
19882025
Plotly.
1989-
length_units : str, optional
1990-
length units.
1991-
Default is 'm'.
19922026
frequency_units : str, optional
19932027
Frequency units that will be used in the plot title.
19942028
Default is rad/s.
@@ -2006,7 +2040,7 @@ def plot_mode_2d(
20062040
The figure object with the plot.
20072041
"""
20082042

2009-
df = self.data_mode(mode, length_units, frequency_units, damping_parameter)
2043+
df = self.data_mode(mode, frequency_units, damping_parameter)
20102044

20112045
damping_name = df["damping_name"].values[0]
20122046
damping_value = df["damping_value"].values[0]
@@ -2059,9 +2093,6 @@ def plot_orbit(
20592093
mode=None,
20602094
nodes=None,
20612095
fig=None,
2062-
frequency_type="wd",
2063-
title=None,
2064-
frequency_units="rad/s",
20652096
**kwargs,
20662097
):
20672098
"""Plot (2D view) the mode shapes.
@@ -2075,17 +2106,6 @@ def plot_orbit(
20752106
Int or list of ints with the nodes selected to be plotted.
20762107
fig : Plotly graph_objects.Figure()
20772108
The figure object with the plot.
2078-
frequency_type : str, optional
2079-
"wd" calculates de map for the damped natural frequencies.
2080-
"wn" calculates de map for the undamped natural frequencies.
2081-
Defaults is "wd".
2082-
title : str, optional
2083-
A brief title to the mode shape plot, it will be displayed above other
2084-
relevant data in the plot area. It does not modify the figure layout from
2085-
Plotly.
2086-
frequency_units : str, optional
2087-
Frequency units that will be used in the plot title.
2088-
Default is rad/s.
20892109
kwargs : optional
20902110
Additional key word arguments can be passed to change the plot layout only
20912111
(e.g. width=1000, height=800, ...).
@@ -4470,7 +4490,6 @@ def plot_bending_moment(
44704490
def plot_deflected_shape(
44714491
self,
44724492
speed,
4473-
samples=101,
44744493
frequency_units="rad/s",
44754494
amplitude_units="m",
44764495
phase_units="rad",
@@ -4493,9 +4512,6 @@ def plot_deflected_shape(
44934512
speed : float, pint.Quantity
44944513
The rotor rotation speed. Must be an element from the speed_range argument
44954514
passed to the class (rad/s).
4496-
samples : int, optional
4497-
Number of samples to generate the orbit for each node.
4498-
Default is 101.
44994515
frequency_units : str, optional
45004516
Frequency units.
45014517
Default is "rad/s"

ross/rotor_assembly.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,12 +769,12 @@ def run_modal(self, speed, num_modes=12, sparse=True, synchronous=False):
769769
"""Run modal analysis.
770770
771771
Method to calculate eigenvalues and eigvectors for a given rotor system.
772-
Tthe natural frequencies and dampings ratios are calculated for a given
772+
The natural frequencies and dampings ratios are calculated for a given
773773
rotor speed. It means that for each speed input there's a different set of
774774
eigenvalues and eigenvectors, hence, different natural frequencies and damping
775775
ratios are returned.
776776
This method will return a ModalResults object which stores all data generated
777-
and also provides so methods for plotting.
777+
and also provides methods for plotting.
778778
779779
Available plotting methods:
780780
.plot_mode_2d()

0 commit comments

Comments
 (0)