marta-marta commited on
Commit
be50c8a
·
1 Parent(s): 5c52661

Incorporating an elasticity tensor to help pinpoint useful structures

Browse files
Files changed (2) hide show
  1. app.py +29 -4
  2. elasticity.py +99 -0
app.py CHANGED
@@ -4,6 +4,8 @@ import math
4
  import matplotlib.pyplot as plt
5
  from huggingface_hub import from_pretrained_keras
6
  import streamlit as st
 
 
7
  # Needed in requirements.txt for importing to use in the transformers model
8
  import tensorflow
9
 
@@ -283,16 +285,35 @@ def generate_unit_cell(shape, density, thickness):
283
  return globals()[shape](int(thickness), float(density), 28)
284
 
285
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
  # Generate the endpoints
287
  number_1 = generate_unit_cell(shape_1, density_1, thickness_1)
288
  number_2 = generate_unit_cell(shape_2, density_2, thickness_2)
289
 
 
 
 
 
290
  # Display the endpoints to the user
291
- if st.button("Generate Endpoint Images"):
292
  plt.figure(1)
293
  st.header("Endpoints to be generated:")
294
- plt.subplot(1, 2, 1), plt.imshow(number_1, cmap='gray', vmin=0, vmax=1)
295
- plt.subplot(1, 2, 2), plt.imshow(number_2, cmap='gray', vmin=0, vmax=1)
 
296
  plt.figure(1)
297
  st.pyplot(plt.figure(1))
298
  ########################################################################################################################
@@ -365,9 +386,13 @@ number_3 = generate_unit_cell(shape_3, density_3, thickness_3)
365
  number_4 = generate_unit_cell(shape_4, density_4, thickness_4)
366
 
367
  # Display the endpoints to the user
368
- if st.button("Generate Endpoint Images for Mesh"):
369
  plt.figure(1)
370
  st.header("Endpoints to be generated:")
 
 
 
 
371
  plt.subplot(2, 2, 1), plt.imshow(number_1, cmap='gray', vmin=0, vmax=1)
372
  plt.subplot(2, 2, 2), plt.imshow(number_2, cmap='gray', vmin=0, vmax=1)
373
  plt.subplot(2, 2, 3), plt.imshow(number_3, cmap='gray', vmin=0, vmax=1)
 
4
  import matplotlib.pyplot as plt
5
  from huggingface_hub import from_pretrained_keras
6
  import streamlit as st
7
+ from elasticity import elasticity
8
+
9
  # Needed in requirements.txt for importing to use in the transformers model
10
  import tensorflow
11
 
 
285
  return globals()[shape](int(thickness), float(density), 28)
286
 
287
 
288
+ def display_arrays(array1, array2, label_1, label_2):
289
+ # A Function to plot two arrays side by side in streamlit
290
+ # Create two columns
291
+ col1, col2 = st.columns(2)
292
+
293
+ # Populate the first column with array1
294
+ col1.header(label_1)
295
+ col1.write(array1)
296
+
297
+ # Populate the second column with array2
298
+ col2.header(label_2)
299
+ col2.write(array2)
300
+
301
+
302
  # Generate the endpoints
303
  number_1 = generate_unit_cell(shape_1, density_1, thickness_1)
304
  number_2 = generate_unit_cell(shape_2, density_2, thickness_2)
305
 
306
+ # Calculate the elasticity for the shapes:
307
+ elasticity_1 = elasticity(number_1)
308
+ elasticity_2 = elasticity(number_2)
309
+
310
  # Display the endpoints to the user
311
+ if st.button("Generate Endpoint Images and Elasticity Tensors"):
312
  plt.figure(1)
313
  st.header("Endpoints to be generated:")
314
+ plt.subplot(1, 2, 1), plt.imshow(number_1, cmap='gray', vmin=0, vmax=1), plt.title("Shape 1:")
315
+ display_arrays(elasticity_1, elasticity_2, "Elasticity Tensor of Shape 1", "Elasticity Tensor of Shape 2")
316
+ plt.subplot(1, 2, 2), plt.imshow(number_2, cmap='gray', vmin=0, vmax=1), plt.title("Shape 2:")
317
  plt.figure(1)
318
  st.pyplot(plt.figure(1))
319
  ########################################################################################################################
 
