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

📄 toyfdtd1 c source code listing.htm

📁 toyFDTD GNU
💻 HTM
📖 第 1 页 / 共 5 页
字号:
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtepsdx*(hz[i][j][k-1] - hz[i-1][j][k-1]));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Update the ez values:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=1; i&lt;(nx); i++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=1; j&lt;(ny); j++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(k=0; k&lt;(nz); k++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ez[i][j][k] += (dtepsdx*(hy[i][j-1][k] - hy[i-1][j-1][k]) -&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dtepsdy*(hx[i-1][j][k] - hx[i-1][j-1][k]));&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /////////////////////////////////////////////////////////////////////////&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // Compute the boundary conditions:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // OK, so I'm yanking your chain on this one.&nbsp; The PEC condition is&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // enforced by setting the tangential E field components on all the&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // faces of the mesh to zero every timestep (except the stimulus&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // face).&nbsp; But the lazy/efficient way out is to initialize those&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // vectors to zero and never compute them again, which is exactly&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // what happens in this code.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }// end mainloop&nbsp; /////////////////////////////////////////////////////////////////////&nbsp;&nbsp; // Output section:&nbsp;&nbsp;&nbsp; // The output routine is repeated one last time to write out&nbsp;&nbsp;&nbsp; // the last data computed.&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // time in simulated seconds that the simulation has progressed:&nbsp;&nbsp; currentSimulatedTime = dt*(double)iteration;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // print to standard output the iteration number&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; and current simulated time:&nbsp; fprintf(stdout, "#%d %lgsec", iteration, currentSimulatedTime);&nbsp; // 3D data output for the last timestep:&nbsp;&nbsp; // create the filename for this iteration,&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; which includes the iteration number:&nbsp; sprintf(filename, "c_%06d.bob", iteration);&nbsp; // open a new data file for this iteration:&nbsp; while ((openFilePointer = fopen(filename, "wb")) == NULL)&nbsp;&nbsp;&nbsp; {// if the file fails to open, print an error message to&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; standard output:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fprintf(stderr, "Difficulty opening c_%06d.bob", iteration);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; perror(" ");&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; // find the max and min values to be output this timestep:&nbsp;&nbsp;&nbsp; min = FLT_MAX;&nbsp; max = -FLT_MAX;&nbsp; for(i=0; i&lt;(nx+1); i++)&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=0; j&lt;(ny+1); j++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(k=0; k&lt;(nz); k++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ez[i][j][k] &lt; min) min = ez[i][j][k];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ez[i][j][k] &gt; max) max = ez[i][j][k];&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; // update the tracking variables for minimum and maximum&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; values for the entire simulation:&nbsp; if (min &lt; simulationMin) simulationMin = min;&nbsp; if (max &gt; simulationMax) simulationMax = max;&nbsp;&nbsp;&nbsp; // set norm to be max or min, whichever is greater in magnitude:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; norm = (fabs(max) &gt; fabs(min)) ? fabs(max) : fabs(min);&nbsp; if (norm == 0.0)&nbsp;&nbsp;&nbsp; {// if everything is zero, give norm a tiny value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; to avoid division by zero:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; norm = DBL_EPSILON;&nbsp;&nbsp;&nbsp; }&nbsp; scalingValue = 127.0/norm;&nbsp; // write to standard output the minimum and maximum values&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; from this iteration and the minimum and maximum values&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; that will be written to the bob file this iteration:&nbsp; fprintf(stdout, "\t%lg(%d) &lt; ez BoB &lt; %lg(%d)",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; min, (int)(127.0 + scalingValue*min),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; max, (int)(127.0 + scalingValue*max));&nbsp;&nbsp;&nbsp; // scale each ez value in the mesh to the range of integers&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; from zero through 254 and write them to the output&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; file for this iteration:&nbsp; for(k=0; k&lt;(nz); k++)&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(j=0; j&lt;(ny+1); j++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for(i=0; i&lt;(nx+1); i++)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; putc((int)(127.0 +&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; scalingValue*ez[i][j][k]), openFilePointer);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp; // close the output file for this iteration:&nbsp; fclose(openFilePointer);&nbsp;&nbsp;&nbsp; // write the dimensions and name of the output file for this&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; iteration to the viz file:&nbsp; fprintf(vizFilePointer, "%dx%dx%d %s\n", nx+1, ny+1, nz, filename);&nbsp; // write identification of the corners of the mesh and the max&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp; and min values for this iteration to the viz file:&nbsp; fprintf(vizFilePointer, "bbox: 0.0 0.0 0.0 %lg %lg %lg %lg %lg\n",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dx*(double)nx, dy*(double)ny, dz*(double)nz, min, max);&nbsp; /////////////////////////////////////////////////////////////////////////////&nbsp; // close the viz file for this simulation:&nbsp; fclose(vizFilePointer);&nbsp; /////////////////////////////////////////////////////////////////////////////&nbsp; // write some progress notes to standard output:&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; // print out how much memory has been allocated:&nbsp;&nbsp;&nbsp; fprintf(stdout, "Dynamically allocated %d bytes\n", allocatedBytes);&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; // print out some simulation parameters:&nbsp;&nbsp; fprintf(stdout, "PLOT_MODULUS = %d\n", PLOT_MODULUS);&nbsp;&nbsp; fprintf(stdout, "rectangular waveguide\n");&nbsp;&nbsp; fprintf(stdout, "Stimulus = %lg Hertz ", FREQUENCY);&nbsp;&nbsp; fprintf(stdout, "continuous plane wave\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "Meshing parameters:\n");&nbsp;&nbsp; fprintf(stdout, "%dx%dx%d cells\n", nx, ny, nz);&nbsp;&nbsp; fprintf(stdout, "dx=%lg, dy=%lg, dz=%lg meters\n", dx, dy, dz);&nbsp;&nbsp; fprintf(stdout, "%lg x %lg x %lg meter^3 simulation region\n",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GUIDE_WIDTH, GUIDE_HEIGHT, LENGTH_IN_WAVELENGTHS*lambda);&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "Time simulated was %lg seconds, %d timesteps\n",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; totalSimulatedTime, MAXIMUM_ITERATION);&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "3D output scaling parameters:\n");&nbsp;&nbsp; fprintf(stdout, "Autoscaling every timestep\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; // print out simulationMin and simulationMax:&nbsp;&nbsp; fprintf(stdout, "Minimum output value was %lg \n", simulationMin);&nbsp;&nbsp; fprintf(stdout, "Maximum output value was %lg \n", simulationMax);&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; // print out a bob command line, including the dimensions&nbsp;&nbsp;&nbsp; //&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; of the output files:&nbsp;&nbsp; fprintf(stdout, "bob -cmap chengGbry.cmap -s %dx%dx%d *.bob\n",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nx+1, ny+1, nz);&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; // print out a viz command line:&nbsp;&nbsp; fprintf(stdout, "viz ToyFDTD1c.viz\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;&nbsp; fprintf(stdout, "\n");&nbsp;<FONT color=#000000>}// end main&nbsp;&nbsp;&nbsp;</FONT><FONT color=#ff0000>&nbsp;</FONT></FONT></FONT></PRE>&nbsp;</TD></TR>  <TR>    <TD></TD>    <TD>      <CENTER><B><FONT face=Arial,Helvetica><FONT size=+2><A       href="http://www.borg.umn.edu/toyfdtd/ToyFDTD.html">ToyFDTD       home</A></FONT></FONT></B>       <P><B><FONT face=Arial,Helvetica><FONT size=+1><A       href="http://www.borg.umn.edu/toyfdtd/ToyFDTD1.html">back to ToyFDTD1 main       page</A></FONT></FONT></B> <BR><B><FONT face=Arial,Helvetica><FONT       size=+1><A       href="mailto:lemiller@borg.umn.edu">lemiller@borg.umn.edu</A></FONT></FONT></B></CENTER></P></TD></TR></TBODY></TABLE><P><IMG height=92 hspace=180 src="ToyFDTD1 C source code listing.files/ToyFDTDbar.gif" width=750 align=textTop SGI_SRC="/hosts/congo.borg.umn.edu/usr/local/www/toyfdtd/logos/ToyFDTDbar.gif" NOSAVE> <BR>&nbsp; <BR>&nbsp; <BR>&nbsp; </P></BODY></HTML>

⌨️ 快捷键说明

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