FrontISTR  5.7.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  for (i = 0; i < NUM_CONTROL_PARAS; i++) {
234  parameters[i] = (char *)calloc(128, sizeof(char));
235  if (parameters[i] == NULL) {
236  fprintf(stderr, "There is no enough memory for parameters\n");
237  exit(0);
238  }
239  }
240 
241  parameters[0] = "adaptive_repartition";
242  len_para[0] = 11;
243  parameters[1] = "num_of_criteria";
244  len_para[1] = 12;
245  parameters[2] = "balance_rate";
246  len_para[2] = 10;
247  parameters[3] = "num_of_repartition";
248  len_para[3] = 15;
249  parameters[4] = "itr_rate";
250  len_para[4] = 8;
251  parameters[5] = "wgtflag";
252  len_para[5] = 7;
253  parameters[6] = "vwgt_filename";
254  len_para[6] = 9;
255  parameters[7] = "adjwgt_filename";
256  len_para[7] = 12;
257  parameters[8] = "machine_wgt";
258  len_para[8] = 11;
259 
260  for (i = 0; i < NUM_CONTROL_PARAS; i++) {
261  stat_para[i] = 0;
262  }
263  ctl_para->num_criteria = 1;
264 
265  offset = 0;
266  cont_flag = 1;
267  while (cont_flag) {
268  if (fgets(buf, MAX_LINE_LEN, fp) != NULL) {
269  if ((HECMW_dlb_is_blank_line(buf) == 0) &&
270  (HECMW_dlb_is_comment_line(buf) == 0))
271  break;
272  } else
273  cont_flag = 0;
274  }
275 
276  hit = 0;
277  if (cont_flag == 1) cont_flag = HECMW_dlb_get_keyword_repart(buf, ctl_para);
278  while (cont_flag == 1) {
279  if (fgets(buf, MAX_LINE_LEN, fp) != NULL) {
280  if ((HECMW_dlb_is_blank_line(buf) == 0) &&
281  (HECMW_dlb_is_comment_line(buf) == 0))
282  break;
283  } else
284  cont_flag = 0;
285  }
286 
287  while (cont_flag == 1) {
288  hit = -1;
289  location = HECMW_dlb_get_keyword_item(buf, para);
290  for (i = 1; i < NUM_CONTROL_PARAS; i++) {
291  if ((strncmp(para, parameters[i], len_para[i])) == 0) {
292  hit = i;
293  stat_para[i] = 1;
294  break;
295  }
296  }
297  /* fprintf(stderr, "para=%s hit=%d\n", para, hit);
298  */
299  if ((hit >= 0) && (hit < NUM_CONTROL_PARAS)) {
300  switch (hit) {
301  /* case 0:
302  sf[k].surface_style=get_int_item(para, buf,
303  &location);
304  break;
305  */
306  case 1:
307  ctl_para->num_criteria = HECMW_dlb_get_int_item(para, buf, &location);
308  break;
309  case 2:
310  ctl_para->balance_rate =
311  (float *)calloc(ctl_para->num_criteria, sizeof(float));
312  for (i = 0; i < ctl_para->num_criteria; i++) {
313  ctl_para->balance_rate[i] =
314  (float)HECMW_dlb_get_double_item(para, buf, &location);
315  }
316  break;
317  case 3:
318  ctl_para->num_repartition =
319  HECMW_dlb_get_int_item(para, buf, &location);
320  break;
321  case 4:
322  ctl_para->itr_rate =
323  (float)HECMW_dlb_get_double_item(para, buf, &location);
324  break;
325  case 5:
326  ctl_para->wgtflag = HECMW_dlb_get_int_item(para, buf, &location);
327  break;
328  case 6:
329  HECMW_dlb_get_string_item(para, buf, &location,
330  ctl_para->vwgt_filename);
331  break;
332  case 7:
333  HECMW_dlb_get_string_item(para, buf, &location,
334  ctl_para->adjwgt_filename);
335  break;
336  case 8:
337  ctl_para->machine_wgt = (float *)calloc(pesize, sizeof(float));
338  for (i = 0; i < pesize; i++) {
339  ctl_para->machine_wgt[i] =
340  (float)HECMW_dlb_get_double_item(para, buf, &location);
341  }
342  break;
343  /* case 9:
344  fscanf(contfp, "%s", ctl_para->output_filename);
345  */
346 
347  break;
348  }
349  }
350  while (cont_flag) {
351  if (fgets(buf, MAX_LINE_LEN, fp) != NULL) {
352  if ((HECMW_dlb_is_blank_line(buf) == 0) &&
353  (HECMW_dlb_is_comment_line(buf) == 0))
354  break;
355  } else
356  cont_flag = 0;
357  }
358  if (cont_flag == 0) break;
359  }
360  /* check the parameters */
361  /* if(stat_para[0]==0) {
362  strncpy(ctl_para->adaptive_repartition, "off", 3);
363  }
364 
365  if((strncmp(ctl_para->adaptive_repartition, "on", 2)!=0) &&
366  (strncmp(ctl_para->adaptive_repartition, "off", 3)!=0)){
367  fprintf(stderr, "adaptive_repartition should be on
368  or off\n");
369  fprintf(stderr, "Please re-input again\n");
370  exit(0);
371  }
372  */
373  if (stat_para[1] == 0) {
374  ctl_para->num_criteria = 1;
375  }
376 
377  if (ctl_para->num_criteria <= 0) {
378  fprintf(
379  stderr,
380  "#### HEC-MW-DLB-E1001: num_of_criteria should be greater than 0\n");
381  fprintf(stderr, "Please re-input a correct one\n");
382  exit(0);
383  }
384  if (stat_para[2] == 0) {
385  ctl_para->balance_rate =
386  (float *)calloc(ctl_para->num_criteria, sizeof(float));
387  for (i = 0; i < ctl_para->num_criteria; i++)
388  ctl_para->balance_rate[i] = 1.05;
389  }
390  for (i = 0; i < ctl_para->num_criteria; i++) {
391  if (ctl_para->balance_rate[i] < 1.0) {
392  fprintf(stderr,
393  "#### HEC-MW-DLB-E1002: The balance rate should be >=1.0\n");
394  fprintf(stderr, "Please input again\n");
395  exit(0);
396  }
397  }
398 
399  if (stat_para[3] == 0) {
400  ctl_para->num_repartition = pesize;
401  }
402  if (ctl_para->num_repartition < 1) {
403  fprintf(
404  stderr,
405  "#### HEC-MW-DLB-E1003: The num_of_repartition cannot less than 1\n");
406  fprintf(stderr, "Please input again\n");
407  exit(0);
408  }
409  if (stat_para[4] == 0) {
410  ctl_para->itr_rate = 10000.0;
411  }
412  if (ctl_para->itr_rate < 0.0) {
413  fprintf(stderr, "#### HEC-MW-DLB-E1004:itr_rate cannot be less than 0.0\n");
414  exit(0);
415  }
416 
417  if (stat_para[5] == 0) {
418  ctl_para->wgtflag = 0;
419  }
420  if ((ctl_para->wgtflag < 0) || (ctl_para->wgtflag > 3)) {
421  fprintf(stderr, "#### HEC-MW-DLB-E1005:wgtflag only can be in 0--3\n");
422  exit(0);
423  }
424  if (stat_para[8] == 0) {
425  ctl_para->machine_wgt = (float *)calloc(pesize, sizeof(float));
426  for (i = 0; i < pesize; i++) ctl_para->machine_wgt[i] = 1.0 / (float)pesize;
427  }
428 
429  return;
430 }
431 
433  int stat_para[NUM_CONTROL_PARAS],
434  int pesize) {
435  int i;
436 
437  strncpy(ctl_para->adaptive_repartition, "on", 3);
438  ctl_para->num_criteria = 1;
439 
440  ctl_para->balance_rate = (float *)calloc(1, sizeof(float));
441  ctl_para->balance_rate[0] = 1.02;
442  ctl_para->num_repartition = pesize;
443  ctl_para->itr_rate = 10000.0;
444  ctl_para->wgtflag = 0;
445  for (i = 6; i < NUM_CONTROL_PARAS; i++) stat_para[i] = 0;
446  stat_para[8] = 1;
447  ctl_para->machine_wgt = (float *)calloc(pesize, sizeof(float));
448  for (i = 0; i < pesize; i++) ctl_para->machine_wgt[i] = 1.0 / (float)pesize;
449 
450  return;
451 }
_control_para_struct::num_repartition
int num_repartition
Definition: hecmw_repart.h:38
NUM_CONTROL_PARAS
#define NUM_CONTROL_PARAS
Definition: hecmw_repart.h:27
HECMW_dlb_get_int_item
int HECMW_dlb_get_int_item(char *para, char *buf, int *start_location)
Definition: hecmw_dlb_read_control.c:59
_control_para_struct::adjwgt_filename
char adjwgt_filename[128]
Definition: hecmw_repart.h:47
_control_para_struct::num_criteria
int num_criteria
Definition: hecmw_repart.h:36
_control_para_struct
Definition: hecmw_repart.h:34
hecmw_repart.h
HECMW_dlb_get_keyword_item
int HECMW_dlb_get_keyword_item(char *buf, char *para)
Definition: hecmw_dlb_read_control.c:114
HECMW_dlb_get_keyword_repart
int HECMW_dlb_get_keyword_repart(char *buf, Control_para *ctl_para)
Definition: hecmw_dlb_read_control.c:135
HECMW_dlb_get_double_item
double HECMW_dlb_get_double_item(char *para, char *buf, int *start_location)
Definition: hecmw_dlb_read_control.c:87
_control_para_struct::adaptive_repartition
char adaptive_repartition[4]
Definition: hecmw_repart.h:35
HECMW_dlb_is_comment_line
int HECMW_dlb_is_comment_line(char *buf)
Definition: hecmw_dlb_read_control.c:24
hecmw_dlb_set_default_control
void hecmw_dlb_set_default_control(Control_para *ctl_para, int stat_para[NUM_CONTROL_PARAS], int pesize)
Definition: hecmw_dlb_read_control.c:432
MAX_LINE_LEN
#define MAX_LINE_LEN
Definition: hecmw_repart.h:25
hecmw_dlb_read_control
void hecmw_dlb_read_control(char *contfile, Control_para *ctl_para, int stat_para[NUM_CONTROL_PARAS], int pesize)
Definition: hecmw_dlb_read_control.c:214
_control_para_struct::itr_rate
float itr_rate
Definition: hecmw_repart.h:39
_control_para_struct::balance_rate
float * balance_rate
Definition: hecmw_repart.h:37
HECMW_dlb_is_blank_line
int HECMW_dlb_is_blank_line(char *buf)
Definition: hecmw_dlb_read_control.c:8
HECMW_dlb_get_string_item
void HECMW_dlb_get_string_item(char *para, char *buf, int *start_location, char para2[128])
Definition: hecmw_dlb_read_control.c:35
_control_para_struct::vwgt_filename
char vwgt_filename[128]
Definition: hecmw_repart.h:46
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
_control_para_struct::wgtflag
int wgtflag
Definition: hecmw_repart.h:40
_control_para_struct::machine_wgt
float * machine_wgt
Definition: hecmw_repart.h:48
HECMW_dlb_print_exit
void HECMW_dlb_print_exit(char *var)
Definition: hecmw_dlb_mem_util.c:16