From 954bf97571376d82e2da2f8bbb0040ac2f6271c6 Mon Sep 17 00:00:00 2001 From: German Date: Tue, 6 Sep 2022 12:08:34 +0200 Subject: [PATCH 1/8] Fixing some formatting issues --- .../_commands/solution/load_step_options.py | 75 +++++++++++-------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/src/ansys/mapdl/core/_commands/solution/load_step_options.py b/src/ansys/mapdl/core/_commands/solution/load_step_options.py index 97a412125d3..d2726d800c4 100644 --- a/src/ansys/mapdl/core/_commands/solution/load_step_options.py +++ b/src/ansys/mapdl/core/_commands/solution/load_step_options.py @@ -9,12 +9,15 @@ def autots(self, key="", **kwargs): key Automatic time stepping key: - OFF - Do not use automatic time stepping. + OFF + Do not use automatic time stepping. - ON - Use automatic time stepping (default). + ON + Use automatic time stepping (default). - AUTO - The program determines whether to use automatic time stepping (used by - Workbench). + AUTO + The program determines whether to use automatic time stepping (used by + Workbench). Notes ----- @@ -22,10 +25,10 @@ def autots(self, key="", **kwargs): over this load step. If Key = ON, both time step prediction and time step bisection will be used. - You cannot use automatic time stepping [AUTOTS], line search [LNSRCH], + You cannot use automatic time stepping ``AUTOTS``, line search ``LNSRCH``, or the DOF solution predictor [PRED] with the arc-length method - [ARCLEN, ARCTRM]. If you activate the arc-length method after you set - AUTOTS, LNSRCH, or PRED, a warning message appears. If you choose to + ``ARCLEN, ARCTRM``. If you activate the arc-length method after you set + ``AUTOTS, LNSRCH``, or ``PRED``, a warning message appears. If you choose to proceed with the arc-length method, the program disables your automatic time stepping, line search, and DOF predictor settings, and the time step size is controlled by the arc-length method internally. @@ -53,9 +56,9 @@ def campbell(self, action="", **kwargs): Notes ----- - For an analysis involving a prestressed structure, the CAMPBELL command + For an analysis involving a prestressed structure, the ``CAMPBELL`` command specifies whether or not to prepare the result file to support a - Campbell diagram analysis (PRCAMP or PLCAMP). + Campbell diagram analysis (``PRCAMP`` or ``PLCAMP``). To prestress a structure, the program performs a static solution before the linear perturbation modal solution. @@ -465,7 +468,7 @@ def nsubst(self, nsbstp="", nsbmx="", nsbmn="", carry="", **kwargs): nsbstp Number of substeps to be used for this load step (i.e., the time step size or frequency increment). If automatic time stepping is - used (AUTOTS), NSBSTP defines the size of the first substep. + used (``AUTOTS``), ``NSBSTP`` defines the size of the first substep. nsbmx Maximum number of substeps to be taken (i.e., the minimum time step @@ -481,18 +484,20 @@ def nsubst(self, nsbstp="", nsbmx="", nsbmn="", carry="", **kwargs): Time step carryover key (program-determined default depending on the problem physics): - OFF - Use NSBSTP to define time step at start of each load step. + OFF + Use NSBSTP to define time step at start of each load step. - ON - Use final time step from previous load step as the starting time step (if - automatic time stepping is used). + ON + Use final time step from previous load step as the starting time step (if + automatic time stepping is used). Notes ----- - See DELTIM for an alternative input. It is recommended that all fields + See ``DELTIM`` for an alternative input. It is recommended that all fields of this command be specified for solution efficiency and robustness. - When the arc-length method is active (ARCLEN command), the NSBMX and - NSBMN arguments are ignored. + When the arc-length method is active (``ARCLEN`` command), the ``NSBMX`` and + ``NSBMN`` arguments are ignored. This command is also valid in PREP7. """ @@ -510,11 +515,13 @@ def numexp(self, num="", begrng="", endrng="", elcalc="", **kwargs): num The number of solutions to expand. This value is required. - Num - Number of solutions to expand. + Num + Number of solutions to expand. - ALL - Expand all substeps between BEGRNG and ENDRNG (provided that ENDRNG > 0). If - BEGRNG and ENDRNG have no specified values, this option - expands all substeps of all load steps. + ALL + Expand all substeps between ``BEGRNG`` and ``ENDRNG`` (provided that ENDRNG > 0). If + ``BEGRNG`` and ``ENDRNG`` have no specified values, this option + expands all substeps of all load steps. begrng, endrng Beginning and ending time (or frequency) range for expanded @@ -523,25 +530,27 @@ def numexp(self, num="", begrng="", endrng="", elcalc="", **kwargs): elcalc The element-calculation key: - YES - Calculate element results, nodal loads, and reaction loads. This value is the - default. + YES + Calculate element results, nodal loads, and reaction loads. This value is the + default. - NO - Do not calculate these items. + NO + Do not calculate these items. Notes ----- Specifies a range of solutions to be expanded from analyses that use - mode-superposition methods (ANTYPE,HARMIC or TRANS) or substructuring - (ANTYPE,SUBSTR). + mode-superposition methods (``ANTYPE,HARMIC`` or ``TRANS``) or substructuring + (``ANTYPE,SUBSTR``). For ANTYPE,TRANS, NUM, evenly spaced solutions are expanded between - time BEGRNG and time ENDRNG. + time BEGRNG and time ``ENDRNG``. For ANTYPE,HARMIC, NUM, evenly spaced solutions are expanded between - frequency BEGRNG and frequency ENDRNG. + frequency BEGRNG and frequency ``ENDRNG``. The first expansion in all cases is done at the first point beyond - BEGRNG (that is, at BEGRNG + (ENDRNG - BEGRNG) / NUM)). + BEGRNG (that is, at ``BEGRNG + (ENDRNG - BEGRNG) / NUM``)). The resulting results file will maintain the same load step, substep, and time (or frequency) values as the use pass. @@ -549,17 +558,17 @@ def numexp(self, num="", begrng="", endrng="", elcalc="", **kwargs): For a single expansion of a solution, or for multiple expansions when the solutions are not evenly spaced (such as in a mode-superposition harmonic analysis with the cluster option), ANSYS, Inc. recommends - issuing one or more EXPSOL commands. + issuing one or more ``EXPSOL`` commands. The NUMEXP command is invalid in these cases: - In a substructing analysis (ANTYPE,SUBST) when a factorized matrix file - (the .LN22 file generated by the sparse solver) does not exist, causing + In a substructing analysis (``ANTYPE,SUBST``) when a factorized matrix file + (the ``.LN22`` file generated by the sparse solver) does not exist, causing ANSYS to employ the full-resolve method. - If the full-resolve option is selected using the SEOPT command. + If the full-resolve option is selected using the ``SEOPT`` command. - In both situations, use the EXPSOL command to perform a single + In both situations, use the ``EXPSOL`` command to perform a single expansion for each solution desired. This command is also valid in PREP7. From 2c2a7d8eb57dfbfb12fad808ad601c42f9bbdab9 Mon Sep 17 00:00:00 2001 From: German Date: Tue, 6 Sep 2022 12:22:31 +0200 Subject: [PATCH 2/8] Adding ``frequency_values`` and unit tests --- src/ansys/mapdl/core/post.py | 24 +++++++++++++++++++++++- tests/test_post.py | 12 ++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/ansys/mapdl/core/post.py b/src/ansys/mapdl/core/post.py index 0ba0eccfbd1..72885357157 100644 --- a/src/ansys/mapdl/core/post.py +++ b/src/ansys/mapdl/core/post.py @@ -169,9 +169,31 @@ def time_values(self) -> np.ndarray: 75.20949687626468] """ self._mapdl.post1(mute=True) - list_rsp = self._mapdl.set("LIST") + with self._mapdl.run_as_routine("POST1"): + list_rsp = self._mapdl.set("LIST") return np.genfromtxt(list_rsp.splitlines(), skip_header=3)[:, 1] + @property + def frequency_values(self) -> np.ndarray: + """Return an array of the frequency values for all result sets. + + Returns + ------- + numpy.ndarray + Numpy array of the frequency values for all result sets. + + Examples + -------- + Get all the time values after loading POST1. + + >>> mapdl.post1() + >>> mapdl.post_processing.frequency_values + array([ 220., 240., 260., 280., 300., 320., 340., 360., 380., + 400., 420., 440.]) + """ + # Because in MAPLD is the same. + return self.time_values + def _reset_cache(self): """Reset local cache""" self._set_loaded = False diff --git a/tests/test_post.py b/tests/test_post.py index bfa2b1c3e1e..b5b04cf0f6e 100644 --- a/tests/test_post.py +++ b/tests/test_post.py @@ -793,6 +793,18 @@ def test_general_plotter_returns(mapdl, static_solve): ) +def test_time_frequency_values(mapdl, contact_solve): + assert np.allclose( + mapdl.post_processing.time_values, mapdl.post_processing.frequency_values + ) + + +def test_time_values(mapdl, contact_solve): + assert np.allclose( + mapdl.post_processing.time_values, np.array([0.2, 0.4, 0.7, 1.0]) + ) + + ############################################################################### # @pytest.mark.parametrize('comp', COMPONENT_STRESS_TYPE) # def test_nodal_thermal_component_strain(mapdl, thermal_solve, comp): From c2a1cc6effb895681bcd8468d4ca91317b4a06cc Mon Sep 17 00:00:00 2001 From: German Date: Tue, 6 Sep 2022 12:34:14 +0200 Subject: [PATCH 3/8] Adding example --- .../00-mapdl-examples/acoustic_analysis.py | 159 ++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 examples/00-mapdl-examples/acoustic_analysis.py diff --git a/examples/00-mapdl-examples/acoustic_analysis.py b/examples/00-mapdl-examples/acoustic_analysis.py new file mode 100644 index 00000000000..fe8a3c2162b --- /dev/null +++ b/examples/00-mapdl-examples/acoustic_analysis.py @@ -0,0 +1,159 @@ +""" +.. _ref_acoustic_analysis: + +==================== +3D Acoustic Analysis +==================== + +This example shows how to perform an acoustic analysis using PyMAPDL and ``FLUID`` elements. + + +""" + +############################################################################### +# Launching PyMAPDL +# ================= +# +# loading libraries. +from matplotlib import pyplot as plt + +from ansys.mapdl.core import launch_mapdl + +# Launching PyMAPDL +mapdl = launch_mapdl() + +mapdl.clear() +mapdl.prep7() +mapdl.units("SI") # SI - International system (m, kg, s, K). + +############################################################################### +# Element and Material Properties +# =============================== +# Define the ``FLUID30`` and ``FLUID130`` element types. + +mapdl.et(1, "FLUID30", kop1=2) +mapdl.et(2, "FLUID130", kop1=1) + + +# Define the material properties + +mapdl.mp("SONC", 1, 1500) # sonc in m/s +mapdl.mp("DENS", 1, 1000) # Density in kg/m3 +mapdl.mp("SONC", 2, 1500) # sonc in m/s +mapdl.mp("DENS", 2, 1000) # Density in kg/m3 +print(mapdl.mplist()) + + +# Define the real const +mapdl.r("1", "1e-6") # Reference pressure for R set 1 +mapdl.r("2", "1") # Reference pressure for R set 2 +print(mapdl.rlist()) + + +############################################################################### +# Geometry Definition +# =================== +# Create a simple sphere +# + +vnum = mapdl.sphere(rad1=0.5, rad2=1.0) +mapdl.vsbw("all") +mapdl.vplot(show_area_numbering=True) + +############################################################################### +# Geometry Meshing +# ================ +# First the material and elements are selected +# + +mapdl.type(itype=1) +mapdl.real(nset=1) +mapdl.mat(mat=1) +mapdl.mshape(1, "3D") + + +############################################################################### +# Then we choose the element size and perform the mesh. + +mapdl.esize(0.25) +mapdl.vmesh("all") +mapdl.eplot(show_node_numbering=False) + +############################################################################### +# Boundary Conditions +# =================== +# Add surface boundary condition to the nodes using :func:`Mapdl.sf() ` +# and the option ``SHLD`` for *Surface normal velocity or acceleration*. + +mapdl.csys(2) +mapdl.asel("s", "loc", "x", 0.5) +mapdl.csys(0) +mapdl.nsla("S", 1) +mapdl.sf("all", "SHLD", 0.05) +mapdl.allsel() + +############################################################################### +# Solve the model +# =============== +# Using :func:`Mapdl.solve() ` +# +mapdl.allsel() +mapdl.run("/SOLU") +mapdl.antype(3) +mapdl.harfrq(freqb=200, freqe=1000) +mapdl.autots("off") +mapdl.nsubst(40) +# mapdl.deltim(10) +mapdl.kbc(0) + +mapdl.outres("erase") # Save less data in order to reduce the size of .rst file +mapdl.outres("all", "none") # NOTE that other output like stresses is not saved +mapdl.outres("nsol", "all") # Save pressure and displacement +mapdl.outres("fgrad", "all") # Save velocities +mapdl.outres("misc", "all") # For post processing calculations + +mapdl.solve() + +############################################################################### +# Post1: Time step results +# ======================== +# +# Listing the results +mapdl.post1() +print(mapdl.set("LIST")) + +############################################################################### +# Post26: Time dependent results +# ============================== +# +# Getting results for specific nodes + +mapdl.post26() +freqs = mapdl.post_processing.time_values[::2] + +nodes = [276, 400] + +for node_ in nodes: + + # Getting results + node_pressure = mapdl.nsol(3, node_, "spl") + node_sound_pressure_level = mapdl.nsol(4, node_, "SPLA") + + # Plotting + fig, ax = plt.subplots(1, 2) + + ax[0].plot(freqs, node_pressure) + ax[0].set_xlabel("Frequencies (Hz)") + ax[0].set_ylabel("Sound pressure level (Pa)") + + ax[1].plot(freqs, node_sound_pressure_level, label="Nodal Sound Pressure") + ax[1].set_xlabel("Frequencies (Hz)") + ax[1].set_ylabel("A-weighted sound pressure level (dBA)") + + fig.suptitle(f"Node {node_} Results") + fig.show() + +############################################################################### +# Stop MAPDL +# +mapdl.exit() From 14f008bfa74bce6c90a9fb3257391193961796ef Mon Sep 17 00:00:00 2001 From: German Date: Tue, 6 Sep 2022 12:36:01 +0200 Subject: [PATCH 4/8] Removing call to post1 --- src/ansys/mapdl/core/post.py | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ansys/mapdl/core/post.py b/src/ansys/mapdl/core/post.py index 72885357157..37c62e90740 100644 --- a/src/ansys/mapdl/core/post.py +++ b/src/ansys/mapdl/core/post.py @@ -168,7 +168,6 @@ def time_values(self) -> np.ndarray: 75.03939292229019, 75.20949687626468] """ - self._mapdl.post1(mute=True) with self._mapdl.run_as_routine("POST1"): list_rsp = self._mapdl.set("LIST") return np.genfromtxt(list_rsp.splitlines(), skip_header=3)[:, 1] From 1be27e911d5b9ab0598bdd24fe5387b8db7f52b2 Mon Sep 17 00:00:00 2001 From: German Date: Tue, 6 Sep 2022 13:03:21 +0200 Subject: [PATCH 5/8] Getting only values for one node. --- .../00-mapdl-examples/acoustic_analysis.py | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/examples/00-mapdl-examples/acoustic_analysis.py b/examples/00-mapdl-examples/acoustic_analysis.py index fe8a3c2162b..2d83ded3c07 100644 --- a/examples/00-mapdl-examples/acoustic_analysis.py +++ b/examples/00-mapdl-examples/acoustic_analysis.py @@ -130,28 +130,26 @@ mapdl.post26() freqs = mapdl.post_processing.time_values[::2] +node_ = 276 -nodes = [276, 400] +# Getting results +node_pressure = mapdl.nsol(3, node_, "spl") +node_sound_pressure_level = mapdl.nsol(4, node_, "SPLA") -for node_ in nodes: +# Plotting +fig, ax = plt.subplots(1, 2) - # Getting results - node_pressure = mapdl.nsol(3, node_, "spl") - node_sound_pressure_level = mapdl.nsol(4, node_, "SPLA") +ax[0].plot(freqs, node_pressure) +ax[0].set_xlabel("Frequencies (Hz)") +ax[0].set_ylabel("Sound pressure level (Pa)") - # Plotting - fig, ax = plt.subplots(1, 2) +ax[1].plot(freqs, node_sound_pressure_level, label="Nodal Sound Pressure") +ax[1].set_xlabel("Frequencies (Hz)") +ax[1].set_ylabel("A-weighted sound\npressure level (dBA)") - ax[0].plot(freqs, node_pressure) - ax[0].set_xlabel("Frequencies (Hz)") - ax[0].set_ylabel("Sound pressure level (Pa)") - - ax[1].plot(freqs, node_sound_pressure_level, label="Nodal Sound Pressure") - ax[1].set_xlabel("Frequencies (Hz)") - ax[1].set_ylabel("A-weighted sound pressure level (dBA)") - - fig.suptitle(f"Node {node_} Results") - fig.show() +fig.suptitle(f"Node {node_} Results") +fig.tight_layout() +fig.show() ############################################################################### # Stop MAPDL From 0018c68025d9a89e3b757a56a7705b2fc187c220 Mon Sep 17 00:00:00 2001 From: German Date: Tue, 6 Sep 2022 13:10:54 +0200 Subject: [PATCH 6/8] Removing plot pos1 --- examples/00-mapdl-examples/acoustic_analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/00-mapdl-examples/acoustic_analysis.py b/examples/00-mapdl-examples/acoustic_analysis.py index 2d83ded3c07..e45f67cbb22 100644 --- a/examples/00-mapdl-examples/acoustic_analysis.py +++ b/examples/00-mapdl-examples/acoustic_analysis.py @@ -89,7 +89,7 @@ mapdl.asel("s", "loc", "x", 0.5) mapdl.csys(0) mapdl.nsla("S", 1) -mapdl.sf("all", "SHLD", 0.05) +mapdl.sf("all", "SHLD", 5) mapdl.allsel() ############################################################################### From aff0c2f16c0537063c0be0f8baf6b7e15cdfe913 Mon Sep 17 00:00:00 2001 From: German Date: Wed, 7 Sep 2022 10:57:42 +0200 Subject: [PATCH 7/8] Removing second material and element --- examples/00-mapdl-examples/acoustic_analysis.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/examples/00-mapdl-examples/acoustic_analysis.py b/examples/00-mapdl-examples/acoustic_analysis.py index e45f67cbb22..168be5fd250 100644 --- a/examples/00-mapdl-examples/acoustic_analysis.py +++ b/examples/00-mapdl-examples/acoustic_analysis.py @@ -32,21 +32,17 @@ # Define the ``FLUID30`` and ``FLUID130`` element types. mapdl.et(1, "FLUID30", kop1=2) -mapdl.et(2, "FLUID130", kop1=1) # Define the material properties mapdl.mp("SONC", 1, 1500) # sonc in m/s mapdl.mp("DENS", 1, 1000) # Density in kg/m3 -mapdl.mp("SONC", 2, 1500) # sonc in m/s -mapdl.mp("DENS", 2, 1000) # Density in kg/m3 print(mapdl.mplist()) # Define the real const mapdl.r("1", "1e-6") # Reference pressure for R set 1 -mapdl.r("2", "1") # Reference pressure for R set 2 print(mapdl.rlist()) @@ -103,7 +99,6 @@ mapdl.harfrq(freqb=200, freqe=1000) mapdl.autots("off") mapdl.nsubst(40) -# mapdl.deltim(10) mapdl.kbc(0) mapdl.outres("erase") # Save less data in order to reduce the size of .rst file From dfbcc6a06ee5427d3950257d1a568891c6905899 Mon Sep 17 00:00:00 2001 From: Alex Kaszynski Date: Wed, 7 Sep 2022 13:30:12 -0600 Subject: [PATCH 8/8] Apply suggestions from code review --- .../00-mapdl-examples/acoustic_analysis.py | 27 ++++++++----------- src/ansys/mapdl/core/post.py | 2 +- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/examples/00-mapdl-examples/acoustic_analysis.py b/examples/00-mapdl-examples/acoustic_analysis.py index 168be5fd250..9f265ba7cce 100644 --- a/examples/00-mapdl-examples/acoustic_analysis.py +++ b/examples/00-mapdl-examples/acoustic_analysis.py @@ -1,5 +1,5 @@ """ -.. _ref_acoustic_analysis: +.. _acoustic_analysis_example: ==================== 3D Acoustic Analysis @@ -11,15 +11,13 @@ """ ############################################################################### -# Launching PyMAPDL -# ================= -# -# loading libraries. +# Launch PyMAPDL +# ============== +# Launch PyMAPDL and load ``matplotlib``. from matplotlib import pyplot as plt from ansys.mapdl.core import launch_mapdl -# Launching PyMAPDL mapdl = launch_mapdl() mapdl.clear() @@ -35,7 +33,6 @@ # Define the material properties - mapdl.mp("SONC", 1, 1500) # sonc in m/s mapdl.mp("DENS", 1, 1000) # Density in kg/m3 print(mapdl.mplist()) @@ -49,8 +46,7 @@ ############################################################################### # Geometry Definition # =================== -# Create a simple sphere -# +# Create a simple sphere. vnum = mapdl.sphere(rad1=0.5, rad2=1.0) mapdl.vsbw("all") @@ -59,7 +55,7 @@ ############################################################################### # Geometry Meshing # ================ -# First the material and elements are selected +# First select the material and elements. # mapdl.type(itype=1) @@ -67,9 +63,8 @@ mapdl.mat(mat=1) mapdl.mshape(1, "3D") - ############################################################################### -# Then we choose the element size and perform the mesh. +# Then choose the element size and perform the mesh. mapdl.esize(0.25) mapdl.vmesh("all") @@ -125,11 +120,11 @@ mapdl.post26() freqs = mapdl.post_processing.time_values[::2] -node_ = 276 +node = 276 # Getting results -node_pressure = mapdl.nsol(3, node_, "spl") -node_sound_pressure_level = mapdl.nsol(4, node_, "SPLA") +node_pressure = mapdl.nsol(3, node, "spl") +node_sound_pressure_level = mapdl.nsol(4, node, "SPLA") # Plotting fig, ax = plt.subplots(1, 2) @@ -142,7 +137,7 @@ ax[1].set_xlabel("Frequencies (Hz)") ax[1].set_ylabel("A-weighted sound\npressure level (dBA)") -fig.suptitle(f"Node {node_} Results") +fig.suptitle(f"Node {node} Results") fig.tight_layout() fig.show() diff --git a/src/ansys/mapdl/core/post.py b/src/ansys/mapdl/core/post.py index 37c62e90740..db7af730853 100644 --- a/src/ansys/mapdl/core/post.py +++ b/src/ansys/mapdl/core/post.py @@ -190,7 +190,7 @@ def frequency_values(self) -> np.ndarray: array([ 220., 240., 260., 280., 300., 320., 340., 360., 380., 400., 420., 440.]) """ - # Because in MAPLD is the same. + # Because in MAPDL is the same. return self.time_values def _reset_cache(self):