ConfigFile.cpp 9.03 KB
Newer Older
1
#include <algorithm>
2
#include <errno.h>
3
#include <string.h>
4
5
6
7

#include "ConfigFile.h"
#include "atoms.hpp"
#include "eprintf.h"
8
#include "polyhedron.h"
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

const char * PATH;
const char * SCREENSHOT;
int ISOS;
int TIMESTEPS;
float **isocolours; // [ISOS][4];
const char **plyfiles;
float **translations;
float userpos[3];

float BACKGROUND[3];
int* numAtoms; //[timesteps]
float **atoms; //[timesteps][numAtoms[i]*4] //xyzu, u=atom number
float atomScaling;
std::vector<float> *clonedAtoms;
int numClonedAtoms;
int *basisvectorreps;

bool showTrajectories;
std::vector<int> atomtrajectories;
std::vector<std::vector<int>> atomtrajectoryrestarts;

float abc[3][3]; //basis vectors
bool has_abc = false;

int repetitions[3];
35
Solid *solid;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

const char * loadConfigFileErrors[] =
{
	"All Ok",//0
	"file could not be opened", //-1
	"unrecognized parameter",//-2
	"values with no previous iso", //-3
	"colours with no previous iso",//-4
	"translations with no previous correct iso",//-5
	"Missing isos and xyzfile",//-6
	"missing values",//-7
	"timesteps from config file and from atom file do not match",//-8, unused, now minimum is used
	"isos parameter specified twice",//-9
	"Error reading unit cell parameters", //-10
	"Error reading repetitions",//-11
	"Non-periodic, but repetitions requested", //-12
	"No basis vectors, but repetitions requested", //-13
	"Error loading config file",// -14
54
	"Error reading atomglyph", //-15
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
	"Error loading xyz file, add 100 to see the error",//<-100
	"Error loading cube file, add 100 to see the error",//<-200
	"Error loading json file, add 200 to see the error",//<-300
};

void updateTIMESTEPS (int timesteps)
{
if (TIMESTEPS==0)
	TIMESTEPS=timesteps;
else
	TIMESTEPS=std::min(TIMESTEPS, timesteps);
}

int readString(FILE *f, char *s)
{
	char s2[100];
	int r, c;
	r = fscanf(f, "%99s", s2);
	if (r!=1)
		return -1;
	if (s2[0]!='"') {
		strcpy(s, s2);
		return 0;
	} else {
		strcpy(s, s2+1); //skip "
		size_t l = strlen(s);
		if (s[l-1] == '"') {
			s[l-1] = '\0';
			return 0;
		}
		else {
			char *p = s2;
			do {
				c = fgetc(f);
				*p++ = c;
			} while (c != EOF && c != '"');
			*(p-1) = '\0';
			strcat(s, s2);
		}
	}
	return 0;
}

void fixFile(char * file)
{
#ifdef WIN32
	const char c='\\';
#else
	const char c='/';
#endif
while (*file!='\0') {
	if (*file=='/' || *file=='\\')
		*file=c;
	file++;
}
}

