├── .gitattributes ├── README.md ├── crack ├── FEM_model │ ├── bmats.m │ ├── bmats1.m │ ├── bmats2.m │ ├── boundary_cond2_v1.m │ ├── boundary_cond2_v2.m │ ├── cart_deriv.m │ ├── dbe.m │ ├── dbe2.m │ ├── fem_frac_v1_2.m │ ├── fem_hole_1c.avi │ ├── fract_1ca.inp │ ├── fract_1hca.inp │ ├── fract_stiff_v1.m │ ├── fract_stiff_v2.m │ ├── gauss.m │ ├── initilize.m │ ├── input_fem.m │ ├── input_fem_elast.m │ ├── jacob2.m │ ├── modps.m │ ├── residual_v1.m │ ├── residual_v2.m │ ├── sfr2.m │ ├── stress_fract_v1.m │ ├── stress_fract_v2.m │ ├── write_vtk_fem.m │ └── 脆性断裂的相场模拟.avi └── crack.pdf ├── sintering ├── .vscode │ └── settings.json ├── free_energ_sint_v1.m ├── sintering.m └── 烧结过程的相场模拟.pdf ├── snowflake ├── Snow_Flake.m ├── matlab_snowflake.pdf ├── nucleus.m └── nucleus_8.m └── spinodal_decomposition └── matlab_spinodal_decomposition.pptx /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # matlab_code 2 | simulate by solving the phase field equations using a centered finite difference method 3 | 4 | the video of matlab lesson 5 | sintering: https://www.bilibili.com/video/BV1VT4y1L7Mx 6 | snowflake: https://www.bilibili.com/video/BV1KK411n7xy 7 | crack: waiting... 8 | -------------------------------------------------------------------------------- /crack/FEM_model/bmats.m: -------------------------------------------------------------------------------- 1 | function [bmatx]=bmats(cartd,shape,nnode) 2 | 3 | ngash=0; 4 | 5 | for inode=1:nnode 6 | 7 | mgash=ngash+1; 8 | ngash=mgash+1; 9 | 10 | bmatx(1,mgash)=cartd(1,inode); 11 | bmatx(1,ngash)=0.0; 12 | bmatx(2,mgash)=0.0; 13 | bmatx(2,ngash)=cartd(2,inode); 14 | bmatx(3,mgash)=cartd(2,inode); 15 | bmatx(3,ngash)=cartd(1,inode); 16 | 17 | end 18 | end 19 | 20 | -------------------------------------------------------------------------------- /crack/FEM_model/bmats1.m: -------------------------------------------------------------------------------- 1 | function [bmatx1]=bmats1(dgdx,nelem,nnode,nstre,nevab,kgasp) 2 | 3 | format long; 4 | 5 | bmatx1 = zeros(nelem,nstre,nevab); 6 | 7 | ngash=0; 8 | 9 | for inode=1:nnode 10 | 11 | bmatx1( :,1,inode)=dgdx( :,kgasp,1,inode); 12 | bmatx1( :,2,inode)=dgdx( :,kgasp,2,inode); 13 | 14 | end 15 | 16 | end %endfunction 17 | 18 | -------------------------------------------------------------------------------- /crack/FEM_model/bmats2.m: -------------------------------------------------------------------------------- 1 | function [bmatx]=bmats2(dgdx,nelem,nnode,nstre,nevab,kgasp) 2 | 3 | format long; 4 | 5 | bmatx = zeros(nelem,nstre,nevab); 6 | 7 | ngash=0; 8 | 9 | for inode=1:nnode 10 | 11 | mgash=ngash+1; 12 | ngash=mgash+1; 13 | 14 | bmatx( :,1,mgash)=dgdx( :,kgasp,1,inode); 15 | bmatx( :,1,ngash)=0.0; 16 | bmatx( :,2,mgash)=0.0; 17 | bmatx( :,2,ngash)=dgdx( :,kgasp,2,inode); 18 | bmatx( :,3,mgash)=dgdx( :,kgasp,2,inode); 19 | bmatx( :,3,ngash)=dgdx( :,kgasp,1,inode); 20 | 21 | end 22 | end %endfunction 23 | -------------------------------------------------------------------------------- /crack/FEM_model/boundary_cond2_v1.m: -------------------------------------------------------------------------------- 1 | function[gstif,gforce,treac] =boundary_cond2_v1(npoin,nvfix,nofix,iffix,fixed,ndofn,facto, ... 2 | gstif,gforce,tdisp) 3 | 4 | 5 | global out1; 6 | 7 | format long; 8 | 9 | ndofn2=2; 10 | ntotv=npoin*ndofn; 11 | 12 | for ivfix=1:nvfix 13 | for idofn=1:ndofn2 14 | treac(ivfix,idofn) =0.0; 15 | end 16 | end 17 | 18 | for ivfix = 1:nvfix 19 | lnode = nofix(ivfix); 20 | 21 | for idofn =1:ndofn2 22 | if(iffix(ivfix,idofn) == 1) 23 | itotv =(lnode - 1)*ndofn2 +idofn; 24 | for jtotv = 1:ntotv 25 | 26 | treac(ivfix,idofn) =treac(ivfix,idofn)- gstif(itotv,jtotv)*tdisp(jtotv); 27 | gstif(itotv,jtotv) = 0.0; 28 | 29 | end 30 | 31 | gstif(itotv,itotv) = 1.0; 32 | gforce(itotv) = fixed(ivfix,idofn)*facto-tdisp(itotv); 33 | end 34 | end 35 | end 36 | 37 | end %endfunction 38 | -------------------------------------------------------------------------------- /crack/FEM_model/boundary_cond2_v2.m: -------------------------------------------------------------------------------- 1 | function[gstif,gforce,treac] =boundary_cond2_v2(npoin,nvfix,nofix,iffix,fixed,ndofn,facto, ... 2 | gstif,gforce,tdisp) 3 | 4 | format long; 5 | 6 | ndofn2=2; 7 | ntotv=npoin*ndofn; 8 | treac = zeros(nvfix,ndofn2); 9 | 10 | for ivfix = 1:nvfix 11 | lnode = nofix(ivfix); 12 | 13 | for idofn =1:ndofn2 14 | if(iffix(ivfix,idofn) == 1) 15 | itotv =(lnode - 1)*ndofn2 +idofn; 16 | 17 | treac(ivfix,idofn) =treac(ivfix,idofn)- gstif(itotv,:)*tdisp(1:ntotv); 18 | 19 | gstif(itotv,: ) = 0.0; 20 | 21 | gstif(itotv,itotv) = 1.0; 22 | gforce(itotv) = fixed(ivfix,idofn)*facto-tdisp(itotv); 23 | 24 | end 25 | end 26 | end 27 | 28 | end %endfunction 29 | -------------------------------------------------------------------------------- /crack/FEM_model/cart_deriv.m: -------------------------------------------------------------------------------- 1 | function [dgdx,dvolum] = cart_deriv(npoin,nelem,nnode,nstre,ndime,ndofn, ... 2 | ngaus,ntype,lnods,coord,posgp,weigp) 3 | 4 | 5 | ngaus2 = ngaus; 6 | if(nnode == 3) 7 | ngaus2 = 1; 8 | end 9 | 10 | mgaus = ngaus*ngaus2; 11 | 12 | dvolum = zeros(nelem,mgaus); 13 | 14 | dgdx = zeros(nelem,mgaus,ndime,nnode); 15 | 16 | for ielem = 1:nelem 17 | for inode=1:nnode 18 | lnode=lnods(ielem,inode); 19 | for idime=1:ndime 20 | elcod(idime,inode)=coord(lnode,idime); 21 | end 22 | end 23 | 24 | %=== gauss points: 25 | 26 | kgasp=0; 27 | for igaus=1:ngaus 28 | exisp=posgp(igaus); 29 | for jgaus=1:ngaus2 30 | etasp =posgp(jgaus); 31 | if(nnode ==3) 32 | etasp=posgp(ngaus+igaus); 33 | end 34 | 35 | 36 | kgasp=kgasp+1; 37 | mgaus=mgaus+1; 38 | 39 | [shape,deriv]=sfr2(exisp,etasp,nnode); 40 | [cartd,djacb,gpcod]=jacob2(ielem,elcod,kgasp,shape,deriv,nnode,ndime); 41 | 42 | dvolu=djacb*weigp(igaus)*weigp(jgaus); 43 | 44 | if(nnode == 3) 45 | dvolu=djacb*weigp(igaus); 46 | end 47 | 48 | dvolum(ielem,kgasp)=dvolu; 49 | 50 | for idime=1:ndime 51 | for inode=1:nnode 52 | dgdx(ielem,kgasp,idime,inode)=cartd(idime,inode); 53 | end 54 | end 55 | 56 | end %igaus 57 | end %jgaus 58 | end % ielem 59 | 60 | end 61 | 62 | %endfunction 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /crack/FEM_model/dbe.m: -------------------------------------------------------------------------------- 1 | function [dbmat] = dbe(nevab,nstre,bmatx,dmatx) 2 | 3 | format long; 4 | 5 | for istre=1:nstre 6 | for ievab=1:nevab 7 | dbmat(istre,ievab)=0.0; 8 | for jstre=1:nstre 9 | dbmat(istre,ievab)=dbmat(istre,ievab)+dmatx(istre,jstre) ... 10 | *bmatx(jstre,ievab); 11 | end 12 | end 13 | end 14 | 15 | end %endfunction 16 | 17 | -------------------------------------------------------------------------------- /crack/FEM_model/dbe2.m: -------------------------------------------------------------------------------- 1 | function [dbmat] = dbe2(nelem,nevab,nstre,bmatx,dmatx) 2 | 3 | %multiply bmatx with dmatx 4 | 5 | format long; 6 | 7 | dbmat = zeros(nelem,nstre,nevab); 8 | 9 | for istre=1:nstre 10 | for ievab=1:nevab 11 | %dbmat( :,istre,ievab)=0.0; 12 | for jstre=1:nstre 13 | dbmat( :,istre,ievab)=dbmat( :,istre,ievab)+dmatx(istre,jstre) ... 14 | *bmatx( :,jstre,ievab); 15 | end 16 | end 17 | end 18 | 19 | end %endfunction 20 | -------------------------------------------------------------------------------- /crack/FEM_model/fem_frac_v1_2.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % % 3 | % FEM PHASE-FIELD CODE FOR FRACTURE % 4 | % % 5 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 6 | 7 | % get initial wall time: 8 | time0=clock(); 9 | 10 | icase =1; 11 | 12 | global in; 13 | if(icase == 1) 14 | in = fopen('fract_1ca.inp','r'); 15 | end 16 | 17 | if(icase == 2) 18 | in = fopen('fract_1hca.inp','r'); 19 | end 20 | 21 | global out; 22 | out = fopen('result_1.out','w'); 23 | 24 | global out2; 25 | out2 = fopen('force-disp','w'); 26 | 27 | %--- input time integration parameters: 28 | 29 | nstep= 4000; 30 | nprnt= 25; 31 | dtime=1.0; 32 | 33 | miter= 5; 34 | toler= 5.0e-3; 35 | 36 | tfacto = 0.0; 37 | dfacto = 0.0005; 38 | 39 | isolve =2; 40 | 41 | % Material spefici parameters: 42 | 43 | constk = 1.0e-6; 44 | cenerg = 0.001; 45 | constl = 0.125; 46 | constn = 2; 47 | coneta = 20.0e5*2; 48 | 49 | %----------- 50 | %input data: 51 | %----------- 52 | 53 | [npoin,nelem,nvfix,ntype,nnode,ndofn,ndime,ngaus, ... 54 | nstre,nmats,nprop,lnods,matno,coord,props,nofix, ... 55 | iffix,fixed]=input_fem_elast(); 56 | 57 | ntotv = npoin*ndofn; 58 | ndofn2 = 2; 59 | ntotv2 = npoin*ndofn2; 60 | 61 | [tdisp,stres,stran] =initilize(nelem,npoin,nnode,ngaus,ndofn,... 62 | nstre,nvfix,isolve,icase); 63 | 64 | [posgp, weigp] =gauss(ngaus,nnode); 65 | 66 | if(isolve == 2) 67 | 68 | [dgdx,dvolum] = cart_deriv(npoin,nelem,nnode,nstre,ndime,ndofn, ... 69 | ngaus,ntype,lnods,coord,posgp,weigp); 70 | end 71 | 72 | %--- time integration 73 | 74 | for istep = 1:nstep 75 | 76 | tfacto = tfacto + dfacto; 77 | 78 | tdisp_old = tdisp; 79 | gforce = zeros(ntotv,1); 80 | 81 | if(isolve == 1) 82 | 83 | [gstif]= fract_stiff_v1(npoin,nelem,nnode,nstre,ndime,ndofn, ... 84 | ngaus,ntype,lnods,coord,props,posgp, ... 85 | weigp,dtime,constk,cenerg,constl,constn, ... 86 | coneta,stres,stran,tdisp,tdisp_old); 87 | 88 | end 89 | % 90 | if(isolve == 2) 91 | 92 | [gstif]= fract_stiff_v2(npoin,nelem,nnode,nstre,ndime,ndofn, ... 93 | ngaus,ntype,lnods,coord,props,posgp, ... 94 | weigp,dgdx,dvolum,dtime,constk,cenerg, ... 95 | constl,constn,coneta,stres,stran,tdisp, ... 96 | tdisp_old); 97 | 98 | end 99 | 100 | %--- newton iteration: 101 | 102 | for iter = 1:miter 103 | 104 | %--- boundary conditions: 105 | 106 | if(isolve == 1) 107 | 108 | [gstif,gforce,treac] =boundary_cond2_v1(npoin,nvfix,nofix,iffix,fixed,ndofn, ... 109 | tfacto,gstif,gforce,tdisp); 110 | end 111 | 112 | if(isolve == 2) 113 | 114 | [gstif,gforce,treac] =boundary_cond2_v2(npoin,nvfix,nofix,iffix,fixed,ndofn,... 115 | tfacto,gstif,gforce,tdisp); 116 | end 117 | 118 | %--- solve: 119 | 120 | asdis = gstif\gforce; 121 | 122 | %--- update: 123 | 124 | tdisp = tdisp + asdis; 125 | 126 | %--- adjust small deviations: 127 | 128 | if(isolve == 1) 129 | for ipoin=1:npoin 130 | itotv = ntotv2 +ipoin; 131 | if(tdisp(itotv) > 0.999) 132 | tdisp(itotv) = 0.999; 133 | end 134 | if(tdisp(itotv) < 0.0) 135 | tdisp(itotv) = 0.0; 136 | end 137 | end 138 | 139 | end 140 | % 141 | if(isolve == 2) 142 | dummy =tdisp(ntotv2+1:ntotv); 143 | 144 | inrange = (dummy > 0.999); 145 | dummy(inrange) =1.0; 146 | inrange = ( dummy < 0.0); 147 | dummy(inrange) = 0.0; 148 | tdisp(ntotv2+1:ntotv) = dummy; 149 | end 150 | 151 | %---calculate stress & strain increments: 152 | 153 | if (isolve == 1) 154 | 155 | [stran,stres] = stress_fract_v1(asdis,nelem,npoin,nnode,ngaus,nstre,props, ... 156 | ntype,ndofn,ndime,lnods,matno,coord,posgp,weigp, ... 157 | tdisp); 158 | end 159 | 160 | if(isolve == 2) 161 | [stran,stres] = stress_fract_v2(asdis,nelem,npoin,nnode,ngaus,nstre,props, ... 162 | ntype,ndofn,ndime,lnods,matno,coord,posgp,weigp, ... 163 | dgdx,dvolum,tdisp); 164 | end 165 | 166 | %--- check norm for convergence: 167 | 168 | normF = norm(gforce,2); 169 | 170 | if(normF <= toler) 171 | break; 172 | end 173 | 174 | %--- calculate residual force vector: 175 | 176 | if(isolve == 1) 177 | 178 | [gforce]= residual_v1(npoin,nelem,nnode,nstre,ndime,ndofn, ... 179 | ngaus,ntype,lnods,coord,props,posgp, ... 180 | weigp,dtime,constk,cenerg,constl,constn, ... 181 | coneta,stres,stran,tdisp,tdisp_old); 182 | end 183 | 184 | if(isolve ==2 ) 185 | 186 | [gforce]= residual_v2(npoin,nelem,nnode,nstre,ndime,ndofn, ... 187 | ngaus,ntype,lnods,coord,props,posgp, ... 188 | weigp,dgdx,dvolum,dtime,constk,cenerg, ... 189 | constl,constn,coneta,stres,stran,tdisp, ... 190 | tdisp_old); 191 | end 192 | 193 | end %end of Newton 194 | 195 | %--- print data for force-disp curves: 196 | 197 | lnode = nofix(nvfix); 198 | 199 | nvfix2 =nvfix/2; 200 | 201 | sumr = 0.0; 202 | for ivfix=1:nvfix2 203 | sumr = sumr +treac(ivfix,2); 204 | end 205 | 206 | fprintf(out2,'%14.6e %14.6e\n',tdisp((lnode-1)*2+2),sumr); 207 | 208 | %--- print results: 209 | 210 | if(mod(istep,nprnt) == 0 ) 211 | 212 | fprintf('Done step: %5d\n',istep); 213 | 214 | %fname=sprintf('time_%d.out',istep); 215 | %out=fopen(fname,'w'); 216 | %ntotv2 =npoin*ndofn2; 217 | %for ipoin = 1:npoin 218 | %itotv =ntotv2 + ipoin; 219 | %fprintf(out,'%14.6e %14.6e %14.6e\n',coord(ipoin,1), coord(ipoin,2),tdisp(itotv)); 220 | %end 221 | %fclose(out); 222 | 223 | %--- write to vtk file with updated mesh 224 | 225 | for ipoin=1:npoin 226 | for idofn=1:ndofn2 227 | itotv =(ipoin-1)*ndofn2+idofn; 228 | cord2(ipoin,idofn) =coord(ipoin,idofn) + 10.0*tdisp(itotv); 229 | jtotv=ntotv2+ipoin; 230 | cont1(ipoin)=tdisp(jtotv); 231 | end 232 | end 233 | 234 | write_vtk_fem(npoin,nelem,nnode,lnods,cord2,istep,cont1); 235 | 236 | end %if 237 | 238 | end %istep 239 | 240 | compute_time = etime(clock(),time0) 241 | 242 | fprintf(out,'compute time: %7d\n',compute_time); 243 | 244 | -------------------------------------------------------------------------------- /crack/FEM_model/fem_hole_1c.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kipa200/Phase_field_code/98ccc5daa7520cb21529b88be73a3c3275be0380/crack/FEM_model/fem_hole_1c.avi -------------------------------------------------------------------------------- /crack/FEM_model/fract_stiff_v1.m: -------------------------------------------------------------------------------- 1 | function [gstif]= fract_stiff_v1(npoin,nelem,nnode,nstre,ndime,ndofn, ... 2 | ngaus,ntype,lnods,coord,props,posgp, ... 3 | weigp,dtime,constk,cenerg,constl,constn, ... 4 | coneta,stres,stran,tdisp,tdisp_old) 5 | 6 | format long; 7 | 8 | %--- order of integration 9 | 10 | ngaus2=ngaus; 11 | if(nnode == 3) 12 | ngaus2=1; 13 | end 14 | 15 | mgaus = ngaus*ngaus2; 16 | 17 | %--initialize global and local stiffness: 18 | 19 | ntotv = npoin*ndofn; 20 | nevab = nnode*ndofn; 21 | ndofn2 = ndofn-1; 22 | ndofn3 = ndofn-2; 23 | ntotv2 = npoin*ndofn2; 24 | nevab2 = nnode*ndofn2; 25 | nevab3 = nnode*ndofn3; 26 | 27 | %---global stiffness: 28 | 29 | gstif = sparse(ntotv,ntotv); 30 | 31 | for ielem=1:nelem 32 | 33 | %--initialize element stiffnesses & rhs: 34 | 35 | for ievab=1:nevab 36 | eload(ievab) = 0.0; 37 | for jevab=1:nevab 38 | estif(ievab,jevab) = 0.0; 39 | end 40 | end 41 | 42 | for ievab=1:nevab2 43 | eload1(ievab) = 0.0; 44 | for jevab=1:nevab2 45 | estif1(ievab,jevab) = 0.0; 46 | end 47 | end 48 | 49 | for ievab=1:nevab2 50 | for jevab=1:nevab3 51 | estif2(ievab,jevab) = 0.0; 52 | estif3(jevab,ievab) = 0.0; 53 | end 54 | end 55 | 56 | for ievab=1:nevab3 57 | eload2(ievab) = 0.0; 58 | for jevab=1:nevab3 59 | estif4(ievab,jevab) = 0.0;; 60 | end 61 | end 62 | 63 | %--- 64 | %--- element nodal values 65 | %-- 66 | 67 | for inode =1:nnode 68 | lnode = lnods(ielem,inode); 69 | itotv = ntotv2 +lnode; 70 | ephi(inode) = tdisp(itotv); 71 | ephir(inode) = tdisp(itotv)-tdisp_old(itotv); 72 | end 73 | 74 | %--- Coordinates of element nodes: 75 | 76 | for inode=1:nnode 77 | lnode=lnods(ielem,inode); 78 | for idime=1:ndime 79 | elcod(idime,inode)=coord(lnode,idime); 80 | end 81 | end 82 | 83 | %--- integrate element stiffness 84 | 85 | kgasp=0; 86 | for igaus=1:ngaus 87 | exisp=posgp(igaus); 88 | for jgaus=1:ngaus2 89 | etasp =posgp(jgaus); 90 | if(nnode ==3) 91 | etasp=posgp(ngaus+igaus); 92 | end 93 | 94 | kgasp=kgasp+1; 95 | [shape,deriv]=sfr2(exisp,etasp,nnode); 96 | 97 | [cartd,djacb,gpcod]=jacob2(ielem,elcod,kgasp,shape,deriv,nnode,ndime); 98 | 99 | dvolu=djacb*weigp(igaus)*weigp(jgaus); 100 | 101 | if(nnode == 3) 102 | dvolu=djacb*weigp(igaus); 103 | end 104 | 105 | %-- values at the integration points: 106 | 107 | phigp =0.0; 108 | phirgp = 0.0; 109 | 110 | for inode=1:nnode 111 | phigp = phigp + ephi(inode)*shape(inode); 112 | phirgp = phirgp + ephir(inode)*shape(inode); 113 | end 114 | 115 | 116 | mtype = 1; 117 | [dmatx] = modps(mtype,ntype,nstre,props); 118 | 119 | %--- 120 | 121 | [bmatx]=bmats(cartd,shape,inode); 122 | 123 | [dbmat] = dbe(nevab2,nstre,bmatx,dmatx); 124 | 125 | %element stiffness: 126 | 127 | %--- estif1: 128 | 129 | for ievab=1:nevab2 130 | for jevab=1:nevab2 131 | for istre=1:nstre 132 | 133 | estif1(ievab,jevab)=estif1(ievab,jevab)+((1.0-phigp)^2 +constk)* ... 134 | bmatx(istre,ievab)*dbmat(istre,jevab)*dvolu; 135 | 136 | end 137 | end 138 | end 139 | 140 | %---estif2: 141 | 142 | for ievab=1:nevab2 143 | dummy(ievab) =0.0; 144 | end 145 | 146 | for istre =1:nstre 147 | for inode =1:nnode 148 | dummy(istre,inode) =stres(ielem,kgasp,istre)*shape(inode); 149 | end 150 | end 151 | 152 | for ievab=1:nevab2 153 | for jevab=1:nevab3 154 | for istre=1:nstre 155 | 156 | estif2(ievab,jevab) = estif2(ievab,jevab)-2.0*(1.0-phigp)*bmatx(istre,ievab)* ... 157 | dummy(istre,jevab)*dvolu; 158 | 159 | end 160 | end 161 | end 162 | 163 | %--- estif4 164 | 165 | for ievab=1:nevab3 166 | for jevab=1:nevab3 167 | for istre=1:ndime 168 | estif4(ievab,jevab) = estif4(ievab,jevab) + cenerg*constl*cartd(istre,ievab)* ... 169 | cartd(istre,jevab)*dvolu; 170 | end 171 | end 172 | end 173 | 174 | 175 | %--- strain energ 176 | 177 | senerg = 0.0; 178 | for istre=1:nstre 179 | senerg = senerg + 0.5*stres(ielem,kgasp,istre) * stran(ielem,kgasp,istre); 180 | end 181 | 182 | for inode=1:nnode 183 | for jnode=1:nnode 184 | estif4(inode,jnode) = estif4(inode,jnode) +((cenerg/constl) + 2.0*senerg)* ... 185 | shape(inode)*shape(jnode)*dvolu; 186 | 187 | end 188 | end 189 | 190 | %--- penalty term: 191 | 192 | constx = 0.0; 193 | if(phirgp < 0.0 ) 194 | constx = -phirgp; 195 | end 196 | 197 | for inode=1:nnode 198 | for jnode=1:nnode 199 | estif4(inode,jnode) = estif4(inode,jnode) +(coneta/dtime)*constx^(constn-1)* ... 200 | shape(inode)*shape(jnode)*dvolu; 201 | end 202 | end 203 | 204 | end %jgaus 205 | end %igaus 206 | 207 | %--- global stiffness matrix: 208 | 209 | %--- assemble estif1: 210 | 211 | for inode =1:nnode 212 | lnode = lnods(ielem,inode); 213 | for idofn =1:ndofn2 214 | ievab =(inode-1)*ndofn2+idofn; 215 | itotv =(lnode-1)*ndofn2+idofn; 216 | % 217 | for jnode =1:nnode 218 | knode = lnods(ielem,jnode); 219 | for jdofn =1:ndofn2 220 | jevab =(jnode-1)*ndofn2+jdofn; 221 | jtotv =(knode-1)*ndofn2+jdofn; 222 | 223 | gstif = gstif +sparse(itotv,jtotv,estif1(ievab,jevab),ntotv,ntotv); 224 | 225 | end 226 | end 227 | end 228 | end 229 | 230 | % assemble estif2 : 231 | 232 | for inode =1:nnode 233 | lnode = lnods(ielem,inode); 234 | for idofn =1:ndofn2 235 | ievab =(inode-1)*ndofn2+idofn; 236 | itotv =(lnode-1)*ndofn2+idofn; 237 | % 238 | for jnode =1:nnode 239 | knode = lnods(ielem,jnode); 240 | for jdofn =1:ndofn3 241 | jevab =(jnode-1)*ndofn3+jdofn; 242 | jtotv =(knode-1)*ndofn3+jdofn+ntotv2; 243 | 244 | gstif = gstif +sparse(itotv,jtotv,estif2(ievab,jevab),ntotv,ntotv); 245 | 246 | end 247 | end 248 | end 249 | end 250 | 251 | % assembe estif 3 as transpose of estif2: 252 | 253 | for inode =1:nnode 254 | lnode = lnods(ielem,inode); 255 | for idofn =1:ndofn3 256 | ievab =(inode-1)*ndofn3+idofn; 257 | itotv =(lnode-1)*ndofn3+idofn+ntotv2; 258 | % 259 | for jnode =1:nnode 260 | knode = lnods(ielem,jnode); 261 | for jdofn =1:ndofn2 262 | jevab =(jnode-1)*ndofn2+jdofn; 263 | jtotv =(knode-1)*ndofn2+jdofn; 264 | 265 | gstif = gstif +sparse(itotv,jtotv,estif2(jevab,ievab),ntotv,ntotv); 266 | 267 | end 268 | end 269 | end 270 | end 271 | 272 | % assemble estif4: 273 | 274 | for inode =1:nnode 275 | lnode = lnods(ielem,inode); 276 | for idofn =1:ndofn3 277 | ievab =(inode-1)*ndofn3+idofn; 278 | itotv =(lnode-1)*ndofn3+idofn+ntotv2; 279 | % 280 | for jnode =1:nnode 281 | knode = lnods(ielem,jnode); 282 | for jdofn =1:ndofn3 283 | jevab =(jnode-1)*ndofn3+jdofn; 284 | jtotv =(knode-1)*ndofn3+jdofn+ntotv2; 285 | 286 | gstif = gstif +sparse(itotv,jtotv,estif4(ievab,jevab),ntotv,ntotv); 287 | 288 | end 289 | end 290 | end 291 | end 292 | 293 | end %ielem 294 | 295 | end %endfunction 296 | 297 | 298 | 299 | 300 | 301 | -------------------------------------------------------------------------------- /crack/FEM_model/fract_stiff_v2.m: -------------------------------------------------------------------------------- 1 | function [gstif]= fract_stiff_v2(npoin,nelem,nnode,nstre,ndime,ndofn, ... 2 | ngaus,ntype,lnods,coord,props,posgp, ... 3 | weigp,dgdx,dvolum,dtime,constk,cenerg, ... 4 | constl,constn,coneta,stres,stran,tdisp, ... 5 | tdisp_old) 6 | 7 | format long; 8 | 9 | %--- order of integration 10 | 11 | ngaus2=ngaus; 12 | if(nnode == 3) 13 | ngaus2=1; 14 | end 15 | 16 | mgaus = ngaus*ngaus2; 17 | 18 | %--initialize global and local stiffness: 19 | 20 | ntotv = npoin*ndofn; 21 | nevab = nnode*ndofn; 22 | ndofn2 = ndofn-1; 23 | ndofn3 = ndofn-2; 24 | ntotv2 = npoin*ndofn2; 25 | nevab2 = nnode*ndofn2; 26 | nevab3 = nnode*ndofn3; 27 | 28 | %---global stiffness: 29 | 30 | gstif = sparse(ntotv,ntotv); 31 | 32 | %--- element stiffnesses 33 | 34 | estif = zeros(nelem,nevab,nevab); 35 | estif1 = zeros(nelem,nevab2,nevab2); 36 | estif2 = zeros(nelem,nevab2,nevab3); 37 | estif3 = zeros(nelem,nevab3,nevab2); 38 | estif4 = zeros(nelem,nevab3,nevab3); 39 | 40 | eload = zeros(nelem,nevab); 41 | eload1 = zeros(nelem,nevab2); 42 | eload2 = zeros(nelem,nevab3); 43 | 44 | %--- 45 | %--- element nodal values 46 | %-- 47 | 48 | ephi = zeros(nelem,nnode); 49 | ephir = zeros(nelem,nnode); 50 | 51 | for inode =1:nnode 52 | lnode = lnods( :,inode); 53 | itotv = ntotv2 +lnode; 54 | ephi( :,inode) = tdisp(itotv); 55 | ephir( :,inode) = tdisp(itotv)-tdisp_old(itotv); 56 | end 57 | 58 | %--- integrate element stiffness 59 | 60 | kgasp=0; 61 | for igaus=1:ngaus 62 | exisp=posgp(igaus); 63 | for jgaus=1:ngaus2 64 | etasp =posgp(jgaus); 65 | if(nnode ==3) 66 | etasp=posgp(ngaus+igaus); 67 | end 68 | 69 | kgasp=kgasp+1; 70 | [shape,deriv]=sfr2(exisp,etasp,nnode); 71 | 72 | %-- values at the integration points: 73 | 74 | phigp =zeros(nelem,1); 75 | phirgp =zeros(nelem,1); 76 | 77 | for inode=1:nnode 78 | phigp = phigp + ephi( :,inode)*shape(inode); 79 | phirgp = phirgp + ephir( :,inode)*shape(inode); 80 | end 81 | 82 | mtype = 1; 83 | [dmatx] = modps(mtype,ntype,nstre,props); 84 | 85 | %--- cartesien derivative matrices; 86 | 87 | [bmatx1]=bmats1(dgdx,nelem,nnode,nstre,nevab2,kgasp); 88 | [bmatx2]=bmats2(dgdx,nelem,nnode,nstre,nevab2,kgasp); 89 | 90 | [dbmat] = dbe2(nelem,nevab2,nstre,bmatx2,dmatx); 91 | 92 | %element stiffness: 93 | 94 | %--- estif1: 95 | 96 | for ievab=1:nevab2 97 | for jevab=1:nevab2 98 | for istre=1:nstre 99 | 100 | estif1( :,ievab,jevab)=estif1( :,ievab,jevab)+((1.0-phigp).^2 +constk).* ... 101 | bmatx2( :,istre,ievab).*dbmat( :,istre,jevab).*dvolum(:,kgasp); 102 | 103 | end 104 | end 105 | end 106 | 107 | %---estif2: 108 | 109 | dummy = zeros(nelem,nevab2); 110 | 111 | for istre =1:nstre 112 | for inode =1:nnode 113 | dummy( :,istre,inode) =stres( :,kgasp,istre)*shape(inode); 114 | end 115 | end 116 | 117 | for ievab=1:nevab2 118 | for jevab=1:nevab3 119 | for istre=1:nstre 120 | 121 | estif2( :,ievab,jevab) = estif2( :,ievab,jevab)-2.0*(1.0-phigp).*bmatx2( :,istre,ievab).* ... 122 | dummy( :,istre,jevab).*dvolum( :,kgasp); 123 | 124 | end 125 | end 126 | end 127 | 128 | %--- estif4 129 | 130 | for ievab=1:nevab3 131 | for jevab=1:nevab3 132 | for istre=1:ndime 133 | estif4( :,ievab,jevab) = estif4( :,ievab,jevab) + cenerg*constl*bmatx1( :,istre,ievab).* ... 134 | bmatx1( :,istre,jevab).*dvolum( :,kgasp); 135 | end 136 | end 137 | end 138 | 139 | %--- strain energ 140 | 141 | senerg =zeros(nelem,1); 142 | for istre=1:nstre 143 | senerg = senerg + 0.5*stres( :,kgasp,istre) .* stran( :,kgasp,istre); 144 | end 145 | 146 | for inode=1:nnode 147 | for jnode=1:nnode 148 | estif4( :,inode,jnode) = estif4( :,inode,jnode) +((cenerg/constl) + 2.0*senerg).* ... 149 | shape(inode)*shape(jnode).*dvolum( :,kgasp); 150 | 151 | end 152 | end 153 | 154 | 155 | %--- penalty term: 156 | 157 | constx = zeros(nelem,1); 158 | inrange = (phirgp < 0 ); 159 | constx(inrange) = -phirgp(inrange); 160 | 161 | 162 | for inode=1:nnode 163 | for jnode=1:nnode 164 | estif4( :,inode,jnode) = estif4( :,inode,jnode) +(coneta/dtime)*constx.^(constn-1)* ... 165 | shape(inode)*shape(jnode).*dvolum( :,kgasp); 166 | end 167 | end 168 | 169 | end %jgaus 170 | end %igaus 171 | 172 | %--- global stiffness matrix 173 | 174 | %--- assemble estif1: 175 | 176 | for inode =1:nnode 177 | lnode = lnods( :,inode); 178 | for idofn =1:ndofn2 179 | ievab =(inode-1)*ndofn2+idofn; 180 | itotv =(lnode-1)*ndofn2+idofn; 181 | % 182 | for jnode =1:nnode 183 | knode = lnods( :,jnode); 184 | for jdofn =1:ndofn2 185 | jevab =(jnode-1)*ndofn2+jdofn; 186 | jtotv =(knode-1)*ndofn2+jdofn; 187 | 188 | gstif = gstif +sparse(itotv,jtotv,estif1( :,ievab,jevab),ntotv,ntotv); 189 | 190 | end 191 | end 192 | end 193 | end 194 | 195 | % assemble estif2 : 196 | 197 | for inode =1:nnode 198 | lnode = lnods( :,inode); 199 | for idofn =1:ndofn2 200 | ievab =(inode-1)*ndofn2+idofn; 201 | itotv =(lnode-1)*ndofn2+idofn; 202 | % 203 | for jnode =1:nnode 204 | knode = lnods( :,jnode); 205 | for jdofn =1:ndofn3 206 | jevab =(jnode-1)*ndofn3+jdofn; 207 | jtotv =(knode-1)*ndofn3+jdofn+ntotv2; 208 | 209 | gstif = gstif +sparse(itotv,jtotv,estif2( :,ievab,jevab),ntotv,ntotv); 210 | 211 | end 212 | end 213 | end 214 | end 215 | 216 | % assembe estif 3 as transpose of estif2: 217 | 218 | for inode =1:nnode 219 | lnode = lnods( :,inode); 220 | for idofn =1:ndofn3 221 | ievab =(inode-1)*ndofn3+idofn; 222 | itotv =(lnode-1)*ndofn3+idofn+ntotv2; 223 | % 224 | for jnode =1:nnode 225 | knode = lnods( :,jnode); 226 | for jdofn =1:ndofn2 227 | jevab =(jnode-1)*ndofn2+jdofn; 228 | jtotv =(knode-1)*ndofn2+jdofn; 229 | 230 | gstif = gstif +sparse(itotv,jtotv,estif2( :,jevab,ievab),ntotv,ntotv); 231 | 232 | end 233 | end 234 | end 235 | end 236 | 237 | % assemble estif4: 238 | 239 | for inode =1:nnode 240 | lnode = lnods( :,inode); 241 | for idofn =1:ndofn3 242 | ievab =(inode-1)*ndofn3+idofn; 243 | itotv =(lnode-1)*ndofn3+idofn+ntotv2; 244 | % 245 | for jnode =1:nnode 246 | knode = lnods( :,jnode); 247 | for jdofn =1:ndofn3 248 | jevab =(jnode-1)*ndofn3+jdofn; 249 | jtotv =(knode-1)*ndofn3+jdofn+ntotv2; 250 | 251 | gstif = gstif +sparse(itotv,jtotv,estif4( :,ievab,jevab),ntotv,ntotv); 252 | 253 | end 254 | end 255 | end 256 | end 257 | 258 | end %endfunction 259 | 260 | 261 | 262 | 263 | 264 | -------------------------------------------------------------------------------- /crack/FEM_model/gauss.m: -------------------------------------------------------------------------------- 1 | function [posgp, weigp] =gauss(ngaus,nnode) 2 | 3 | format long; 4 | 5 | if(nnode == 3) 6 | 7 | if(ngaus == 1) 8 | posgp(1)=1.0/3.0; 9 | posgp(2)=1.0/3.0; 10 | weigp(1)=0.5; 11 | end 12 | 13 | if(ngaus == 3) 14 | posgp(1)=0.5; 15 | posgp(2)=0.5; 16 | posgp(3)=0.0; 17 | % 18 | posgp(4)=0.0; 19 | posgp(5)=0.5; 20 | posgp(6)=0.5; 21 | 22 | weigp(1)=1.0/6.0; 23 | weigp(2)=1.0/6.0; 24 | weigp(3)=1.0/6.0; 25 | end 26 | 27 | if(ngaus == 7) 28 | posgp(1)=0.0; 29 | posgp(2)=0.5; 30 | posgp(3)=1.0; 31 | posgp(4)=0.5; 32 | posgp(5)=0.0; 33 | posgp(6)=0.0; 34 | posgp(7)=1.0/3.0; 35 | 36 | posgp(8)=0.0; 37 | posgp(9)=0.0; 38 | posgp(10)=0.0; 39 | posgp(11)=0.5; 40 | posgp(12)=1.0; 41 | posgp(13)=0.5; 42 | posgp(14)=1.0/3.0; 43 | 44 | weigp(1)=1.0/40.0; 45 | weigp(2)=1.0/15.0; 46 | weigp(3)=1.0/40.0; 47 | weigp(4)=1.0/15.0; 48 | weigp(5)=1.0/40.0; 49 | weigp(6)=1.0/15.0; 50 | weigp(7)=9.0/40.0 51 | end 52 | 53 | end 54 | 55 | 56 | 57 | if (nnode ~=3) 58 | 59 | if(ngaus == 2) 60 | posgp(1)=-0.57735026918963; 61 | weigp(1)=1.0; 62 | end 63 | if(ngaus > 2) 64 | posgp(1)=-0.7745966241483; 65 | posgp(2)=0.0; 66 | weigp(1)=0.55555555555556; 67 | weigp(2)=0.88888888888889; 68 | end 69 | 70 | kgaus=ngaus/2; 71 | for igash=1:kgaus 72 | jgash=ngaus+1-igash; 73 | posgp(jgash)=-posgp(igash); 74 | weigp(jgash)=weigp(igash); 75 | end 76 | end 77 | 78 | end %endfunction 79 | -------------------------------------------------------------------------------- /crack/FEM_model/initilize.m: -------------------------------------------------------------------------------- 1 | function[tdisp,stres,stran] =initilize(nelem,npoin,nnode,ngaus,ndofn,... 2 | nstre,nvfix,isolve,icase) 3 | 4 | format long; 5 | 6 | ndofn2 = ndofn-1; 7 | ntotv2 = npoin*ndofn2; 8 | ntotv = npoin*ndofn; 9 | 10 | ngaus2 = ngaus; 11 | if(nnode == 3) 12 | ngaus2=1; 13 | end 14 | mgaus = ngaus*ngaus2; 15 | 16 | %-- initialize stress and strain: 17 | 18 | if(isolve == 1) 19 | 20 | for ielem =1:nelem 21 | for igaus=1:mgaus 22 | for istre=1:nstre 23 | 24 | stres(ielem,igaus,istre) = 0.0; 25 | stran(ielem,igaus,istre) = 0.0; 26 | end 27 | end 28 | end 29 | 30 | for itotv=1:ntotv 31 | tdisp(itotv,1)=0.0; 32 | end 33 | 34 | end %if 35 | 36 | if(isolve == 2) 37 | 38 | stres = zeros(nelem,mgaus,nstre); 39 | stran = zeros(nelem,mgaus,nstre); 40 | 41 | tdisp =zeros(ntotv,1); 42 | 43 | end %if 44 | 45 | if(icase == 1) 46 | 47 | ncrack =30; 48 | crack(1) = 2010; 49 | crack(2) = 2011; 50 | crack(3) = 2012; 51 | crack(4) = 2013; 52 | crack(5) = 2014; 53 | crack(6) = 2015; 54 | crack(7) = 2016; 55 | crack(8) = 2017; 56 | crack(9) = 2018; 57 | crack(10) = 2019; 58 | crack(11) = 2020; 59 | crack(12) = 2021; 60 | crack(13) = 2022; 61 | crack(14) = 2023; 62 | crack(15) = 2024; 63 | % 64 | crack(16) = 2051; 65 | crack(17) = 2052; 66 | crack(18) = 2053; 67 | crack(19) = 2054; 68 | crack(20) = 2055; 69 | crack(21) = 2056; 70 | crack(22) = 2057; 71 | crack(23) = 2058; 72 | crack(24) = 2059; 73 | crack(25) = 2060; 74 | crack(26) = 2061; 75 | crack(27) = 2062; 76 | crack(28) = 2063; 77 | crack(29) = 2064; 78 | crack(30) = 2065; 79 | 80 | for icrack=1:ncrack 81 | lnode = crack(icrack); 82 | itotv=ntotv2+lnode; 83 | tdisp(itotv) = 0.99; 84 | end 85 | 86 | end 87 | 88 | if(icase == 2) 89 | 90 | ncrack =26; 91 | crack(1) = 291; 92 | crack(2) = 292; 93 | crack(3) = 296; 94 | crack(4) = 297; 95 | crack(5) = 1553; 96 | crack(6) = 1554; 97 | crack(7) = 1555; 98 | crack(8) = 1556; 99 | crack(9) = 1557; 100 | crack(10) = 1558; 101 | crack(11) = 1559; 102 | crack(12) = 1560; 103 | crack(13) = 1561; 104 | crack(14) = 1562; 105 | crack(15) = 1563; 106 | crack(16) = 1568; 107 | crack(17) = 1569; 108 | crack(18) = 1570; 109 | crack(19) = 1571; 110 | crack(20) = 1572; 111 | crack(21) = 1573; 112 | crack(22) = 1574; 113 | crack(23) = 1575; 114 | crack(24) = 1576; 115 | crack(25) = 1577; 116 | crack(26) = 1578; 117 | 118 | for icrack=1:ncrack 119 | lnode = crack(icrack); 120 | itotv=ntotv2+lnode; 121 | tdisp(itotv) = 0.99; 122 | end 123 | 124 | end 125 | 126 | 127 | end %endfunction 128 | 129 | -------------------------------------------------------------------------------- /crack/FEM_model/input_fem.m: -------------------------------------------------------------------------------- 1 | function [npoin,nelem,nvfix,ntype,nnode,ndofn,ndime,ngaus, ... 2 | nstre,nmats,nprop,lnods,matno,coord,props,nofix, ... 3 | iffix,fixed]=input_fem() 4 | 5 | global in; 6 | global out; 7 | 8 | 9 | %read the input data: 10 | 11 | npoin=fscanf(in,"%d",1); 12 | nelem=fscanf(in,"%d",1); 13 | nvfix=fscanf(in,"%d",1); 14 | ntype=fscanf(in,"%d",1); 15 | nnode=fscanf(in,"%d",1); 16 | ndofn=fscanf(in,"%d",1); 17 | ndime=fscanf(in,"%d",1); 18 | ngaus=fscanf(in,"%d",1); 19 | nstre=fscanf(in,"%d",1); 20 | nmats=fscanf(in,"%d",1); 21 | nprop=fscanf(in,"%d",1); 22 | 23 | 24 | %read the element node numbers & material property number 25 | 26 | for ielem=1:nelem 27 | jelem=fscanf(in,"%d",1); 28 | dummy=fscanf(in,"%d",[nnode+1,1]); 29 | for inode=1:nnode 30 | lnods(jelem,inode)=dummy(inode); 31 | end 32 | matno(jelem)=dummy(nnode+1); 33 | end 34 | 35 | %read nodal coordinates: 36 | 37 | for ipoin=1:npoin 38 | jpoin=fscanf(in,"%d",1); 39 | dummy=fscanf(in,"%lf %lf",[2,1]); 40 | for idime=1:ndime 41 | coord(ipoin,idime)=dummy(idime); 42 | end 43 | end 44 | 45 | %Read constraint nodes and their values: 46 | 47 | for ivfix=1:nvfix 48 | nofix(ivfix)=fscanf(in,"%d",1); 49 | dummy1=fscanf(in,"%d %d",[2,1]); 50 | dummy2=fscanf(in,"%lf %lf",[2,1]); 51 | for idime=1:ndime 52 | iffix(ivfix,idime)=dummy1(idime); 53 | fixed(ivfix,idime)=dummy2(idime); 54 | end 55 | end 56 | 57 | %read Material properties: 58 | for imats=1:nmats 59 | jmats=fscanf(in,"%d",1); 60 | dummy=fscanf(in,"%lf %lf",[2,1]); 61 | for iprop=1:nprop 62 | props(jmats,iprop)=dummy(iprop); 63 | end 64 | end 65 | 66 | 67 | %%printout: 68 | 69 | fprintf(out,"************************ Results *****************************\n"); 70 | 71 | fprintf(out,"Number of Elements : %5d\n",nelem); 72 | fprintf(out,"Number of Node : %5d\n",npoin); 73 | fprintf(out,"Number of Fixed nodes : %5d\n",nvfix); 74 | fprintf(out,"Number of Nodes per element : %5d\n",nnode); 75 | fprintf(out,"Number of Integration points: %5d\n",ngaus); 76 | fprintf(out,"Number of Materials : %5d\n",nmats); 77 | fprintf(out,"Number of properties : %5d\n",nprop); 78 | 79 | if(ntype == 1) 80 | fprintf(out,"Plane-strain elasticity solution\n"); 81 | end 82 | if(ntype == 2) 83 | fprintf(out,"Plane-stress elasticity solution\n"); 84 | end 85 | 86 | fprintf(out,"Element connectivity\n"); 87 | fprintf(out,"Element No Node numbers Property Id\n"); 88 | 89 | for ielem=1:nelem 90 | 91 | fprintf(out,"%5d",ielem); 92 | for inode=1:nnode 93 | fprintf(out,"%5d",lnods(ielem,inode)); 94 | end 95 | fprintf(out,"%5d\n",matno(ielem)); 96 | end 97 | 98 | fprintf(out,"Nodal Coordinates\n"); 99 | fprintf(out," Node Number X-cord Y-cord\n"); 100 | for ipoin=1:npoin 101 | fprintf(out,"%5d %14.6e %14.6e\n", ipoin,coord(ipoin,1),coord(ipoin,2)); 102 | end 103 | 104 | fprintf(out,"Fixed Nodes Fixed DOFs Values\n"); 105 | 106 | for ivfix=1:nvfix 107 | 108 | fprintf(out,"%5d %5d %5d %14.6e %14.6e\n",nofix(ivfix),iffix(ivfix,1),iffix(ivfix,2), ... 109 | fixed(ivfix,1),fixed(ivfix,2)); 110 | end 111 | 112 | fprintf(out,"Material Properties\n") 113 | for imats=1:nmats 114 | fprintf(out," %d %14.6e %14.6e\n", props(imats,1),props(imats,2)); 115 | end 116 | 117 | 118 | 119 | 120 | 121 | end %end function 122 | -------------------------------------------------------------------------------- /crack/FEM_model/input_fem_elast.m: -------------------------------------------------------------------------------- 1 | function [npoin,nelem,nvfix,ntype,nnode,ndofn,ndime,ngaus, ... 2 | nstre,nmats,nprop,lnods,matno,coord,props,nofix, ... 3 | iffix,fixed]=input_fem_elast( ) 4 | 5 | format long; 6 | 7 | global in; 8 | global out; 9 | 10 | %--- read the Control data: 11 | 12 | npoin=fscanf(in,'%d',1); 13 | nelem=fscanf(in,'%d',1); 14 | nvfix=fscanf(in,'%d',1); 15 | ntype=fscanf(in,'%d',1); 16 | nnode=fscanf(in,'%d',1); 17 | ndofn=fscanf(in,'%d',1); 18 | ndime=fscanf(in,'%d',1); 19 | ngaus=fscanf(in,'%d',1); 20 | nstre=fscanf(in,'%d',1); 21 | nmats=fscanf(in,'%d',1); 22 | nprop=fscanf(in,'%d',1); 23 | 24 | 25 | %--- Element node numbers & material property number 26 | 27 | for ielem=1:nelem 28 | jelem=fscanf(in,'%d',1); 29 | dummy=fscanf(in,'%d',[nnode+1,1]); 30 | for inode=1:nnode 31 | lnods(jelem,inode)=dummy(inode); 32 | end 33 | matno(jelem)=dummy(nnode+1); 34 | end 35 | 36 | %--- Nodal coordinates: 37 | 38 | for ipoin=1:npoin 39 | jpoin=fscanf(in,'%d',1); 40 | dummy=fscanf(in,'%lf %lf',[2,1]); 41 | for idime=1:ndime 42 | coord(ipoin,idime)=dummy(idime); 43 | end 44 | end 45 | 46 | %--- Constraint nodes and their values: 47 | 48 | for ivfix=1:nvfix 49 | nofix(ivfix)=fscanf(in,'%d',1); 50 | dummy1=fscanf(in,'%d %d',[2,1]); 51 | dummy2=fscanf(in,'%lf %lf',[2,1]); 52 | for idime=1:ndime 53 | iffix(ivfix,idime)=dummy1(idime); 54 | fixed(ivfix,idime)=dummy2(idime); 55 | end 56 | end 57 | 58 | %--- Material properties: 59 | 60 | for imats=1:nmats 61 | jmats=fscanf(in,'%d',1); 62 | dummy=fscanf(in,'%lf %lf',[2,1]); 63 | for iprop=1:nprop 64 | props(jmats,iprop)=dummy(iprop); 65 | end 66 | end 67 | 68 | %%printout: 69 | 70 | fprintf(out,'*****************************\n'); 71 | fprintf(out,'* FEM input data *\n'); 72 | fprintf(out,'*****************************\n'); 73 | fprintf(out,'\n'); 74 | 75 | fprintf(out,'Number of Elements : %5d\n',nelem); 76 | fprintf(out,'Number of Node : %5d\n',npoin); 77 | fprintf(out,'Number of Fixed nodes : %5d\n',nvfix); 78 | fprintf(out,'Number of Nodes per element : %5d\n',nnode); 79 | fprintf(out,'Number of Integration points : %5d\n',ngaus); 80 | fprintf(out,'Number of Materials : %5d\n',nmats); 81 | fprintf(out,'Number of properties : %5d\n',nprop); 82 | fprintf(out,'\n'); 83 | %-- 84 | 85 | if(ntype == 1) 86 | fprintf(out,'Plane-stress elasticity solution\n'); 87 | end 88 | 89 | if(ntype == 2) 90 | fprintf(out,'Plane-strain elasticity solution\n'); 91 | end 92 | %-- 93 | 94 | end %endfunction 95 | -------------------------------------------------------------------------------- /crack/FEM_model/jacob2.m: -------------------------------------------------------------------------------- 1 | function [cartd,djacb,gpcod] =jacob2(ielem,elcod,kgasp,shape,deriv,nnode,ndime) 2 | 3 | format long; 4 | 5 | %gauss point coordinates: 6 | 7 | cg = elcod*shape'; 8 | 9 | gpcod(1,kgasp)=cg(1); 10 | gpcod(2,kgasp)=cg(2); 11 | 12 | %jacobian 13 | 14 | xjacm = deriv*elcod'; 15 | 16 | %Determinate of Jacobian 17 | 18 | djacb=xjacm(1,1)*xjacm(2,2)-xjacm(1,2)*xjacm(2,1); 19 | 20 | if(djacb <= 0.0) 21 | fprintf('Element No: %5d\n',ielem); 22 | error('Program terminated zero or negative area'); 23 | end 24 | 25 | %cartesion derivatives: 26 | 27 | xjaci(1,1)=xjacm(2,2)/djacb; 28 | xjaci(2,2)=xjacm(1,1)/djacb; 29 | xjaci(1,2)=-xjacm(1,2)/djacb; 30 | xjaci(2,1)=-xjacm(2,1)/djacb; 31 | 32 | cartd = xjaci*deriv; 33 | 34 | 35 | end %endfunction 36 | -------------------------------------------------------------------------------- /crack/FEM_model/modps.m: -------------------------------------------------------------------------------- 1 | function [dmatx] = modps(mtype,ntype,nstre,props) 2 | 3 | format long; 4 | 5 | %--- Material Parameters: 6 | 7 | young=props(mtype,1); 8 | poiss=props(mtype,2); 9 | 10 | for istre=1:3 11 | for jstre=1:3 12 | dmatx(istre,jstre)=0.0; 13 | end 14 | end 15 | 16 | if(ntype == 1) 17 | 18 | %--- Plane Stress: 19 | 20 | const=young/(1.0-poiss*poiss); 21 | dmatx(1,1)=const; 22 | dmatx(2,2)=const; 23 | dmatx(1,2)=const*poiss; 24 | dmatx(2,1)=const*poiss; 25 | dmatx(3,3)=(1.0-2.0*poiss)*const/2.0; 26 | end 27 | 28 | if(ntype == 2) 29 | 30 | %--- Plane Strain: 31 | 32 | const=young*(1.0-poiss)/((1+poiss)*(1.0-2.0*poiss)); 33 | dmatx(1,1)=const; 34 | dmatx(2,2)=const; 35 | dmatx(1,2)=const*poiss/(1.0-poiss); 36 | dmatx(2,1)=const*poiss/(1.0-poiss); 37 | dmatx(3,3)=(1.0-2.0*poiss)*const/(2.0*(1.0-poiss)); 38 | 39 | end 40 | end %endfunction 41 | 42 | -------------------------------------------------------------------------------- /crack/FEM_model/residual_v1.m: -------------------------------------------------------------------------------- 1 | function [rload ]= residual_v1(npoin,nelem,nnode,nstre,ndime,ndofn, ... 2 | ngaus,ntype,lnods,coord,props,posgp, ... 3 | weigp,dtime,constk,cenerg,constl,constn, ... 4 | coneta,stres,stran,tdisp,tdisp_old) 5 | 6 | format long; 7 | 8 | %--- order of integration 9 | 10 | ngaus2=ngaus; 11 | if(nnode == 3) 12 | ngaus2=1; 13 | end 14 | 15 | mgaus = ngaus*ngaus2; 16 | 17 | %--initialize global and local rhs vectors: 18 | 19 | ntotv = npoin*ndofn; 20 | nevab = nnode*ndofn; 21 | ndofn2 = ndofn-1; 22 | ndofn3 = ndofn-2; 23 | ntotv2 = npoin*ndofn2; 24 | nevab2 = nnode*ndofn2; 25 | nevab3 = nnode*ndofn3; 26 | 27 | %---global residuals: 28 | 29 | rload = zeros(ntotv,1); 30 | 31 | for ielem=1:nelem 32 | 33 | %--- initialize element loads: 34 | 35 | for ievab=1:nevab 36 | eload(ievab) = 0.0; 37 | end 38 | 39 | for ievab=1:nevab2 40 | eload1(ievab) = 0.0; 41 | end 42 | 43 | for ievab=1:nevab3 44 | eload2(ievab) =0.0; 45 | end 46 | 47 | %-- 48 | %--- elemental values 49 | %-- 50 | 51 | for inode =1:nnode 52 | lnode = lnods(ielem,inode); 53 | itotv = ntotv2 +lnode; 54 | ephi(inode) = tdisp(itotv); 55 | ephir(inode) = tdisp(itotv)-tdisp_old(itotv); 56 | end 57 | 58 | %--- Coordinates of element nodes: 59 | 60 | for inode=1:nnode 61 | lnode=lnods(ielem,inode); 62 | for idime=1:ndime 63 | elcod(idime,inode)=coord(lnode,idime); 64 | end 65 | end 66 | 67 | %--- integrate elemental loads: 68 | 69 | kgasp=0; 70 | for igaus=1:ngaus 71 | exisp=posgp(igaus); 72 | for jgaus=1:ngaus2 73 | etasp =posgp(jgaus); 74 | if(nnode ==3) 75 | etasp=posgp(ngaus+igaus); 76 | end 77 | 78 | kgasp=kgasp+1; 79 | 80 | [shape,deriv]=sfr2(exisp,etasp,nnode); 81 | 82 | [cartd,djacb,gpcod]=jacob2(ielem,elcod,kgasp,shape,deriv,nnode,ndime); 83 | 84 | dvolu=djacb*weigp(igaus)*weigp(jgaus); 85 | 86 | if(nnode == 3) 87 | dvolu=djacb*weigp(igaus); 88 | end 89 | 90 | %--- values at the integration points: 91 | 92 | phigp = 0.0; 93 | phirgp = 0.0; 94 | 95 | for inode=1:nnode 96 | phigp = phigp + ephi(inode)*shape(inode); 97 | phirgp = phirgp + ephir(inode)*shape(inode); 98 | end 99 | 100 | mtype = 1; 101 | [dmatx] = modps(mtype,ntype,nstre,props); 102 | 103 | %---; 104 | 105 | [bmatx]=bmats(cartd,shape,inode); 106 | 107 | % strain energy: 108 | 109 | senerg = 0.0; 110 | for istre=1:nstre 111 | 112 | senerg = senerg + 0.5*stres(ielem,kgasp,istre) * stran(ielem,kgasp,istre); 113 | 114 | end 115 | 116 | %--- penalty term: 117 | 118 | constx = 0.0; 119 | if(phirgp < 0.0 ) 120 | constx = -phirgp; 121 | end 122 | 123 | %--- residuals (rhs) 124 | 125 | %--- eload1: 126 | 127 | for ievab=1:nevab2 128 | for istre=1:nstre 129 | 130 | eload1(ievab) = eload1(ievab) + ((1.0-phigp)^2 + constk)*bmatx(istre,ievab)* ... 131 | stres(ielem,kgasp,istre)*dvolu; 132 | 133 | end 134 | end 135 | 136 | %--- eload2 137 | 138 | for istre=1:ndime 139 | dummy(istre) = 0.0; 140 | end 141 | 142 | for istre = 1:ndime 143 | for ievab = 1:nevab3 144 | dummy(istre) = dummy(istre) + cartd(istre,ievab)*phigp; 145 | end 146 | end 147 | 148 | for ievab=1:nevab3 149 | for istre=1:ndime 150 | eload2(ievab) = eload2(ievab) + cenerg*constl*cartd(istre,ievab)* ... 151 | dummy(istre)*dvolu; 152 | end 153 | end 154 | 155 | for inode=1:nnode 156 | eload2(inode) = eload2(inode) + ((cenerg/constl)+2.0*senerg)* ... 157 | phigp*shape(inode)*dvolu; 158 | end 159 | 160 | for inode=1:nnode 161 | eload2(inode) = eload2(inode) - 2.0*shape(inode)* ... 162 | (senerg-0.5*(coneta/dtime)*constx^constn)*dvolu; 163 | 164 | end 165 | 166 | end %jgaus 167 | end %igaus 168 | 169 | %--- assemble global residuals: 170 | 171 | for inode=1:nnode 172 | lnode = lnods(ielem,inode); 173 | for idofn =1:ndofn2 174 | ievab=(inode-1)*ndofn2+idofn; 175 | itotv=(lnode-1)*ndofn2+idofn; 176 | rload(itotv) = rload(itotv) +eload1( :,ievab); 177 | end 178 | end 179 | 180 | for inode=1:nnode 181 | lnode = lnods(ielem,inode); 182 | for idofn =1:ndofn3 183 | ievab=(inode-1)*ndofn3+idofn; 184 | itotv=(lnode-1)*ndofn3+idofn+ntotv2; 185 | rload(itotv) = rload(itotv) + eload2( :,ievab); 186 | end 187 | end 188 | 189 | end %ielem 190 | 191 | end %endfunction 192 | 193 | 194 | 195 | 196 | 197 | -------------------------------------------------------------------------------- /crack/FEM_model/residual_v2.m: -------------------------------------------------------------------------------- 1 | function [rload ]= residual_v2(npoin,nelem,nnode,nstre,ndime,ndofn, ... 2 | ngaus,ntype,lnods,coord,props,posgp, ... 3 | weigp,dgdx,dvolum,dtime,constk,cenerg, ... 4 | constl,constn,coneta,stres,stran,tdisp, ... 5 | tdisp_old) 6 | 7 | format long; 8 | 9 | %--- order of integration 10 | 11 | ngaus2=ngaus; 12 | if(nnode == 3) 13 | ngaus2=1; 14 | end 15 | 16 | mgaus = ngaus*ngaus2; 17 | 18 | %--initialize global and local rhs vectors: 19 | 20 | ntotv = npoin*ndofn; 21 | nevab = nnode*ndofn; 22 | ndofn2 = ndofn-1; 23 | ndofn3 = ndofn-2; 24 | ntotv2 = npoin*ndofn2; 25 | nevab2 = nnode*ndofn2; 26 | nevab3 = nnode*ndofn3; 27 | 28 | %---global residuals: 29 | 30 | rload = zeros(ntotv,1); 31 | 32 | %--- element loads: 33 | 34 | eload = zeros(nelem,nevab); 35 | eload1 = zeros(nelem,nevab2); 36 | eload2 = zeros(nelem,nevab3); 37 | 38 | %-- 39 | %--- elemental values 40 | %-- 41 | 42 | ephi = zeros(nelem,nnode); 43 | ephir = zeros(nelem,nnode); 44 | 45 | for inode =1:nnode 46 | lnode = lnods( :,inode); 47 | itotv = ntotv2 +lnode; 48 | ephi( :,inode) = tdisp(itotv); 49 | ephir( :,inode) = tdisp(itotv)-tdisp_old(itotv); 50 | end 51 | 52 | %--- integrate elemental loads: 53 | 54 | kgasp=0; 55 | for igaus=1:ngaus 56 | exisp=posgp(igaus); 57 | for jgaus=1:ngaus2 58 | etasp =posgp(jgaus); 59 | if(nnode ==3) 60 | etasp=posgp(ngaus+igaus); 61 | end 62 | 63 | kgasp=kgasp+1; 64 | [shape,deriv]=sfr2(exisp,etasp,nnode); 65 | 66 | %--- values at the integration points: 67 | 68 | phigp =zeros(nelem,1); 69 | phirgp =zeros(nelem,1); 70 | 71 | for inode=1:nnode 72 | phigp = phigp + ephi( :,inode)*shape(inode); 73 | phirgp = phirgp + ephir( :,inode)*shape(inode); 74 | end 75 | 76 | mtype = 1; 77 | [dmatx] = modps(mtype,ntype,nstre,props); 78 | 79 | %--- cartesien derivative matrices; 80 | 81 | [bmatx1]=bmats1(dgdx,nelem,nnode,nstre,nevab2,kgasp); 82 | [bmatx2]=bmats2(dgdx,nelem,nnode,nstre,nevab2,kgasp); 83 | 84 | 85 | % strain energy: 86 | 87 | senerg =zeros(nelem,1); 88 | for istre=1:nstre 89 | 90 | senerg = senerg + 0.5*stres( :,kgasp,istre) .* stran( :,kgasp,istre); 91 | 92 | end 93 | 94 | %--- penalty term: 95 | 96 | constx = zeros(nelem,1); 97 | inrange = (phirgp < 0 ); 98 | constx(inrange) = -phirgp(inrange); 99 | 100 | %--- residuals (rhs) 101 | 102 | %--- eload1: 103 | 104 | for ievab=1:nevab2 105 | for istre=1:nstre 106 | 107 | eload1( :,ievab) = eload1( :,ievab) + ((1.0-phigp).^2 + constk) .*bmatx2( :,istre,ievab).* ... 108 | stres( :,kgasp,istre).*dvolum( :,kgasp); 109 | 110 | end 111 | end 112 | 113 | %--- eload2 114 | 115 | dummy = zeros(nelem,ndime); 116 | 117 | for istre = 1:ndime 118 | for ievab = 1:nevab3 119 | dummy( :,istre) = dummy( :,istre) + bmatx1( :,istre,ievab).*phigp; 120 | end 121 | end 122 | 123 | for ievab=1:nevab3 124 | for istre=1:ndime 125 | eload2( :,ievab) = eload2( :,ievab) + cenerg*constl*bmatx1( :,istre,ievab).* ... 126 | dummy( :,istre).*dvolum( :,kgasp); 127 | end 128 | end 129 | 130 | for inode=1:nnode 131 | eload2( :,inode) = eload2( :,inode) + ((cenerg/constl)+2.0*senerg).* ... 132 | phigp*shape(inode).*dvolum( :,kgasp); 133 | end 134 | 135 | for inode=1:nnode 136 | eload2( :,inode) = eload2( :,inode) - 2.0*shape(inode)* ... 137 | (senerg-0.5*(coneta/dtime)*constx.^constn).*dvolum( :,kgasp); 138 | 139 | end 140 | 141 | end %jgaus 142 | end %igaus 143 | 144 | %--- assemble global residuals: 145 | 146 | for inode=1:nnode 147 | lnode = lnods( :,inode); 148 | for idofn =1:ndofn2 149 | ievab=(inode-1)*ndofn2+idofn; 150 | itotv=(lnode-1)*ndofn2+idofn; 151 | rload(itotv) = rload(itotv) +eload1( :,ievab); 152 | end 153 | end 154 | 155 | for inode=1:nnode 156 | lnode = lnods( :,inode); 157 | for idofn =1:ndofn3 158 | ievab=(inode-1)*ndofn3+idofn; 159 | itotv=(lnode-1)*ndofn3+idofn+ntotv2; 160 | rload(itotv) = rload(itotv) + eload2( :,ievab); 161 | end 162 | end 163 | 164 | end %endfunction 165 | 166 | 167 | 168 | 169 | 170 | -------------------------------------------------------------------------------- /crack/FEM_model/sfr2.m: -------------------------------------------------------------------------------- 1 | function [shape, deriv] = sfr2(exisp,etasp,nnode) 2 | 3 | format long; 4 | 5 | if(nnode == 3) 6 | %--- 3 nodes elements: 7 | 8 | s=exisp; 9 | t=etasp; 10 | p=1.0-s-t; 11 | 12 | shape(1)=p; 13 | shape(2)=s; 14 | shape(3)=t; 15 | 16 | deriv(1,1)=-1.0; 17 | deriv(1,2)=1.0; 18 | deriv(1,3)=0.0; 19 | 20 | deriv(2,1)=-1.0; 21 | deriv(2,2)=0.0; 22 | deriv(2,3)=1.0; 23 | 24 | end 25 | 26 | if(nnode == 4 ) 27 | %--- 4 nodes elements 28 | 29 | s=exisp; 30 | t=etasp; 31 | st=s*t; 32 | 33 | shape(1)=(1.0-t-s+st)*0.25; 34 | shape(2)=(1.0-t+s-st)*0.25; 35 | shape(3)=(1.0+t+s+st)*0.25; 36 | shape(4)=(1.0+t-s-st)*0.25; 37 | % 38 | deriv(1,1)=(-1.0+t)*0.25; 39 | deriv(1,2)=(1.0-t)*0.25; 40 | deriv(1,3)=(1.0+t)*0.25; 41 | deriv(1,4)=(-1.0-t)*0.25; 42 | % 43 | deriv(2,1)=(-1.0+s)*0.25; 44 | deriv(2,2)=(-1.0-s)*0.25; 45 | deriv(2,3)=(1.0+s)*0.25; 46 | deriv(2,4)=(1.0-s)*0.25; 47 | 48 | end 49 | 50 | if(nnode == 8 ) 51 | %-- 8 nodes elements 52 | 53 | s=exisp; 54 | t=etasp; 55 | s2=2.0*s; 56 | t2=2.0*t; 57 | ss=s*s; 58 | tt=t*t; 59 | st=s*t; 60 | sst=s*s*t; 61 | stt=s*t*t; 62 | st2=2.0*s*t; 63 | 64 | shape(1)=(-1.0+st+ss+tt-sst-stt)*0.25; 65 | shape(2)=(1.0-t-ss+sst)*0.5; 66 | shape(3)=(-1.0-st+ss+tt-sst+stt)*0.25; 67 | shape(4)=(1.0+s-tt-stt)*0.5; 68 | shape(5)=(-1.0+st+ss+tt+sst+stt)*0.25; 69 | shape(6)=(1.0+t-ss-sst)*0.5; 70 | shape(7)=(-1.0-st+ss+tt+sst-stt)*0.25; 71 | shape(8)=(1.0-s-tt+stt)*0.5; 72 | % 73 | deriv(1,1)=(t+s2-st2-tt)*0.25; 74 | deriv(1,2)=-s+st; 75 | deriv(1,3)=(-t+s2-st2+tt)*0.25; 76 | deriv(1,4)=(1.0-tt)*0.5; 77 | deriv(1,5)=(t+s2+st2+tt)*0.25; 78 | deriv(1,6)=-s-st; 79 | deriv(1,7)=(-t+s2+st2-tt)*0.25; 80 | deriv(1,8)=(-1.0+tt)*0.5; 81 | % 82 | deriv(2,1)=(s+t2-ss-st2)*0.25; 83 | deriv(2,2)=(-1.0+ss)*0.5; 84 | deriv(2,3)=(-s+t2-ss+st2)*0.25; 85 | deriv(2,4)=-t-st; 86 | deriv(2,5)=(s+t2+ss+st2)*0.25; 87 | deriv(2,6)=(1.0-ss)*0.5; 88 | deriv(2,7)=(-s+t2+ss-st2)*0.25; 89 | deriv(2,8)=-t+st; 90 | 91 | end 92 | 93 | end %endfunction 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /crack/FEM_model/stress_fract_v1.m: -------------------------------------------------------------------------------- 1 | function [stran,stres] = stress_fract_v1(asdis,nelem,npoin,nnode,ngaus,nstre,props, ... 2 | ntype,ndofn,ndime,lnods,matno,coord,posgp,weigp, ... 3 | tdisp) 4 | format long; 5 | 6 | %--- order of integration 7 | ngaus2=ngaus; 8 | if(nnode == 3) 9 | ngaus2=1; 10 | end 11 | 12 | mgaus = ngaus*ngaus2; 13 | 14 | %--initialize: 15 | 16 | ndofn2 =2; 17 | nevab2 =nnode*ndofn; 18 | ntotv2 =npoin*ndofn2; 19 | nevab = nnode*ndofn2; 20 | 21 | for ielem =1:nelem 22 | 23 | for igaus =1:mgaus 24 | for istre =1:nstre 25 | stres(ielem,igaus,istre) = 0.0; 26 | stran(ielem,igaus,istre) = 0.0; 27 | end 28 | end 29 | 30 | %--Nodal displacements: 31 | 32 | for inode =1:nnode 33 | lnode = lnods(ielem,inode); 34 | for idofn=1:ndofn2 35 | ievab =(inode-1)*ndofn2+idofn; 36 | itotv =(lnode-1)*ndofn2+idofn; 37 | eldis(ievab)=tdisp(itotv); 38 | end 39 | end 40 | 41 | %--- elasticity matrix: 42 | 43 | mtype =1; 44 | [dmatx] = modps(mtype,ntype,nstre,props); 45 | 46 | %--- Coordinates of element nodes: 47 | 48 | for inode=1:nnode 49 | lnode=lnods(ielem,inode); 50 | for idime=1:ndime 51 | elcod(idime,inode)=coord(lnode,idime); 52 | end 53 | end 54 | 55 | %--- calculate strains and stresses at integration points: 56 | 57 | kgasp=0; 58 | 59 | for igaus=1:ngaus 60 | exisp=posgp(igaus); 61 | for jgaus=1:ngaus2 62 | etasp =posgp(jgaus); 63 | if(nnode ==3) 64 | etasp=posgp(ngaus+igaus); 65 | end 66 | 67 | kgasp=kgasp+1; 68 | 69 | [shape,deriv]=sfr2(exisp,etasp,nnode); 70 | 71 | [cartd,djacb,gpcod]=jacob2(ielem,elcod,kgasp,shape,deriv,nnode,ndime); 72 | 73 | %--- strain matrix: 74 | 75 | [bmatx]=bmats(cartd,shape,inode); 76 | 77 | %--- calculate the strains: 78 | 79 | for istre =1:nstre 80 | for ievab =1:nevab 81 | 82 | stran(ielem,kgasp,istre) = stran(ielem,kgasp,istre) +bmatx(istre,ievab)*eldis(ievab); 83 | 84 | end 85 | end 86 | 87 | %calculate stresses: 88 | 89 | for istre =1:nstre 90 | for jstre =1:nstre 91 | 92 | stres(ielem,kgasp,istre) = stres(ielem,kgasp,istre) + dmatx(istre,jstre)*stran(ielem,kgasp,jstre); 93 | 94 | end 95 | end 96 | 97 | end %igaus 98 | end %jgaus 99 | 100 | end % ielem 101 | end %endfunction 102 | 103 | -------------------------------------------------------------------------------- /crack/FEM_model/stress_fract_v2.m: -------------------------------------------------------------------------------- 1 | function [stran,stres] = stress_fract_v2(asdis,nelem,npoin,nnode,ngaus,nstre,props, ... 2 | ntype,ndofn,ndime,lnods,matno,coord,posgp,weigp, ... 3 | dgdx,dvolum,tdisp) 4 | format long; 5 | 6 | %--- order of integration 7 | ngaus2=ngaus; 8 | if(nnode == 3) 9 | ngaus2=1; 10 | end 11 | 12 | mgaus = ngaus*ngaus2; 13 | 14 | %--initialize: 15 | 16 | ndofn2 =2; 17 | nevab2 =nnode*ndofn; 18 | ntotv2 =npoin*ndofn2; 19 | nevab = nnode*ndofn2; 20 | 21 | stres = zeros(nelem,mgaus,nstre); 22 | stran = zeros(nelem,mgaus,nstre); 23 | eldis = zeros(nelem,nevab); 24 | 25 | %--Nodal displacements: 26 | 27 | for inode =1:nnode 28 | lnode = lnods( :,inode); 29 | for idofn=1:ndofn2 30 | ievab =(inode-1)*ndofn2+idofn; 31 | itotv =(lnode-1)*ndofn2+idofn; 32 | eldis( :,ievab)=tdisp(itotv); 33 | end 34 | end 35 | 36 | %--- elasticity matrix: 37 | 38 | mtype =1; 39 | [dmatx] = modps(mtype,ntype,nstre,props); 40 | 41 | %--- calculate strains and stresses at integration points: 42 | 43 | kgasp=0; 44 | 45 | for igaus=1:ngaus 46 | exisp=posgp(igaus); 47 | for jgaus=1:ngaus2 48 | etasp =posgp(jgaus); 49 | if(nnode ==3) 50 | etasp=posgp(ngaus+igaus); 51 | end 52 | 53 | kgasp=kgasp+1; 54 | 55 | [shape,deriv]=sfr2(exisp,etasp,nnode); 56 | 57 | %--- strain matrix: 58 | 59 | [bmatx]=bmats2(dgdx,nelem,nnode,nstre,nevab,kgasp); 60 | 61 | %--- calculate the strains: 62 | 63 | for istre =1:nstre 64 | for ievab =1:nevab 65 | 66 | stran( :,kgasp,istre) = stran( :,kgasp,istre) +bmatx( :,istre,ievab).*eldis( :,ievab); 67 | 68 | end 69 | end 70 | 71 | %calculate stresses: 72 | 73 | for istre =1:nstre 74 | for jstre =1:nstre 75 | 76 | stres( :,kgasp,istre) = stres( :,kgasp,istre) + dmatx(istre,jstre).*stran( :,kgasp,jstre); 77 | 78 | end 79 | end 80 | 81 | end %igaus 82 | end %jgaus 83 | 84 | end %endfunction 85 | 86 | -------------------------------------------------------------------------------- /crack/FEM_model/write_vtk_fem.m: -------------------------------------------------------------------------------- 1 | function [ ] = write_vtk_fem(npoin,nelem,nnode,lnods,coord,istep,cont1) 2 | 3 | format long; 4 | 5 | %-- open file 6 | 7 | fname=sprintf('time_%d.vtk',istep); 8 | out =fopen(fname,'w'); 9 | 10 | 11 | %--- start writing: 12 | 13 | %% header 14 | fprintf(out,'# vtk DataFile Version 2.0\n'); 15 | fprintf(out,'time_10.vtk\n'); 16 | fprintf(out,'ASCII\n'); 17 | fprintf(out,'DATASET UNSTRUCTURED_GRID\n'); 18 | 19 | %write nodal coordinates: 20 | 21 | fprintf(out,'POINTS %5d float\n',npoin); 22 | 23 | dummy = 0.0; 24 | 25 | for ipoin=1:npoin 26 | fprintf(out,'%14.6f %14.6f %14.6f\n',coord(ipoin,1),coord(ipoin,2), dummy); 27 | end 28 | 29 | %--- write element connectivity: 30 | 31 | iconst1 = nelem*(nnode+1); 32 | 33 | fprintf(out,'CELLS %5d %5d\n', nelem,iconst1); 34 | 35 | for ielem=1:nelem 36 | fprintf(out,'%5d',nnode); 37 | for inode=1:nnode 38 | fprintf(out,'%5d',(lnods(ielem,inode)-1)); 39 | end 40 | fprintf(out,'\n'); 41 | end 42 | 43 | %--- write cell types: 44 | 45 | if(nnode == 8) 46 | ntype = 23; 47 | end 48 | 49 | if(nnode == 4) 50 | ntype = 9; 51 | end 52 | 53 | if(nnode == 3) 54 | ntype = 5; 55 | end 56 | 57 | fprintf(out,'CELL_TYPES %5d\n',nelem); 58 | 59 | for i=1:nelem 60 | fprintf(out,'%2d\n', ntype); 61 | end 62 | 63 | 64 | %--- write Nodal scaler & vector values: 65 | 66 | fprintf(out,'POINT_DATA %5d\n',npoin); 67 | 68 | %--- write concentration values as scalar: 69 | 70 | fprintf(out,'SCALARS Con float 1\n'); 71 | 72 | fprintf(out,'LOOKUP_TABLE default\n'); 73 | 74 | for ipoin=1:npoin 75 | fprintf(out,'%14.6e\n',cont1(ipoin)); 76 | end 77 | 78 | 79 | fclose(out); 80 | 81 | end %endfunction 82 | -------------------------------------------------------------------------------- /crack/FEM_model/脆性断裂的相场模拟.avi: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kipa200/Phase_field_code/98ccc5daa7520cb21529b88be73a3c3275be0380/crack/FEM_model/脆性断裂的相场模拟.avi -------------------------------------------------------------------------------- /crack/crack.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kipa200/Phase_field_code/98ccc5daa7520cb21529b88be73a3c3275be0380/crack/crack.pdf -------------------------------------------------------------------------------- /sintering/.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "matlab.mlintpath": "D:\\Matlab\\R2020a\\bin\\win64\\mlint.exe" 3 | } -------------------------------------------------------------------------------- /sintering/free_energ_sint_v1.m: -------------------------------------------------------------------------------- 1 | function [dfdcon,dfdeta] =free_energ_sint_v1(i,j,con,eta,etas,npart,iflag) 2 | 3 | format long; 4 | 5 | A=16.0; 6 | B= 1.0; 7 | 8 | dfdcon =0.0; 9 | dfdeta =0.0; 10 | 11 | 12 | if(iflag == 1) 13 | %-- 14 | %-- 濃度場自由能迭代: 15 | %-- 16 | 17 | sum2 = 0.0; 18 | sum3 = 0.0; 19 | 20 | for ipart = 1:npart 21 | 22 | sum2 = sum2 + etas(i,j,ipart).^2; 23 | sum3 = sum3 + etas(i,j,ipart).^3; 24 | end 25 | 26 | dfdcon = B*(2.0*con(i,j) + 4.0*sum3 - 6.0*sum2) - 2.0*A*con(i,j)^2 .* ... 27 | (1.0-con(i,j)) + 2.0*A*con(i,j)*(1.0-con(i,j))^2; 28 | 29 | end %if 30 | 31 | if(iflag == 2) 32 | %-- 33 | %-- 相場自由能迭代: 34 | %-- 35 | 36 | sum2 =0.0; 37 | 38 | for ipart = 1: npart 39 | 40 | sum2 = sum2 + etas(i,j,ipart)^2; 41 | end 42 | 43 | dfdeta = B*(-12.0*eta(i,j)^2 .* (2.0-con(i,j)) +12.0 *eta(i,j)* (1.0-con(i,j)) + ... 44 | 12.0*eta(i,j)*sum2); 45 | end %if 46 | 47 | end %endfunction 48 | -------------------------------------------------------------------------------- /sintering/sintering.m: -------------------------------------------------------------------------------- 1 | %== 获得初始时间,设置数据显示格式: 2 | time0=clock(); 3 | format long; 4 | 5 | %-- 设置单元尺寸: 6 | 7 | Nx = 100; 8 | Ny = 100; 9 | NxNy= Nx*Ny; 10 | 11 | dx = 0.5; 12 | dy = 0.5; 13 | 14 | %--- 设置积分步: 15 | 16 | nstep = 5000; 17 | nprint = 50; 18 | dtime= 1.0e-4; 19 | 20 | %--- 材料相关属性: 21 | npart = 9; %粒子数2或9 22 | % 23 | coefm = 5.0; 24 | coefk = 2.0; 25 | coefl = 5.0; 26 | % 27 | dvol = 0.040; 28 | dvap = 0.002; 29 | dsur = 16.0; 30 | dgrb = 1.6; 31 | %--准备结果存储空间: 32 | 33 | X = 0:0.5:49.5; 34 | Y = 0:0.5:49.5; 35 | Zcon = zeros(100, 100); 36 | Zeta1 = zeros(100, 100); 37 | Zeta2 = zeros(100, 100); 38 | %---- 39 | %微结构初始化,程序设置了粒子数为2,9两种情况: 40 | %---- 41 | for ipart =1:npart 42 | for i=1:Nx 43 | for j=1:Ny 44 | con(i,j) =0.0; 45 | etas(i,j,ipart) = 0.0; 46 | end 47 | end 48 | end % ipart 49 | %---设置9粒子圆心 50 | if(npart ~= 2) 51 | 52 | R = 10.0; 53 | 54 | xc(1)=29.0; 55 | yc(1)=50.0; 56 | 57 | xc(2)=50.0; 58 | yc(2)=50.0; 59 | 60 | xc(3)=71.0; 61 | yc(3)=50.0; 62 | 63 | xc(4)=50.0; 64 | yc(4)=29.0; 65 | 66 | xc(5)=50.0; 67 | yc(5)=71.0; 68 | 69 | xc(6)=39.0; 70 | yc(6)=39.0; 71 | 72 | xc(7)=61.0; 73 | yc(7)=39.0; 74 | 75 | xc(8)=39.0; 76 | yc(8)=61.0; 77 | 78 | xc(9)=61.0; 79 | yc(9)=61.0; 80 | 81 | for ipart=1:npart 82 | 83 | Rx = R; 84 | 85 | if(ipart > 5 ) 86 | Rx = 0.5*R; 87 | end 88 | 89 | for i=1:Nx 90 | for j=1:Ny 91 | 92 | xx1 =sqrt((i-xc(ipart))^2 +(j-yc(ipart))^2); 93 | 94 | if( xx1 <= Rx) 95 | con(i,j)= 0.999; 96 | etas(i,j,ipart) =0.999; 97 | 98 | end %if 99 | end % j 100 | end % i 101 | end % ipart 102 | end % if 103 | 104 | %--- 105 | if(npart == 2) 106 | 107 | R1 = 20.0; 108 | R2 = 0.5*R1; 109 | 110 | x1 = Nx/2; 111 | y1 = 40.0; 112 | y2 = 70.0; 113 | 114 | for i=1:Nx 115 | for j=1:Ny 116 | 117 | xx1 =sqrt((i-x1)^2 +(j-y1)^2); 118 | xx2 =sqrt((i-x1)^2 +(j-y2)^2); 119 | 120 | if( xx1 <= R1) 121 | con(i,j)=0.999; 122 | etas(i,j,1) =0.999; 123 | end % if 124 | 125 | if(xx2 <= R2) 126 | con(i,j) = 0.999; 127 | etas(i,j,1)=0.0; 128 | etas(i,j,2)=0.9999; 129 | end % if 130 | end % j 131 | end % i 132 | end %if 133 | 134 | %--- 135 | %-- 初始化 eta: 136 | for i=1:Nx 137 | for j=1:Ny 138 | eta(i,j) = 0.0; 139 | end 140 | end 141 | 142 | for istep =1:nstep 143 | 144 | %-- 浓度场迭代: 145 | iflag = 1; 146 | for i=1:Nx 147 | for j=1:Ny 148 | 149 | jp=j+1; 150 | jm=j-1; 151 | 152 | ip=i+1; 153 | im=i-1; 154 | 155 | if(im == 0) 156 | im=Nx; 157 | end 158 | if(ip == (Nx+1)) 159 | ip=1; 160 | end 161 | 162 | if(jm == 0) 163 | jm = Ny; 164 | end 165 | 166 | if(jp == (Ny+1)) 167 | jp=1; 168 | end 169 | 170 | hne=con(ip,j); 171 | hnw=con(im,j); 172 | hns=con(i,jm); 173 | hnn=con(i,jp); 174 | hnc=con(i,j); 175 | 176 | lap_con(i,j) =(hnw + hne + hns + hnn -4.0*hnc)/(dx*dy); 177 | 178 | %--- 计算自由能对浓度的导数: 179 | 180 | [dfdcon,dfdeta]=free_energ_sint_v1(i,j,con,eta,etas,npart,iflag); 181 | dummy(i,j) = dfdcon - 0.5*coefm*lap_con(i,j); 182 | 183 | end %for j 184 | end %for i 185 | 186 | %-- 187 | 188 | for i=1:Nx 189 | for j=1:Ny 190 | 191 | jp=j+1; 192 | jm=j-1; 193 | 194 | ip=i+1; 195 | im=i-1; 196 | 197 | if(im == 0) 198 | im=Nx; 199 | end 200 | if(ip == (Nx+1)) 201 | ip=1; 202 | end 203 | 204 | if(jm == 0) 205 | jm = Ny; 206 | end 207 | 208 | if(jp == (Ny+1)) 209 | jp=1; 210 | end 211 | 212 | hne=dummy(ip,j); 213 | hnw=dummy(im,j); 214 | hns=dummy(i,jm); 215 | hnn=dummy(i,jp); 216 | hnc=dummy(i,j); 217 | 218 | lap_dummy(i,j) =(hnw + hne + hns + hnn -4.0*hnc)/(dx*dy); 219 | 220 | %-- Mobility插值函数: 221 | 222 | phi = con(i,j)^3 *(10.0-15.0*con(i,j) + 6.0*con(i,j)^2); 223 | 224 | sum =0.0; 225 | for ipart =1:npart 226 | for jpart =1:npart 227 | if(ipart ~= jpart) 228 | sum = sum + etas(i,j,ipart)*etas(i,j,jpart); 229 | end 230 | end 231 | end 232 | 233 | mobil = dvol*phi + dvap*(1.0-phi) + dsur*con(i,j)*(1.0-con(i,j)) + dgrb*sum; 234 | 235 | %-- 时间积分: 236 | 237 | con(i,j) = con(i,j) + dtime*mobil*lap_dummy(i,j); 238 | 239 | %-- 处理偏差值: 240 | 241 | if(con(i,j) >= 0.9999); 242 | con(i,j)= 0.9999; 243 | end 244 | 245 | if(con(i,j) < 0.00001); 246 | con(i,j) = 0.00001; 247 | end 248 | Zcon(i,j) = con(i,j); 249 | end %j 250 | end %i 251 | 252 | %-- 相场迭代: 253 | iflag = 2; 254 | for ipart = 1:npart 255 | 256 | for i=1:Nx 257 | for j=1:Ny 258 | eta(i,j) = etas(i,j,ipart); 259 | end 260 | end 261 | 262 | for i=1:Nx 263 | for j=1:Ny 264 | 265 | jp=j+1; 266 | jm=j-1; 267 | 268 | ip=i+1; 269 | im=i-1; 270 | 271 | if(im == 0) 272 | im=Nx; 273 | end 274 | if(ip == (Nx+1)) 275 | ip=1; 276 | end 277 | 278 | if(jm == 0) 279 | jm = Ny; 280 | end 281 | 282 | if(jp == (Ny+1)) 283 | jp=1; 284 | end 285 | 286 | hne=eta(ip,j); 287 | hnw=eta(im,j); 288 | hns=eta(i,jm); 289 | hnn=eta(i,jp); 290 | hnc=eta(i,j); 291 | 292 | lap_eta(i,j) =(hnw + hne + hns + hnn -4.0*hnc)/(dx*dy); 293 | 294 | %--- 自由能对相的导数: 295 | [dfdcon,dfdeta]=free_energ_sint_v1(i,j,con,eta,etas,npart,iflag); 296 | %-- 时间积分 297 | 298 | eta(i,j) = eta(i,j) - dtime * coefl*(dfdeta - 0.5 *coefk*lap_eta(i,j)); 299 | 300 | %-- 偏差值处理: 301 | 302 | if(eta(i,j) >= 0.9999); 303 | eta(i,j) = 0.9999; 304 | end 305 | 306 | if(eta(i,j) < 0.0001); 307 | eta(i,j) = 0.0001; 308 | end 309 | end %j 310 | end %i 311 | 312 | %-- 313 | 314 | for i=1:Nx 315 | for j=1:Ny 316 | etas(i,j,ipart) = eta (i,j); 317 | if(ipart==1) 318 | Zeta1(i, j) = etas(i,j,ipart); 319 | else 320 | Zeta2(i, j) = etas(i,j,ipart); 321 | end 322 | end%end for 323 | end 324 | end %ipart 325 | 326 | %---- 结果输出 327 | 328 | if((mod(istep,nprint) == 0) || (istep == 1) ) 329 | subplot(1,3,1) 330 | pcolor(X, Y, Zcon) 331 | axis([0 50 0 50]); 332 | 333 | subplot(1,3,2) 334 | pcolor(X, Y, Zeta1) 335 | axis([0 50 0 50]); 336 | 337 | subplot(1,3,3) 338 | pcolor(X, Y, Zeta2) 339 | axis([0 50 0 50]); 340 | 341 | M(istep) = getframe; 342 | end %if 343 | end %istep 344 | 345 | %--- calculate compute time: 346 | 347 | compute_time = etime(clock(),time0); 348 | fprintf('Compute Time: %10d\n',compute_time); 349 | 350 | -------------------------------------------------------------------------------- /sintering/烧结过程的相场模拟.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kipa200/Phase_field_code/98ccc5daa7520cb21529b88be73a3c3275be0380/sintering/烧结过程的相场模拟.pdf -------------------------------------------------------------------------------- /snowflake/Snow_Flake.m: -------------------------------------------------------------------------------- 1 | %== 获得初始时间,设置数据显示格式: 2 | time0 = clock(); 3 | format long; 4 | 5 | %-- 设置单元尺寸: 6 | 7 | Nx = 300; 8 | Ny = 300; 9 | NxNy = Nx * Ny; 10 | 11 | dx = 0.03; 12 | dy = 0.03; 13 | 14 | %--- 设置积分步: 15 | 16 | nstep = 4000; 17 | nprint = 50; 18 | dtime = 1.0e-4; 19 | 20 | %--- 材料相关属性: 21 | 22 | tau = 0.0003; %时间演化系数 23 | epsilonb = 0.01; %各向异性梯度能量系数平均值0.01 24 | kappa = 1.8; %无量纲潜热,正比于潜热,反比于冷却强度 1.8 25 | delta = 0.02; %各向异性强度0.02 26 | aniso = 6.0; %j:各向异性模数4&6 27 | alpha = 0.9; %过冷系数0.9 28 | gamma = 10.0; %温差放大系数10.0 29 | teq = 1.0; %平衡温度1.0 30 | theta0 = 0.2; %初始偏移角0.2 31 | seed = 10.0; %定义晶核种子大小5.0 32 | 33 | %--相/温度初始化: 34 | %[phi, tempr] = nucleus(Nx, Ny, seed); 35 | phi = zeros(Nx, Ny); 36 | tempr = zeros(Nx, Ny); 37 | 38 | for i = 1:Nx 39 | for j = 1:Ny 40 | if ((i - Nx / 2)^2 + (j - Ny / 2)^2 < seed) 41 | phi(i, j) = 1.0; 42 | end 43 | end 44 | end 45 | 46 | %--准备结果存储空间: 47 | 48 | X = 0:0.03:9 - 0.03; 49 | Y = 0:0.03:9 - 0.03; 50 | Z = zeros(300, 300); 51 | 52 | %- 迭代 53 | 54 | for istep = 1:nstep 55 | 56 | %---- 57 | % calculate the laplacians and epsilon: 58 | %--- 59 | 60 | for i = 1:Nx 61 | for j = 1:Ny 62 | jp = j + 1; 63 | jm = j - 1; 64 | 65 | ip = i + 1; 66 | im = i - 1; 67 | 68 | if (im == 0) 69 | im = Nx; 70 | end 71 | 72 | if (ip == (Nx + 1)) 73 | ip = 1; 74 | end 75 | 76 | if (jm == 0) 77 | jm = Ny; 78 | end 79 | 80 | if (jp == (Ny + 1)) 81 | jp = 1; 82 | end 83 | 84 | hne = phi(ip, j); 85 | hnw = phi(im, j); 86 | hns = phi(i, jm); 87 | hnn = phi(i, jp); 88 | hnc = phi(i, j); 89 | 90 | lap_phi(i, j) = (hnw + hne + hns + hnn -4.0 * hnc) / (dx * dy); 91 | 92 | hne = tempr(ip, j); 93 | hnw = tempr(im, j); 94 | hns = tempr(i, jm); 95 | hnn = tempr(i, jp); 96 | hnc = tempr(i, j); 97 | 98 | lap_tempr(i, j) = (hnw + hne + hns + hnn -4.0 * hnc) / (dx * dy); 99 | 100 | %--phidx,phidy: 101 | 102 | phidx(i, j) = (phi(ip, j) - phi(im, j)) / (2.0 * dx); 103 | phidy(i, j) = (phi(i, jp) - phi(i, jm)) / (2.0 * dy); 104 | 105 | %-- 计算偏移角,求角度用atan2,求值用atan 106 | 107 | theta = atan2(phidy(i, j), phidx(i, j)); 108 | 109 | %--- epsl及其导数: 110 | 111 | epsilon(i, j) = epsilonb * (1.0 + delta * cos(aniso * (theta - theta0))); 112 | 113 | epsilon_deriv(i, j) = -epsilonb * aniso * delta * sin(aniso * (theta - theta0)); 114 | %depsl/dtheta 115 | 116 | end %for j 117 | 118 | end %for i 119 | 120 | %----求解相/温度 121 | 122 | for i = 1:Nx 123 | 124 | for j = 1:Ny 125 | 126 | jp = j + 1; 127 | jm = j - 1; 128 | 129 | ip = i + 1; 130 | im = i - 1; 131 | 132 | if (im == 0) 133 | im = Nx; 134 | end 135 | 136 | if (ip == (Nx + 1)) 137 | ip = 1; 138 | end 139 | 140 | if (jm == 0) 141 | jm = Ny; 142 | end 143 | 144 | if (jp == (Ny + 1)) 145 | jp = 1; 146 | end 147 | 148 | phiold = phi(i, j); 149 | 150 | %-- first term: 151 | 152 | term1 = (epsilon(i, jp) * epsilon_deriv(i, jp) * phidx(i, jp) - ... 153 | epsilon(i, jm) * epsilon_deriv(i, jm) * phidx(i, jm)) / (2.0 * dy); 154 | 155 | %-- second term: 156 | 157 | term2 = -(epsilon(ip, j) * epsilon_deriv(ip, j) * phidy(ip, j) - ... 158 | epsilon(im, j) * epsilon_deriv(im, j) * phidy(im, j)) / (2.0 * dx); 159 | 160 | %-- factor m: 161 | 162 | %m = alpha/pi * atan(gamma*(teq-tempr(i,j))); 163 | m = alpha / pi * atan(gamma * (teq - tempr(i, j))); 164 | %-- Time integration: 165 | 166 | phi(i, j) = phi(i, j) +(dtime / tau) * (term1 + term2 + epsilon(i, j)^2 * lap_phi(i, j) + ... 167 | phiold * (1.0 - phiold) * (phiold -0.5 + m)); 168 | 169 | if (phi(i, j) < 10^ - 30) 170 | phi(i, j) = 10^ - 30; 171 | end 172 | 173 | if (phi(i, j) > 10^30) 174 | phi(i, j) = 10^30; 175 | end 176 | 177 | Z(i, j) = phi(i, j); 178 | %-- evolve temperature: 179 | 180 | tempr(i, j) = tempr(i, j) +dtime * lap_tempr(i, j) + kappa * (phi(i, j) - phiold); 181 | 182 | if (tempr(i, j) < 10^ - 30) 183 | tempr(i, j) = 10^ - 30; 184 | end 185 | 186 | if (tempr(i, j) > 10^30) 187 | tempr(i, j) = 10^30; 188 | end 189 | 190 | end 191 | 192 | end 193 | 194 | %---- 输出结果 195 | 196 | if (mod(istep, nprint) == 0) 197 | subplot(1,2,1) 198 | pcolor(X, Y, Zcon) 199 | subplot(1,2,2) 200 | pcolor(X, Y, Zeta) 201 | M(istep) = getframe; 202 | end %if 203 | 204 | end %istep 205 | 206 | %--- 显示结束时间: 207 | 208 | compute_time = etime(clock(), time0); 209 | fprintf('Compute Time: %10d\n', compute_time); 210 | -------------------------------------------------------------------------------- /snowflake/matlab_snowflake.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kipa200/Phase_field_code/98ccc5daa7520cb21529b88be73a3c3275be0380/snowflake/matlab_snowflake.pdf -------------------------------------------------------------------------------- /snowflake/nucleus.m: -------------------------------------------------------------------------------- 1 | function [phi,tempr] = nucleus(Nx,Ny,seed) 2 | 3 | format long; 4 | 5 | phi = zeros(Nx,Ny); 6 | tempr = zeros(Nx,Ny); 7 | 8 | for i=1:Nx 9 | for j=1:Ny 10 | if ((i-Nx/2)^2+(j-Ny/2)^2 < seed) 11 | phi(i,j) = 1.0; 12 | end 13 | end 14 | end 15 | 16 | end %endfunction 17 | -------------------------------------------------------------------------------- /snowflake/nucleus_8.m: -------------------------------------------------------------------------------- 1 | function [phi,tempr] = nucleus(Nx,Ny,seed) 2 | 3 | format long; 4 | 5 | %for i=1:Nx 6 | %for j=1:Ny 7 | 8 | %phi(i,j) = 0.0; 9 | %tempr(i,j) = 0.0; 10 | 11 | %end 12 | %end 13 | 14 | phi = zeros(Nx,Ny); 15 | tempr = zeros(Nx,Ny); 16 | 17 | for i=1:Nx 18 | for j=1:Ny 19 | if ((i-Nx/8)^2+(j-Ny/2)^2 < seed) 20 | phi(i,j) = 1.0; 21 | end 22 | if ((i-2*Nx/8)^2+(j-Ny/2)^2 < seed) 23 | phi(i,j) = 1.0; 24 | end 25 | if ((i-3*Nx/8)^2+(j-Ny/2)^2 < seed) 26 | phi(i,j) = 1.0; 27 | end 28 | if ((i-4*Nx/8)^2+(j-Ny/2)^2 < seed) 29 | phi(i,j) = 1.0; 30 | end 31 | if ((i-5*Nx/8)^2+(j-Ny/2)^2 < seed) 32 | phi(i,j) = 1.0; 33 | end 34 | if ((i-6*Nx/8)^2+(j-Ny/2)^2 < seed) 35 | phi(i,j) = 1.0; 36 | end 37 | if ((i-7*Nx/8)^2+(j-Ny/2)^2 < seed) 38 | phi(i,j) = 1.0; 39 | end 40 | if ((i-8*Nx/8)^2+(j-Ny/2)^2 < seed) 41 | phi(i,j) = 1.0; 42 | end 43 | end 44 | end 45 | 46 | end %endfunction 47 | -------------------------------------------------------------------------------- /spinodal_decomposition/matlab_spinodal_decomposition.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kipa200/Phase_field_code/98ccc5daa7520cb21529b88be73a3c3275be0380/spinodal_decomposition/matlab_spinodal_decomposition.pptx --------------------------------------------------------------------------------