⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fdtd1_4.c

📁 用c语言仿真了FDTD算法,给大家提供一种思路
💻 C
字号:
/*FD1D_1.4.c  1D FDTD simulation */
/*simulation of a sinusoidal wave hitting a dielectric medium */
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#define KE 200                /*KE is the number of cells to be used */
#define pi 3.1415926535
main ()
{
  float ex[KE],hy[KE];
  float low_m1,low_m2,high_m1,high_m2;
  int   n,k,NSTEPS,kstart;
  float T,ddx,dt,epsz,epsilon,sigma,eaf;
  float cb[KE];
  float pulse;
  float freq_in;
  FILE *fp , *fopen();

  /*initialize*/
  for (k=1; k<KE;k++)
  {
    ex[k]=0;
    hy[k]=0;
    cb[k]=0.5;
  }    
printf("Dielectric starts at -->");
scanf("%d",&kstart);
printf("Epsilon -->");
scanf("%f",&epsilon);
printf("%d %6.2f  \n",kstart,epsilon);
for (k=kstart;k<=KE;k++)
{
  cb[k]=0.5/epsilon;
}
for (k=1;k<=KE;k++)
{
  printf("%2d %6.3f \n",k,cb[k]);}
	T=0;
	NSTEPS=1;
	ddx=0.01;     /*set the cell size to 1 cm*/
	dt=ddx/(2*3e8);   /*calculate the time step*/
	/*These parameters specify the input pulse*/
	printf("Input freq_in (MHz) -->");
	scanf("%f",&freq_in);
	freq_in=freq_in*1e6;
	printf("%8.0f\n",freq_in);
  while (NSTEPS>0) {
  printf("NETEPS --> ");	/*NETEPS is the center of times the main loop has excuted*/
  scanf ("%d",&NSTEPS);
  printf("%d\n",NSTEPS);
  n=0;
  for (n=1;n<=NSTEPS;n++)
  {
    
    T=T+1;    /*T keeps track of the totle number of times the main loop is excuted*/
 /*main FDTD loop*/
 /*calculate the EX filed*/
 for (k=1;k<KE;k++)
 {
   ex[k]=ex[k]+cb[k]*(hy[k-1]-hy[k]);}
   /*put a sinusoidal source at the cell 5 p0*/
   pulse=sin(2*pi*freq_in*dt*T);
   ex[5]=ex[5]+pulse;
   printf("%5.1f %6.3f %6.3f\n",T,pulse,ex[5]);
   /*Absorbing Boundary Condition */
   ex[0]=low_m2;
   low_m2=low_m1;
   low_m1=ex[1];
   ex[KE-1]=high_m2;
   high_m2=high_m1;
   high_m1=ex[KE-2];
   /*calculate the Hy field */
  for (k=0;k<KE-1;k++)
 {
   hy[k]=hy[k]+0.5*(ex[k]-ex[k+1]);
 }
 /*end of the main FDTD loop*/
 /*at the end of the calculation,print out the ex and hy field*/
 for (k=1;k<KE;k++)
 {
  printf("%3d %6.3f %6.3f\n",k,ex[k],hy[k]); }
  
  /*write the E filed out to a file "EX"*/
  fp=fopen("Ex","w");
  for (k=1;k<=KE;k++)
  {
    fprintf (fp,"%6.3f\n",ex[k]);}
    fclose(fp);
    fp=fopen("Hy","w");
  for (k=1;k<=KE;k++)
  {
    fprintf (fp,"%6.3f\n",hy[k]);}
    fclose(fp);
    printf("T=%5.0f\n",T);
  }
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -