📄 fbg_write.cpp
字号:
/*
本程序作用:编写光栅结构和调用函数计算其反射透射谱以及脉冲时延特性
p_表示参数变量;s_表示开关变量;n_表示数量变量;
相移个数最多20个;
*/
#include <iostream>
#include <complex>
#include <fstream>
using namespace std;
#define PI 3.1415926535898
extern void DFBLaser(long double,long double,int,int);
void main()
{
long double np,n0,dnmax,wave_min,wave_max,fbg_length,sam_period,cent_lambda,p_ch_prd,wide_gau,phase_eps,att=0;
float sam_ocr,p_dnmax;//sam_ocr用于接收 Occupy Rate,para_用做参数存储
int n_grating,n_wave,n_ap_cos,n_ps,p_ap_gau,p_ch_pha,n_eps;//长度分段个数,波长分段个数,
long double *z,*dcm,*acm,*k,*phase,*dz,*n;//写出时使用的位置,直流调制,交流调制,波矢,相位,间距,高斯半宽wide_gau
int i,j;
char s_ap_cos,s_sam,s_ap_gau,s_ch_pha,s_ch_prd,s_ps,s_eps;
char ent;//
FILE *PARA_input,*FBG_struct,*FBG_n;//读取和写入指针。
long double posit_ps[21],phase_ps[20],posit_eps[21];//posit_ps多一个,用来存放尾地址1
//打开文件,无法打开时报错并退出。
if((PARA_input=fopen("PARA_input.dat","r"))==NULL)
{
cout<<"无法找到PARA_input.dat文件。回车键退出:";
cin>>ent;
exit(0);
}
else
cout<<"PARA_input.dat文件打开成功。"<<endl;
FBG_struct=fopen("FBG_struct.dat","w");//写出光栅结构
FBG_n=fopen("FBG_n.dat","w");//写n(z)
//从文件中读取参数
fscanf(PARA_input,"初始折射率n0 :%lf\n",&n0);
fscanf(PARA_input,"折射率dnmax :%lf\n",&dnmax);
fscanf(PARA_input,"折射率调制系数 :%f\n",&p_dnmax);
fscanf(PARA_input,"最短波长(nm) :%lf\n",&wave_min);
fscanf(PARA_input,"中心波长(nm) :%lf\n",¢_lambda);
fscanf(PARA_input,"最长波长(nm) :%lf\n",&wave_max);
fscanf(PARA_input,"波长分段个数 :%d\n",&n_wave);
fscanf(PARA_input,"光栅长度(mm) :%lf\n",&fbg_length);
fbg_length*=1e6;//单位统一为纳米;
fscanf(PARA_input,"光栅分段个数 :%d\n",&n_grating);
fscanf(PARA_input,"是否进行采样 :%c\n",&s_sam);
fscanf(PARA_input,"采样周期(mm) :%lf\n",&sam_period);
sam_period*=1e6;
fscanf(PARA_input,"采样占空比 :%f\n",&sam_ocr);
fscanf(PARA_input,"等效相移调制 :%c\n",&s_eps);
fscanf(PARA_input,"等效相移个数 :%d\n",&n_eps);
fscanf(PARA_input,"等效相移位置 :%lf",&posit_eps[0]);
for(i=1;i<n_eps;i++)
fscanf(PARA_input,",%lf",&posit_eps[i]);
for(i--;i>-1;i--)
posit_eps[i]*=1e6;
fscanf(PARA_input,"\n等效相移大小 :%lf",&phase_eps);
//for(i=1;i<n_eps;i++)
// fscanf(PARA_input,",%lf",&phase_eps[i]);
if((n_grating*sam_period/fbg_length*sam_ocr)-(int)(n_grating*sam_period/fbg_length*sam_ocr)>0.00001)
{
cout<<"调整参数!"<<endl;
cin>>ent;
return;
}
fscanf(PARA_input,"\n是否余弦切趾 :%c\n",&s_ap_cos);
fscanf(PARA_input,"余弦切趾个数 :%d\n",&n_ap_cos);
fscanf(PARA_input,"是否高斯切趾 :%c\n",&s_ap_gau);
fscanf(PARA_input,"高斯切趾半宽 :L/%lf\n",&wide_gau);
wide_gau=fbg_length/wide_gau;//把分段数转化为半宽
fscanf(PARA_input,"超高斯系数 :%d\n",&p_ap_gau);
fscanf(PARA_input,"啁啾周期调制 :%c\n",&s_ch_prd);
fscanf(PARA_input,"周期啁啾参数c :%lf\n",&p_ch_prd);
fscanf(PARA_input,"啁啾相位调制 :%c\n",&s_ch_pha);
fscanf(PARA_input,"相位啁啾参数F :%dPI\n",&p_ch_pha);
if(s_ch_prd!='y')
p_ch_prd=0;
if(s_ch_pha!='y')
p_ch_pha=0;
if((s_ch_prd=='y')&&(s_ch_pha=='y'))
{
cout<<"不可同时进行啁啾相位和周期调制!回车键退出:";
cin>>ent;
exit(0);
}
fscanf(PARA_input,"是否相移调制 :%c\n",&s_ps);
fscanf(PARA_input,"相移调制个数 :%d\n",&n_ps);
fscanf(PARA_input,"相移调制位置 :%lf",&posit_ps[0]);
for(i=1;i<n_ps;i++)
fscanf(PARA_input,",%lf",&posit_ps[i]);
posit_ps[i]=1;//这句话用于后面的循环至i+1的位置
fscanf(PARA_input,"\n相移相位(PI/*) :%lf\n",&phase_ps[0]);
for(i=1;i<n_ps;i++)
fscanf(PARA_input,",%lf",&phase_ps[i]);
//下次添加首先写出 \n 换行再取值。
//动态分配储存块,并对储存块分配进行检验
z=(long double *)calloc(n_grating, sizeof(long double));//返回一个指针,该指针指向的空闲空间足以容纳由n_grating个long double长的对象组成的数组
dcm=(long double *)calloc(n_grating, sizeof(long double));
acm=(long double *)calloc(n_grating, sizeof(long double));
k=(long double *)calloc(n_grating, sizeof(long double));
phase=(long double *)calloc(n_grating, sizeof(long double));
dz=(long double *)calloc(n_grating, sizeof(long double));
n=(long double *)calloc(n_grating, sizeof(long double));
if((z==NULL)||(dcm==NULL)||(acm==NULL)||(k==NULL)||(phase==NULL)||(dz==NULL))
{
cout<<"不能分配内存!回车键退出:";
cin>>ent;
exit(0);
}
//对写出参数进行初次赋值
np=n0+dnmax;
for(i=0;i<n_grating;i++)
{
dz[i]=fbg_length/n_grating;//每个小段的长度等于总长度除以光栅个数(dz长度变化可以在这里修改)
z[i]=(i==0)?(-fbg_length/2):(z[i-1]+dz[i-1]);//以中点为光栅z=0点,每个光栅的位置等于上一光栅位置加上上一光栅长度dl
dcm[i]=n0;//直流调制保持为n0
acm[i]=dnmax*p_dnmax;
k[i]=2*PI/(cent_lambda/2/np+p_ch_prd*z[i]);//(cent_lambda/2/np)为光栅周期
phase[i]=p_ch_pha*PI*z[i]*z[i]/fbg_length/fbg_length;//啁啾相位调制(!!!!这一段有问题)
}
//cosine切趾调制
if(s_ap_cos=='y')//如果进行切趾调制,执行。
for(i=0;i<n_grating;i++)
acm[i]*=fabs(cos(PI*z[i]/fbg_length*n_ap_cos));
//高斯切趾调制
if(s_ap_gau=='y')
for(i=0;i<n_grating;i++)
acm[i]*=exp(-pow(4*log(2)*pow(z[i],2)/pow(wide_gau,2),p_ap_gau));//这里做了一个整个光栅上为半宽为wide_gau的高斯调制
//采样
if(s_sam=='y')
for(i=0;i<n_grating;i++)
if(i%(int)(n_grating*sam_period/fbg_length)>=(int)(n_grating*sam_period/fbg_length*sam_ocr))
acm[i]=0;
//等效相移
if(s_eps=='y')
{
for(i=1;i<n_eps;i++)
posit_eps[i]+=posit_eps[i-1]+sam_period*phase_eps;
for(i=0;i<n_eps;i++)
for(j=n_grating-1;j>n_grating/fbg_length*posit_eps[i]-1;j--)
acm[j]=acm[j-(int)(n_grating*sam_period/fbg_length*phase_eps)];
}
//相移
if(s_ps=='y')
for(i=0;i<n_ps;i++)
for(j=(int)(n_grating*posit_ps[i]);j<n_grating*posit_ps[i+1];j++)
if(phase_ps[i]!=0)
phase[j]+=PI/phase_ps[i];
//写n
for(i=0;i<n_grating;i++)
{
n[i]=np+acm[i]*cos(k[i]*z[i]+phase[i]);
fprintf(FBG_n,"%.7f %.14f\n",z[i]/1e6,n[i]);
}
cout<<"FBG_n.dat is written."<<endl;
//写光栅结构文件
for(i=0;i<n_grating;i++)
fprintf(FBG_struct,"%.6f %.7f %.14f %.14f %.14f %.7f\n",z[i]/1e6,dcm[i],acm[i],k[i],phase[i],dz[i]/1e6);//程序中长度均用纳米表示,文件中用微米表示
printf("FBG_struct.dat is written.\n");
//调用函数
free(z);
free(dcm);
free(acm);
free(k);
free(phase);
free(dz);
fclose(PARA_input);
fclose(FBG_struct);
fclose(FBG_n);
DFBLaser(wave_max,wave_min,n_wave,n_grating);
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -