HydroCODE_2D 0.1
This is a implementation of fully explict forward Euler scheme for 2-D Euler equations of motion on Eulerian coordinate
io_control.c
浏览该文件的文档.
1
6#include <errno.h>
7#include <stdio.h>
8#include <string.h>
9#include <stdlib.h>
10#include <math.h>
11#include <ctype.h>
12
13#include "../include/var_struc.h"
14#include "../include/tools.h"
15
16/*
17 * To realize cross-platform programming.
18 * ACCESS: Determine access permissions for files or folders.
19 * - mode=0: Test for existence.
20 * - mode=2: Test for write permission.
21 * - mode=4: Test for read permission.
22 */
23#ifdef _WIN32
24#include <io.h>
25#define ACCESS(path,mode) _access((path),(mode))
26#elif __linux__
27#include <unistd.h>
28#define ACCESS(path,mode) access((path),(mode))
29#endif
30
31
39void example_io(const char *example, char *add_mkdir, const int i_or_o)
40{
41 const int dim = (int)config[0];
42 const int el = (int)config[8];
43 const int order = (int)config[9];
44
45 char *str_tmp, str_order[11];
46 switch (dim)
47 {
48 case 1 :
49 str_tmp = "one-dim/"; break;
50 case 2 :
51 str_tmp = "two-dim/"; break;
52 case 3 :
53 str_tmp = "three-dim/"; break;
54 default :
55 fprintf(stderr, "Strange computational dimension!\n");
56 exit(2);
57 }
58 if (i_or_o == 0) // Output
59 {
60 strcpy(add_mkdir, "../../data_out/");
61 strcat(add_mkdir, str_tmp);
62 switch (el)
63 {
64 case 0 :
65 str_tmp = "EUL_"; break;
66 case 1 :
67 str_tmp = "LAG_"; break;
68 case 2 :
69 str_tmp = "ALE_"; break;
70 default :
71 fprintf(stderr, "Strange description method of fluid motion!\n");
72 exit(2);
73 }
74 strcat(add_mkdir, str_tmp);
75 sprintf(str_order, "%d_order/", order);
76 strcat(add_mkdir, str_order);
77 }
78 else // Input
79 {
80 strcpy(add_mkdir, "../../data_in/");
81 strcat(add_mkdir, str_tmp);
82 }
83 strcat(add_mkdir, example);
84
85 if (i_or_o == 0)
86 {
87 if(CreateDir(add_mkdir) == 1)
88 {
89 fprintf(stderr, "Output directory '%s' construction failed.\n", add_mkdir);
90 exit(1);
91 }
92 else
93 printf("Output directory '%s' is constructed.\n", add_mkdir);
94 }
95 else if (ACCESS(add_mkdir,4) == -1)
96 {
97 fprintf(stderr, "Input directory '%s' is unreadable!\n", add_mkdir);
98 exit(1);
99 }
100
101 strcat(add_mkdir, "/");
102}
103
104
111int flu_var_count(FILE * fp, const char * add)
112{
113 int num = 0; // Data number.
114 /* We read characters one by one from the data file.
115 * "flg" helps us to count.
116 * -# 1: when read a number-using character (0, 1, 2, ..., e, E, minus sign and dot).
117 * -# 0: when read a non-number-using character.
118 */
119 int flg = 0;
120 int ch;
121
122 while((ch = getc(fp)) != EOF) // Count the data number.
123 {
124 if (ch == 45 || ch == 46 || ch == 69 || ch == 101 || isdigit(ch))
125 flg = 1;
126 else if (!isspace(ch))
127 {
128 fprintf(stderr, "Input contains illegal character(ASCII=%d, flag=%d) in the file '%s'!\n", ch, flg, add);
129 return 0;
130 }
131 else if (flg) // Read in the space.
132 {
133 num++;
134 flg = 0;
135 }
136 }
137
138 rewind(fp);
139 return num;
140}
141
142
150int flu_var_count_line(FILE * fp, const char * add, int * n_x)
151{
152 int line = 0, column = 0;
153 /* We read characters one by one from the data file.
154 * "flg" helps us to count.
155 * -# 1: when read a number-using character (0, 1, 2, ..., e, E, minus sign and dot).
156 * -# 0: when read a non-number-using character.
157 */
158 int flag = 0;
159 int ch;
160
161 do { // Count the data line number.
162 ch = getc(fp);
163 if(ch == '\n' || ch == EOF)
164 {
165 if(flag)
166 ++column;
167 flag = 0;
168 if(column)
169 {
170 if(!line)
171 *n_x = column;
172 else if(column != *n_x)
173 {
174 printf("Error in input data file '%s', line=%d, column=%d, n_x=%d\n", add, line, column, *n_x);
175 return 0;
176 }
177 ++line;
178 column = 0;
179 }
180 }
181 else if(ch == 45 || ch == 46 || ch == 69 || ch == 101 || isdigit(ch))
182 flag = 1;
183 else if (!isspace(ch))
184 {
185 printf("Input contains illigal character(ASCII=%d, flag=%d) in the file '%s', line=%d!\n", ch, flag, add, line);
186 return 0;
187 }
188 else if(flag)
189 {
190 ++column;
191 flag = 0;
192 }
193 } while(ch != EOF);
194
195 rewind(fp);
196 return line;
197}
198
199
208int flu_var_read(FILE * fp, double * U, const int num)
209{
210 int idx = 0, j = 0; // j is a frequently used index for spatial variables.
211 char number[100]; // A string that stores a number.
212 char ch, *endptr;
213 // int sign = 1;
214
215 while((ch = getc(fp)) != EOF)
216 {
217 if(isspace(ch) && idx)
218 {
219 number[idx] = '\0';
220 idx = 0;
221 // format_string() and str2num() in 'str_num_common.c' are deprecated.
222 /*
223 sign = format_string(number);
224 if(!sign)
225 return j+1;
226 else if(j == num)
227 return j;
228 U[j] = sign * str2num(number);
229 */
230 errno = 0;
231 U[j] = strtod(number, &endptr);
232 if (errno == ERANGE || *endptr != '\0')
233 {
234 printf("The %dth entry in the initial data file is not a double-precision floats.\n", j+1);
235 return j+1;
236 }
237 else if(j == num)
238 {
239 printf("Error on the initial data file reading!\n");
240 return j;
241 }
242 ++j;
243 }
244 else if((ch == 46) || (ch == 45) || (ch == 69) || (ch == 101) || isdigit(ch))
245 number[idx++] = ch;
246 }
247 return 0;
248}
int flu_var_read(FILE *fp, double *U, const int num)
This function reads the initial data file to generate the initial data.
Definition: io_control.c:208
int flu_var_count_line(FILE *fp, const char *add, int *n_x)
This function counts the line and column number of the numbers are there in the initial data file.
Definition: io_control.c:150
void example_io(const char *example, char *add_mkdir, const int i_or_o)
This function produces folder path for data input or output.
Definition: io_control.c:39
int flu_var_count(FILE *fp, const char *add)
This function counts how many numbers are there in the initial data file.
Definition: io_control.c:111
int CreateDir(const char *pPath)
This is a function that recursively creates folders.
Definition: sys_pro.c:57
double config[]
Initial configuration data array.
Definition: hydrocode.c:115