DEV Community

ササザワヨウスケ
ササザワヨウスケ

Posted on

calculate the cross-sectional force of frame structure using python and API

I tried to shoot with a python staged in a textbook of structural mechanics

For those who find structural mechanics difficult for some people, and who enjoy studying python but are not good at structural mechanics.
I would like to solve a structural mechanics problem using python.

The example to be taken up this time is

Draw the Q and M figures of the overhang shown in Figure 4.20.

image

I would like to solve it quickly with python and api.

Click here for all source code
https://colab.research.google.com/drive/11cMEEUSFgu3hVw0vLOo1PZhvwSiM-5Ys?usp=sharing

Commentary

In an environment called Google colaboratory that can be used without installing python provided free of charge by Google
Solve the problem.

Calculation flow

  1. Generate input data
  2. Let the API calculate the section force
  3. Display the calculation result

1. Generate input data

Input data is recorded in this variable body.

body = dict()
Enter fullscreen mode Exit fullscreen mode

Define a "node"

Define the coordinates and numbers of members and intersections of members in a skeleton structure such as a truss

image

n1 = {"x":0, "y":0, "z":0}
n2 = {"x":2, "y":0, "z":0}
n3 = {"x":6, "y":0, "z":0}

body["node"] = {
    "1": n1,
    "2": n2,
    "3": n3 
    }
Enter fullscreen mode Exit fullscreen mode

Define a "member"

Define member numbers and the following information in the skeleton structure

  • ni: The leftmost point of the member
  • nj: Rightmost point of the member
  • e: Material number described later

image

body["member"] = {
    "1": {"ni":"1", "nj":"2", "e":"1"},
    "2": {"ni":"2", "nj":"3", "e":"1"}
        }
Enter fullscreen mode Exit fullscreen mode

Define "element"

Define "element" means material

number and information below
In this problem, the material is uniform and there is no particular specification, so I entered an appropriate value.

  • E: Elastic modulus
  • G: Shear modulus
  • Xp: Coefficient of expansion
  • A: Cross-sectional area
  • J: Torsion constant
  • Iy: Moment of inertia of area around the y-axis
  • Iz: Moment of inertia of area around the z-axis
body["element"] = {
    "1":{
       "1":{ "E":20000000, "G":770000, "Xp":0.00001, "A":1.0, "J":1.0, "Iy":1.0, "Iz":1.0 }
      }
    }
Enter fullscreen mode Exit fullscreen mode

Define fulcrum information

  • n: Fixed rating number
  • tx: 1 if the displacement in the x direction is fixed, 0 if not fixed
  • ty: 1, if the displacement in the y direction is fixed, 0 if not fixed
  • tz: 1 if the displacement in the z direction is fixed, 0 if not fixed
  • rx: 1 to fix rotation around x-axis, 0 otherwise
  • ry: 1, if the rotation around the y-axis is fixed, 0 if not fixed
  • rz: 1, if the rotation around the z-axis is fixed, 0 if not fixed

image

body["fix_node"] = { 
    "1":[
       {"n":"2","tx":1,"ty":1,"tz":1,"rx":0,"ry":0,"rz":0},
       {"n":"3","tx":0,"ty":1,"tz":1,"rx":0,"ry":0,"rz":0}
      ]
    }
Enter fullscreen mode Exit fullscreen mode

Define load information

  • fix_node: fulcrum information number
  • element: Material information number
  • load_node
    • n: Rating number to load the load
    • tx: Load acting in the x direction
    • ty: Load acting in the y direction
    • tz: Load acting in the z direction
    • rx: moment acting around the x-axis
    • ry: Moment acting around the y-axis
    • rz: moment acting around the z axis

image

body["load"] = {
      "1":{
         "fix_node": "1",
         "element": "1",
         "load_node":[
                      { "n":"1","tx":0,"ty":0,"tz":2,"rx":0,"ry":0,"rz":0 }
                      ]
        }
    }
Enter fullscreen mode Exit fullscreen mode

2. Let the API calculate the section force

The calculation of the section force uses an API called StructuralEngine.
If you POST the above data here, it will calculate the section force.

response = requests.post(
    'https://asia-northeast1-the-structural-engine.cloudfunctions.net/frameWeb-2',
     json.dumps(body),
     headers={'Content-Type': 'application/json'})
