calculate the cross-sectional force of frame structure using python and API
Tags: python, api, structure, civilengineering
Published: 2021-02-14 03:10:47

# 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](https://dev-to-uploads.s3.amazonaws.com/i/zlr033dloifq1jjtkh40.png)

 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.

```python
body = dict()
```

### Define a "node"

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

![image](https://dev-to-uploads.s3.amazonaws.com/i/mfwbyh6g8valmtk0db8a.png)
 
```python
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 
    }
```

### 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](https://dev-to-uploads.s3.amazonaws.com/i/bh0rmcwxskq2t2ijx0l2.png)
 
```python
body["member"] = {
    "1": {"ni":"1", "nj":"2", "e":"1"},
    "2": {"ni":"2", "nj":"3", "e":"1"}
        }
```

### 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

```python
body["element"] = {
    "1":{
       "1":{ "E":20000000, "G":770000, "Xp":0.00001, "A":1.0, "J":1.0, "Iy":1.0, "Iz":1.0 }
      }
    }
```

### 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](https://dev-to-uploads.s3.amazonaws.com/i/63cbnso4if7mv2eracs7.png)


```python
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}
      ]
    }
``` 

### 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](https://dev-to-uploads.s3.amazonaws.com/i/sm7z4u2npqn42csjbq7l.png)


```python
body["load"] = {
      "1":{
         "fix_node": "1",
         "element": "1",
         "load_node":[
                      { "n":"1","tx":0,"ty":0,"tz":2,"rx":0,"ry":0,"rz":0 }
                      ]
        }
    }
``` 

## 2. Let the API calculate the section force

The calculation of the section force uses an API called [StructuralEngine](https://structuralengine.com/).
If you POST the above data here, it will calculate the section force.

```python
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)   
```

```json
{'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}}}}}
```

## 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

```python
# 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']
```

### Draw Q and M diagrams

```python
# 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']]
```

Draw a diagram from the above information.

```python
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")
```

![image](https://dev-to-uploads.s3.amazonaws.com/i/sqylt267myvnopy31oci.png)


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.