FrontISTR  5.9.0
Large-scale structural analysis program with finit element method
hecmw_dlb_read_control.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
6 #include "hecmw_repart.h"
7 
8 int HECMW_dlb_is_blank_line(char *buf) {
9  int i, j;
10  int flag;
11 
12  i = 0;
13  flag = 1;
14  while (buf[i] != '\n') {
15  if (buf[i] != ' ') {
16  flag = 0;
17  break;
18  }
19  i++;
20  }
21  return (flag);
22 }
23 
24 int HECMW_dlb_is_comment_line(char *buf) {
25  int i, j;
26  int flag;
27  flag = 0;
28  if (buf[0] == '#')
29  flag = 1;
30  else if ((buf[0] == '!') && (buf[1] == '!'))
31  flag = 1;
32  return (flag);
33 }
34 
35 void HECMW_dlb_get_string_item(char *para, char *buf, int *start_location,
36  char para2[128]) {
37  int value;
38  int i, j;
39 
40  i = *start_location;
41  while ((buf[i] == ',') || (buf[i] == ' ') || (buf[i] == '=')) i++;
42  if (buf[i] == '\n') {
43  fprintf(stderr, "No string value for %s\n", para);
45  "ERROR: HEC-MW-VIS-E0002: The control parameter format error: should "
46  "start from !");
47  }
48  j = 0;
49  while ((buf[i] != ' ') && (buf[i] != ',') && (buf[i] != '\n')) {
50  para2[j] = buf[i];
51  i++;
52  j++;
53  }
54  para2[j] = '\0';
55  *start_location = i;
56  return;
57 }
58 
59 int HECMW_dlb_get_int_item(char *para, char *buf, int *start_location) {
60  int value;
61  int i, j;
62  char para2[128];
63 
64  i = *start_location;
65  while ((buf[i] == ',') || (buf[i] == ' ') || (buf[i] == '=')) i++;
66  if (buf[i] == '\n') {
68  "ERROR: HEC-MW-VIS-E0003:The control parameter format error:No integer "
69  "value for %s");
70  }
71  j = 0;
72  while ((buf[i] != ' ') && (buf[i] != ',') && (buf[i] != '\n')) {
73  para2[j] = buf[i];
74  i++;
75  j++;
76  }
77  para2[j] = '\0';
78  if ((isdigit(para2[0]) == 0) && (para2[0] != '+') && (para2[0] != '-')) {
79  fprintf(stderr, "ERROR: HEC-MW-VIS-E0004: %s should be integer \n", para);
80  HECMW_dlb_print_exit("Please re-input and run again!");
81  }
82  value = atoi(para2);
83  *start_location = i;
84  return (value);
85 }
86 
87 double HECMW_dlb_get_double_item(char *para, char *buf, int *start_location) {
88  double value;
89  int i, j;
90  char para2[128];
91 
92  i = *start_location;
93  while ((buf[i] == ',') || (buf[i] == ' ') || (buf[i] == '=')) i++;
94  if (buf[i] == '\n') {
95  fprintf(stderr, "No integer value for %s\n", para);
96  HECMW_dlb_print_exit("The control parameter format error:!");
97  }
98  j = 0;
99  while ((buf[i] != ' ') && (buf[i] != ',') && (buf[i] != '\n')) {
100  para2[j] = buf[i];
101  i++;
102  j++;
103  }
104  para2[j] = '\0';
105  if ((isdigit(para2[0]) == 0) && (para2[0] != '+') && (para2[0] != '-')) {
106  fprintf(stderr, "ERROR: HEC-MW-VIS-E0005:%s should be a real \n", para);
107  HECMW_dlb_print_exit("Please re-input and run again!");
108  }
109  value = atof(para2);
110  *start_location = i;
111  return (value);
112 }
113 
114 int HECMW_dlb_get_keyword_item(char *buf, char *para) {
115  int i, j;
116  int flag;
117  i = 0;
118  while (buf[i] == ' ') i++;
119  if (buf[i] != '!') {
120  fprintf(stderr, "Please check the line %s\n", buf);
121  HECMW_dlb_print_exit("The control parameter format error:!");
122  }
123  i++;
124  j = 0;
125  while ((buf[i] != ' ') && (buf[i] != '=') && (buf[i] != ',') &&
126  (buf[i] != '\n')) {
127  para[j] = buf[i];
128  i++;
129  j++;
130  }
131  para[j] = '\0';
132  return (i);
133 }
134 
135 int HECMW_dlb_get_keyword_repart(char *buf, Control_para *ctl_para) {
136  int i, j;
137  int flag;
138  char para[128], para2[128];
139 
140  i = 0;
141  while (buf[i] == ' ') i++;
142  if (buf[i] != '!')
144  "ERROR: HEC-MW-DLB-E0002: The control parameter format error: should "
145  "start from !");
146  i = i + 1;
147  j = 0;
148  while ((buf[i] != ' ') && (buf[i] != ',') && (buf[i] != '\n')) {
149  para[j] = buf[i];
150  i++;
151  j++;
152  }
153  flag = 1;
154  if ((strncmp(para, "DLB_CTRL", 8) != 0) &&
155  (strncmp(para, "dlb_ctrl", 8) != 0)) {
156  flag = 0;
157  return (flag);
158  }
159  /* while((buf[i]!='=') && (buf[i]!='\n'))
160  i++;
161  if(buf[i]=='\n')
162  HECMW_dlb_print_exit("ERROR: HEC-MW-DLB-E0006:The control
163  parameter format error: DLB_METHOD");
164  i++;
165  while((buf[i]==' ') && (buf[i]!='\n'))
166  i++;
167  if(buf[i]=='\n')
168  HECMW_dlb_print_exit("ERROR: HEC-MW-VIS-E0006:The control
169  parameter format error: visual ID");
170  j=0;
171  while((buf[i]!=' ') && (buf[i]!=',') && (buf[i]!='=') &&
172  (buf[i]!='\n')) {
173  para[j]=buf[i];
174  i++;
175  j++;
176  }
177  if((strncmp(para, "PSR", 3)==0) || (strncmp(para, "psr", 3)==0)) {
178  flag=1;
179  return (flag);
180  }
181 
182  if((strncmp(para, "PVR", 3)==0) || (strncmp(para, "pvr", 3)==0)) {
183  flag=2;
184  return (flag);
185  }
186 
187  HECMW_dlb_print_exit("ERROR: HEC-MW-VIS-E0007:The control parameter
188  format error: method only can be PSR or PVR");
189  */
190  strncpy(ctl_para->adaptive_repartition, "off", 3);
191 
192  while ((buf[i] == ',') || (buf[i] == ' ')) i++;
193  while (buf[i] != '\n') {
194  j = 0;
195  while ((buf[i] != ' ') && (buf[i] != ',') && (buf[i] != '=') &&
196  (buf[i] != '\n')) {
197  para[j] = buf[i];
198  i++;
199  j++;
200  }
201  para[j] = '\0';
202  if ((strncmp(para, "method", 6) == 0) ||
203  (strncmp(para, "METHOD", 6) == 0)) {
204  HECMW_dlb_get_string_item(para, buf, &i, para2);
205  if ((strncmp(para, "ADAPT", 5) == 0) || (strncmp(para, "adapt", 5) == 0))
206  strncpy(ctl_para->adaptive_repartition, "on", 2);
207  }
208 
209  while ((buf[i] == ',') || (buf[i] == ' ')) i++;
210  }
211  return flag;
212 }
213 
214 void hecmw_dlb_read_control(char *contfile, Control_para *ctl_para,
215  int stat_para[NUM_CONTROL_PARAS], int pesize) {
216  int i, j, k, offset;
217  char buf[MAX_LINE_LEN];
218  char *parameters[NUM_CONTROL_PARAS];
219  int len_para[NUM_CONTROL_PARAS];
220 
221  char para[128], para1[128];
222  int hit;
223  int surface_num;
224  int len_str;
225  int location, visual_method;
226  int flag, flag_surface;
227  int cont_flag;
228 
229  FILE *fp;
230 
231  fp = fopen(contfile, "r");
232  if (fp == NULL) HECMW_dlb_print_exit("Cannot find the control input file");
233  parameters[0] = "adaptive_repartition";
234  len_para[0] = 11;
235  parameters[1] = "num_of_criteria";
236  len_para[1] = 12;
237  parameters[2] = "balance_rate";
238  len_para[2] = 10;
239  parameters[3] = "num_of_repartition";
240  len_para[3] = 15;
241  parameters[4] = "itr_rate";
242  len_para[4] = 8;
243  parameters[5] = "wgtflag";
244  len_para[5] = 7;
245  parameters[6] = "vwgt_filename";
246  len_para[6] = 9;
247  parameters[7] = "adjwgt_filename";
248  len_para[7] = 12;
249  parameters[8] = "machine_wgt";
250  len_para[8] = 11;
251 
252  for (i = 0; i < NUM_CONTROL_PARAS; i++) {
253  stat_para[i] = 0;
254  }
255  ctl_para->num_criteria = 1;
256 
257  offset = 0;
258  cont_flag = 1;
259  while (cont_flag) {
260  if (fgets(buf, MAX_LINE_LEN, fp) != NULL) {
261  if ((HECMW_dlb_is_blank_line(buf) == 0) &&
262  (HECMW_dlb_is_comment_line(buf) == 0))
263  break;
264  } else
265  cont_flag = 0;
266  }
267 
268  hit = 0;
269  if (cont_flag == 1) cont_flag = HECMW_dlb_get_keyword_repart(buf, ctl_para);
270  while (cont_flag == 1) {
271  if (fgets(buf, MAX_LINE_LEN, fp) != NULL) {
272  if ((HECMW_dlb_is_blank_line(buf) == 0) &&
273  (HECMW_dlb_is_comment_line(buf) == 0))
274  break;
275  } else
276  cont_flag = 0;
277  }
278 
279  while (cont_flag == 1) {
280  hit = -1;
281  location = HECMW_dlb_get_keyword_item(buf, para);
282  for (i = 1; i < NUM_CONTROL_PARAS; i++) {
283  if ((strncmp(para, parameters[i], len_para[i])) == 0) {
284  hit = i;
285  stat_para[i] = 1;
286  break;
287  }
288  }
289  /* fprintf(stderr, "para=%s hit=%d\n", para, hit);
290  */
291  if ((hit >= 0) && (hit < NUM_CONTROL_PARAS)) {
292  switch (hit) {
293  /* case 0:
294  sf[k].surface_style=get_int_item(para, buf,
295  &location);
296  break;
297  */
298  case 1:
299  ctl_para->num_criteria = HECMW_dlb_get_int_item(para, buf, &location);
300  break;
301  case 2:
302  ctl_para->balance_rate =
303  (float *)calloc(ctl_para->num_criteria, sizeof(float));
304  for (i = 0; i < ctl_para->num_criteria; i++) {
305  ctl_para->balance_rate[i] =
306  (float)HECMW_dlb_get_double_item(para, buf, &location);
307  }
308  break;
309  case 3:
310  ctl_para->num_repartition =
311  HECMW_dlb_get_int_item(para, buf, &location);
312  break;
313  case 4:
314  ctl_para->itr_rate =
315  (float)HECMW_dlb_get_double_item(para, buf, &location);
316  break;
317  case 5:
318  ctl_para->wgtflag = HECMW_dlb_get_int_item(para, buf, &location);
319  break;
320  case 6:
321  HECMW_dlb_get_string_item(para, buf, &location,
322  ctl_para->vwgt_filename);
323  break;
324  case 7:
325  HECMW_dlb_get_string_item(para, buf, &location,
326  ctl_para->adjwgt_filename);
327  break;
328  case 8:
329  ctl_para->machine_wgt = (float *)calloc(pesize, sizeof(float));
330  for (i = 0; i < pesize; i++) {
331  ctl_para->machine_wgt[i] =
332  (float)HECMW_dlb_get_double_item(para, buf, &location);
333  }
334  break;
335  /* case 9:
336  fscanf(contfp, "%s", ctl_para->output_filename);
337  */
338 
339  break;
340  }
341  }
342  while (cont_flag) {
343  if (fgets(buf, MAX_LINE_LEN, fp) != NULL) {
344  if ((HECMW_dlb_is_blank_line(buf) == 0) &&
345  (HECMW_dlb_is_comment_line(buf) == 0))
346  break;
347  } else
348  cont_flag = 0;
349  }
350  if (cont_flag == 0) break;
351  }
352  /* check the parameters */
353  /* if(stat_para[0]==0) {
354  strncpy(ctl_para->adaptive_repartition, "off", 3);
355  }
356 
357  if((strncmp(ctl_para->adaptive_repartition, "on", 2)!=0) &&
358  (strncmp(ctl_para->adaptive_repartition, "off", 3)!=0)){
359  fprintf(stderr, "adaptive_repartition should be on
360  or off\n");
361  fprintf(stderr, "Please re-input again\n");
362  exit(0);
363  }
364  */
365  if (stat_para[1] == 0) {
366  ctl_para->num_criteria = 1;
367  }
368 
369  if (ctl_para->num_criteria <= 0) {
370  fprintf(
371  stderr,
372  "#### HEC-MW-DLB-E1001: num_of_criteria should be greater than 0\n");
373  fprintf(stderr, "Please re-input a correct one\n");
374  exit(EXIT_FAILURE);
375  }
376  if (stat_para[2] == 0) {
377  ctl_para->balance_rate =
378  (float *)calloc(ctl_para->num_criteria, sizeof(float));
379  for (i = 0; i < ctl_para->num_criteria; i++)
380  ctl_para->balance_rate[i] = 1.05;
381  }
382  for (i = 0; i < ctl_para->num_criteria; i++) {
383  if (ctl_para->balance_rate[i] < 1.0) {
384  fprintf(stderr,
385  "#### HEC-MW-DLB-E1002: The balance rate should be >=1.0\n");
386  fprintf(stderr, "Please input again\n");
387  exit(EXIT_FAILURE);
388  }
389  }
390 
391  if (stat_para[3] == 0) {
392  ctl_para->num_repartition = pesize;
393  }
394  if (ctl_para->num_repartition < 1) {
395  fprintf(
396  stderr,
397  "#### HEC-MW-DLB-E1003: The num_of_repartition cannot less than 1\n");
398  fprintf(stderr, "Please input again\n");
399  exit(EXIT_FAILURE);
400  }
401  if (stat_para[4] == 0) {
402  ctl_para->itr_rate = 10000.0;
403  }
404  if (ctl_para->itr_rate < 0.0) {
405  fprintf(stderr, "#### HEC-MW-DLB-E1004:itr_rate cannot be less than 0.0\n");
406  exit(EXIT_FAILURE);
407  }
408 
409  if (stat_para[5] == 0) {
410  ctl_para->wgtflag = 0;
411  }
412  if ((ctl_para->wgtflag < 0) || (ctl_para->wgtflag > 3)) {
413  fprintf(stderr, "#### HEC-MW-DLB-E1005:wgtflag only can be in 0--3\n");
414  exit(EXIT_FAILURE);
415  }
416  if (stat_para[8] == 0) {
417  ctl_para->machine_wgt = (float *)calloc(pesize, sizeof(float));
418  for (i = 0; i < pesize; i++) ctl_para->machine_wgt[i] = 1.0 / (float)pesize;
419  }
420 
421  return;
422 }
423 
425  int stat_para[NUM_CONTROL_PARAS],
426  int pesize) {
427  int i;
428 
429  strncpy(ctl_para->adaptive_repartition, "on", 3);
430  ctl_para->num_criteria = 1;
431 
432  ctl_para->balance_rate = (float *)calloc(1, sizeof(float));
433  ctl_para->balance_rate[0] = 1.02;
434  ctl_para->num_repartition = pesize;
435  ctl_para->itr_rate = 10000.0;
436  ctl_para->wgtflag = 0;
437  for (i = 6; i < NUM_CONTROL_PARAS; i++) stat_para[i] = 0;
438  stat_para[8] = 1;
439  ctl_para->machine_wgt = (float *)calloc(pesize, sizeof(float));
440  for (i = 0; i < pesize; i++) ctl_para->machine_wgt[i] = 1.0 / (float)pesize;
441 
442  return;
443 }
void HECMW_dlb_print_exit(char *var)
void hecmw_dlb_read_control(char *contfile, Control_para *ctl_para, int stat_para[NUM_CONTROL_PARAS], int pesize)
void HECMW_dlb_get_string_item(char *para, char *buf, int *start_location, char para2[128])
void hecmw_dlb_set_default_control(Control_para *ctl_para, int stat_para[NUM_CONTROL_PARAS], int pesize)
int HECMW_dlb_get_int_item(char *para, char *buf, int *start_location)
int HECMW_dlb_get_keyword_item(char *buf, char *para)
int HECMW_dlb_is_comment_line(char *buf)
int HECMW_dlb_is_blank_line(char *buf)
double HECMW_dlb_get_double_item(char *para, char *buf, int *start_location)
int HECMW_dlb_get_keyword_repart(char *buf, Control_para *ctl_para)
#define NULL
#define MAX_LINE_LEN
Definition: hecmw_repart.h:25
#define NUM_CONTROL_PARAS
Definition: hecmw_repart.h:27
char adjwgt_filename[128]
Definition: hecmw_repart.h:47
char vwgt_filename[128]
Definition: hecmw_repart.h:46
char adaptive_repartition[4]
Definition: hecmw_repart.h:35