result = response.json()   
print(result)   
Enter fullscreen mode Exit fullscreen mode
{'1': {'disg': {'1': {'dx': 0.0, 'dy': 0.0, 'dz': 8e-07, 'rx': 0.0, 'ry': 4.666666666666666e-07, 'rz': 0.0}, '2': {'dx': 0.0, 'dy': 0.0, 'dz': 0.0, 'rx': 0.0, 'ry': 2.6666666666666667e-07, 'rz': 0.0}, '3': {'dx': 0.0, 'dy': 0.0, 'dz': 0.0, 'rx': 0.0, 'ry': -1.333333333333333e-07, 'rz': 0.0}}, 'reac': {'2': {'tx': 0.0, 'ty': 0.0, 'tz': -1.0000000000000016, 'mx': 0.0, 'my': 0.0, 'mz': 0.0}, '3': {'tx': 0.0, 'ty': 0.0, 'tz': 1.0000000000000002, 'mx': 0.0, 'my': 0.0, 'mz': 0.0}}, 'fsec': {'1': {'P1': {'fxi': -0.0, 'fyi': -0.0, 'fzi': -2.0000000000000018, 'mxi': 0.0, 'myi': -3.552713678800501e-15, 'mzi': 0.0, 'fxj': 0.0, 'fyj': 0.0, 'fzj': -2.0000000000000018, 'mxj': -0.0, 'myj': 4.000000000000002, 'mzj': -0.0, 'L': 2.0}}, '2': {'P1': {'fxi': -0.0, 'fyi': -0.0, 'fzi': 1.0000000000000002, 'mxi': 0.0, 'myi': 4.0, 'mzi': 0.0, 'fxj': 0.0, 'fyj': 0.0, 'fzj': 1.0000000000000002, 'mxj': -0.0, 'myj': -4.440892098500626e-16, 'mzj': -0.0, 'L': 4.0}}}}}
Enter fullscreen mode Exit fullscreen mode

3. Display the calculation result

Draw Q and M diagrams from the analysis results obtained by the API.

Extract information on section force

Since the information is stored in the following hierarchy, it is taken out and drawn.

  • "1": Case number
    • "1", "2": Part number
    • fsec: Cross-sectional force
      • P1: Points of interest
      • fzi: Shear force on the left side in the z-axis direction
      • fzj: Shear force on the right side in the z-axis direction
      • myi: Bending moment on the left side around the y-axis
      • myj: Bending moment on the right side around the y-axis
# Take out the information of Case 1
case1 = result['1']

# Extract section force information
fsec = case1['fsec']

# Take out the information of member number 1
member1 = fsec['1']['P1']

# Take out the information of member number 2
member2 = fsec['2']['P1']
Enter fullscreen mode Exit fullscreen mode

Draw Q and M diagrams

# X-coordinate of member
x = [n1 ['x'], n2 ['x'], n2 ['x'], n3 ['x']]
# Y coordinate of member
y = [n1 ['y'], n2 ['y'], n2 ['y'], n3 ['y']]
# Shear force
yQ = [member1 ['fzi'], member1 ['fzj'], member2 ['fzi'], member2 ['fzj']]
# Bending moment
yM = [member1 ['myi'], member1 ['myj'], member2 ['myi'], member2 ['myj']]
Enter fullscreen mode Exit fullscreen mode

Draw a diagram from the above information.

fig = plt.figure()

ax1 = fig.add_subplot(1, 2, 1)
ax1.set_ylim([2, -3])
ax1.set_xlim([-1, 7])
ax1.plot(x, y, marker ='o', color = "darkblue")
ax1.plot(x, yQ, color = "lightblue")
ax1.fill_between( x, yQ, color="lightblue", alpha=0.2)
ax1.set_title("Q")


ax2 = fig.add_subplot(1, 2, 2)
ax2.set_ylim([-1, 5])
ax2.set_xlim([-1, 7])
ax2.plot(x, y, marker ='o', color = "darkblue")
ax2.plot(x, yM, color = "lightblue")
ax2.fill_between( x, yM, color="lightblue", alpha=0.2)
ax2.set_title("M")
Enter fullscreen mode Exit fullscreen mode

image

The answers matched

Summary

I tried to solve the problem described in the textbook of structural mechanics with python

Even if you are not good at structural mechanics, if you have python, you can proceed without understanding
I hope you will deepen your understanding of structural mechanics while playing around.

Top comments (0)