386
  number_4 = generate_unit_cell(shape_4, density_4, thickness_4)
387
 
388
  # Display the endpoints to the user
389
+ if st.button("Generate Endpoint Images for Mesh and Elasticity Tensors"):
390
  plt.figure(1)
391
  st.header("Endpoints to be generated:")
392
+ elasticity_3 = elasticity(number_3)
393
+ elasticity_4 = elasticity(number_4)
394
+ display_arrays(elasticity_1, elasticity_2, "Elasticity Tensor of Shape 1", "Elasticity Tensor of Shape 2")
395
+ display_arrays(elasticity_3, elasticity_4, "Elasticity Tensor of Shape 3", "Elasticity Tensor of Shape 4")
396
  plt.subplot(2, 2, 1), plt.imshow(number_1, cmap='gray', vmin=0, vmax=1)
397
  plt.subplot(2, 2, 2), plt.imshow(number_2, cmap='gray', vmin=0, vmax=1)
398
  plt.subplot(2, 2, 3), plt.imshow(number_3, cmap='gray', vmin=0, vmax=1)
elasticity.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import scipy.sparse as sp
3
+ from scipy.sparse.linalg import spsolve
4
+
5
+
6
+ def elasticity(x):
7
+ # x (2D array): contains 1's and 0's
8
+ # penal (positive float): affects penalization of cells, but has no effect here since no gray values
9
+
10
+ penal = 1
11
+ nely, nelx = np.shape(x)
12
+
13
+ ## MATERIAL PROPERTIES
14
+ EO = 1
15
+ Emin = 1e-9
16
+ nu = 0.3
17
+
18
+ ## PREPARE FINITE ELEMENT ANALYSIS
19
+ A11 = np.array([[12, 3, -6, -3], [3, 12, 3, 0], [-6, 3, 12, -3], [-3, 0, -3, 12]])
20
+ A12 = np.array([[-6, -3, 0, 3], [-3, -6, -3, -6], [0, -3, -6, 3], [3, -6, 3, -6]])
21
+ B11 = np.array([[-4, 3, -2, 9], [3, -4, -9, 4], [-2, -9, -4, -3], [9, 4, -3, -4]])
22
+ B12 = np.array([[2, -3, 4, -9], [-3, 2, 9, -2], [4, 9, 2, 3], [-9, -2, 3, 2]])
23
+
24
+ KE = 1 / (1 - nu**2) / 24 * (np.block([[A11, A12], [np.transpose(A12), A11]]) + nu * np.block([[B11, B12], [np.transpose(B12), B11]]))
25
+
26
+ nodenrs = np.arange(1, (1 + nelx) * (1 + nely) + 1).reshape((1 + nely, 1 + nelx), order="F")
27
+ edofVec = np.reshape(2 * nodenrs[:-1, :-1] + 1, (nelx * nely, 1), order="F")
28
+ edofMat = np.tile(edofVec, (1, 8)) + np.tile(np.concatenate(([0, 1], 2*nely+np.array([2,3,0,1]), [-2, -1])), (nelx*nely, 1))
29
+
30
+ iK = np.reshape(np.kron(edofMat, np.ones((8, 1))).T, (64 * nelx * nely, 1), order='F') # Need order F to match the reshaping of Matlab
31
+ jK = np.reshape(np.kron(edofMat, np.ones((1, 8))).T, (64 * nelx * nely, 1), order='F')
32
+
33
+ ## PERIODIC BOUNDARY CONDITIONS
34
+ e0 = np.eye(3)
35
+ ufixed = np.zeros((8, 3))
36
+ U = np.zeros((2*(nely+1)*(nelx+1), 3))
37
+
38
+ alldofs = np.arange(1, 2*(nely+1)*(nelx+1)+1)
39
+ n1 = np.concatenate((nodenrs[-1, [0, -1]], nodenrs[0, [-1,0]]))
40
+ d1 = np.reshape(([[(2*n1-1)], [2*n1]]), (1, 8), order='F') # four corner points of the object
41
+ n3 = np.concatenate((nodenrs[1:-1, 0].T, nodenrs[-1, 1:-1]))
42
+ d3 = np.reshape(([[(2*n3-1)], [2*n3]]), (1, 2*(nelx+nely-2)), order='F') # Left and Bottom boundaries
43
+ n4 = np.concatenate((nodenrs[1:-1, -1].flatten(), nodenrs[0, 1:-1].flatten()))
44
+ d4 = np.reshape(([[(2*n4-1)], [2*n4]]), (1, 2*(nelx+nely-2)), order='F') # Right and Top Boundaries
45
+ d2 = np.setdiff1d(alldofs, np.hstack([d1, d3, d4])) # All internal nodes in the shape
46
+
47
+ for j in range(0, 3):
48
+ ufixed[2:4, j] = np.array([[e0[0, j], e0[2, j] / 2], [e0[2, j] / 2, e0[1, j]]]) @ np.array([nelx, 0])
49
+ ufixed[6:8, j] = np.array([[e0[0, j], e0[2, j]/2], [e0[2, j]/2, e0[1, j]]]) @ np.array([0, nely])
50
+ ufixed[4:6, j] = ufixed[2:4, j]+ufixed[6:8, j]
51
+
52
+ wfixed = np.concatenate((np.tile(ufixed[2:4, :], (nely-1, 1)), np.tile(ufixed[6:8, :], (nelx-1, 1))))
53
+
54
+ ## INITIALIZE ITERATION
55
+ qe = np.empty((3, 3), dtype=object)
56
+ Q = np.zeros((3, 3))
57
+ xPhys = x
58
+
59
+ '''
60
+ # For printing out large arrays
61
+ with np.printoptions(threshold=np.inf):
62
+ K_copy[np.abs(K_copy) < 0.000001] = 0
63
+ print(K_copy)
64
+ '''
65
+
66
+ ## FE-ANALYSIS
67
+ sK = (KE.flatten(order='F')[:, np.newaxis] * (Emin+xPhys.flatten(order='F').T**penal*(EO-Emin)))[np.newaxis, :].reshape(-1, 1, order='F')
68
+
69
+ K = sp.coo_matrix((sK.flatten(order='F'), (iK.flatten(order='F')-1, jK.flatten(order='F')-1)), shape=(np.shape(alldofs)[0], np.shape(alldofs)[0]))
70
+ K = (K + K.transpose())/2
71
+ K = sp.csr_matrix(np.nan_to_num(K)) # Remove the NAN values and replace them with 0's and large finite values
72
+ K = K.tocsc() # Converting to csc from csr transposes the matrix to match the orientation in MATLAB
73
+
74
+
75
+ k1 = K[d2-1][:, d2-1]
76
+ k2 = (K[d2[:,None]-1, d3-1] + K[d2[:,None]-1, d4-1])
77
+ k3 = (K[d3-1, d2[:, None]-1] + K[d4-1, d2[:, None]-1])
78
+ k4 = K[d3-1,d3.T-1] + K[d4-1, d4.T-1] + K[d3-1, d4.T-1] + K[d4-1, d3.T-1]
79
+
80
+ Kr_top = sp.hstack((k1, k2))
81
+ Kr_bottom = sp.hstack((k3.T, k4))
82
+ Kr = sp.vstack((Kr_top, Kr_bottom)).tocsc()
83
+
84
+ U[d1-1, :] = ufixed
85
+ U[np.concatenate((d2-1, d3.ravel()-1)), :] = spsolve(Kr, ((-sp.vstack((K[d2[:, None]-1, d1-1], (K[d3-1, d1.T-1]+K[d4-1, d1.T-1]).T)))*ufixed-sp.vstack((K[d2-1, d4.T-1].T, (K[d3-1, d4.T-1]+K[d4-1, d4.T-1]).T))*wfixed))
86
+ U[d4-1, :] = U[d3-1, :]+wfixed
87
+
88
+
89
+ ## OBJECTIVE FUNCTION AND SENSITIVITY ANALYSIS
90
+ for i in range(0, 3):
91
+ for j in range(0, 3):
92
+ U1 = U[:, i] # not quite right
93
+ U2 = U[:, j]
94
+ qe[i, j] = np.reshape(np.sum((U1[edofMat-1] @ KE) * U2[edofMat-1], axis=1), (nely, nelx), order='F') / (nelx * nely)
95
+ Q[i, j] = sum(sum((Emin+xPhys**penal*(EO-Emin))*qe[i, j]))
96
+ Q[np.abs(Q) < 0.000001] = 0
97
+
98
+ return Q
99
+