int loadConfigFile(const char * f)
{
	//default values
	eprintf ("load config file start");
	bool nonperiodic=false;
	char base_url[1024]="http://enc-testing-nomad.esc.rzg.mpg.de/v1.0/materials/";
	char material[1024]="";
	BACKGROUND[0] = 0.95f;
	BACKGROUND[1] = 0.95f;
	BACKGROUND[2] = 0.95f;
	SCREENSHOT="C:\\temp\\frame";
	ISOS = 0;
	TIMESTEPS=0;
	PATH=strdup("");
	numAtoms=0;
	atomScaling=1;
	clonedAtoms=0;
	showTrajectories = false;
	basisvectorreps=0;
	numClonedAtoms=0;
132
	has_abc=false;
133
134
135
136
	for (int i=0;i<3;i++)
		repetitions[i]=1;
	for (int i=0;i<3;i++)
		userpos[i] = 0;
137
	solid=0;
138
139
140
141
142
	//
	FILE *F = fopen(f, "r");
	if (F == 0)
	{
		eprintf( "Could not open config file %s\n", f);
143
		eprintf("Error: %d", errno);
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
		return -1;
	}
	char s[100];
	int r;
	while (!feof(F)) {
		r=fscanf(F, "%99s", s);
		if (r <= 0)
			continue;
		if (s[0] == '#') {//comment
			discardline(F);
			continue;
		}
		if (!strcmp(s, "timesteps")) {
			int timesteps;
			r = fscanf(F, "%d", &timesteps);
			if (TIMESTEPS==0)
				TIMESTEPS=timesteps;
			else
				TIMESTEPS=std::min(TIMESTEPS, timesteps);
		}
		else if (!strcmp(s, "isos")) {
			if (ISOS!=0) 
				return -9;
			r = fscanf(F, "%d", &ISOS);
			plyfiles = new const char*[ISOS];
			isocolours = new float*[ISOS];
			translations = new float*[ISOS];
			for (int i = 0; i < ISOS; i++) {
				plyfiles[i] = strdup("");
				isocolours[i] = new float[4];
				translations[i] = new float[3];
				//default values
				for (int j = 0; j < 4; j++)
					isocolours[i][j] = 1.f;
				for (int j = 0; j < 3; j++)
					translations[i][j] = 0.f;
			}
		}
		else if (!strcmp(s, "values")) {
			if (ISOS == 0) {
				eprintf( "values with no previous correct isos parameter\n");
				fclose(F);
				return -3;
			}
			for (int i = 0; i < ISOS; i++) {
				r=readString(F, s);
				if (r!=0)
					return -14;
				free ((void*)(plyfiles[i]));
				plyfiles[i] = strdup(s);
			}

		}
		else if (!strcmp(s, "colours")) {
			if (ISOS == 0) {
				eprintf( "colours with no previous correct isos parameter\n");
				return -4;
			}
			for (int i = 0; i < ISOS; i++) {
				for (int j = 0; j < 4; j++)
					r = fscanf(F, "%f", &(isocolours[i][j]));
				//if (r==0)
			}
		}
		else if (!strcmp(s, "path")) {
			r=readString(F, s);
			if (r!=0)
				return -14;			
			free((void*)PATH);
			PATH = strdup(s);
		}
		else if (!strcmp(s, "background")) {
			for (int i = 0; i < 3; i++)
				r = fscanf(F, "%f", BACKGROUND + i);
		}
		else if (!strcmp(s, "translations")) {
			if (ISOS == 0) {
				eprintf( "translations with no previous correct isos parameter\n");
				fclose(F);
				return -5;
			}
			for (int i=0;i<ISOS;i++)
				for (int j = 0; j < 3; j++) {
					r = fscanf(F, "%f", &(translations[i][j]));
				}
		}
		else if (!strcmp(s, "userpos")) {
			for (int j = 0; j < 3; j++) {
				r = fscanf(F, "%f", &(userpos[j]));
			}
		}
		else if (!strcmp(s, "screenshot")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			SCREENSHOT = strdup(s);
		}
		else if (!strcmp(s, "xyzfile")||!strcmp(s, "atomfile")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			int timesteps;
			char file[256];
			sprintf (file, "%s%s", PATH, s);
			fixFile(file);
			int e;
			e=readAtomsXYZ(file, &numAtoms, &timesteps, &atoms);
			if (e<0)
				return e-100;
			updateTIMESTEPS (timesteps);
254
			//eprintf ("After read of xyzfile, numatoms 0 =%d", numAtoms[0]);
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
		}
		else if (!strcmp(s, "cubefile")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			int timesteps;
			char file[256];
			sprintf(file, "%s%s", PATH, s);
			fixFile(file);
			int e;
			e = readAtomsCube(file, &numAtoms, &timesteps, &atoms);
			if (e<0)
				return e - 200;
			updateTIMESTEPS (timesteps);
			//	return -8;
		}
		else if (!strcmp(s, "atomscaling")) {
			r = fscanf(F, "%f", &atomScaling);
		}
		else if (!strcmp(s, "abc")) {
			for (int i=0;i<3;i++)
				for (int j=0;j<3;j++) {
					r = fscanf(F, "%f", &(abc[i][j]));
					if (r!=1)
						return -10;
				}
			has_abc = true;
		}
		else if (!strcmp(s, "json")) {
			r=readString(F, s);
			if (r!=0)
				return -14;
			char file[256];
			sprintf(file, "%s%s", PATH, s);
			fixFile(file);
			int e;
			int timesteps;
			//rgh fixme, we know only one
			e = readAtomsJson (file, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms);
			if (e<0)
				return e-300;
			numClonedAtoms=clonedAtoms[0].size()/4;
			has_abc=true;
			updateTIMESTEPS (timesteps);
		} 
		else if (!strcmp(s, "baseurl")) {
			r=readString (F, base_url);
			if (r!=0)
				return -14;
		} 
		else if (!strcmp(s, "jsonurl")) {
			int e;
			int timesteps;
			r=readString (F, material);
			if (r!=0)
				return -14;
			char url[2048];
			sprintf (url, "%s%s", base_url, material);
			//rgh fixme, we know only one
			eprintf ("load config file start, before readAtomsJsonURL");
			e = readAtomsJsonURL (url, &numAtoms, &timesteps, &atoms, abc, &clonedAtoms);
			eprintf ("load config file start, after readAtomsJsonURL");
			if (e<0)
				return e-300;
			numClonedAtoms=clonedAtoms[0].size()/4;
			has_abc=true;
			updateTIMESTEPS (timesteps);
		}
		else if (!strcmp(s, "showtrajectory")) {
			showTrajectories = true;
			int atom;
			while (1 == (r = fscanf(F, "%d", &atom))) {
				atomtrajectories.push_back(atom - 1);
			}
			if (atomtrajectories.size() == 0) //empty showtrajectory
				showTrajectories = false;
		} else if (!strcmp(s, "nonperiodic")) {
			nonperiodic=true;
		} else if (!strcmp(s, "repetitions")) {
			for (int j=0;j<3;j++) {
					r = fscanf(F, "%d", repetitions+j);
					if (r!=1)
						return -11;
			}
339
340
341
342
343
344
345
346
347
348
349
350
		} else if (!strcmp(s, "atomglyph")) {
			r=fscanf (F, "%s", s);
			if (r==0)
				return -15;
			if (!strcmp(s, "icosahedron"))
				solid=new Solid(Solid::Type::Icosahedron);
			else if(!strcmp(s, "octahedron"))
				solid=new Solid(Solid::Type::Octahedron);
			else if(!strcmp(s, "tetrahedron"))
				solid=new Solid(Solid::Type::Tetrahedron);
			else
				return -15;
351
352
353
		} else if (!strcmp (s, "\x0d")) { //discard windows newline (problem in Sebastian Kokott's phone (?!)
			continue;
		} else {
354
			eprintf( "Unrecognized parameter %s\n", s);
355
356
			for (int i=0;i<strlen(s);i++)
				eprintf ("<%d>", s[i]);
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
			fclose(F);
			return -2;
		}
	}

//verification and additional processing
	fclose(F);

	if (ISOS == 0 && numAtoms==0) {
		eprintf( "Missing isos and atomfile parameter\n");
		return -6;
	} 
	if (ISOS !=0 && plyfiles[0] == 0) {
		eprintf( "Missing values parameter\n");
		fclose(F);
		return -7;
	}
	if (nonperiodic) {
		numClonedAtoms=0;
		if (repetitions[0]!=1 ||repetitions[1]!=1 ||repetitions[2]!=1)
			return -12;
	}
	if (repetitions[0]!=1 ||repetitions[1]!=1 ||repetitions[2]!=1) {
		if (!has_abc)
			return (-13);
		numClonedAtoms=0;
	}

	if (showTrajectories && atomtrajectories[0]<0) {
		atomtrajectories.clear();
		for (int i=0;i<*numAtoms;i++)
			atomtrajectories.push_back(i);
	}
390
	//eprintf ("Before returning, numatoms 0 =%d", numAtoms[0]);
391
	return 0;
392
}