How to create beam bending moment (BMD) and shear force diagram (SFD) using PyMAPDL and PyDPF

Member, Employee Posts: 222
100 Comments 100 Likes Second Anniversary Name Dropper
✭✭✭✭
edited February 2024 in Structures

shear force and bending moment diagram example below uses BEAM188 elements .

example uses pyMAPDL to run the simulation
Pydpf to extract result
matplotlib to plot the SF and BM diagrams

You can alter the beam SECTYPE,SECDATA, fix_location and force location as input arguments of the function

Tagged:

Welcome!

It looks like you're new here. Sign in or register to get started.

Comments

  • Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited February 2024

    Inputs and import statements

    1. #Import statements
    2. from ansys.mapdl.core import launch_mapdl
    3. import numpy as np
    4. import matplotlib.pyplot as plt
    5. from matplotlib.cm import ScalarMappable
    6. from matplotlib.colors import Normalize
    7. from matplotlib import ticker
    8. from ansys.dpf import core as dpf
    9. import os
    10. run_location=r"D:\apdl\test\4"
    11. nproc=4
    12. override=True
    13. mapdl = launch_mapdl(run_location=run_location, nproc=nproc, override=override)

    Creation of model using pyMAPDL

    1. def create_model_setup(SECDATA,
    2. BEAM_LENGTH,
    3. LINE_DIV,
    4. LENGTH_DIV,
    5. YM,
    6. MU,
    7. SECTYPE,
    8. SECSUBTYPE):
    9. mapdl.clear("All")
    10. mapdl.prep7()
    11. mapdl.et('', 'BEAM188')
    12. mapdl.mp(lab='EX', mat='1', c0='%s'%(YM))
    13. mapdl.mp(lab='PRXY', mat='1', c0='%s'%(MU))
    14. mapdl.sectype(secid='1', type_='BEAM', subtype='%s'%(SECTYPE))
    15. mapdl.secdata(tuple(SECDATA))
    16. mapdl.seccontrol(1E15,1E15)
    17. mapdl.k(npt='1', x='0', y='0', z='0')
    18. mapdl.k(npt='2', x='%s'%(BEAM_LENGTH), y='0', z='0')
    19. mapdl.l(p1='1', p2='2', ndiv='%s'%(LINE_DIV))
    20. mapdl.lesize(nl1='ALL', size='', angsiz='', ndiv='%s'%(LENGTH_DIV))
    21. mapdl.lmesh(nl1='ALL')

    Adding Loads and Bc and Solving them

    1. def run_simulation(fix_loc,
    2. pin_loc,
    3. concentrated_loc,
    4. concentrated_force,
    5. distributed_loc,
    6. distributed_force,
    7. elem_plot_flag):
    8. mapdl.solution()
    9. mapdl.ddele(node='ALL', lab='ALL')
    10. mapdl.fdele(node='ALL', lab='ALL')
    11. mapdl.sfedele(elem='ALL', lkey='ALL', lab='ALL')
    12. for fix_lo in fix_loc:
    13. fix_node = mapdl.queries.node(fix_lo[0], fix_lo[1], fix_lo[2])
    14. mapdl.d(node='%s'%(fix_node), lab='ALL', value='0')
    15. for pin_lo in pin_loc:
    16. pin_node = mapdl.queries.node(pin_lo[0], pin_lo[1], pin_lo[2])
    17. mapdl.d(node='%s'%(pin_node), lab='UX', value='0')
    18. mapdl.d(node='%s'%(pin_node), lab='UY', value='0')
    19. mapdl.d(node='%s'%(pin_node), lab='UZ', value='0')
    20. mapdl.d(node='%s'%(pin_node), lab='ROTY', value='0')
    21. mapdl.d(node='%s'%(pin_node), lab='ROTZ', value='0')
    22. for count, force_lo in enumerate(concentrated_loc):
    23. force_node = mapdl.queries.node(force_lo[0], force_lo[1], force_lo[2])
    24. mapdl.f(node='%s'%(force_node), lab='FY', value=concentrated_force[count])
    25. for count, force_lo in enumerate(distributed_loc):
    26. mapdl.esel(type_='S', item='CENT', comp='X', vmin=force_lo[0], vmax=force_lo[1])
    27. mapdl.sfbeam(elem='ALL', lkey='2', lab='PRES', vali=distributed_force[count])
    28. mapdl.allsel(labt='ALL', entity='ALL')
    29. mapdl.allsel(labt='ALL', entity='ALL')
    30. mapdl.outres(item='ALL', freq='ALL')
    31. if elem_plot_flag == True:
    32. mapdl.eplot(plot_bc=True, bc_labels=["UX", "UY", "UZ", "FX", "FY", "FZ"], plot_bc_labels=True)
    33. mapdl.solve()
    34. mapdl.save(fname='test', ext='db')

    Extracting shear force and bending moments using DPF and plot using matplotlib

    1. def plot_bending_moment_shear_force(rstpath,
    2. smiscI,
    3. smiscJ,
    4. plotname):
    5. # data source
    6. my_data_sources = dpf.DataSources(rstpath)
    7. # model
    8. my_model = dpf.Model(rstpath)
    9. # mesh
    10. my_mesh = my_model.metadata.meshed_region
    11. # Get result operator
    12. result_operator = dpf.operators.result.__getattribute__("smisc")
    13. result_field_container = result_operator(
    14. time_scoping=[1], data_sources=my_data_sources, mesh=my_mesh, item_index=smiscI
    15. ).outputs.fields_container.get_data()
    16. # First Field usually field corresponding to first time scoping
    17. resultfield = result_field_container[0]
    18. element_ids = resultfield.scoping.ids
    19. i_nodes_coordinates = [my_mesh.elements.element_by_id(
    20. x).nodes[1].coordinates[0] for x in element_ids]
    21. BMY_T_I = resultfield.data_as_list
    22. result_field_container = result_operator(
    23. time_scoping=[1], data_sources=my_data_sources, mesh=my_mesh, item_index=smiscJ
    24. ).outputs.fields_container.get_data()
    25. # First Field usually field corresponding to first time scoping
    26. resultfield = result_field_container[0]
    27. element_ids = resultfield.scoping.ids
    28. j_nodes_coordinates = [my_mesh.elements.element_by_id(
    29. x).nodes[1].coordinates[0] for x in element_ids]
    30. BMY_T_J = resultfield.data_as_list
    31. # Assuming i_nodes_coordinates, j_nodes_coordinates, BMY_T_I, BMY_T_J are defined
    32. x = np.concatenate((i_nodes_coordinates, j_nodes_coordinates))
    33. y = np.concatenate((BMY_T_I, BMY_T_J))
    34. # Create a colormap
    35. cmap = plt.get_cmap('viridis')
    36. norm = Normalize(vmin=np.min(y), vmax=np.max(y))
    37. # Create a ScalarMappable
    38. sm = ScalarMappable(cmap=cmap, norm=norm)
    39. sm.set_array([])
    40. # Create the bar chart
    41. fig, ax = plt.subplots(figsize=(15, 5)) # Adjust the figure size
    42. ax.bar(x, y, color=cmap(norm(y)))
    43. ax.set_xlabel('X_distance')
    44. ax.set_title(plotname+' Diagram')
    45. # Set y-axis limits
    46. ax.set_ylim([np.min(y), np.max(y)])
    47. # Create an array of 10 evenly spaced values between min(y) and max(y)
    48. ticks = np.linspace(np.min(y), np.max(y), 10)
    49. # Add a colorbar with specific ticks
    50. cbar = fig.colorbar(sm, ax=ax, ticks=ticks)
    51. cbar.set_label(plotname)
    52. # Format the colorbar tick labels to have two decimal places
    53. cbar.formatter = ticker.FuncFormatter(lambda x, pos: '{:.4f}'.format(x))
    54. cbar.update_ticks()
    55. plt.show()

    Function calls Below

    1. ### Call the defined functions####
    2. # create model
    3. create_model_setup(
    4. SECDATA=[5, 8, 2], # SECDATA = [BEAM_RADIUS,CIRCUM_DIV,RADIAL_DIV]
    5. BEAM_LENGTH=100,
    6. LINE_DIV=50,
    7. LENGTH_DIV=1,
    8. YM=2E8,
    9. MU=0.3,
    10. SECTYPE="CSOLID",
    11. SECSUBTYPE="CIRBEAM",
    12. )
    13. #Run simulation
    14. run_simulation(
    15. fix_loc=[[10, 0, 0], [90, 0, 0]],
    16. pin_loc=[[50, 0, 0]],
    17. concentrated_loc = [[30, 0, 0], [80, 0, 0],[100, 0, 0]],
    18. concentrated_force = [1000, 500,500],
    19. distributed_loc=[[0,100]],
    20. distributed_force = [1000],
    21. elem_plot_flag = True
    22. )
    23. # Bending Moment diagram
    24. plot_bending_moment_shear_force(
    25. rstpath=os.path.join(run_location, "file.rst"),
    26. smiscI=32,
    27. smiscJ=37,
    28. plotname="Bending Moment"
    29. )
    30. # Shear Force diagram
    31. plot_bending_moment_shear_force(
    32. rstpath=r"D:\apdl\test\4\file.rst",
    33. smiscI=6,
    34. smiscJ=19,
    35. plotname="Shear Force"
    36. )
    37. mapdl.exit()
  • Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited February 2024


  • Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited February 2024
  • Member Posts: 2
    Second Anniversary First Comment
    **

    Hi,
    i am getting error while running above code.

    The operator SMISC doesn't exist in the registry. Check its spelling in the documentation or verify its availability in your loaded plugins. The current available operator names can be accessed using 'available_operator_names' method."
    how to rectify it?
    thanks

  • Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited May 2024

    @deepak
    In recent releases its
    dpf.operators.result.smisc
    in earlier releases its
    dpf.operators.result.mapdl.smisc

    Please check these

Welcome!

It looks like you're new here. Sign in or register to get started.