ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/applications/nanoparticleBuilder/nanoparticleBuilderCmd.c
Revision: 3053
Committed: Wed Oct 18 19:35:07 2006 UTC (18 years, 6 months ago) by gezelter
Content type: text/plain
File size: 17918 byte(s)
Log Message:
bug fixes for vacancies

File Contents

# Content
1 /*
2 File autogenerated by gengetopt version 2.13.1
3 generated with the following command:
4 gengetopt -F nanoparticleBuilderCmd --unamed-opts --no-handle-error
5
6 The developers of gengetopt consider the fixed text that goes in all
7 gengetopt output files to be in the public domain:
8 we make no copyright claims on it.
9 */
10
11 /* If we use autoconf. */
12 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "getopt.h"
21
22 #include "nanoparticleBuilderCmd.h"
23
24 static
25 void clear_given (struct gengetopt_args_info *args_info);
26 static
27 void clear_args (struct gengetopt_args_info *args_info);
28
29 static int
30 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error);
31
32 static int
33 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error);
34
35 struct shellRadius_list
36 {
37 double shellRadius_arg;
38 struct shellRadius_list * next;
39 };
40
41 struct molFraction_list
42 {
43 double molFraction_arg;
44 struct molFraction_list * next;
45 };
46
47 static char *
48 gengetopt_strdup (const char *s);
49
50 static
51 void clear_given (struct gengetopt_args_info *args_info)
52 {
53 args_info->help_given = 0 ;
54 args_info->version_given = 0 ;
55 args_info->output_given = 0 ;
56 args_info->latticeConstant_given = 0 ;
57 args_info->radius_given = 0 ;
58 args_info->shellRadius_given = 0 ;
59 args_info->molFraction_given = 0 ;
60 args_info->vacancyPercent_given = 0 ;
61 args_info->vacancyInnerRadius_given = 0 ;
62 args_info->vacancyOuterRadius_given = 0 ;
63 }
64
65 static
66 void clear_args (struct gengetopt_args_info *args_info)
67 {
68 args_info->output_arg = NULL;
69 args_info->shellRadius_arg = NULL;
70 args_info->molFraction_arg = NULL;
71 }
72
73 void
74 cmdline_parser_print_version (void)
75 {
76 printf ("%s %s\n", CMDLINE_PARSER_PACKAGE, CMDLINE_PARSER_VERSION);
77 }
78
79 void
80 cmdline_parser_print_help (void)
81 {
82 cmdline_parser_print_version ();
83 printf("\n%s\n", "Builds spherical random or core-shell nanoparticles and outputs an OOPSE \nstartup file");
84 printf("\nUsage: nanoparticleBuilder [OPTIONS]... [FILES]...\n\n");
85 printf("%s\n"," -h, --help Print help and exit");
86 printf("%s\n"," -V, --version Print version and exit");
87 printf("%s\n"," -o, --output=STRING Output file name");
88 printf("%s\n"," --latticeConstant=DOUBLE Lattice spacing in Angstroms for cubic lattice.");
89 printf("%s\n"," --radius=DOUBLE Nanoparticle radius in Angstroms");
90 printf("%s\n"," --shellRadius=DOUBLE Radius containing within it only molecules of a \n specific component. Specified for each \n component > 1 in the template file.");
91 printf("%s\n"," --molFraction=DOUBLE Builds a multi-component random alloy \n nanoparticle. A mole Fraction must be \n specified for each component > 1 in the \n template file.");
92 printf("%s\n"," --vacancyPercent=DOUBLE Percentage of atoms to remove from within \n vacancy range");
93 printf("%s\n"," --vacancyInnerRadius=DOUBLE\n Radius arround core-shell where vacancies \n should be located.");
94 printf("%s\n"," --vacancyOuterRadius=DOUBLE\n Radius arround core-shell where vacancies \n should be located.");
95
96 }
97
98 void
99 cmdline_parser_init (struct gengetopt_args_info *args_info)
100 {
101 clear_given (args_info);
102 clear_args (args_info);
103
104 args_info->inputs = NULL;
105 args_info->inputs_num = 0;
106 }
107
108 void
109 cmdline_parser_free (struct gengetopt_args_info *args_info)
110 {
111
112 unsigned int i;
113 if (args_info->output_arg)
114 {
115 free (args_info->output_arg); /* free previous argument */
116 args_info->output_arg = 0;
117 }
118 if (args_info->shellRadius_arg)
119 {
120 free (args_info->shellRadius_arg); /* free previous argument */
121 args_info->shellRadius_arg = 0;
122 }
123 if (args_info->molFraction_arg)
124 {
125 free (args_info->molFraction_arg); /* free previous argument */
126 args_info->molFraction_arg = 0;
127 }
128
129 for (i = 0; i < args_info->inputs_num; ++i)
130 free (args_info->inputs [i]);
131
132 if (args_info->inputs_num)
133 free (args_info->inputs);
134
135 clear_given (args_info);
136 }
137
138 /* gengetopt_strdup() */
139 /* strdup.c replacement of strdup, which is not standard */
140 char *
141 gengetopt_strdup (const char *s)
142 {
143 char *result = NULL;
144 if (!s)
145 return result;
146
147 result = (char*)malloc(strlen(s) + 1);
148 if (result == (char*)0)
149 return (char*)0;
150 strcpy(result, s);
151 return result;
152 }
153
154 static char *
155 get_multiple_arg_token(const char *arg)
156 {
157 char *tok, *ret;
158 size_t len;
159
160 if (!arg)
161 return NULL;
162
163 tok = strchr (arg, ',');
164
165 if (tok)
166 len = (size_t)(tok - arg + 1);
167 else
168 len = strlen (arg) + 1;
169
170 ret = (char *) malloc (len);
171 strncpy (ret, arg, len-1);
172 ret[len-1] = '\0';
173
174 return ret;
175 }
176
177 static char *
178 get_multiple_arg_token_next(const char *arg)
179 {
180 char *tok;
181
182 if (!arg)
183 return NULL;
184
185 tok = strchr (arg, ',');
186
187 if (! tok || strlen(tok) == 1)
188 return 0;
189
190 return tok+1;
191 }
192
193 int
194 cmdline_parser (int argc, char * const *argv, struct gengetopt_args_info *args_info)
195 {
196 return cmdline_parser2 (argc, argv, args_info, 0, 1, 1);
197 }
198
199 int
200 cmdline_parser2 (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required)
201 {
202 int result;
203
204 result = cmdline_parser_internal (argc, argv, args_info, override, initialize, check_required, NULL);
205
206 return result;
207 }
208
209 int
210 cmdline_parser_required (struct gengetopt_args_info *args_info, const char *prog_name)
211 {
212 int result = EXIT_SUCCESS;
213
214 if (cmdline_parser_required2(args_info, prog_name, NULL) > 0)
215 result = EXIT_FAILURE;
216
217 return result;
218 }
219
220 int
221 cmdline_parser_required2 (struct gengetopt_args_info *args_info, const char *prog_name, const char *additional_error)
222 {
223 int error = 0;
224
225 if (! args_info->output_given)
226 {
227 fprintf (stderr, "%s: '--output' ('-o') option required%s\n", prog_name, (additional_error ? additional_error : ""));
228 error = 1;
229 }
230 if (! args_info->latticeConstant_given)
231 {
232 fprintf (stderr, "%s: '--latticeConstant' option required%s\n", prog_name, (additional_error ? additional_error : ""));
233 error = 1;
234 }
235 if (! args_info->radius_given)
236 {
237 fprintf (stderr, "%s: '--radius' option required%s\n", prog_name, (additional_error ? additional_error : ""));
238 error = 1;
239 }
240
241 return error;
242 }
243
244 int
245 cmdline_parser_internal (int argc, char * const *argv, struct gengetopt_args_info *args_info, int override, int initialize, int check_required, const char *additional_error)
246 {
247 int c; /* Character of the parsed option. */
248 char *multi_token, *multi_next; /* for multiple options */
249
250 int i; /* Counter */
251
252 struct shellRadius_list * shellRadius_list = NULL,* shellRadius_new = NULL;
253 struct molFraction_list * molFraction_list = NULL,* molFraction_new = NULL;
254 int error = 0;
255 struct gengetopt_args_info local_args_info;
256
257 if (initialize)
258 cmdline_parser_init (args_info);
259
260 cmdline_parser_init (&local_args_info);
261
262 optarg = 0;
263 optind = 1;
264 opterr = 1;
265 optopt = '?';
266
267 while (1)
268 {
269 int option_index = 0;
270 char *stop_char;
271
272 static struct option long_options[] = {
273 { "help", 0, NULL, 'h' },
274 { "version", 0, NULL, 'V' },
275 { "output", 1, NULL, 'o' },
276 { "latticeConstant", 1, NULL, 0 },
277 { "radius", 1, NULL, 0 },
278 { "shellRadius", 1, NULL, 0 },
279 { "molFraction", 1, NULL, 0 },
280 { "vacancyPercent", 1, NULL, 0 },
281 { "vacancyInnerRadius", 1, NULL, 0 },
282 { "vacancyOuterRadius", 1, NULL, 0 },
283 { NULL, 0, NULL, 0 }
284 };
285
286 stop_char = 0;
287 c = getopt_long (argc, argv, "hVo:", long_options, &option_index);
288
289 if (c == -1) break; /* Exit from `while (1)' loop. */
290
291 switch (c)
292 {
293 case 'h': /* Print help and exit. */
294 cmdline_parser_print_help ();
295 cmdline_parser_free (&local_args_info);
296 exit (EXIT_SUCCESS);
297
298 case 'V': /* Print version and exit. */
299 cmdline_parser_print_version ();
300 cmdline_parser_free (&local_args_info);
301 exit (EXIT_SUCCESS);
302
303 case 'o': /* Output file name. */
304 if (local_args_info.output_given)
305 {
306 fprintf (stderr, "%s: `--output' (`-o') option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
307 goto failure;
308 }
309 if (args_info->output_given && ! override)
310 continue;
311 local_args_info.output_given = 1;
312 args_info->output_given = 1;
313 if (args_info->output_arg)
314 free (args_info->output_arg); /* free previous string */
315 args_info->output_arg = gengetopt_strdup (optarg);
316 break;
317
318
319 case 0: /* Long option with no short option */
320 /* Lattice spacing in Angstroms for cubic lattice.. */
321 if (strcmp (long_options[option_index].name, "latticeConstant") == 0)
322 {
323 if (local_args_info.latticeConstant_given)
324 {
325 fprintf (stderr, "%s: `--latticeConstant' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
326 goto failure;
327 }
328 if (args_info->latticeConstant_given && ! override)
329 continue;
330 local_args_info.latticeConstant_given = 1;
331 args_info->latticeConstant_given = 1;
332 args_info->latticeConstant_arg = strtod (optarg, NULL);
333 }
334 /* Nanoparticle radius in Angstroms. */
335 else if (strcmp (long_options[option_index].name, "radius") == 0)
336 {
337 if (local_args_info.radius_given)
338 {
339 fprintf (stderr, "%s: `--radius' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
340 goto failure;
341 }
342 if (args_info->radius_given && ! override)
343 continue;
344 local_args_info.radius_given = 1;
345 args_info->radius_given = 1;
346 args_info->radius_arg = strtod (optarg, NULL);
347 }
348 /* Radius containing within it only molecules of a specific component. Specified for each component > 1 in the template file.. */
349 else if (strcmp (long_options[option_index].name, "shellRadius") == 0)
350 {
351 local_args_info.shellRadius_given++;
352
353 multi_token = get_multiple_arg_token(optarg);
354 multi_next = get_multiple_arg_token_next (optarg);
355
356 while (1)
357 {
358 shellRadius_new = (struct shellRadius_list *) malloc (sizeof (struct shellRadius_list));
359 shellRadius_new->next = shellRadius_list;
360 shellRadius_list = shellRadius_new;
361 shellRadius_new->shellRadius_arg = strtod (multi_token, NULL);
362 free (multi_token);
363
364 if (multi_next)
365 {
366 multi_token = get_multiple_arg_token(multi_next);
367 multi_next = get_multiple_arg_token_next (multi_next);
368 local_args_info.shellRadius_given++;
369 }
370 else
371 break;
372 }
373 break;
374 }
375 /* Builds a multi-component random alloy nanoparticle. A mole Fraction must be specified for each component > 1 in the template file.. */
376 else if (strcmp (long_options[option_index].name, "molFraction") == 0)
377 {
378 local_args_info.molFraction_given++;
379
380 multi_token = get_multiple_arg_token(optarg);
381 multi_next = get_multiple_arg_token_next (optarg);
382
383 while (1)
384 {
385 molFraction_new = (struct molFraction_list *) malloc (sizeof (struct molFraction_list));
386 molFraction_new->next = molFraction_list;
387 molFraction_list = molFraction_new;
388 molFraction_new->molFraction_arg = strtod (multi_token, NULL);
389 free (multi_token);
390
391 if (multi_next)
392 {
393 multi_token = get_multiple_arg_token(multi_next);
394 multi_next = get_multiple_arg_token_next (multi_next);
395 local_args_info.molFraction_given++;
396 }
397 else
398 break;
399 }
400 break;
401 }
402 /* Percentage of atoms to remove from within vacancy range. */
403 else if (strcmp (long_options[option_index].name, "vacancyPercent") == 0)
404 {
405 if (local_args_info.vacancyPercent_given)
406 {
407 fprintf (stderr, "%s: `--vacancyPercent' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
408 goto failure;
409 }
410 if (args_info->vacancyPercent_given && ! override)
411 continue;
412 local_args_info.vacancyPercent_given = 1;
413 args_info->vacancyPercent_given = 1;
414 args_info->vacancyPercent_arg = strtod (optarg, NULL);
415 }
416 /* Radius arround core-shell where vacancies should be located.. */
417 else if (strcmp (long_options[option_index].name, "vacancyInnerRadius") == 0)
418 {
419 if (local_args_info.vacancyInnerRadius_given)
420 {
421 fprintf (stderr, "%s: `--vacancyInnerRadius' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
422 goto failure;
423 }
424 if (args_info->vacancyInnerRadius_given && ! override)
425 continue;
426 local_args_info.vacancyInnerRadius_given = 1;
427 args_info->vacancyInnerRadius_given = 1;
428 args_info->vacancyInnerRadius_arg = strtod (optarg, NULL);
429 }
430 /* Radius arround core-shell where vacancies should be located.. */
431 else if (strcmp (long_options[option_index].name, "vacancyOuterRadius") == 0)
432 {
433 if (local_args_info.vacancyOuterRadius_given)
434 {
435 fprintf (stderr, "%s: `--vacancyOuterRadius' option given more than once%s\n", argv[0], (additional_error ? additional_error : ""));
436 goto failure;
437 }
438 if (args_info->vacancyOuterRadius_given && ! override)
439 continue;
440 local_args_info.vacancyOuterRadius_given = 1;
441 args_info->vacancyOuterRadius_given = 1;
442 args_info->vacancyOuterRadius_arg = strtod (optarg, NULL);
443 }
444
445 break;
446 case '?': /* Invalid option. */
447 /* `getopt_long' already printed an error message. */
448 goto failure;
449
450 default: /* bug: option not considered. */
451 fprintf (stderr, "%s: option unknown: %c%s\n", CMDLINE_PARSER_PACKAGE, c, (additional_error ? additional_error : ""));
452 abort ();
453 } /* switch */
454 } /* while */
455
456
457 if (local_args_info.shellRadius_given && shellRadius_list)
458 {
459 struct shellRadius_list *tmp;
460 args_info->shellRadius_arg = (double *) realloc (args_info->shellRadius_arg, (args_info->shellRadius_given + local_args_info.shellRadius_given) * sizeof (double));
461 for (i = (local_args_info.shellRadius_given - 1); i >= 0; --i)
462 {
463 tmp = shellRadius_list;
464 args_info->shellRadius_arg [i + args_info->shellRadius_given] = shellRadius_list->shellRadius_arg;
465 shellRadius_list = shellRadius_list->next;
466 free (tmp);
467 }
468 }
469
470 if (local_args_info.molFraction_given && molFraction_list)
471 {
472 struct molFraction_list *tmp;
473 args_info->molFraction_arg = (double *) realloc (args_info->molFraction_arg, (args_info->molFraction_given + local_args_info.molFraction_given) * sizeof (double));
474 for (i = (local_args_info.molFraction_given - 1); i >= 0; --i)
475 {
476 tmp = molFraction_list;
477 args_info->molFraction_arg [i + args_info->molFraction_given] = molFraction_list->molFraction_arg;
478 molFraction_list = molFraction_list->next;
479 free (tmp);
480 }
481 }
482
483
484 args_info->shellRadius_given += local_args_info.shellRadius_given;
485 local_args_info.shellRadius_given = 0;
486 args_info->molFraction_given += local_args_info.molFraction_given;
487 local_args_info.molFraction_given = 0;
488
489 if (check_required)
490 {
491 error += cmdline_parser_required2 (args_info, argv[0], additional_error);
492 }
493
494 cmdline_parser_free (&local_args_info);
495
496 if ( error )
497 return (EXIT_FAILURE);
498
499 if (optind < argc)
500 {
501 int i = 0 ;
502
503 args_info->inputs_num = argc - optind ;
504 args_info->inputs =
505 (char **)(malloc ((args_info->inputs_num)*sizeof(char *))) ;
506 while (optind < argc)
507 args_info->inputs[ i++ ] = gengetopt_strdup (argv[optind++]) ;
508 }
509
510 return 0;
511
512 failure:
513 if (shellRadius_list)
514 {
515 struct shellRadius_list *tmp;
516 while (shellRadius_list)
517 {
518 tmp = shellRadius_list;
519 shellRadius_list = shellRadius_list->next;
520 free (tmp);
521 }
522 }
523 if (molFraction_list)
524 {
525 struct molFraction_list *tmp;
526 while (molFraction_list)
527 {
528 tmp = molFraction_list;
529 molFraction_list = molFraction_list->next;
530 free (tmp);
531 }
532 }
533
534 cmdline_parser_free (&local_args_info);
535 return (EXIT_FAILURE);
536 }