ansys / pymapdl

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

Display PSD input curve #906

Open pmaroneh opened 2 years ago

pmaroneh commented 2 years ago

I'm creating a PSD analysis with the following:

mapdl.slashsolu()
mapdl.antype('spectr')
mapdl.spopt('psd') # power spectral density
mapdl.psdunit(1,'accg',9.81*1000) # Use input table 1 with acceleration spectrum in terms of acceleration due to gravity
mapdl.psdfrq(1,' ',1,40,50,70.71678,100,700,900) # Define the frequency points in the input table 1
mapdl.psdval(1,.01,.01,0.1,1,10,10,1) # Define the PSD values in the input table 1

I'd like to plot the PSD curve, with mapdl.psdgraph(1); but this does not create a plot anywhere. The output returns: image And indeed file012.png is generated in my working directory, but it cannot be opened.

mikerife commented 2 years ago

Hi @pmaroneh Out of curiosity is file012.png size exactly 1Kb? If so try the following:

mapdl.slashsolu()
mapdl.antype('spectr')
mapdl.spopt('psd') # power spectral density
mapdl.psdunit(1,'accg',9.81*1000) # Use input table 1 with acceleration spectrum in terms of acceleration due to gravity
mapdl.psdfrq(1,' ',1,40,50,70.71678,100,700,900) # Define the frequency points in the input table 1
mapdl.psdval(1,.01,.01,0.1,1,10,10,1) # Define the PSD values in the input table 1

mapdl.run('/show,png')
mapdl.psdgraph(1)
mapdl.run('/show')
mapdl.run('/rep')

What happens? mike

pmaroneh commented 2 years ago

Hi @mikerife . Indeed if I export the image to a output file through /show,png ... /show,close image then it is exported fine and can be opened. However it would be really useful to be able to plot the graph in the IDE directly. Not sure if this could be considered as an enhancement request (@akaszynski or @germa89 ?) I'm currently trying to find a way to store the results returned by prvar into two python arrays so as to be able to plot the graph in my IDE with the use of matplotlib. (Edit: Just realized that I replied with the example of plotting the response spectrum and not the input spectrum, but the issue was the same with both graphs)

mikerife commented 2 years ago

Hi @pmaroneh & @germa89 & @akaszynski P - if you did not get around to storing the PSD values and plotting them here is a solution I came up with. Hope it helps. Mike

def read_psd_file(file_name):
    read_file = open(file_name, 'r')
    psd = []
    val = []
    while True:
        line = read_file.readline()
        line = line.rstrip()
        line = line.rstrip('\n')
        line_text = line.split(',')

        if line_text[0] == 'PSDFRQ':
            for item in line_text[3:]:
                if item != '':
                    #print(item)
                    psd.append(item.strip())

        if line_text[0] == 'PSDVAL':
            for item in line_text[2:]:
                if item != '':
                    #print(item)
                    val.append(item.strip())
        if not line:
            break
    read_file.close()
    return psd, val

mapdl.prep7()
mapdl.cdwrite('load','psd','cdb')
psd1, val1 = read_psd_file('psd.cdb')
print(psd1, val1)

import matplotlib.pyplot as plt

fig, ax = plt.subplots()

ax.plot(psd1, val1, linewidth=2.0)
ax.set(xlim=(0, 5), xticks=range(0, 5),
       ylim=(0, 3), yticks=range(0, 3))

plt.show()

I'm not showing my test model but from the cdb the psd curve data: InputPSD

And the resulting matplotlib plot: PSDPlot

I'll have to work on getting the axes just right. But the plot is not too terrible. Mike

pmaroneh commented 1 year ago

Hi @mikerife , that's a neat workaround, thanks! Hope we have a native solution some day though.