Homog_hierar.py 7.11 KB
Newer Older
Antoine MARTIN's avatar
Antoine MARTIN committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205


from dolfin import *
import numpy as np
import matplotlib.pyplot as plt
from Reference import reference
from math import *
from random import uniform


def homog_hierar(tabC,scheme,schemebis):

    pp,qq,rr,ss=tabC.shape
    CVoigt = np.array([[0., 0., 0.],
                [0., 0., 0.],
                [0., 0., 0.]])
    for i in range(pp):
        for j in range(pp):
            CVoigt=CVoigt+tabC[i,j]

    CVoigt=CVoigt/pp/pp
    
    p=int(log(pp)/log(2))
    
    
    gamma0=np.array([[[0. for i in range(12)] for j in range(12)] for n in range(p)])
    gamma12=np.array([[[0. for i in range(12)] for j in range(12)] for n in range(p)])
    t0=np.load('influence_tensors-nu=0.0-patch_size=02-max_depth=10.npy')
    t12=np.load('influence_tensors-nu=0.5-patch_size=02-max_depth=10.npy')
    for n in range(p):
        for j in range(4):
            for k in range(3):
                for i in range(4):
                    for l in range(3):
                        gamma0[n,3*i+l,3*j+k]=t0[9-n,i+1,j+1,l,k]
                        gamma12[n,3*i+l,3*j+k]=t12[9-n,i+1,j+1,l,k]
    
    
    
    
    
    def auxiliary_problem(C_list, n,gamma,K,scheme):
        
        M=np.array([[0. for i in range(12)] for j in range(12)])
        
        for i in range(4):
            S=np.linalg.inv(C_list[i]-K)
            for k in range(3):
                for l in range(3):
                    M[i*3+l,3*i+k]=S[l,k]
                    
        gamma_=gamma[n-1]                
        T=np.linalg.inv(M-gamma_)              
        
        Chom_=np.array([[1., 0., 0.],
    [0., 1., 0.],
    [0., 0., 1.]])
        Chom_DEF=np.array([[0.,0.,0.],[0.,0.,0.],[0.,0.,0.]])
        Chom_CONT=np.array([[0.,0.,0.],[0.,0.,0.],[0.,0.,0.]])
        MT_=np.array([[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]])
        T_=np.array([[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]])
        E=np.array([np.array([[0., 0., 0.],
    [0., 0., 0.],
    [0., 0., 0.]]) for i in range(4)])
        B=np.zeros((4,3,3))
        MT=np.dot(M,T)
        for k in range(3):
            for l in range(3):
                for j in range(4):
                    for i in range(4):
                        E[i,l,k]+=MT[3*i+l,3*j+k]
                        T_[l,k]+=0.25*T[3*i+l,3*j+k]
                        MT_[l,k]+=0.25*MT[3*i+l,3*j+k]
        for i in range(4):
            E[i]=np.dot(E[i],np.linalg.inv(MT_))
 
        Chom_=K+np.dot(T_,np.linalg.inv(MT_)) 
        for i in range(4):
            B[i]=np.dot(np.dot(C_list[i],E[i]),np.linalg.inv(Chom_))

        if scheme=='CONT' or scheme=='DEF':
            for i in range(4):       
                Chom_DEF+=0.25*np.dot(np.transpose(E[i]),np.dot(C_list[i],E[i]))
        if scheme=='CONT':
            Chom_CONT=np.dot(Chom_,np.dot(np.linalg.inv(Chom_DEF),np.transpose(Chom_)))
        
        if scheme=='HH':
            return Chom_,E,B
        if scheme=='DEF':
            return Chom_DEF,E,B
        if scheme=='CONT':
            return Chom_CONT,E,B
            
        
    def tC(tC_,tA,tB,n,gamma,K,scheme):
        t1=np.array([[np.zeros((3,3)) for i in range(2**(n-1))] for j in range(2**(n-1))])
        for i in range(2**(n-1)):
            for j in range(2**(n-1)):
                C_list=[tC_[2*i+1,2*j],tC_[2*i+1,2*j+1],tC_[2*i,2*j],tC_[2*i,2*j+1]]
                t1[i,j],Aloc,Bloc=auxiliary_problem(C_list, n,gamma,K,scheme)
                for ii in range(2**(p-n)):
                    for jj in range(2**(p-n)):
                        tA[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj]=np.dot(tA[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj],Aloc[0])
                        tB[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj]=np.dot(tB[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj],Bloc[0])
                for ii in range(2**(p-n)):
                    for jj in range(2**(p-n)):
                        tA[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj+2**(p-n)]=np.dot(tA[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj+2**(p-n)],Aloc[1])
                        tB[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj+2**(p-n)]=np.dot(tB[i*2**(p-n+1)+ii+2**(p-n),j*2**(p-n+1)+jj+2**(p-n)],Bloc[1])
                for ii in range(2**(p-n)):
                    for jj in range(2**(p-n)):
                        tA[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj]=np.dot(tA[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj],Aloc[2])
                        tB[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj]=np.dot(tB[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj],Bloc[2])
                for ii in range(2**(p-n)):
                    for jj in range(2**(p-n)):
                        tA[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj+2**(p-n)]=np.dot(tA[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj+2**(p-n)],Aloc[3])
                        tB[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj+2**(p-n)]=np.dot(tB[i*2**(p-n+1)+ii,j*2**(p-n+1)+jj+2**(p-n)],Bloc[3])
        return t1
        
  
    

    def Chom(tC0,n,K_,scheme):
        # K[2,2]=K[0,0]-K[0,1]
        # mu=np.matrix.trace(np.dot(K,K0))/4
        # km=np.matrix.trace(np.dot(K,J0))/3
        # nu=(3*km-2*mu)/2/(3*km)
        C_11=(K_[0,0]+K_[1,1])/2
        C_12=(K_[0,1]+K_[1,0])/2
        nu=1/(C_11/C_12+1)
        mu=C_11/2*(1-2*nu)/(1-nu)
        C11=2*mu*(1-nu)/(1-2*nu)
        C22=C11
        C12=nu/(1-nu)*C11
        C33=2*mu
        K=np.array([[C11,C12,0.],[C12,C22,0.],[0.,0.,C33]])
        gamma=-((1-2*nu)*gamma0+nu*gamma12)/(1-nu)/mu
        t=tC0        
        tA=np.array([[np.array([[1., 0., 0.],
    [0., 1., 0.],
    [0., 0., 1.]]) for i in range(2**p)] for j in range(2**p)])
        tB=np.array([[np.array([[1.,0.,0.],[0.,1.,0.],[0.,0.,1.]]) for i in range(2**p)] for j in range(2**p)])
        for i in range(n):
            t2=tC(t,tA,tB,n-i,gamma,K,scheme)
            t=t2
        return(K,t[0,0],tA,tB)
       
    
    C_hom1=CVoigt
    err=1.0
    it=1
    C_hom_list=[]
    tA_list=[]
    while err > 1e-2:
        print("itération "+str(it)+'\n')
        C_hom=C_hom1
        C0,C_hom1,tA,tB=Chom(tabC,p,C_hom,scheme)
        if schemebis=='HHbis':
            C_hom2=np.zeros((3,3))
            for i in range(pp):
                for j in range(qq):
                    C_hom2+=np.dot(tabC[i,j],tA[i,j])/pp/qq
        if schemebis=='DEFbis':
            C_hom2=np.zeros((3,3))
            for i in range(pp):
                for j in range(qq):
                    C_hom2+=np.dot(np.transpose(tA[i,j]),np.dot(tabC[i,j],tA[i,j]))/pp/qq
        if schemebis=='CONTbis':
            Scont=np.zeros((3,3))
            for i in range(pp):
                for j in range(qq):
                    Scont+=np.dot(np.transpose(tB[i,j]),np.dot(np.linalg.inv(tabC[i,j]),tB[i,j]))/pp/qq
            C_hom2=np.linalg.inv(Scont)
        C_hom1=C_hom2
        err=abs(C_hom1[0,0]-C_hom[0,0])/C_hom[0,0]
        it+=1
        C_hom_list.append(np.array([C0,C_hom1]))
        tA_list.append(tA)
    print(C_hom1)
    CA=np.zeros((3,3))
    CDEF=np.zeros((3,3))
    SCONT=np.zeros((3,3))
   
    for i in range(pp):
        for j in range(qq):
            CA+=np.dot(tabC[i,j],tA[i,j])/pp/qq
            CDEF+=np.dot(np.transpose(tA[i,j]),np.dot(tabC[i,j],tA[i,j]))/pp/qq
            SCONT+=np.dot(np.transpose(tB[i,j]),np.dot(np.linalg.inv(tabC[i,j]),tB[i,j]))/pp/qq
    CCONT=np.linalg.inv(SCONT)
    
     
    return CA,CDEF,CCONT,tA