@ -1,5 +1,6 @@
from __future__ import division
# from __future__ import division
import numpy as np
import numpy as np
import os
from scipy . sparse import coo_matrix
from scipy . sparse import coo_matrix
from scipy . sparse . linalg import spsolve
from scipy . sparse . linalg import spsolve
from matplotlib import colors
from matplotlib import colors
@ -11,251 +12,274 @@ import torch.nn as nn
import torch . nn . functional as F
import torch . nn . functional as F
from utils . data_standardizer import standardization
from utils . data_standardizer import standardization
from utils . data_loader import data_loader
from utils . mesh_reshape import Ms_u_reshape
from options . topopt_options import TopoptOption
def top_EMsFEA ( nelx , nely , volfrac , penal , rmin , ft , mod_idx , m ) :
def top_EMsFEA ( opt ) :
print ( " Minimum compliance problem with OC " )
mod_idx = opt . mod_idx
print ( " ndes: " + str ( nelx ) + " x " + str ( nely ) )
m = opt . ms_ratio_to
print ( " volfrac: " + str ( volfrac ) + " , rmin: " + str ( rmin ) + " , penal: " + str ( penal ) )
nelx = opt . nelx_to
print ( " Filter method: " + [ " Sensitivity based " , " Density based " ] [ ft ] )
nely = opt . nely_to
# Max and min stiffness
volfrac = opt . volfrac
Emin = 1e-9
rmin = opt . rmin
Emax = 1.0
penal = opt . penal
c_nelx = int ( nelx / m )
ft = opt . ft # ft==0 -> sens, ft==1 -> dens
c_nely = int ( nely / m )
# dofs:
print ( " Minimum compliance problem with OC " )
ndof = 2 * ( nelx + 1 ) * ( nely + 1 )
print ( " ndes: " + str ( nelx ) + " x " + str ( nely ) )
coarse_ndof = 2 * ( c_nelx + 1 ) * ( c_nely + 1 )
print ( " volfrac: " + str ( volfrac ) + " , rmin: " + str ( rmin ) + " , penal: " + str ( penal ) )
# Allocate design variables (as array), initialize and allocate sens.
print ( " Filter method: " + [ " Sensitivity based " , " Density based " ] [ ft ] )
x = volfrac * np . ones ( nely * nelx , dtype = float )
xold = x . copy ( )
# Max and min stiffness
xPhys = x . copy ( )
Emin = 1e-9
g = 0 # must be initialized to use the NGuyen/Paulino OC approach
Emax = 1.0
dc = np . zeros ( ( nely , nelx ) , dtype = float )
c_nelx = int ( nelx / m )
# FE: Build the index vectors for the for coo matrix format.
c_nely = int ( nely / m )
KE = lk ( )
# dofs:
edofMat = np . zeros ( ( nelx * nely , 8 ) , dtype = int )
ndof = 2 * ( nelx + 1 ) * ( nely + 1 )
for elx in range ( nelx ) :
coarse_ndof = 2 * ( c_nelx + 1 ) * ( c_nely + 1 )
for ely in range ( nely ) :
# Allocate design variables (as array), initialize and allocate sens.
el = ely + elx * nely
x = volfrac * np . ones ( nely * nelx , dtype = float )
n1 = ( nely + 1 ) * elx + ely
xold = x . copy ( )
n2 = ( nely + 1 ) * ( elx + 1 ) + ely
xPhys = x . copy ( )
edofMat [ el , : ] = np . array ( [ 2 * n1 + 2 , 2 * n1 + 3 , 2 * n2 + 2 , 2 * n2 + 3 , 2 * n2 , 2 * n2 + 1 , 2 * n1 , 2 * n1 + 1 ] )
g = 0 # must be initialized to use the NGuyen/Paulino OC approach
# Construct the index pointers for the coo format
dc = np . zeros ( ( nely , nelx ) , dtype = float )
iK = np . kron ( edofMat , np . ones ( ( 8 , 1 ) ) ) . flatten ( )
# FE: Build the index vectors for the for coo matrix format.
jK = np . kron ( edofMat , np . ones ( ( 1 , 8 ) ) ) . flatten ( )
KE = lk ( )
edofMat = np . zeros ( ( nelx * nely , 8 ) , dtype = int )
for elx in range ( nelx ) :
coarse_edofMat = np . zeros ( ( c_nelx * c_nely , 8 ) , dtype = int )
for ely in range ( nely ) :
for elx in range ( c_nelx ) :
el = ely + elx * nely
for ely in range ( c_nely ) :
n1 = ( nely + 1 ) * elx + ely
el = ely + elx * c_nely
n2 = ( nely + 1 ) * ( elx + 1 ) + ely
n1 = ( c_nely + 1 ) * elx + ely
edofMat [ el , : ] = np . array ( [ 2 * n1 + 2 , 2 * n1 + 3 , 2 * n2 + 2 , 2 * n2 + 3 , 2 * n2 , 2 * n2 + 1 , 2 * n1 , 2 * n1 + 1 ] )
n2 = ( c_nely + 1 ) * ( elx + 1 ) + ely
# Construct the index pointers for the coo format
coarse_edofMat [ el , : ] = np . array ( [ 2 * n1 + 2 , 2 * n1 + 3 , 2 * n2 + 2 , 2 * n2 + 3 , 2 * n2 , 2 * n2 + 1 , 2 * n1 , 2 * n1 + 1 ] )
iK = np . kron ( edofMat , np . ones ( ( 8 , 1 ) ) ) . flatten ( )
# Construct the index pointers for the coo format
jK = np . kron ( edofMat , np . ones ( ( 1 , 8 ) ) ) . flatten ( )
coarse_iK = np . kron ( coarse_edofMat , np . ones ( ( 8 , 1 ) ) ) . flatten ( )
coarse_jK = np . kron ( coarse_edofMat , np . ones ( ( 1 , 8 ) ) ) . flatten ( )
coarse_edofMat = np . zeros ( ( c_nelx * c_nely , 8 ) , dtype = int )
for elx in range ( c_nelx ) :
for ely in range ( c_nely ) :
# Filter: Build (and assemble) the index+data vectors for the coo matrix format
el = ely + elx * c_nely
nfilter = int ( nelx * nely * ( ( 2 * ( np . ceil ( rmin ) - 1 ) + 1 ) * * 2 ) )
n1 = ( c_nely + 1 ) * elx + ely
iH = np . zeros ( nfilter )
n2 = ( c_nely + 1 ) * ( elx + 1 ) + ely
jH = np . zeros ( nfilter )
coarse_edofMat [ el , : ] = np . array ( [ 2 * n1 + 2 , 2 * n1 + 3 , 2 * n2 + 2 , 2 * n2 + 3 , 2 * n2 , 2 * n2 + 1 , 2 * n1 , 2 * n1 + 1 ] )
sH = np . zeros ( nfilter )
# Construct the index pointers for the coo format
cc = 0
coarse_iK = np . kron ( coarse_edofMat , np . ones ( ( 8 , 1 ) ) ) . flatten ( )
for i in range ( nelx ) :
coarse_jK = np . kron ( coarse_edofMat , np . ones ( ( 1 , 8 ) ) ) . flatten ( )
for j in range ( nely ) :
row = i * nely + j
kk1 = int ( np . maximum ( i - ( np . ceil ( rmin ) - 1 ) , 0 ) )
kk2 = int ( np . minimum ( i + np . ceil ( rmin ) , nelx ) )
# Filter: Build (and assemble) the index+data vectors for the coo matrix format
ll1 = int ( np . maximum ( j - ( np . ceil ( rmin ) - 1 ) , 0 ) )
nfilter = int ( nelx * nely * ( ( 2 * ( np . ceil ( rmin ) - 1 ) + 1 ) * * 2 ) )
ll2 = int ( np . minimum ( j + np . ceil ( rmin ) , nely ) )
iH = np . zeros ( nfilter )
for k in range ( kk1 , kk2 ) :
jH = np . zeros ( nfilter )
for l in range ( ll1 , ll2 ) :
sH = np . zeros ( nfilter )
col = k * nely + l
cc = 0
fac = rmin - np . sqrt ( ( ( i - k ) * ( i - k ) + ( j - l ) * ( j - l ) ) )
for i in range ( nelx ) :
iH [ cc ] = row
for j in range ( nely ) :
jH [ cc ] = col
row = i * nely + j
sH [ cc ] = np . maximum ( 0.0 , fac )
kk1 = int ( np . maximum ( i - ( np . ceil ( rmin ) - 1 ) , 0 ) )
cc = cc + 1
kk2 = int ( np . minimum ( i + np . ceil ( rmin ) , nelx ) )
# Finalize assembly and convert to csc format
ll1 = int ( np . maximum ( j - ( np . ceil ( rmin ) - 1 ) , 0 ) )
H = coo_matrix ( ( sH , ( iH , jH ) ) , shape = ( nelx * nely , nelx * nely ) ) . tocsc ( )
ll2 = int ( np . minimum ( j + np . ceil ( rmin ) , nely ) )
Hs = H . sum ( 1 )
for k in range ( kk1 , kk2 ) :
# BC's and support
for l in range ( ll1 , ll2 ) :
# dofs=np.arange(2*(nelx+1)*(nely+1))
col = k * nely + l
# fixed=np.union1d(dofs[0:2*(nely+1):2],np.array([2*(nelx+1)*(nely+1)-1]))
fac = rmin - np . sqrt ( ( ( i - k ) * ( i - k ) + ( j - l ) * ( j - l ) ) )
# free=np.setdiff1d(dofs,fixed)
iH [ cc ] = row
jH [ cc ] = col
coarse_dofs = np . arange ( 2 * ( c_nelx + 1 ) * ( c_nely + 1 ) )
sH [ cc ] = np . maximum ( 0.0 , fac )
coarse_fixed = np . union1d ( coarse_dofs [ 0 : 2 * ( c_nely + 1 ) : 2 ] , np . array ( [ 2 * ( c_nelx + 1 ) * ( c_nely + 1 ) - 1 ] ) )
cc = cc + 1
coarse_free = np . setdiff1d ( coarse_dofs , coarse_fixed )
# Finalize assembly and convert to csc format
H = coo_matrix ( ( sH , ( iH , jH ) ) , shape = ( nelx * nely , nelx * nely ) ) . tocsc ( )
# Solution and RHS vectors
Hs = H . sum ( 1 )
# f=np.zeros((ndof,1))
# BC's and support
# u=np.zeros((ndof,1))
# dofs=np.arange(2*(nelx+1)*(nely+1))
# fixed=np.union1d(dofs[0:2*(nely+1):2],np.array([2*(nelx+1)*(nely+1)-1]))
c_f = np . zeros ( ( coarse_ndof , 1 ) )
# free=np.setdiff1d(dofs,fixed)
c_u = np . zeros ( ( coarse_ndof , 1 ) )
# Set load
coarse_dofs = np . arange ( 2 * ( c_nelx + 1 ) * ( c_nely + 1 ) )
# f[1,0]=-1
coarse_fixed = np . union1d ( coarse_dofs [ 0 : 2 * ( c_nely + 1 ) : 2 ] , np . array ( [ 2 * ( c_nelx + 1 ) * ( c_nely + 1 ) - 1 ] ) )
coarse_free = np . setdiff1d ( coarse_dofs , coarse_fixed )
c_f [ 1 , 0 ] = - 1
# Initialize plot and plot the initial design
# Solution and RHS vectors
plt . ion ( ) # Ensure that redrawing is possible
# f=np.zeros((ndof,1))
fig , ax = plt . subplots ( )
# u=np.zeros((ndof,1))
im = ax . imshow ( - xPhys . reshape ( ( nelx , nely ) ) . T , cmap = ' gray ' , \
interpolation = ' none ' , norm = colors . Normalize ( vmin = - 1 , vmax = 0 ) )
c_f = np . zeros ( ( coarse_ndof , 1 ) )
fig . show ( )
c_u = np . zeros ( ( coarse_ndof , 1 ) )
# Set loop counter and gradient vectors
# Set load
loop = 0
# f[1,0]=-1
change = 1
dv = np . ones ( nely * nelx )
c_f [ 1 , 0 ] = - 1
dc = np . ones ( nely * nelx )
# Initialize plot and plot the initial design
ce = np . ones ( nely * nelx )
plt . ion ( ) # Ensure that redrawing is possible
while change > 0.01 and loop < 2000 :
fig , ax = plt . subplots ( )
loop = loop + 1
im = ax . imshow ( - xPhys . reshape ( ( nelx , nely ) ) . T , cmap = ' gray ' , \
# Setup and solve FE problem
interpolation = ' none ' , norm = colors . Normalize ( vmin = - 1 , vmax = 0 ) )
coarse_xPhys = xPhys [ 12 : : 25 ]
fig . show ( )
sK = ( ( KE . flatten ( ) [ np . newaxis ] ) . T * ( Emin + ( coarse_xPhys ) * * penal * ( Emax - Emin ) ) ) . flatten ( order = ' F ' )
# Set loop counter and gradient vectors
K = coo_matrix ( ( sK , ( coarse_iK , coarse_jK ) ) , shape = ( coarse_ndof , coarse_ndof ) ) . tocsc ( )
loop = 0
# Remove constrained dofs from matrix
change = 1
K = K [ coarse_free , : ] [ : , coarse_free ]
dv = np . ones ( nely * nelx )
# Solve coarse situation
dc = np . ones ( nely * nelx )
c_u [ coarse_free , 0 ] = spsolve ( K , c_f [ coarse_free , 0 ] )
ce = np . ones ( nely * nelx )
# Predict fine situation
while change > 0.01 and loop < 2000 :
u = pred_net ( c_u , xPhys , c_nelx , c_nely , m , ' checkpoints/ANN_mod1/ANN_mod1_opt.pt ' )
loop = loop + 1
# Setup and solve FE problem
# print(f.shape, f)
coarse_xPhys = xPhys [ 12 : : 25 ]
# print(K.shape, K)
sK = ( ( KE . flatten ( ) [ np . newaxis ] ) . T * ( Emin + ( coarse_xPhys ) * * penal * ( Emax - Emin ) ) ) . flatten ( order = ' F ' )
# print(f[free,0])
K = coo_matrix ( ( sK , ( coarse_iK , coarse_jK ) ) , shape = ( coarse_ndof , coarse_ndof ) ) . tocsc ( )
# print(u.shape, u)
# Remove constrained dofs from matrix
K = K [ coarse_free , : ] [ : , coarse_free ]
# Objective and sensitivity
# Solve coarse situation
ce [ : ] = ( np . dot ( u [ edofMat ] . reshape ( nelx * nely , 8 ) , KE ) * u [ edofMat ] . reshape ( nelx * nely , 8 ) ) . sum ( 1 )
c_u [ coarse_free , 0 ] = spsolve ( K , c_f [ coarse_free , 0 ] )
obj = ( ( Emin + xPhys * * penal * ( Emax - Emin ) ) * ce ) . sum ( )
dc [ : ] = ( - penal * xPhys * * ( penal - 1 ) * ( Emax - Emin ) ) * ce
# Predict fine situation
dv [ : ] = np . ones ( nely * nelx )
u = pred_net ( c_u , xPhys , opt )
# Sensitivity filtering:
if ft == 0 :
# u=pred_net(c_u,xPhys,c_nelx,c_nely,m,'checkpoints/ANN_mod1/ANN_mod1_opt.pt')
dc [ : ] = np . asarray ( ( H * ( x * dc ) ) [ np . newaxis ] . T / Hs ) [ : , 0 ] / np . maximum ( 0.001 , x )
elif ft == 1 :
# print(f.shape, f)
dc [ : ] = np . asarray ( H * ( dc [ np . newaxis ] . T / Hs ) ) [ : , 0 ]
# print(K.shape, K)
dv [ : ] = np . asarray ( H * ( dv [ np . newaxis ] . T / Hs ) ) [ : , 0 ]
# print(f[free,0])
# Optimality criteria
# print(u.shape, u)
xold [ : ] = x
( x [ : ] , g ) = oc ( nelx , nely , x , volfrac , dc , dv , g )
# Objective and sensitivity
# Filter design variables
ce [ : ] = ( np . dot ( u [ edofMat ] . reshape ( nelx * nely , 8 ) , KE ) * u [ edofMat ] . reshape ( nelx * nely , 8 ) ) . sum ( 1 )
if ft == 0 : xPhys [ : ] = x
obj = ( ( Emin + xPhys * * penal * ( Emax - Emin ) ) * ce ) . sum ( )
elif ft == 1 : xPhys [ : ] = np . asarray ( H * x [ np . newaxis ] . T / Hs ) [ : , 0 ]
dc [ : ] = ( - penal * xPhys * * ( penal - 1 ) * ( Emax - Emin ) ) * ce
# Compute the change by the inf. norm
dv [ : ] = np . ones ( nely * nelx )
change = np . linalg . norm ( x . reshape ( nelx * nely , 1 ) - xold . reshape ( nelx * nely , 1 ) , np . inf )
# Sensitivity filtering:
# Plot to screen
if ft == 0 :
im . set_array ( - xPhys . reshape ( ( nelx , nely ) ) . T )
dc [ : ] = np . asarray ( ( H * ( x * dc ) ) [ np . newaxis ] . T / Hs ) [ : , 0 ] / np . maximum ( 0.001 , x )
fig . canvas . draw ( )
elif ft == 1 :
# Write iteration history to screen (req. Python 2.6 or newer)
dc [ : ] = np . asarray ( H * ( dc [ np . newaxis ] . T / Hs ) ) [ : , 0 ]
print ( " it.: {0} , obj.: {1:.3f} Vol.: {2:.3f} , ch.: {3:.3f} " . format ( \
dv [ : ] = np . asarray ( H * ( dv [ np . newaxis ] . T / Hs ) ) [ : , 0 ]
loop , obj , ( g + volfrac * nelx * nely ) / ( nelx * nely ) , change ) )
# Optimality criteria
xold [ : ] = x
( x [ : ] , g ) = oc ( nelx , nely , x , volfrac , dc , dv , g )
np . save ( ' results/top88_ ' + mod_idx + ' _xPhys_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , xPhys . reshape ( ( nelx , nely ) ) . T )
# Filter design variables
np . save ( ' results/top88_ ' + mod_idx + ' _u_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , u )
if ft == 0 : xPhys [ : ] = x
plt . savefig ( ' results/top88_ ' + mod_idx + ' _img_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .jpg ' )
elif ft == 1 : xPhys [ : ] = np . asarray ( H * x [ np . newaxis ] . T / Hs ) [ : , 0 ]
# Compute the change by the inf. norm
# plt.show()
change = np . linalg . norm ( x . reshape ( nelx * nely , 1 ) - xold . reshape ( nelx * nely , 1 ) , np . inf )
# Plot to screen
print ( u . reshape ( nelx + 1 , nely + 1 , 2 ) )
im . set_array ( - xPhys . reshape ( ( nelx , nely ) ) . T )
fig . canvas . draw ( )
# Make sure the plot stays and that the shell remains
# Write iteration history to screen (req. Python 2.6 or newer)
np . save ( ' results/top88_ ' + mod_idx + ' _xPhys_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , xPhys . reshape ( ( nelx , nely ) ) . T )
print ( " it.: {0} , obj.: {1:.3f} Vol.: {2:.3f} , ch.: {3:.3f} " . format ( \
np . save ( ' results/top88_ ' + mod_idx + ' _u_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , u )
loop , obj , ( g + volfrac * nelx * nely ) / ( nelx * nely ) , change ) )
plt . savefig ( ' results/top88_ ' + mod_idx + ' _img_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .jpg ' )
plt . show ( )
print ( " Press any key... " )
np . save ( ' results/EMsNetTop_ ' + mod_idx + ' _xPhys_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , xPhys . reshape ( ( nelx , nely ) ) . T )
np . save ( ' results/EMsNetTop_ ' + mod_idx + ' _u_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , u )
plt . savefig ( ' results/EMsNetTop_ ' + mod_idx + ' _img_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .jpg ' )
# plt.show()
print ( u . reshape ( nelx + 1 , nely + 1 , 2 ) )
# Make sure the plot stays and that the shell remains
np . save ( ' results/EMsNetTop_ ' + mod_idx + ' _xPhys_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , xPhys . reshape ( ( nelx , nely ) ) . T )
np . save ( ' results/EMsNetTop_ ' + mod_idx + ' _u_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .npy ' , u )
plt . savefig ( ' results/EMsNetTop_ ' + mod_idx + ' _img_ ' + str ( nelx ) + ' _ ' + str ( nely ) + ' .jpg ' )
plt . show ( )
print ( " Press any key... " )
#element stiffness matrix
#element stiffness matrix
def lk ( ) :
def lk ( ) :
E = 1
E = 1
nu = 0.3
nu = 0.3
k = np . array ( [ 1 / 2 - nu / 6 , 1 / 8 + nu / 8 , - 1 / 4 - nu / 12 , - 1 / 8 + 3 * nu / 8 , - 1 / 4 + nu / 12 , - 1 / 8 - nu / 8 , nu / 6 , 1 / 8 - 3 * nu / 8 ] )
k = np . array ( [ 1 / 2 - nu / 6 , 1 / 8 + nu / 8 , - 1 / 4 - nu / 12 , - 1 / 8 + 3 * nu / 8 , - 1 / 4 + nu / 12 , - 1 / 8 - nu / 8 , nu / 6 , 1 / 8 - 3 * nu / 8 ] )
KE = E / ( 1 - nu * * 2 ) * np . array ( [ [ k [ 0 ] , k [ 1 ] , k [ 2 ] , k [ 3 ] , k [ 4 ] , k [ 5 ] , k [ 6 ] , k [ 7 ] ] ,
KE = E / ( 1 - nu * * 2 ) * np . array ( [ [ k [ 0 ] , k [ 1 ] , k [ 2 ] , k [ 3 ] , k [ 4 ] , k [ 5 ] , k [ 6 ] , k [ 7 ] ] ,
[ k [ 1 ] , k [ 0 ] , k [ 7 ] , k [ 6 ] , k [ 5 ] , k [ 4 ] , k [ 3 ] , k [ 2 ] ] ,
[ k [ 1 ] , k [ 0 ] , k [ 7 ] , k [ 6 ] , k [ 5 ] , k [ 4 ] , k [ 3 ] , k [ 2 ] ] ,
[ k [ 2 ] , k [ 7 ] , k [ 0 ] , k [ 5 ] , k [ 6 ] , k [ 3 ] , k [ 4 ] , k [ 1 ] ] ,
[ k [ 2 ] , k [ 7 ] , k [ 0 ] , k [ 5 ] , k [ 6 ] , k [ 3 ] , k [ 4 ] , k [ 1 ] ] ,
[ k [ 3 ] , k [ 6 ] , k [ 5 ] , k [ 0 ] , k [ 7 ] , k [ 2 ] , k [ 1 ] , k [ 4 ] ] ,
[ k [ 3 ] , k [ 6 ] , k [ 5 ] , k [ 0 ] , k [ 7 ] , k [ 2 ] , k [ 1 ] , k [ 4 ] ] ,
[ k [ 4 ] , k [ 5 ] , k [ 6 ] , k [ 7 ] , k [ 0 ] , k [ 1 ] , k [ 2 ] , k [ 3 ] ] ,
[ k [ 4 ] , k [ 5 ] , k [ 6 ] , k [ 7 ] , k [ 0 ] , k [ 1 ] , k [ 2 ] , k [ 3 ] ] ,
[ k [ 5 ] , k [ 4 ] , k [ 3 ] , k [ 2 ] , k [ 1 ] , k [ 0 ] , k [ 7 ] , k [ 6 ] ] ,
[ k [ 5 ] , k [ 4 ] , k [ 3 ] , k [ 2 ] , k [ 1 ] , k [ 0 ] , k [ 7 ] , k [ 6 ] ] ,
[ k [ 6 ] , k [ 3 ] , k [ 4 ] , k [ 1 ] , k [ 2 ] , k [ 7 ] , k [ 0 ] , k [ 5 ] ] ,
[ k [ 6 ] , k [ 3 ] , k [ 4 ] , k [ 1 ] , k [ 2 ] , k [ 7 ] , k [ 0 ] , k [ 5 ] ] ,
[ k [ 7 ] , k [ 2 ] , k [ 1 ] , k [ 4 ] , k [ 3 ] , k [ 6 ] , k [ 5 ] , k [ 0 ] ] ] ) ;
[ k [ 7 ] , k [ 2 ] , k [ 1 ] , k [ 4 ] , k [ 3 ] , k [ 6 ] , k [ 5 ] , k [ 0 ] ] ] ) ;
return ( KE )
return ( KE )
# Optimality criterion
# Optimality criterion
def oc ( nelx , nely , x , volfrac , dc , dv , g ) :
def oc ( nelx , nely , x , volfrac , dc , dv , g ) :
l1 = 0
l1 = 0
l2 = 1e9
l2 = 1e9
move = 0.2
move = 0.2
# reshape to perform vector operations
# reshape to perform vector operations
xnew = np . zeros ( nelx * nely )
xnew = np . zeros ( nelx * nely )
while ( l2 - l1 ) / ( l1 + l2 ) > 1e-3 :
while ( l2 - l1 ) / ( l1 + l2 ) > 1e-3 :
lmid = 0.5 * ( l2 + l1 )
lmid = 0.5 * ( l2 + l1 )
xnew [ : ] = np . maximum ( 0.0 , np . maximum ( x - move , np . minimum ( 1.0 , np . minimum ( x + move , x * np . sqrt ( - dc / dv / lmid ) ) ) ) )
xnew [ : ] = np . maximum ( 0.0 , np . maximum ( x - move , np . minimum ( 1.0 , np . minimum ( x + move , x * np . sqrt ( - dc / dv / lmid ) ) ) ) )
gt = g + np . sum ( ( dv * ( xnew - x ) ) )
gt = g + np . sum ( ( dv * ( xnew - x ) ) )
if gt > 0 :
if gt > 0 :
l1 = lmid
l1 = lmid
else :
else :
l2 = lmid
l2 = lmid
return ( xnew , gt )
return ( xnew , gt )
def pred_net ( coarse_u , fine_x , coarse_nelx , coarse_nely , m , model_load_path , standard = False , device = 0 ) :
def pred_net ( coarse_u , global_x , opt ) :
coarse_density = np . zeros ( shape = ( coarse_nely * coarse_nelx , m * m ) )
m = opt . ms_ratio_to
fine_x = fine_x . reshape ( coarse_nely * m , coarse_nelx * m )
nelx = opt . nelx_to
for ely in range ( coarse_nely ) :
nely = opt . nely_to
for elx in range ( coarse_nelx ) :
coarse_nelx = int ( nelx / m )
coarse_density [ elx + ely * m ] = fine_x [ ely * m : ( ely + 1 ) * m , elx * m : ( elx + 1 ) * m ] . flatten ( )
coarse_nely = int ( nely / m )
print ( coarse_density . shape )
c_N = coarse_nelx * coarse_nely
N = ( opt . ms_ratio_to + 1 ) * * 2 * 2
global_displace = coarse_u . reshape ( coarse_nelx + 1 , coarse_nely + 1 , 2 )
# Generate coarse mesh density
global_displace = np . dstack ( ( global_displace [ : , : , 0 ] . T , global_displace [ : , : , 1 ] . T ) )
global_density = global_x . reshape ( nelx , nely ) # -> (nelx , nely)
coarse_displace = np . zeros ( shape = ( coarse_nely * coarse_nelx , 4 , 2 ) )
coarse_density = np . lib . stride_tricks . as_strided (
for ely in range ( coarse_nely ) :
global_density ,
for elx in range ( coarse_nelx ) :
shape = ( coarse_nelx , coarse_nely , m , m ) ,
coarse_displace [ elx + ely ] [ 0 ] = global_displace [ ely , elx , : ]
strides = global_density . itemsize * np . array ( [ nely * m , m , nely , 1 ] )
coarse_displace [ elx + ely ] [ 1 ] = global_displace [ ely , ( elx + 1 ) , : ]
)
coarse_displace [ elx + ely ] [ 2 ] = global_displace [ ( ely + 1 ) , elx , : ]
coarse_displace [ elx + ely ] [ 3 ] = global_displace [ ( ely + 1 ) , ( elx + 1 ) , : ]
# Generate coarse mesh displacement
print ( coarse_displace . shape )
coarse_displace = np . lib . stride_tricks . as_strided (
coarse_u ,
X = np . hstack ( ( coarse_density [ : , : ] , coarse_displace [ : , : , 0 ] , coarse_displace [ : , : , 1 ] ) )
shape = ( coarse_nelx , coarse_nely , 2 , 2 , 2 ) ,
model = torch . load ( model_load_path )
strides = coarse_u . itemsize * np . array ( [ ( coarse_nely + 1 ) * 2 , 1 * 2 , ( coarse_nely + 1 ) * 2 , 1 * 2 , 1 ] )
if standard :
)
X = standardization ( X )
device = f ' cuda: { device } ' if torch . cuda . is_available ( ) else ' cpu '
# data preprocess
X = torch . from_numpy ( X ) . type ( torch . float32 ) . to ( device )
X = np . hstack ( ( coarse_density . reshape ( c_N , m * m ) , coarse_displace . reshape ( c_N , 8 ) ) )
pred = model ( X ) . cpu ( ) . detach ( ) . numpy ( )
if opt . is_standard :
X = standardization ( X )
# print(pred)
X = torch . from_numpy ( X ) . type ( torch . float32 ) . to ( opt . device )
u_reconstructed = np . zeros ( shape = ( coarse_nely * m + 1 , coarse_nelx * m + 1 , 2 ) )
for ely in range ( coarse_nely ) :
# predict
for elx in range ( coarse_nelx ) :
model = torch . load ( opt . pretrained_model_path )
u_reconstructed [ ely * m : ( ely + 1 ) * m + 1 , elx * m : ( elx + 1 ) * m + 1 , : ] = pred [ elx + ely * m ] . reshape ( ( m + 1 , m + 1 , 2 ) )
pred = torch . zeros ( X . shape [ 0 ] , N )
# u_reconstructed = u_reconstructed.reshape(coarse_nely*m+1, coarse_nelx*m+1,2)
for batch_idx , data_batch in enumerate ( X ) :
u_reconstructed = np . dstack ( ( u_reconstructed [ : , : , 0 ] . T , u_reconstructed [ : , : , 1 ] . T ) )
pred_ShapeFunction = model ( data_batch )
u_reconstructed = u_reconstructed . flatten ( )
pred [ batch_idx , : ] = pred_ShapeFunction . reshape ( N , 8 ) @ data_batch [ 25 : ]
pred = pred . to ( ' cpu ' ) . detach ( ) . numpy ( )
return u_reconstructed
pred = Ms_u_reshape ( pred , coarse_nelx , coarse_nely , m )
pred = pred . reshape ( ( nelx + 1 ) * ( nely + 1 ) * 2 , 1 )
return pred
# The real main driver
# The real main driver
if __name__ == " __main__ " :
if __name__ == " __main__ " :
mod_idx = ' test1 '
# Load parmetaers
m = 5
opt = TopoptOption ( ) . parse ( )
nelx = 180
# mod_idx='test1'
nely = 60
# m=5
volfrac = 0.4
# nelx=180
rmin = 5.4
# nely=60
penal = 3.0
# volfrac=0.4
ft = 1 # ft==0 -> sens, ft==1 -> dens
# rmin=5.4
top_EMsFEA ( nelx , nely , volfrac , penal , rmin , ft , mod_idx , m )
# penal=3.0
# ft=1 # ft==0 -> sens, ft==1 -> dens
top_EMsFEA ( opt )
# u=np.load('./results/coarse_u.npy')
# x=np.load('./results/global_x.npy')
# pred_net(u,x,opt)