├── .DS_Store ├── README.md ├── gradient_mat.m ├── laplacian.m ├── main.m ├── nucleus.m ├── printResult.py └── test.m /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhiyingq/PhaseFieldSimulation/7407d14438f46fc6d3980aee74c7841df0856ed4/.DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # PhaseFieldSimulation 2 | 3 | This is phase-field simulation program on the crystallization of polymer dendritic crystal. Call the "main.m" file to generate data and call "printResult.py" to print the reuslt. You may change the paths to the files in the "printResult.py" file. 4 | -------------------------------------------------------------------------------- /gradient_mat.m: -------------------------------------------------------------------------------- 1 | %This function calculates the one-dimensinal gradients for x and y 2 | %directions of a given matrix. 3 | 4 | function[matdx,matdy]=gradient_mat(matx,Nx,Ny,dx,dy) 5 | 6 | format long; 7 | 8 | % the built-in function "gradient" is used in this function 9 | 10 | [matdx,matdy]=gradient(matx); 11 | 12 | %-- for periodic boundaries: 13 | 14 | matdx(1:Nx,1)=0.5*(matx(1:Nx,2)-matx(1:Nx,Nx)); 15 | matdx(1:Nx,Nx)=0.5*(matx(1:Nx,1)-matx(1:Nx,Nx-1)); 16 | 17 | matdy(1,1:Ny)=0.5*(matx(2,1:Ny)-matx(Ny,1:Ny)); 18 | matdy(Ny,1:Ny)=0.5*(matx(1,1:Ny)-matx(Ny-1,1:Ny)); 19 | 20 | matdx=2.0*matdx/dx; 21 | matdy=2.0*matdy/dy; 22 | 23 | end 24 | 25 | -------------------------------------------------------------------------------- /laplacian.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhiyingq/PhaseFieldSimulation/7407d14438f46fc6d3980aee74c7841df0856ed4/laplacian.m -------------------------------------------------------------------------------- /main.m: -------------------------------------------------------------------------------- 1 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 | % Phase Field Finite Difference % 3 | % Code For % 4 | % Dendritic Crystallization % 5 | % Optimized For Matlab % 6 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 | 8 | %== get initial wall time: (format long:显示15位双精度) 9 | time0=clock(); 10 | format long; 11 | 12 | %-- Simulation cell parameters: 13 | Nx=300; %x格点数 14 | Ny=300; %y格点数 15 | NxNy=Nx*Ny; 16 | 17 | dx=0.03; %grid spacing between two grid points in the x-direction 18 | dy=0.03; %grid spacing between two grid points in the x-direction 19 | 20 | %-- Time integration parameters: 21 | 22 | nstep=4000; % time integration seps 23 | nprint=50; % Output frequency to write the results to file 24 | dtime=1.e-4; % Time increment for numerical integration 25 | 26 | %-- Material specific parameters: 27 | 28 | tau=0.0003; % τ 29 | epsilonb=0.01; % ε拔 30 | mu=1.0; % μ 31 | kappa=1.8; % κ 32 | delta=0.02; % δ 33 | aniso=6.0; % j 34 | alpha=0.9; % α 35 | gamma=10.0; % γ 36 | teq=1.0; % Teq 37 | theta0=0.2; % θ0 38 | seed=5.0; % The size of the initial seed (In grid numbers) 39 | % See function nucleus 40 | pix=4.0*atan(1.0); % The value of pi 41 | 42 | %--- Initialize and introduce initial nuclei; 43 | 44 | [phi,tempr]=nucleus(Nx,Ny,seed); 45 | % Initialize the phi and temper arrays and introduce the seed in center 46 | %--- Laplacian templet 47 | 48 | [laplacian]=laplacian(Nx,Ny,dx,dy); 49 | %Calculate the finite difference template for the laplacians 50 | 51 | %-- Evolution, calculate Eqs4.51 and Eqs4.52 52 | %-- 53 | 54 | for istep=1:nstep 55 | 56 | phiold=phi; %首先定义了phi_old和phi是相等的 57 | 58 | %-- 59 | % calculate the laplacians and epsilon: 60 | %-- 61 | 62 | phi2=reshape(phi',NxNy,1); %将phi(Nx,Ny)转换成一维数组phi2(Nx*Ny),phi'为phi的转置,只 63 | %只有转置后才能正确地按照第一行、第二行的顺序进行reshape 64 | lap_phi2=laplacian*phi2; %计算phi2的拉普拉斯值,?2φ 65 | 66 | [lap_phi]=vec2matx(lap_phi2,Nx); %将lap_phi2(Nx*Ny)转化到二维矩阵lap_phi(Nx,Ny) 67 | 68 | %-- 69 | 70 | tempx=reshape(tempr',NxNy,1); %将tempr(Nx,Ny)转换为一维数组tempx(NxNy) 71 | 72 | lap_tempx=laplacian*tempx; %计算?2T 73 | 74 | [lap_tempr]=vec2matx(lap_tempx,Nx); %将lap_tempx(Nx*Ny)转换为二维矩阵lap_tempr(Nx,Ny) 75 | 76 | %--gradients of phi: 77 | 78 | [phidy,phidx]=gradient_mat(phi,Nx,Ny,dx,dy); %计算Cartesian derivatives of phi,?φ 79 | %一维方向上的梯度,但加上了边界条件以及考虑了dx和dy 80 | %calculate angle: 81 | 82 | theta=atan2(phidy,phidx); %calculate theta:θ=tan-1(φy/φx) 83 | %这里的atan2是定义在(-π,π)上的,这里的theta是一个数组 84 | %--epsilon and its derivative: 85 | 86 | epsilon=epsilonb*(1.0+delta*cos(aniso*(theta-theta0))); %calculate ε,它是一个数组 87 | 88 | epsilon_deriv=-epsilonb*aniso*delta*sin(aniso.*(theta-theta0)); 89 | %calculate ?ε/?θ .*指的是两个矩阵的对应元素相乘,aij*bij,而不是矩阵乘法,它是一个数组 90 | %--- first term of Eq.4.51 计算第一项 91 | 92 | dummyx=epsilon.*epsilon_deriv.*phidx; 93 | 94 | [term1,dummy]=gradient_mat(dummyx,Nx,Ny,dx,dy); 95 | 96 | %--- second term of Eq.4.51 计算第二项 97 | 98 | dummyy=-epsilon.*epsilon_deriv.*phidy; 99 | 100 | [dummy,term2]=gradient_mat(dummyy,Nx,Ny,dx,dy); 101 | 102 | %--- factor m: 103 | 104 | m=(alpha/pix)*atan(gamma*(teq-tempr)); 105 | 106 | %-- Time integration: 107 | 108 | phi=phi+(dtime/tau)*(term1+term2+epsilon.^2.*lap_phi+... 109 | phiold.*(1.0-phiold).*(phiold-0.5+m)); 110 | 111 | %-- evolve temperature: 112 | 113 | tempr=tempr+dtime*lap_tempr+kappa*(phi-phiold); 114 | 115 | 116 | %---- print results 117 | 118 | 119 | if(mod(istep,nprint)==0) 120 | 121 | fprintf('done step: %5d\n',istep); 122 | 123 | fname1=sprintf('time_%d_phi.txt',istep); 124 | out1=fopen(fname1,'w'); 125 | 126 | for i=1:Nx 127 | for j=1:Ny 128 | 129 | fprintf(out1,'%14.6e\n',phi(i,j)); 130 | end 131 | end 132 | 133 | fname2=sprintf('time_%d_tempr.txt',istep); 134 | out2=fopen(fname2,'w'); 135 | 136 | for i=1:Nx 137 | for j=1:Ny 138 | 139 | fprintf(out2,'%14.6e\n',tempr(i,j)); 140 | end 141 | end 142 | 143 | 144 | %write_vtk_grid_values(Nx,Ny,dx,dy,istep,phi,tempr); 145 | 146 | end 147 | end 148 | 149 | %calculate compute time: 150 | 151 | compute_time=etime(clock(),time0); 152 | fprintf('Compute Time: %10d\n',compute_time); 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | -------------------------------------------------------------------------------- /nucleus.m: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhiyingq/PhaseFieldSimulation/7407d14438f46fc6d3980aee74c7841df0856ed4/nucleus.m -------------------------------------------------------------------------------- /printResult.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | #a=np.random.random((5,5)) 3 | #np.savetxt('test.txt',a,fmt='%.18e',delimiter=' ') 4 | 5 | fname_phi1='/Users/changing/Documents/MATLAB/Graduation/time_50_phi.txt' 6 | fname_phi2='/Users/changing/Documents/MATLAB/Graduation/time_1000_phi.txt' 7 | fname_phi3='/Users/changing/Documents/MATLAB/Graduation/time_2000_phi.txt' 8 | fname_phi4='/Users/changing/Documents/MATLAB/Graduation/time_4000_phi.txt' 9 | 10 | 11 | a=np.loadtxt(fname_phi1,dtype=np.float64) 12 | a=a.reshape((300,300)) 13 | 14 | b=np.loadtxt(fname_phi2,dtype=np.float64) 15 | b=b.reshape((300,300)) 16 | 17 | c=np.loadtxt(fname_phi3,dtype=np.float64) 18 | c=c.reshape((300,300)) 19 | 20 | d=np.loadtxt(fname_phi4,dtype=np.float64) 21 | d=d.reshape((300,300)) 22 | 23 | 24 | import matplotlib.pyplot as plt 25 | plt.figure() 26 | plt.subplot(221) 27 | plt.imshow(a) 28 | plt.subplot(222) 29 | plt.imshow(b) 30 | plt.subplot(223) 31 | plt.imshow(c) 32 | plt.subplot(224) 33 | plt.imshow(d) 34 | 35 | plt.show() 36 | -------------------------------------------------------------------------------- /test.m: -------------------------------------------------------------------------------- 1 | [a]=laplacian(4,4,0.3,0.3); --------------------------------------------------------------------------------