aboutsummaryrefslogtreecommitdiff
path: root/analyze.c
blob: 09c84c70f0f7d358c5a4187a1fd261c3098ccbbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* See copyright information at the end of the file */

/*! @file analyze.c
 * @brief Analyze results of simulations.
 *
 * @code
 * Usage: analyze [-l LOG LEVEL] [DIR...]
 * Options:
 *   -l [0|NONE] or [1|LOG] or [2|WARN] or [3|VERBOSE]
 *   -e If specified check final configurations for z components != 0
 * @endcode
 */

#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <float.h>
#include <math.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define STD_IMPLEMENTATION
#include "std.h"
#define COMMON_IMPLEMENTATION
#include "common.h"
#define ANALYZE_IMPLEMENTATION
#include "analyze.h"

void
usage(const char program[static 1])
{
	fprintf(stderr, "Usage: %s [-l LOG LEVEL] [DIR...]\n", program);
	fprintf(stderr, "Options:\n");
	fprintf(stderr,
		"\t-l [0|NONE] or [1|LOG] or [2|WARN] or [3|VERBOSE]\n");
	fprintf(stderr,
		"\t-e\tIf specified check final configurations for z "
		"components != 0\n");

	exit(1);
}

static double frigid_minima[MINIMA_CAP] = { 0 };
static double fminima[MINIMA_CAP] = { 0 };

int
main(int argc, char *argv[])
{
	Region *r = region_alloc(16 * MB);
	int opt;
	bool errors_occurred = false, ensure = false;

	if (argc < 2) {
		usage(argv[0]);
	}

	while ((opt = getopt(argc, argv, "hl:eor")) != -1) {
		switch (opt) {
		case 'h':
			usage(argv[0]);
			break;
		case 'l':
			if (strcmp(optarg, "NONE") == 0
				|| strcmp(optarg, "0") == 0) {
				log_level = NO_LOG;
			} else if (strcmp(optarg, "WARN") == 0
				|| strcmp(optarg, "1") == 0) {
				log_level = WARN;
			} else if (strcmp(optarg, "LOG") == 0
				|| strcmp(optarg, "2") == 0) {
				log_level = LOG;
			} else if (strcmp(optarg, "VERBOSE") == 0
				|| strcmp(optarg, "3") == 0) {
				log_level = VERBOSE;
			} else {
				usage(argv[0]);
			}
			break;
		case 'e':
			ensure = true;
			break;
		default:
			break;
		}
	}

	for (; optind < argc; optind++) {
		Metadata m = { 0 };
		DIR *d = NULL;

		strip_slash(argv[optind]);
		if (argv[optind][0] == '-') {
			DBG("%s appears to be an option, options can "
			    "only be placed before directory "
			    "arguments.\n",
				argv[optind]);
			errors_occurred = true;
			break;
		}

		d = opendir(argv[optind]);
		if (!d) {
			DBG("Error opening directory %s: %s\n", argv[optind],
				strerror(errno));
			errors_occurred = true;
			break;
		}

		if (!dir_metadata(r, argv[optind], &m)) {
			DBG("Error reading metadata file, expected "
			    "at least ATOMS, "
			    "MODE, NSTEP, V0%s",
				"\n");
			errors_occurred = true;
			break;
		}

		switch (m.mode) {
		case MODE_ANGLES:
			LOG("Analyzing angles simulation: %s\n",
				argv[optind]);

			if (!analyze_angles(r, d, argv[optind], &m, ensure,
				    frigid_minima, fminima)) {
				errors_occurred = true;
			}

			break;
		case MODE_TRANSLATE:
			LOG("Analyzing translational simulation: %s\n",
				argv[optind]);
			if (!analyze_translate(r, d, argv[optind], &m, ensure,
				    frigid_minima, fminima)) {
				errors_occurred = true;
			}
			break;
		default:
			errors_occurred = true;
			break;
		}

		memset(frigid_minima, 0, sizeof(double) * LEN(frigid_minima));
		memset(fminima, 0, sizeof(double) * LEN(fminima));
		memset(&m, 0, sizeof(Metadata));

		if (d) {
			closedir(d);
		}
		if (errors_occurred) {
			break;
		};
	}

	region_free(r);
	if (errors_occurred) {
		fprintf(stderr,
			"Errors occurred, the analysis might not "
			"be complete. Good luck.%s",
			"\n");
		exit(EXIT_FAILURE);
	}

	return 0;
}

/*
 * Copyright ©️ 2023 Mario Forzanini <mf@marioforzanini.com>
 *
 * This file is part of my bachelor thesis.
 *
 * This file is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This file is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file. If not, see <https://www.gnu.org/licenses/>.
 *
 */