ansys / pymapdl

Pythonic interface to MAPDL
https://mapdl.docs.pyansys.com
MIT License
421 stars 119 forks source link

Command needs to run twice to generate PNG image #1629

Closed germa89 closed 1 year ago

germa89 commented 1 year ago

Discussed in https://github.com/pyansys/pymapdl/discussions/1624

Originally posted by **MrMechanics** November 11, 2022 Hi I've used the LatheCutter.ipynb example and noticed that you can plot elements or lines with "vtk=False" (_mapdl.lplot(vtk=False)_ and _mapdl.eplot(vtk=False)_) in that notebook. This produces png images of the model as they would be displayed in ANSYS, and then shows these png-images directly in the notebook. But when I try to do this in other notebooks the png-files are created (although I usually have to run the command twice because the first png is just a 4 Bytes file that cannot be viewed), but don't show up in the notebook. I'm wondering if there is some setting I have to apply somewhere? The only thing I can think that I haven't tried must be some command hidden inside the LatheCutter.anf file. The reason why I want this to work is because I want to use the _mapdl.plsect('S','EQV',vtk=False)_ command, since I couldn't find any pyansys examples using PLSECT. Edit: So it does seem to work inside PREP7 (at least _mapdl.lplot(vtk=False)_ and _mapdl.eplot(vtk=False)_ do work), but not in POST1. When I run the _mapdl.plsect('S','EQV',vtk=False)_ command in POST1 I get this message: ------------------------------------------------------------------------------------------------- DISPLAY LINEARIZED STRESS USING NODES DEFINED BY LPATH COMMAND. ITEM=S COMP=EQV RHO= 0.0000 CUMULATIVE DISPLAY NUMBER 7 WRITTEN TO FILE file007.png - RASTER MODE. DISPLAY TITLE= crank_pin_with_contact ------------------------------------------------------------------------------------------------- The notebook does generate the png file though. Any ideas why displaying the image works in PREP7 but not POST1?
Jalmenara commented 1 year ago

I have faced a similar problem. I was about to post it in discussion #1624, but since I saw this opened issue I opted to do it here.

I am trying to generate a report using Pymapdl and Jupyter notebooks (with which I am not very familiar yet, I must admit) but I am having troubles when inserting the plot as static images in the exported HTML (on Windows)

My first issue is that I am not able to embed the VTK/Pyvista widgets as static into the HTML using the Jupyter-lab interface (the output appears white), but this is 99% sure not Pymapdl's problem, but jupyter's. I just mention it here in case somebody reads it and can give me some advice. In this attachment you can see the PDF output, up to step 4 of lathe_cutter.ipynb (generated by opening HTML in MS Edge and printing (Ctrl+P))

As a workaround to that, I have decided to switch to non-interactive widgets using the vtk=False option of ansys.mapdl.core.plotting.general_plotter, because embedding static matplotlib plots in HTML does work in my jupyter-lab.

My actual Pymapdl issue is that I am not able to apply even this workaround to a basic pymapdl notebook that I have created from scratch (below), whose output is this: 1_post-lab.pdf.

# %%
%matplotlib inline

# %% [markdown]
#   1. start ANSYS APDL session from python

# %%
from ansys.mapdl.core import launch_mapdl
import os
from os import path
import platform
import numpy as np
curdir = os.getcwd()

# license = "ansys" # Ansys Mechanical Enterprise
license = "mech_2" # Ansys Mechanical Premium
if 'Windows' in platform.system():
    execfile = "C:\\Program Files\\ANSYS Inc\\v212\\ansys\\bin\\winx64\\ANSYS212.exe"    
else:
    execfile = ""

mapdl = launch_mapdl(exec_file=execfile, run_location=curdir, license_type=license, override=True, additional_switches='-smp')

print(mapdl)

# %% [markdown]
#   2. Run the APDL input file and save the .db

# %%
mapdl.input("0_main.inp")

# %%
mapdl.prep7()
mapdl.allsel()

# mapdl.nplot(plot_bc=True)
#mapdl.show('png')
mapdl.nplot(vtk=False)
#mapdl.show('close')

# %%
mapdl.post1()

mapdl.plesol(item='F',comp='X',vtk=False, savefig='plesol.png')

# %%
mapdl.exit()

At first I had not written the initial %matplotlib inline% command, so I thought that that would be the issue; but it was not. I am invoking plotting commands at prep7 and post1 processors, and none of them works. I also tried the mapdl.show('png') + mapdl.show('close')pair, and also nothing changes (they are commented in the version I have posted).

Probably there is something basic which I am not using correctly, but I cannot find what it is. I do not think that it is a problem with my environment, because the lathe_cutter example works fine.

germa89 commented 1 year ago

Hi @Jalmenara

Can you send also the input file 0_main.inp??

Kind regards,

Jalmenara commented 1 year ago

Hi @germa89. Sure! I forgot ...

It is actually the same input I was using in issue #1650

See 0_main.inp ``` FINISH /CLEAR jobname = 'MAPDL_shell_forces_postprocess' /FILNAME, '%jobname%', 1 SAVE ! Plate dimensions LX = 2 LY = 0.5 thickness = 0.005 /PREP7 ! Create keypoints -- Plate corners K, ,0,LY/2,0 K, ,LX,LY/2,0 K, ,LX,-LY/2,0 K, ,0,-LY/2,0 ! Create an area A,1,2,3,4 ! Create ELEMENT TYPE, MATERIAL and SECTION properties to be assigned to ! the subsequently meshed elements ! Select the element type ET,1,SHELL181 ! Create material 1 MP, EX, 1,2e9 MP, DENS, 1,7.8e3 ! Create shell section SECTYPE,1,SHELL ! Assign the thickness that section SECDATA,thickness ! Select the number of divisions at the free edge freeEdgeNDivs = 5 LESIZE, 2, ,,freeEdgeNDivs LESIZE, 4, ,,freeEdgeNDivs LESIZE, 1, ,,LX/LY*freeEdgeNDivs LESIZE, 3, ,,LX/LY*freeEdgeNDivs ! Mesh the area TYPE, 1 MAT, 1 SECNUM, 1 MSHAPE, 0, 2D ! Force the usage of quadrilateral elements AMESH,1 ! Setup boundary conditions targetStress = 100 ! MPa = N/mm2 FT = targetStress*thickness*LY*1e6 ! Clamp the left edge NSEL,S,LOC,X,0 D,ALL,ALL ! Apply an axial force at the free edge (FT/all the nodes) NSEL,S,LOC,X,LX *GET, numnodes, NODE, 0, COUNT F, ALL, FX, FT/numnodes SAVE ! Solve the static analysis FINISH /SOLU ANTYPE,STATIC, NEW SOLVE FINISH SAVE ```
germa89 commented 1 year ago

Hi @Jalmenara

Apologies for the (very) late answer. I'm just cleaning issues.

When I run your code with v0.65.0 I get the following error:

MapdlCommandIgnoredError: DISPLAY ELEMENT SOLUTION,  ITEM=F     COMP=X                 

 *** WARNING ***                         CP =      64.971   TIME= 09:20:45
 Element forces are not available for display.  The PLESOL command is    
 ignored.

Ignore these messages by setting 'ignore_errors'=True

Probably this error wasn't caught by PyMAPDL in the version back then (because you do not mention it).

Please plot other boundary loads/results.