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

📄 phc8.cpp

📁 基于时域有限差分的遗传算法主程序
💻 CPP
字号:
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
using namespace std;

/* 全局变量 */
double p=0.3975e-6;
vector<vector<double> > E;
vector<vector<double> > ER;
/* 函数定义 */
int round(double,double);
double shape(double,double,double);

double shape(double x,double y,double width)
{
	double z;
    if (x<10)   z=0.5*width*(cos(3.1415*x*0.1)+1)*p+0.5*p;
	if ((x>9)&&(x<20))    z=0.5*p;
    if ((x>19)&&(x<30))   z=0.5*width*(cos((29-x)*3.1415*0.1)+1)*p+0.5*p;
	return z;
}

int round(double x,double y)
{
	int z;
    z=int (x/y+0.5);
	return z;
}

void main()
{
	int i,j;
    double r=0.3*p;
    double pgrid = 50;
    double dx=p/pgrid;
    double dz=p/pgrid;
    double L=30*p;
    double H=7*p;
	double Epsilon=1;
    double EpsilonR=2.8*2.8;
	double dist;
    double icenter;
    double jcenter;
	double offset;
	double width=1.8;
    int tx=round(L,dx);
	int tz=round(H,dz);
	E = vector<vector<double> > (tz,vector<double>(tx,EpsilonR));
    ER = vector<vector<double> > (2*tz,vector<double>(tx,EpsilonR));

	/*char file1[] = "E:\\GA\\SGA\\data\\file1.txt";
	ifstream filedata(file1,ios::in);
	filedata >> width;
	filedata.close();*/
	
    for (i=0; i<=tx-1 ; i++)
	{
		for (j=0; j<=tz-1; j++)
		{
			icenter = floor((i+0.5)*dx/p);
		    if(icenter < 10) 
			{
				jcenter=floor((j+0.5)*dz/p-offset/p);
                offset=shape(icenter,jcenter,width);
			    if((jcenter > -1) && (jcenter < 5))
					dist=sqrt(pow(((i+0.5)*dx-(icenter+0.5)*p),2.0)+pow(((j+0.5)*dz-((jcenter+0.5)*p+offset)),2.0));
                else
                    dist=10000;
				if (dist<=r)
					E[j][i]=Epsilon;
			}
			
            if((icenter > 9) && (icenter < 20))
			{
                jcenter=floor((j+0.5)*dz/p-offset/p);
				offset=shape(icenter,jcenter,width);
                if((jcenter > -1) && (jcenter < 5))
                    dist=sqrt(pow(((i+0.5)*dx-(icenter+0.5)*p),2.0)+pow(((j+0.5)*dz-((jcenter+0.5)*p+offset)),2.0)); 
                else
                    dist=10000;
                if (dist<=r)
					E[j][i]=Epsilon;
			}
                    
            if((icenter > 19) && (icenter < 30))
			{
                jcenter=floor((j+0.5)*dz/p-offset/p);
				offset=shape(icenter,jcenter,width);
                if((jcenter > -1) && (jcenter < 5))
                    dist=sqrt(pow(((i+0.5)*dx-(icenter+0.5)*p),2.0)+pow(((j+0.5)*dz-((jcenter+0.5)*p+offset)),2.0));
                else
                    dist=10000;
                if (dist<=r)
                    E[j][i]=Epsilon;
			}

		    ER[j+tz][i]=E[j][i];
			ER[tz-j-1][i]=E[j][i];
		}		
	}
	
	char file[100];
    sprintf(file,"E:\\PhC\\PhC data\\file8.txt");
	ofstream eps(file,ios::out);
	for(j=0; j<=2*tz-1; j++)
	{
		for(i=0; i<=tx-1; i++)
			eps<< ER[j][i] << '\t';
		eps<< endl;
	}
	eps.close();
}

⌨️ 快捷键说明

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