📄 load_save_file_data.cpp
字号:
if ( !( str[k]>='0' && str[k]<='9') )
return 2; //wrong file content
k++;
}
}
j++;
if (j == ny)
{
j = 0;
i++;
}
}
fclose(fname);
}
else
{
return 1; //faild to open the file
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Load 1D matrix with long elements
///////////////////////////////////////////////////////////////////////////////////////
int load_1D_long(long *Tomb, long nx, char * ut_nev)
{
FILE *fname;
char str[100];
long i = 0;
if( (fname = fopen( ut_nev, "r" )) != NULL )
{
while ( fscanf(fname,"%s", &str) != EOF )
{
if (i==nx)
return 3; //wrong dimensions
Tomb[i] = atol(str);
if (Tomb[i] == 0)
{
long k = 0;
while(str[k] != '\0')
{
if ( !( str[k]>='0' && str[k]<='9') )
return 2; //wrong file content
k++;
}
}
i++;
}
fclose(fname);
}
else
{
return 1; //faild to open the file
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Load 1D matrix with double elements
///////////////////////////////////////////////////////////////////////////////////////
int load_1D_double(double *Tomb, long nx, char * ut_nev)
{
FILE *fname;
char str[100];
long i = 0;
if( (fname = fopen( ut_nev, "r" )) != NULL )
{
while ( fscanf(fname,"%s", &str) != EOF )
{
if (i==nx)
return 3; //wrong dimensions
Tomb[i] = atof(str);
if (Tomb[i] == 0)
{
long k = 0;
while(str[k] != '\0')
{
if ( !( str[k]>='0' && str[k]<='9') )
return 2; //wrong file content
k++;
}
}
i++;
}
fclose(fname);
}
else
{
return 1; //faild to open the file
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Load 3D Geom matrix with long elements
///////////////////////////////////////////////////////////////////////////////////////
int load_3D_Geom_long(long ***Tomb, long nx, long ny, long nz, char *ut_nev)
{
FILE *fname;
long i, j, k, n_x, n_y, n_z;
char str[100];
if( (fname = fopen( ut_nev, "r" )) != NULL )
{
long cik = 0;
while ( fscanf(fname,"%s", &str) != EOF )
{
//the first line contains the dimensions of the matrix
switch (cik)
{
case 0: //the x dimension
n_x = atoi(str);
if (n_x != nx)
{
//"wrong file dimension\n"
return 2;
}
cik = 1;
break;
case 1: //the y dimension
n_y = atoi(str);
if (n_y != ny)
{
//"wrong file dimension\n"
return 2;
}
cik = 2;
break;
case 2: //the z dimension
n_z = atoi(str);
if (n_z != nz)
{
//"wrong file dimension\n"
return 2;
}
cik = 3;
break;
case 3:
i = atoi(str);
if (i >= nx)
{
//"wrong file dimension\n"
return 2;
}
cik = 4;
break;
case 4:
j = atoi(str);
if (j >= ny)
{
//"wrong file dimension\n"
return 2;
}
cik = 5;
break;
case 5:
k = atoi(str);
if (k >= nz)
{
//"wrong file dimension\n"
return 2;
}
cik = 6;
break;
case 6:
Tomb[i][j][k] = atoi(str);
cik = 3;
break;
}
}
fclose(fname);
}
else
{
return 1; //"faild to open the file"
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Save xy plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
int save_3D_xy(double ***Tomb, long n_x_a, long n_x_b, long n_y_a, long n_y_b,
long nz, long n_t, char *ut_nev)
{
long i, j;
char *File_Name = NULL;
File_Name =(char *) calloc(512,sizeof(char));
if (!File_Name)
{
return 1;
}
sprintf(File_Name, "%s_%d_%d_%d_%d_%d_%d_xy.dat",ut_nev,n_x_a,n_x_b,n_y_a,n_y_b,nz,n_t);
FILE *fp = NULL;
if ((fp=fopen(File_Name, "w+")) ==NULL)
{
free(File_Name);
return 2;
}
else
{
for (i = n_x_a; i < n_x_b; i++)
{
for (j = n_y_a; j < n_y_b; j++)
{
fprintf(fp,"%6.12f ",Tomb[i][j][nz]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
free(File_Name);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Save xz plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
int save_3D_xz(double ***Tomb, long n_x_a, long n_x_b, long ny, long n_z_a, long n_z_b,
long n_t, char *ut_nev)
{
long i, k;
char *File_Name = NULL;
File_Name =(char *) calloc(512,sizeof(char));
if (!File_Name)
{
return 1;
}
sprintf(File_Name, "%s_%d_%d_%d_%d_%d_%d_xz.dat",ut_nev,n_x_a,n_x_b,ny,n_z_a,n_z_b,n_t);
FILE *fp = NULL;
if ((fp=fopen(File_Name, "w+")) ==NULL)
{
free(File_Name);
return 2;
}
else
{
for (i=n_x_a; i<n_x_b; i++)
{
for (k=n_z_a; k<n_z_b; k++)
{
fprintf(fp,"%6.12f ",Tomb[i][ny][k]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
free(File_Name);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Save yz plane from 3D data structure
///////////////////////////////////////////////////////////////////////////////////////
int save_3D_yz(double ***Tomb, long nx, long n_y_a, long n_y_b, long n_z_a, long n_z_b,
long n_t, char *ut_nev)
{
long j, k;
char *File_Name = NULL;
File_Name =(char *) calloc(512,sizeof(char));
if (!File_Name)
{
return 1;
}
sprintf(File_Name, "%s_%d_%d_%d_%d_%d_%d_yz.dat",ut_nev,nx,n_y_a,n_y_b,n_z_a,n_z_b,n_t);
FILE *fp = NULL;
if ((fp=fopen(File_Name, "w+")) ==NULL)
{
free(File_Name);
return 2;
}
else
{
for (j=n_y_a; j<n_y_b; j++)
{
for (k=n_z_a; k<n_z_b; k++)
{
fprintf(fp,"%6.12f ",Tomb[nx][j][k]);
}
fprintf(fp,"\n");
}
fclose(fp);
}
free(File_Name);
return 0;
}
///////////////////////////////////////////////////////////////////////////////////////
//Load the parameters of the materials with Lorentz dispersion and initialize the
//Mat matrix
///////////////////////////////////////////////////////////////////////////////////////
int Load_Param_LorentzDisp(char **path_name_MatParam, double ***Mat, long n_Mat)
{
int i, j;
//temporary container for the Lorentz material parameters
double **Mat_temp = NULL;
Mat_temp = (double **)calloc(n_Mat,sizeof(double *));
if (Mat_temp == NULL)
{
//memory allocation problem
return 1;
}
//create a temporary matrix which will be used to create the y dimension of Mat
long *n_MatSize_y = NULL;
n_MatSize_y = (long *) calloc(n_Mat,sizeof(long));
if (n_MatSize_y == NULL)
{
//memory allocation problem
return 1;
}
//open the material files and get the number of Lorentz terms
FILE *fname = NULL;
long ind_start, ind_end, ind_length, ind, nr_term;
long jel = 0;
char line[512], *str = NULL;
for ( i = 0; i < n_Mat; i++)
{
//open the file
if( (fname = fopen( path_name_MatParam[i], "r" )) != NULL )
{
nr_term = 0;
j = 0;
while (fgets(line,512,fname) != NULL) //read a line from the file
{
if (line[0] != '/' && line[1] != '/') //if the line starts with // is a commment
{
long k = 0;
while(line[k] != '\0')
{
if ( line[k]>='0' && line[k]<='9' && jel == 0)
{
jel = 1;
ind_start = k;
}
if ( (line[k] >= '0' && line[k] <= '9') &&
(isdigit(line[k+1]) == 0 && line[k+1] != '.' && line[k+1] != 'e' ) )
{
jel = 0;
ind_end = k;
ind_length = ind_end - ind_start;
str = (char *)calloc(ind_length+2,sizeof(char));
for (ind = 0; ind < ind_length+1; ind++)
{
str[ind] = line[ind_start+ind];
}
str[ind_length+1] = '\0';
if(nr_term == 0)
{
//the first element is the number of Lorentz terms
n_MatSize_y[i] = 3*atoi(str) + 3;
Mat_temp[i] = (double*)calloc(n_MatSize_y[i],sizeof(double));
if (Mat_temp[i] == NULL)
{
//memory allocation problem
return 1;
}
}
Mat_temp[i][j] = atof(str);
j++;
if (str)
{
free(str);
str = NULL;
}
if (nr_term > n_MatSize_y[i])
{
//Wrong file dimension
return 3;
}
nr_term++;
}
k++;
}
}
}
fclose(fname);
}
else
{
//cannot open the file
return 2;
}
}
//To have continously allocated memory
*Mat = Init_Matrix_2D<double>(n_Mat,n_MatSize_y);
if ( *Mat == NULL)
{
//memory allocation problem
return 1;
}
for (i = 0; i < n_Mat; i++)
{
for (j = 0; j < n_MatSize_y[i]; j++)
{
(*Mat)[i][j] = Mat_temp[i][j];
}
}
//Free the allocated memory
//Mat_temp
for (i = 0; i < n_Mat; i++)
{
free(Mat_temp[i]);
}
free(Mat_temp);
//n_MatSize_y
free(n_MatSize_y);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -