1 | #include <stdlib.h>
|
---|
2 | #include <stdio.h>
|
---|
3 | #include <string.h>
|
---|
4 | #include <math.h>
|
---|
5 | #include <time.h>
|
---|
6 |
|
---|
7 | #include <gsl/gsl_matrix.h>
|
---|
8 | #include <gsl/gsl_vector.h>
|
---|
9 | #include <gsl/gsl_eigen.h>
|
---|
10 | #include <gsl/gsl_blas.h>
|
---|
11 |
|
---|
12 | #include "NanoCreator.h"
|
---|
13 | #include "version.h"
|
---|
14 |
|
---|
15 |
|
---|
16 | // ---------------------------------- F U N C T I O N S ----------------------------------------------
|
---|
17 |
|
---|
18 | // ================================= File functions ==============================
|
---|
19 |
|
---|
20 | #define LINE_SIZE 80
|
---|
21 | #define NDIM 3
|
---|
22 |
|
---|
23 | /** Allocs memory and prints a message on fail.
|
---|
24 | * \param *size size of alloc in bytes
|
---|
25 | * \param *msg error msg
|
---|
26 | * \return pointer to allocated memory
|
---|
27 | */
|
---|
28 | void * Malloc (size_t size, const char *msg)
|
---|
29 | {
|
---|
30 | void *ptr = malloc(size);
|
---|
31 | if (ptr == NULL) {
|
---|
32 | if (msg == NULL)
|
---|
33 | fprintf(stdout, "ERROR: Malloc\n");
|
---|
34 | else
|
---|
35 | fprintf(stdout, "ERROR: Malloc - %s\n", msg);
|
---|
36 | return NULL;
|
---|
37 | } else {
|
---|
38 | return ptr;
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /** Callocs memory and prints a message on fail.
|
---|
43 | * \param *size size of alloc in bytes
|
---|
44 | * \param *value pointer to initial value
|
---|
45 | * \param *msg error msg
|
---|
46 | * \return pointer to allocated memory
|
---|
47 | */
|
---|
48 | void * Calloc (size_t size, double value, const char *msg)
|
---|
49 | {
|
---|
50 | void *ptr = calloc(size, value);
|
---|
51 | if (ptr == NULL) {
|
---|
52 | if (msg == NULL)
|
---|
53 | fprintf(stdout, "ERROR: Calloc\n");
|
---|
54 | else
|
---|
55 | fprintf(stdout, "ERROR: Calloc - %s\n", msg);
|
---|
56 | return NULL;
|
---|
57 | } else {
|
---|
58 | return ptr;
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Frees memory only if ptr != NULL.
|
---|
63 | * \param *ptr pointer to array
|
---|
64 | * \param *msg
|
---|
65 | */
|
---|
66 | void Free(void * ptr, const char *msg)
|
---|
67 | {
|
---|
68 | if (ptr != NULL)
|
---|
69 | free(ptr);
|
---|
70 | else {
|
---|
71 | if (msg == NULL)
|
---|
72 | fprintf(stdout, "ERROR: Free\n");
|
---|
73 | else
|
---|
74 | fprintf(stdout, "ERROR: Free - %s\n", msg);
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | /** Allocs memory and prints a message on fail.
|
---|
79 | * \param **old_ptr pointer to old memory range
|
---|
80 | * \param *newsize new size of alloc in bytes
|
---|
81 | * \param *msg error msg
|
---|
82 | * \return pointer to allocated memory
|
---|
83 | */
|
---|
84 | void * Realloc (void *old_ptr, size_t newsize, const char *msg)
|
---|
85 | {
|
---|
86 | if (old_ptr == NULL) {
|
---|
87 | fprintf(stdout, "ERROR: Realloc - old_ptr NULL\n");
|
---|
88 | exit(255);
|
---|
89 | }
|
---|
90 | void *ptr = realloc(old_ptr, newsize);
|
---|
91 | if (ptr == NULL) {
|
---|
92 | if (msg == NULL)
|
---|
93 | fprintf(stdout, "ERROR: Realloc\n");
|
---|
94 | else
|
---|
95 | fprintf(stdout, "ERROR: Realloc - %s\n", msg);
|
---|
96 | return NULL;
|
---|
97 | } else {
|
---|
98 | return ptr;
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Reads a file's contents into a char buffer of appropiate size.
|
---|
103 | * \param *filename name of file
|
---|
104 | * \param pointer to integer holding read/allocated buffer length
|
---|
105 | * \return pointer to allocated segment with contents
|
---|
106 | */
|
---|
107 | char * ReadBuffer (char *filename, int *bufferlength)
|
---|
108 | {
|
---|
109 | if ((filename == NULL) || (bufferlength == NULL)) {
|
---|
110 | fprintf(stderr, "ERROR: ReadBuffer - ptr to filename or bufferlength NULL\n");
|
---|
111 | exit(255);
|
---|
112 | }
|
---|
113 | char *buffer = Malloc(sizeof(char)*LINE_SIZE, "ReadBuffer: buffer");
|
---|
114 | int i = 0, number;
|
---|
115 | FILE *file = fopen(filename, "r");
|
---|
116 | if (file == NULL) {
|
---|
117 | fprintf(stderr, "File open error: %s", filename);
|
---|
118 | exit(255);
|
---|
119 | }
|
---|
120 | while ((number = fread(&buffer[i], sizeof(char), LINE_SIZE, file)) == LINE_SIZE) {
|
---|
121 | //fprintf(stdout, "%s", &buffer[i]);
|
---|
122 | i+= LINE_SIZE;
|
---|
123 | buffer = (char *) Realloc(buffer, i+LINE_SIZE, "ReadBuffer: buffer");
|
---|
124 | }
|
---|
125 | fclose(file);
|
---|
126 | fprintf(stdout, "Buffer length is %i\n", i);
|
---|
127 | *bufferlength = i+(number);
|
---|
128 | return buffer;
|
---|
129 | }
|
---|
130 |
|
---|
131 | /** Extracts next line out of a buffer.
|
---|
132 | * \param *buffer buffer to parse for newline
|
---|
133 | * \param *line contains complete line on return
|
---|
134 | * \return length of line, 0 if end of file
|
---|
135 | */
|
---|
136 | int GetNextline(char *buffer, char *line)
|
---|
137 | {
|
---|
138 | if ((buffer == NULL) || (line == NULL)) {
|
---|
139 | fprintf(stderr, "ERROR: GetNextline - ptr to buffer or line NULL\n");
|
---|
140 | exit(255);
|
---|
141 | }
|
---|
142 | int length;
|
---|
143 | char *ptr = strchr(buffer, '\n');
|
---|
144 | //fprintf(stdout, "Newline at %p from %p\n", ptr, buffer);
|
---|
145 | if (ptr == NULL) { // buffer ends
|
---|
146 | return 0;
|
---|
147 | } else {
|
---|
148 | //fprintf(stdout, "length of line is %d\n", length);
|
---|
149 | length = (int)(ptr - buffer)/sizeof(char);
|
---|
150 | strncpy(line, buffer, length);
|
---|
151 | line[length]='\0';
|
---|
152 | return length+1;
|
---|
153 | }
|
---|
154 | }
|
---|
155 |
|
---|
156 | /** Adds commentary stuff (needed for further stages) to Cell xyz files.
|
---|
157 | * \param *filename file name
|
---|
158 | * \param atomicnumner number of atoms in xyz
|
---|
159 | * \param **Vector list of three unit cell vectors
|
---|
160 | * \param **Recivector list of three reciprocal unit cell vectors
|
---|
161 | * \param atomicnumber number of atoms in cell
|
---|
162 | */
|
---|
163 | void AddAtomicNumber(char *filename, int atomicnumber, double **Vector, double **Recivector)
|
---|
164 | {
|
---|
165 | if ((filename == NULL) || (Vector == NULL) || (Recivector == NULL)) {
|
---|
166 | fprintf(stdout, "ERROR: AddAtomicNumber - ptr to filename, Vector or Recivector NULL\n");
|
---|
167 | exit(255);
|
---|
168 | }
|
---|
169 | int bufferlength;
|
---|
170 | char *buffer = ReadBuffer(filename, &bufferlength);
|
---|
171 | FILE *file2 = fopen(filename, "w+");
|
---|
172 | if (file2 == NULL) {
|
---|
173 | fprintf(stdout, "ERROR: AddAtomicNumber: %s can't open for writing\n", filename);
|
---|
174 | exit(255);
|
---|
175 | }
|
---|
176 | double volume = Determinant(Vector);
|
---|
177 | time_t now;
|
---|
178 |
|
---|
179 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
180 | // open for writing and prepend
|
---|
181 | fprintf(file2,"%d\n", atomicnumber); // 2
|
---|
182 | fprintf(file2,"\tgenerated with Nanotube creator on %s", ctime(&now));
|
---|
183 | fwrite(buffer, sizeof(char), bufferlength, file2); // append buffer
|
---|
184 |
|
---|
185 | // Add primitive vectors as comment
|
---|
186 | fprintf(file2,"\n****************************************\n\n");
|
---|
187 | fprintf(file2,"Primitive vectors\n");
|
---|
188 | fprintf(file2,"a(1) = %f\t%f\t%f\n", Vector[0][0], Vector[0][1], Vector[0][2]);
|
---|
189 | fprintf(file2,"a(2) = %f\t%f\t%f\n", Vector[1][0], Vector[1][1], Vector[1][2]);
|
---|
190 | fprintf(file2,"a(3) = %f\t%f\t%f\n", Vector[2][0], Vector[2][1], Vector[2][2]);
|
---|
191 | fprintf(file2,"\nVolume = %f", volume);
|
---|
192 | fprintf(file2,"\nReciprocal Vectors\n");
|
---|
193 | fprintf(file2,"b(1) = %f\t%f\t%f\n", Recivector[0][0], Recivector[0][1], Recivector[0][2]);
|
---|
194 | fprintf(file2,"b(2) = %f\t%f\t%f\n", Recivector[1][0], Recivector[1][1], Recivector[1][2]);
|
---|
195 | fprintf(file2,"b(3) = %f\t%f\t%f\n", Recivector[2][0], Recivector[2][1], Recivector[2][2]);
|
---|
196 |
|
---|
197 | fclose(file2); // close file
|
---|
198 | Free(buffer, "AddAtomicNumber: buffer");
|
---|
199 | }
|
---|
200 |
|
---|
201 | /** Adds commentary stuff (needed for further stages) to Sheet xyz files.
|
---|
202 | * \param *filename file name
|
---|
203 | * \param *axis array with major, minor and no axis
|
---|
204 | * \param *chiral pointer to array with both chiral values
|
---|
205 | * \param *factors pointer to array with length and radius factor
|
---|
206 | * \param seed random number seed
|
---|
207 | * \param numbercell number of atoms in unit cell, needed as length of \a *randomness
|
---|
208 | * \param *randomness for each atom in unit cell a factor between 0..1, giving its probability of appearance
|
---|
209 | */
|
---|
210 | void AddSheetInfo(char *filename, int *axis, int *chiral, int *factors, int seed, int numbercell, double *randomness)
|
---|
211 | {
|
---|
212 | int i;
|
---|
213 | if ((filename == NULL) || (axis == NULL) || (chiral == NULL) || (factors == NULL)) {
|
---|
214 | fprintf(stdout, "ERROR: AddSheetInfo - ptr to filename, axis, chiral or factors NULL\n");
|
---|
215 | exit(255);
|
---|
216 | }
|
---|
217 | // open for writing and append
|
---|
218 | FILE *file2 = fopen(filename,"a");
|
---|
219 | if (file2 == NULL) {
|
---|
220 | fprintf(stderr, "ERROR: AddSheetInfo - can't open %s for appending\n", filename);
|
---|
221 | exit(255);
|
---|
222 | }
|
---|
223 | // Add primitive vectors as comment
|
---|
224 | fprintf(file2,"\n****************************************\n\n");
|
---|
225 | fprintf(file2,"Axis %d\t%d\t%d\n", axis[0], axis[1], axis[2]);
|
---|
226 | fprintf(file2,"(n,m) %d\t%d\n", chiral[0], chiral[1]);
|
---|
227 | fprintf(file2,"factors %d\t%d\n", factors[0], factors[1]);
|
---|
228 | fprintf(file2,"seed %d\n", seed);
|
---|
229 | fprintf(file2,"\nRandomness\n");
|
---|
230 | for (i=0; i<numbercell; i++) {
|
---|
231 | fprintf(file2,"%d %g\n", i, randomness[i]);
|
---|
232 | }
|
---|
233 | fclose(file2);
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | // ================================= Vector functions ==============================
|
---|
238 |
|
---|
239 | /** Transforms a vector b with a matrix A: Ab = x.
|
---|
240 | * \param **matrixref reference to NDIMxNDIM matrix A
|
---|
241 | * \param *vectorref reference to NDIM vector b
|
---|
242 | * \return reference to resulting NDIM vector Ab = x
|
---|
243 | */
|
---|
244 | double *MatrixTrafo(double **matrixref, double *vectorref)
|
---|
245 | {
|
---|
246 | if ((matrixref == NULL) || (vectorref == NULL)) {
|
---|
247 | fprintf(stderr, "ERROR: MatrixTrafo: ptr to matrixref or vectorref NULL\n");
|
---|
248 | exit(255);
|
---|
249 | }
|
---|
250 | //double *returnvector = Calloc(sizeof(double)*NDIM, 0., "MatrixTrafo: returnvector");
|
---|
251 | double *returnvector = calloc(sizeof(double)*NDIM, 0.);
|
---|
252 | if (returnvector == NULL) {
|
---|
253 | fprintf(stderr, "ERROR: MatrixTrafo - returnvector\n");
|
---|
254 | exit(255);
|
---|
255 | }
|
---|
256 | int i,j;
|
---|
257 |
|
---|
258 | for (i=0;i<NDIM;i++)
|
---|
259 | for (j=0;j<NDIM;j++)
|
---|
260 | returnvector[j] += matrixref[i][j] * vectorref[i];
|
---|
261 |
|
---|
262 | return returnvector;
|
---|
263 | }
|
---|
264 | double *MatrixTrafoInverse(double *vectorref, double **matrixref)
|
---|
265 | {
|
---|
266 | if ((matrixref == NULL) || (vectorref == NULL)) {
|
---|
267 | fprintf(stderr, "ERROR: MatrixTrafo: ptr to matrixref or vectorref NULL\n");
|
---|
268 | exit(255);
|
---|
269 | }
|
---|
270 | //double *returnvector = Calloc(sizeof(double)*NDIM, 0., "MatrixTrafo: returnvector");
|
---|
271 | double *returnvector = calloc(sizeof(double)*NDIM, 0.);
|
---|
272 | if (returnvector == NULL) {
|
---|
273 | fprintf(stderr, "ERROR: MatrixTrafo - returnvector\n");
|
---|
274 | exit(255);
|
---|
275 | }
|
---|
276 | int i,j;
|
---|
277 |
|
---|
278 | for (i=0;i<NDIM;i++)
|
---|
279 | for (j=0;j<NDIM;j++)
|
---|
280 | returnvector[i] += matrixref[i][j] * vectorref[j];
|
---|
281 |
|
---|
282 | return returnvector;
|
---|
283 | }
|
---|
284 |
|
---|
285 | /** Inverts a NDIMxNDIM matrix.
|
---|
286 | * \param **matrix to be inverted
|
---|
287 | * \param **inverse allocated space for inverse of \a **matrix
|
---|
288 | */
|
---|
289 | void MatrixInversion(double **matrix, double **inverse)
|
---|
290 | {
|
---|
291 | if ((matrix == NULL) || (inverse == NULL)) {
|
---|
292 | fprintf(stderr, "ERROR: MatrixInversion: ptr to matrix or inverse NULL\n");
|
---|
293 | exit(255);
|
---|
294 | }
|
---|
295 | // determine inverse
|
---|
296 | double det = Determinant(matrix);
|
---|
297 | inverse[0][0] = (matrix[1][1]*matrix[2][2] - matrix[1][2]*matrix[2][1])/det;
|
---|
298 | inverse[1][0] = (matrix[0][2]*matrix[2][1] - matrix[0][1]*matrix[2][2])/det;
|
---|
299 | inverse[2][0] = (matrix[0][1]*matrix[1][2] - matrix[0][2]*matrix[1][1])/det;
|
---|
300 | inverse[0][1] = (matrix[1][2]*matrix[2][0] - matrix[1][0]*matrix[2][2])/det;
|
---|
301 | inverse[1][1] = (matrix[0][0]*matrix[2][2] - matrix[0][2]*matrix[2][0])/det;
|
---|
302 | inverse[2][1] = (matrix[0][2]*matrix[1][0] - matrix[0][0]*matrix[1][2])/det;
|
---|
303 | inverse[0][2] = (matrix[1][0]*matrix[2][1] - matrix[1][1]*matrix[2][0])/det;
|
---|
304 | inverse[1][2] = (matrix[0][1]*matrix[2][0] - matrix[0][0]*matrix[2][1])/det;
|
---|
305 | inverse[2][2] = (matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0])/det;
|
---|
306 |
|
---|
307 | // check inverse
|
---|
308 | int flag = 0;
|
---|
309 | int i,j,k;
|
---|
310 | double tmp;
|
---|
311 | fprintf(stdout, "Checking inverse ... ");
|
---|
312 | for (i=0;i<NDIM;i++)
|
---|
313 | for (j=0;j<NDIM;j++) {
|
---|
314 | tmp = 0.;
|
---|
315 | for (k=0;k<NDIM;k++)
|
---|
316 | tmp += matrix[i][k]*inverse[j][k];
|
---|
317 | if (!flag) {
|
---|
318 | if (i == j) {
|
---|
319 | flag = (fabs(1.-tmp) > MYEPSILON) ? 1 : 0;
|
---|
320 | } else {
|
---|
321 | flag = (fabs(tmp) > MYEPSILON) ? 1 : 0;
|
---|
322 | }
|
---|
323 | }
|
---|
324 | }
|
---|
325 | if (!flag)
|
---|
326 | fprintf(stdout, "ok\n");
|
---|
327 | else
|
---|
328 | fprintf(stdout, "false\n");
|
---|
329 | }
|
---|
330 |
|
---|
331 | /** Flips to double numbers in place.
|
---|
332 | * \param *number1 pointer to first double
|
---|
333 | * \param *number2 pointer to second double
|
---|
334 | */
|
---|
335 | void flip(double *number1, double *number2)
|
---|
336 | {
|
---|
337 | if ((number1 == NULL) || (number2 == NULL)) {
|
---|
338 | fprintf(stderr, "ERROR: flip: ptr to number1 or number2 NULL\n");
|
---|
339 | exit(255);
|
---|
340 | }
|
---|
341 | double tmp = *number1;
|
---|
342 | *number1 = *number2;
|
---|
343 | *number2 = tmp;
|
---|
344 | }
|
---|
345 |
|
---|
346 | /** Transposes a matrix.
|
---|
347 | * \param **matrix pointer to NDIMxNDIM-matrix array
|
---|
348 | */
|
---|
349 | void Transpose(double **matrix)
|
---|
350 | {
|
---|
351 | if (matrix == NULL) {
|
---|
352 | fprintf(stderr, "ERROR: Transpose: ptr to matrix NULL\n");
|
---|
353 | exit(255);
|
---|
354 | }
|
---|
355 | int i,j;
|
---|
356 | for (i=0;i<NDIM;i++)
|
---|
357 | for (j=0;j<i;j++)
|
---|
358 | flip(&matrix[i][j],&matrix[j][i]);
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | /** Computes the determinant of a NDIMxNDIM matrix.
|
---|
363 | * \param **matrix pointer to matrix array
|
---|
364 | * \return det(matrix)
|
---|
365 | */
|
---|
366 | double Determinant(double **matrix) {
|
---|
367 | if (matrix == NULL) {
|
---|
368 | fprintf(stderr, "ERROR: Determinant: ptr to Determinant NULL\n");
|
---|
369 | exit(255);
|
---|
370 | }
|
---|
371 | double det = matrix[0][0] * (matrix[1][1]*matrix[2][2] - matrix[1][2]*matrix[2][1])
|
---|
372 | - matrix[1][1] * (matrix[0][0]*matrix[2][2] - matrix[0][2]*matrix[2][0])
|
---|
373 | + matrix[2][2] * (matrix[0][0]*matrix[1][1] - matrix[0][1]*matrix[1][0]);
|
---|
374 | return det;
|
---|
375 | }
|
---|
376 |
|
---|
377 | /** Adds \a *vector1 onto \a *vector2 coefficient-wise.
|
---|
378 | * \param *vector1 first vector, on return contains sum
|
---|
379 | * \param *vector2 vector which is projected
|
---|
380 | * \return sum of the two vectors
|
---|
381 | */
|
---|
382 | double * VectorAdd(double *vector1, double *vector2)
|
---|
383 | {
|
---|
384 | if ((vector1 == NULL) || (vector2 == NULL)) {
|
---|
385 | fprintf(stderr, "ERROR: VectorAdd: ptr to vector1 or vector2 NULL\n");
|
---|
386 | exit(255);
|
---|
387 | }
|
---|
388 | //double *returnvector = Calloc(sizeof(double)*NDIM, 0., "VectorAdd: returnvector");
|
---|
389 | double *returnvector = calloc(sizeof(double)*NDIM, 0.);
|
---|
390 | if (returnvector == NULL) {
|
---|
391 | fprintf(stderr, "ERROR: VectorAdd - returnvector\n");
|
---|
392 | exit(255);
|
---|
393 | }
|
---|
394 | int i;
|
---|
395 |
|
---|
396 | for (i=0;i<NDIM;i++)
|
---|
397 | returnvector[i] = vector1[i] + vector2[i];
|
---|
398 |
|
---|
399 | return returnvector;
|
---|
400 | }
|
---|
401 |
|
---|
402 | /** Fixed GramSchmidt-Orthogonalization for NDIM vectors
|
---|
403 | * \param @orthvector reference to NDIMxNDIM matrix
|
---|
404 | * \param @orthbetrag reference to NDIM vector with vector magnitudes
|
---|
405 | * \param @axis major-, minor- and noaxis for specific order for the GramSchmidt procedure
|
---|
406 | */
|
---|
407 | void Orthogonalize(double **orthvector, int *axis)
|
---|
408 | {
|
---|
409 | if ((orthvector == NULL) || (axis == NULL)) {
|
---|
410 | fprintf(stderr, "ERROR: Orthogonalize: ptr to orthvector or axis NULL\n");
|
---|
411 | exit(255);
|
---|
412 | }
|
---|
413 | double betrag;
|
---|
414 | int i;
|
---|
415 |
|
---|
416 | // first vector is untouched
|
---|
417 | // second vector
|
---|
418 | betrag = Projection(orthvector[axis[1]], orthvector[axis[0]]);
|
---|
419 | fprintf(stdout,"%lg\t",betrag);
|
---|
420 | for (i=0;i<NDIM;i++)
|
---|
421 | orthvector[axis[1]][i] -= orthvector[axis[0]][i] * betrag;
|
---|
422 | // third vector
|
---|
423 | betrag = Projection(orthvector[axis[0]], orthvector[axis[2]]);
|
---|
424 | fprintf(stdout,"%lg\t",betrag);
|
---|
425 | for (i=0;i<NDIM;i++)
|
---|
426 | orthvector[axis[2]][i] -= orthvector[axis[0]][i] * betrag;
|
---|
427 | betrag = Projection(orthvector[axis[1]], orthvector[axis[2]]);
|
---|
428 | fprintf(stdout,"%lg\n",betrag);
|
---|
429 | for (i=0;i<NDIM;i++)
|
---|
430 | orthvector[axis[2]][i] -= orthvector[axis[1]][i] * betrag;
|
---|
431 | }
|
---|
432 |
|
---|
433 | /** Computes projection of \a *vector2 onto \a *vector1.
|
---|
434 | * \param *vector1 reference vector
|
---|
435 | * \param *vector2 vector which is projected
|
---|
436 | * \return projection
|
---|
437 | */
|
---|
438 | double Projection(double *vector1, double *vector2)
|
---|
439 | {
|
---|
440 | if ((vector1 == NULL) || (vector2 == NULL)) {
|
---|
441 | fprintf(stderr, "ERROR: Projection: ptr to vector1 or vector2 NULL\n");
|
---|
442 | exit(255);
|
---|
443 | }
|
---|
444 | return (ScalarProduct(vector1, vector2)/Norm(vector1)/Norm(vector2));
|
---|
445 | }
|
---|
446 |
|
---|
447 | /** Determine scalar product between two vectors.
|
---|
448 | * \param *vector1 first vector
|
---|
449 | * \param *vector2 second vector
|
---|
450 | * \return scalar product
|
---|
451 | */
|
---|
452 | double ScalarProduct(double *vector1, double *vector2)
|
---|
453 | {
|
---|
454 | if ((vector1 == NULL) || (vector2 == NULL)) {
|
---|
455 | fprintf(stderr, "ERROR: ScalarProduct: ptr to vector1 or vector2 NULL\n");
|
---|
456 | exit(255);
|
---|
457 | }
|
---|
458 | double scp = 0.;
|
---|
459 | int i;
|
---|
460 |
|
---|
461 | for (i=0;i<NDIM;i++)
|
---|
462 | scp += vector1[i] * vector2[i];
|
---|
463 |
|
---|
464 | return scp;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /** Computes norm of \a *vector.
|
---|
468 | * \param *vector pointer to NDIM vector
|
---|
469 | * \return norm of \a *vector
|
---|
470 | */
|
---|
471 | double Norm(double *vector)
|
---|
472 | {
|
---|
473 | if (vector == NULL) {
|
---|
474 | fprintf(stderr, "ERROR: Norm: ptr to vector NULL\n");
|
---|
475 | exit(255);
|
---|
476 | }
|
---|
477 | return sqrt(ScalarProduct(vector, vector));
|
---|
478 | }
|
---|
479 |
|
---|
480 | /** Prints vector to \a *file.
|
---|
481 | * \param *file file or e.g. stdout
|
---|
482 | * \param *vector vector to be printed
|
---|
483 | */
|
---|
484 | void PrintVector(FILE *file, double *vector)
|
---|
485 | {
|
---|
486 | if ((file == NULL) || (vector == NULL)) {
|
---|
487 | fprintf(stderr, "ERROR: PrintVector: ptr to file or vector NULL\n");
|
---|
488 | exit(255);
|
---|
489 | }
|
---|
490 | int i;
|
---|
491 | for (i=0;i<NDIM;i++)
|
---|
492 | fprintf(file, "%5.5f\t", vector[i]);
|
---|
493 | fprintf(file, "\n");
|
---|
494 | }
|
---|
495 |
|
---|
496 | /** Prints matrix to \a *file.
|
---|
497 | * \param *file file or e.g. stdout
|
---|
498 | * \param **matrix matrix to be printed
|
---|
499 | */
|
---|
500 | void PrintMatrix(FILE *file, double **matrix)
|
---|
501 | {
|
---|
502 | if ((file == NULL) || (matrix == NULL)) {
|
---|
503 | fprintf(stderr, "ERROR: PrintMatrix: ptr to file or matrix NULL\n");
|
---|
504 | exit(255);
|
---|
505 | }
|
---|
506 | int i,j;
|
---|
507 | for (i=0;i<NDIM;i++) {
|
---|
508 | for (j=0;j<NDIM;j++)
|
---|
509 | fprintf(file, "%5.5f\t", matrix[i][j]);
|
---|
510 | fprintf(file, "\n");
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 | /** Returns greatest common denominator.
|
---|
515 | * \param a first integer
|
---|
516 | * \param b second integer
|
---|
517 | * \return GCD of a and b
|
---|
518 | */
|
---|
519 | int GCD(int a, int b)
|
---|
520 | {
|
---|
521 | int c;
|
---|
522 | do {
|
---|
523 | c = a % b; /* Rest of integer divison */
|
---|
524 | a = b; b = c; /* flip the two values */
|
---|
525 | } while( c != 0);
|
---|
526 | return a;
|
---|
527 | }
|
---|
528 |
|
---|
529 | /** Determines the biggest diameter of a sheet.
|
---|
530 | * \param **matrix reference to NDIMxNDIM matrix with row vectors
|
---|
531 | * \param *axis reference to NDIM vector with permutation of axis indices [0,1,2]
|
---|
532 | * \param *factors factorsfor axis[0] and axis[1]
|
---|
533 | * \return biggest diameter of sheet
|
---|
534 | */
|
---|
535 | double DetermineBiggestDiameter(double **matrix, int *axis, int *factors)
|
---|
536 | {
|
---|
537 | if ((axis == NULL) || (factors == NULL) || (matrix == NULL)) {
|
---|
538 | fprintf(stderr, "ERROR: DetermineBiggestDiameter: ptr to factors, axis or matrix NULL\n");
|
---|
539 | exit(255);
|
---|
540 | }
|
---|
541 | double diameter[2] = {0.,0.};
|
---|
542 | int i, biggest;
|
---|
543 |
|
---|
544 | for (i=0;i<NDIM;i++) {
|
---|
545 | diameter[0] += (matrix[axis[0]][i]*factors[0] - matrix[axis[1]][i]*factors[1]) * (matrix[axis[0]][i]*factors[0] - matrix[axis[1]][i]*factors[1]);
|
---|
546 | diameter[1] += (matrix[axis[0]][i]*factors[0] + matrix[axis[1]][i]*factors[1]) * (matrix[axis[0]][i]*factors[0] + matrix[axis[1]][i]*factors[1]);
|
---|
547 | }
|
---|
548 | if ((diameter[0] - diameter[1]) > MYEPSILON) {
|
---|
549 | biggest = 0;
|
---|
550 | } else {
|
---|
551 | biggest = 1;
|
---|
552 | }
|
---|
553 | diameter[0] = sqrt(diameter[0]);
|
---|
554 | diameter[1] = sqrt(diameter[1]);
|
---|
555 | fprintf(stdout, "\n\nMajor diameter of the sheet is %5.5f, minor diameter is %5.5f.\n",diameter[biggest],diameter[(biggest+1)%2]);
|
---|
556 |
|
---|
557 | return diameter[biggest];
|
---|
558 | }
|
---|
559 |
|
---|
560 | /** Determines the center of gravity of atoms in a buffer \a bufptr with given \a number
|
---|
561 | * \param *bufptr pointer to char buffer with atoms in (name x y z)-manner
|
---|
562 | * \param number number of atoms/lines to scan
|
---|
563 | * \return NDIM vector (allocated doubles) pointing back to center of gravity
|
---|
564 | */
|
---|
565 | double * CenterOfGravity(char *bufptr, int number)
|
---|
566 | {
|
---|
567 | if (bufptr == NULL) {
|
---|
568 | fprintf(stderr, "ERROR: CenterOfGravity - bufptr NULL\n");
|
---|
569 | exit(255);
|
---|
570 | }
|
---|
571 | double *cog = calloc(sizeof(double)*NDIM, 0.);
|
---|
572 | if (cog == NULL) {
|
---|
573 | fprintf(stderr, "ERROR: CenterOfGravity - cog\n");
|
---|
574 | exit(255);
|
---|
575 | }
|
---|
576 | double *atom = Malloc(sizeof(double)*NDIM, "CenterOfGravity: atom");
|
---|
577 | char name[255], line[255];
|
---|
578 | int i,j;
|
---|
579 |
|
---|
580 | // Determine center of gravity
|
---|
581 | for (i=0;i<number;i++) {
|
---|
582 | bufptr += GetNextline(bufptr, line);
|
---|
583 | sscanf(line, "%s %lg %lg %lg", name, &atom[0], &atom[1], &atom[2]);
|
---|
584 | //fprintf(stdout, "Read Atom %s %lg %lg %lg\n", name, atom[0], atom[1], atom[2]);
|
---|
585 | for (j=0;j<NDIM;j++)
|
---|
586 | cog[j] += atom[j];
|
---|
587 | }
|
---|
588 | for (i=0;i<NDIM;i++)
|
---|
589 | cog[i] /= -number;
|
---|
590 |
|
---|
591 | Free(atom, "CenterOfGravity: atom");
|
---|
592 | return cog;
|
---|
593 | }
|
---|
594 |
|
---|
595 | /** Creates orthogonal vectors to directions axis[0] and axis[1], assuming that axis[2] is always orthogonal.
|
---|
596 | * \param **OrthoVector contains vectors set on return with axis[2] equal to Vector[axis[2]]
|
---|
597 | * \param **Vector vectors to orthogonalize against
|
---|
598 | * \param *axis lookup for which direction is which.
|
---|
599 | */
|
---|
600 | void CreateOrthogonalAxisVectors(double **OrthoVector, double **Vector, int *axis) {
|
---|
601 | int i,j;
|
---|
602 | double factor;
|
---|
603 | // allocate memory
|
---|
604 | int *TempAxis = (int *) Malloc(sizeof(int)*NDIM, "Main: *TempAxis");
|
---|
605 | double **TempVectors = (double **) Malloc(sizeof(double *)*NDIM, "Main: *TempVectors");
|
---|
606 | for (i=0; i<NDIM; i++)
|
---|
607 | TempVectors[i] = (double *) Malloc(sizeof(double)*NDIM, "Main: TempVectors");
|
---|
608 |
|
---|
609 | for (i=0; i<NDIM; i++)
|
---|
610 | for (j=0; j<NDIM; j++)
|
---|
611 | TempVectors[i][j] = Vector[i][j];
|
---|
612 | // GramSchmidt generates Vector[1] orthogonal to Vector[0] and Vector[2] ortho. to Vector[1] and Vector[0]!
|
---|
613 | TempAxis[0] = axis[2]; // (axis 2 is the orthogonal plane axis)
|
---|
614 | TempAxis[1] = axis[0];
|
---|
615 | TempAxis[2] = axis[1];
|
---|
616 | Orthogonalize(TempVectors, TempAxis);
|
---|
617 | factor = Norm(Vector[axis[0]])/Norm(TempVectors[TempAxis[2]]);
|
---|
618 | factor *= (Projection(TempVectors[TempAxis[2]], Vector[axis[0]]) > 0) ? 1. : -1.;
|
---|
619 | for (i=0; i<NDIM; i++)
|
---|
620 | OrthoVector[axis[0]][i] = TempVectors[TempAxis[2]][i]*factor;
|
---|
621 |
|
---|
622 | TempAxis[1] = axis[1];
|
---|
623 | TempAxis[2] = axis[0];
|
---|
624 | for (i=0; i<NDIM; i++)
|
---|
625 | for (j=0; j<NDIM; j++)
|
---|
626 | TempVectors[i][j] = Vector[i][j];
|
---|
627 | Orthogonalize(TempVectors, TempAxis);
|
---|
628 | factor = Norm(Vector[axis[1]])/Norm(TempVectors[TempAxis[2]]);
|
---|
629 | factor *= (Projection(TempVectors[TempAxis[2]], Vector[axis[1]]) > 0) ? 1. : -1.;
|
---|
630 | for (i=0; i<NDIM; i++)
|
---|
631 | OrthoVector[axis[1]][i] = TempVectors[TempAxis[2]][i]*factor;
|
---|
632 |
|
---|
633 | for (i=0; i<NDIM; i++)
|
---|
634 | OrthoVector[axis[2]][i] = Vector[axis[2]][i];
|
---|
635 | //print vectors
|
---|
636 | fprintf(stdout, "Orthogonal vectors are: \n");
|
---|
637 | for (i=0; i<NDIM; i++) {
|
---|
638 | for (j=0; j<NDIM; j++)
|
---|
639 | fprintf(stdout, "%lg\t", OrthoVector[axis[i]][j]);
|
---|
640 | fprintf(stdout, "\n");
|
---|
641 | }
|
---|
642 |
|
---|
643 | // free memory
|
---|
644 | Free(TempAxis, "CreateOrthogonalAxisVectors: *TempAxis");
|
---|
645 | for (i=0; i<NDIM; i++ )
|
---|
646 | Free(TempVectors[i], "CreateOrthogonalAxisVectors: TempVectors");
|
---|
647 | Free(TempVectors, "CreateOrthogonalAxisVectors: *TempVectors");
|
---|
648 | };
|
---|
649 |
|
---|
650 | // ================================= other functions ==============================
|
---|
651 |
|
---|
652 | /** Prints a debug message.
|
---|
653 | * \param *msg debug message
|
---|
654 | */
|
---|
655 | void Debug(char *msg)
|
---|
656 | {
|
---|
657 | if (msg == NULL) {
|
---|
658 | fprintf(stderr, "ERROR: Debug: ptr to msg NULL\n");
|
---|
659 | exit(255);
|
---|
660 | }
|
---|
661 | fprintf(stdout, "%s", msg);
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | // --------------------------------------- M A I N ---------------------------------------------------
|
---|
666 | int main(int argc, char **argv) {
|
---|
667 | char *filename = NULL;
|
---|
668 | char *CellFilename = NULL, *SheetFilename = NULL, *TubeFilename = NULL, *TorusFilename = NULL;
|
---|
669 | char *SheetFilenameAligned = NULL, *TubeFilenameAligned = NULL;
|
---|
670 | double **Vector, **OrthoVector, **Recivector = NULL, **Tubevector = NULL, **TubevectorInverse = NULL;
|
---|
671 | double *atom = NULL, *atom_transformed = NULL;
|
---|
672 | double *x = NULL, *coord = NULL, *tempvector = NULL, *offset = NULL;
|
---|
673 | //double *cog = NULL, *cog_projected = NULL;
|
---|
674 | char stage[6];
|
---|
675 | int axis[NDIM] = {0,1,2}, chiral[2] = {1,1}, factors[NDIM] = {1,1,1};
|
---|
676 | char name[255], line[255], input = 'n';
|
---|
677 | char *CellBuffer = NULL, *SheetBuffer = NULL, *TubeBuffer = NULL, *bufptr = NULL;
|
---|
678 | double *randomness = NULL, percentage; // array with percentages for presence in sheet and beyond
|
---|
679 | int i,j, ggT;
|
---|
680 | int length;
|
---|
681 |
|
---|
682 | fprintf(stdout, "%s\n", ESPACKVersion);
|
---|
683 |
|
---|
684 | // Read command line arguments
|
---|
685 | if (argc <= 2) {
|
---|
686 | fprintf(stdout, "Usage: %s <file> <stage>\n\tWhere <file> specifies a file to start from <stage> or a basename\n\t<stage> is either None, Cell, Sheet, Tube, Torus and specifies where to start the rolling up from.\n\tNote: The .Aligned. files can't be used (rotation is essential).\n", argv[0]);
|
---|
687 | exit(255);
|
---|
688 | } else {
|
---|
689 | // store variables
|
---|
690 | filename = argv[1];
|
---|
691 | strncpy(stage, argv[2], 5);
|
---|
692 | fprintf(stdout, "I will begin at stage %s.\n", stage);
|
---|
693 |
|
---|
694 | // build filenames
|
---|
695 | char *ptr = strrchr(filename, '.');
|
---|
696 | if (ptr == NULL) {
|
---|
697 | ptr = filename;
|
---|
698 | } else {
|
---|
699 | length = strlen(filename);
|
---|
700 | if (ptr != NULL) {
|
---|
701 | length = ((int)(ptr-filename) >= length-5) ? (int)(ptr-filename) : length;
|
---|
702 | filename[length] = '\0'; // eventueller
|
---|
703 | }
|
---|
704 | }
|
---|
705 | fprintf(stdout, "I will use \'%s' as base for the filenames.\n\n", filename);
|
---|
706 | CellFilename = Malloc(sizeof(char)*(length+10), "Main: CellFilename");
|
---|
707 | SheetFilename = Malloc(sizeof(char)*(length+11), "Main: SheetFilename");
|
---|
708 | TubeFilename = Malloc(sizeof(char)*(length+10), "Main: TubeFilename");
|
---|
709 | TorusFilename = Malloc(sizeof(char)*(length+11), "Main: TorusFilename");
|
---|
710 | SheetFilenameAligned = Malloc(sizeof(char)*(length+20), "Main: SheetFilenameAligned");
|
---|
711 | TubeFilenameAligned = Malloc(sizeof(char)*(length+19), "Main: TubeFilenameAligned");
|
---|
712 | sprintf(CellFilename, "%s.Cell.xyz", filename);
|
---|
713 | sprintf(SheetFilename, "%s.Sheet.xyz", filename);
|
---|
714 | sprintf(TubeFilename, "%s.Tube.xyz", filename);
|
---|
715 | sprintf(TorusFilename, "%s.Torus.xyz", filename);
|
---|
716 | sprintf(SheetFilenameAligned, "%s.Sheet.Aligned.xyz", filename);
|
---|
717 | sprintf(TubeFilenameAligned, "%s.Tube.Aligned.xyz", filename);
|
---|
718 | }
|
---|
719 |
|
---|
720 | // Allocating memory
|
---|
721 | Debug ("Allocating memory\n");
|
---|
722 | atom = (double *) Malloc(sizeof(double)*NDIM, "Main: atom");
|
---|
723 | Vector = (double **) Malloc(sizeof(double *)*NDIM, "Main: *Vector");
|
---|
724 | OrthoVector = (double **) Malloc(sizeof(double *)*NDIM, "Main: *OrthoVector");
|
---|
725 | Recivector = (double **) Malloc(sizeof(double *)*NDIM, "Main: *Recivector");
|
---|
726 | Tubevector = (double **) Malloc(sizeof(double *)*NDIM, "Main: *Tubevector");
|
---|
727 | TubevectorInverse = (double **) Malloc(sizeof(double *)*NDIM, "Main: *TubevectorInverse");
|
---|
728 | for (i=0; i<NDIM; i++ ) {
|
---|
729 | Vector[i] = (double *) Malloc(sizeof(double)*NDIM, "Main: Vector");
|
---|
730 | OrthoVector[i] = (double *) Malloc(sizeof(double)*NDIM, "Main: OrthoVector");
|
---|
731 | Recivector[i] = (double *) Malloc(sizeof(double)*NDIM, "Main: Recivector");
|
---|
732 | Tubevector[i] = (double *) Malloc(sizeof(double)*NDIM, "Main: Tubevector");
|
---|
733 | TubevectorInverse[i] = (double *) Malloc(sizeof(double)*NDIM, "Main: TubevectorInverse");
|
---|
734 | }
|
---|
735 |
|
---|
736 | // ======================== STAGE: Cell ==============================
|
---|
737 | // The cell is simply created by transforming relative coordinates within the cell
|
---|
738 | // into cartesian ones using the unit cell vectors.
|
---|
739 |
|
---|
740 | double volume;
|
---|
741 | int numbercell;
|
---|
742 | FILE *CellFile;
|
---|
743 |
|
---|
744 | Debug ("STAGE: None\n");
|
---|
745 | // Read cell vectors from stdin or from file
|
---|
746 | if (!strncmp(stage, "Non", 3)) {
|
---|
747 | fprintf(stdout, "You will give the unit cell of the given substance.\nAfterwards, the programme will create a Sheet, a Tube and a Torus, each with their own xyz-file named accordingly.\n\n");
|
---|
748 | fprintf(stdout, "Enter 1st primitive vector: ");
|
---|
749 | fscanf(stdin, "%lg %lg %lg", &Vector[0][0], &Vector[0][1], &Vector[0][2]);
|
---|
750 | fprintf(stdout, "Enter 2nd primitive vector: ");
|
---|
751 | fscanf(stdin, "%lg %lg %lg", &Vector[1][0], &Vector[1][1], &Vector[1][2]);
|
---|
752 | fprintf(stdout, "Enter 3rd primitive vector: ");
|
---|
753 | fscanf(stdin, "%lg %lg %lg", &Vector[2][0], &Vector[2][1], &Vector[2][2]);
|
---|
754 | fprintf(stdout,"Unit vectors are\n");
|
---|
755 | PrintMatrix(stdout, Vector);
|
---|
756 | } else {
|
---|
757 | char *ptr = NULL;
|
---|
758 | char dummy[10];
|
---|
759 | CellBuffer = bufptr = ReadBuffer(CellFilename, &length);
|
---|
760 | for (i=0;i<NDIM;i++) {
|
---|
761 | sprintf(dummy, "a(%i) = ", i+1);
|
---|
762 | fprintf(stdout, "%s", dummy);
|
---|
763 | while ((length = GetNextline(bufptr, line)) != -1) {
|
---|
764 | bufptr += (length)*sizeof(char);
|
---|
765 | //fprintf(stdout, "LINE at %p: %s\n", bufptr, line);
|
---|
766 | if ((ptr = strstr(line, dummy)) != NULL)
|
---|
767 | break;
|
---|
768 | }
|
---|
769 | ptr += strlen(dummy);
|
---|
770 | sscanf(ptr, "%lg %lg %lg", &Vector[i][0], &Vector[i][1], &Vector[i][2]);
|
---|
771 | fprintf(stdout, "%5.5lg %5.5lg %5.5lg\n", Vector[i][0], Vector[i][1], Vector[i][2]);
|
---|
772 | }
|
---|
773 | }
|
---|
774 |
|
---|
775 | volume = Determinant(Vector);
|
---|
776 | fprintf(stdout,"Volume is %lg\n", volume);
|
---|
777 | MatrixInversion(Vector, Recivector);
|
---|
778 | //Transpose(Recivector); // inverse's got row vectors if normal matrix' got column ones
|
---|
779 | fprintf(stdout, "Reciprocal vector is ");
|
---|
780 | PrintMatrix(stdout, Recivector);
|
---|
781 | fprintf(stdout, "Reciprocal volume is %lg\n", Determinant(Recivector));
|
---|
782 |
|
---|
783 | fprintf(stdout, "Vector magnitudes: %5.5lg %5.5lg %5.5lg\n", Norm(Vector[0]), Norm(Vector[1]), Norm(Vector[2]));
|
---|
784 |
|
---|
785 | Debug ("STAGE: Cell\n");
|
---|
786 | if (!strncmp(stage, "Non", 3)) {
|
---|
787 | fprintf(stdout, "\nHow many atoms are in the unit cell: ");
|
---|
788 | fscanf(stdin, "%i", &numbercell);
|
---|
789 | CellFile = fopen(CellFilename, "w");
|
---|
790 | if (CellFile == NULL) {
|
---|
791 | fprintf(stderr, "ERROR: main - can't open %s for writing\n", CellFilename);
|
---|
792 | exit(255);
|
---|
793 | }
|
---|
794 | fprintf(stdout, "\nNext, you have to enter each atom in the cell as follows, e.g.\n");
|
---|
795 | fprintf(stdout, "Enter \'ChemicalSymbol X Y Z\' (relative to primitive vectors): C 0.5 0.25 0.5\n\n");
|
---|
796 | for (i = 0; i < numbercell; i++) {
|
---|
797 | fprintf(stdout, "Enter for atom %i \'ChemicalSymbol X Y Z\': ", i+1);
|
---|
798 | fscanf(stdin, "%s %lg %lg %lg", name, &atom[0], &atom[1], &atom[2]);
|
---|
799 | tempvector = MatrixTrafo(Vector, atom);
|
---|
800 | fprintf(stdout, "Atom %i: %s %5.5lg %5.5lg %5.5lg\n", i, name, tempvector[0], tempvector[1], tempvector[2]);
|
---|
801 | fprintf(stdout, "Probe: %s %5.5lg %5.5lg %5.5lg\n", name,
|
---|
802 | atom[0]*Vector[0][0]+atom[1]*Vector[1][0]+atom[2]*Vector[2][0],
|
---|
803 | atom[0]*Vector[0][1]+atom[1]*Vector[1][1]+atom[2]*Vector[2][1],
|
---|
804 | atom[0]*Vector[0][2]+atom[1]*Vector[1][2]+atom[2]*Vector[2][2]
|
---|
805 | );
|
---|
806 | fprintf(CellFile, "%s %lg %lg %lg\n", name, tempvector[0], tempvector[1], tempvector[2]);
|
---|
807 | Free(tempvector, "Main: At stage Cell - tempvector");
|
---|
808 | }
|
---|
809 | fflush(CellFile);
|
---|
810 | fclose(CellFile);
|
---|
811 | AddAtomicNumber(CellFilename, numbercell, Vector, Recivector);
|
---|
812 |
|
---|
813 | CellBuffer = ReadBuffer(CellFilename, &length);
|
---|
814 |
|
---|
815 | sprintf(stage, "Cell");
|
---|
816 | } else {
|
---|
817 | bufptr = CellBuffer;
|
---|
818 | GetNextline(bufptr, line);
|
---|
819 | sscanf(line, "%i", &numbercell);
|
---|
820 | }
|
---|
821 |
|
---|
822 | fprintf(stdout, "\nThere are %i atoms in the cell.\n", numbercell);
|
---|
823 |
|
---|
824 | // ======================== STAGE: Sheet =============================
|
---|
825 | // The sheet is a bit more complex. We read the cell in cartesian coordinates
|
---|
826 | // from the file. Next, we have to rotate the unit cell vectors by the so called
|
---|
827 | // chiral angle. This gives a slanted and elongated section upon the sheet of
|
---|
828 | // periodically repeated original unit cells. It only matches up if the factors
|
---|
829 | // were all integer! (That's why the rotation is discrete and the chiral angle
|
---|
830 | // specified not as (cos alpha, sin alpha) but as (n,m)) Also, we want this
|
---|
831 | // section to be rectangular, thus we orthogonalize the original unit vectors
|
---|
832 | // to gain our (later) tube axis.
|
---|
833 | // By looking at the biggest possible diameter we know whose original cells to
|
---|
834 | // look at and check if their respective compounds (contained atoms) still reside
|
---|
835 | // in the rotated, elongated section we need for the later tube.
|
---|
836 | // Then in a for loop we go through every cell. By projecting the vector leading
|
---|
837 | // from the origin to the specific atom down onto the major and minor axis we
|
---|
838 | // know if it's still within the boundaries spanned by these rotated and elongated
|
---|
839 | // (radius-, length factor) unit vectors or not. If yes, its coordinates are
|
---|
840 | // written to file.
|
---|
841 |
|
---|
842 | int numbersheet, biggestdiameter, sheetnr[NDIM], tmp, seed;
|
---|
843 | double x1,x2,x3, angle;
|
---|
844 | char flag = 'n';
|
---|
845 | FILE *SheetFile = NULL;
|
---|
846 | FILE *SheetFileAligned = NULL;
|
---|
847 |
|
---|
848 | Debug ("STAGE: Sheet\n");
|
---|
849 | if (!strncmp(stage, "Cell", 4)) {
|
---|
850 | fprintf(stdout, "\nEnter seed unequal 0 if any of the bonds shall have a randomness in their being: ");
|
---|
851 | fscanf(stdin, "%d", &seed);
|
---|
852 | if (seed != 0)
|
---|
853 | input = 'y';
|
---|
854 | randomness = (double *) Malloc(sizeof(double)*numbercell, "Main: at sheet - randomness");
|
---|
855 | for(i=0;i<numbercell;i++)
|
---|
856 | randomness[i] = 0.;
|
---|
857 | i = 0;
|
---|
858 | fprintf(stdout, "\n");
|
---|
859 | while (input == 'y') {
|
---|
860 | fprintf(stdout, "Enter atom number (-1 0 to end) and percentage (0.0...1.0): ");
|
---|
861 | fscanf(stdin, "%d %lg", &i, &percentage);
|
---|
862 | if (i == -1) { input = 'n'; fprintf(stdout, "Breaking\n"); }
|
---|
863 | else { randomness[i] = 1.-percentage; }
|
---|
864 | };
|
---|
865 |
|
---|
866 | fprintf(stdout, "\nSpecify the axis permutation that is going to be perpendicular to the sheet [tubeaxis, torusaxis, noaxis]: ");
|
---|
867 | fscanf(stdin, "%d %d %d", &axis[0], &axis[1], &axis[2]);
|
---|
868 | fprintf(stdout, "axis: %d %d %d\n", axis[0], axis[1], axis[2]);
|
---|
869 |
|
---|
870 | // create orthogonal vectors individually for each unit cell vector
|
---|
871 | CreateOrthogonalAxisVectors(OrthoVector, Vector, axis);
|
---|
872 | fprintf(stdout, "Orthogonal vector axis[0] %lg\n", Projection(Vector[axis[0]], OrthoVector[axis[0]]));
|
---|
873 | fprintf(stdout, "Orthogonal vector axis[1] %lg\n", Projection(Vector[axis[1]], OrthoVector[axis[1]]));
|
---|
874 |
|
---|
875 | do {
|
---|
876 | fprintf(stdout, "\nNow specify the two natural numbers (m n) defining the chiral angle, \nif the result is crap, try flipping to (m,n): ");
|
---|
877 | fscanf(stdin, "%d %d", &chiral[0], &chiral[1]);
|
---|
878 | ggT = GCD(2*chiral[1]+chiral[0],2*chiral[0]+chiral[1]);
|
---|
879 | fprintf(stdout, "Greatest Common Denominator of (2n+m, 2m+n) is %d\n", ggT);
|
---|
880 | fprintf(stdout, "chiral0: %d\tchiral1: %d\n", chiral[0], chiral[1]);
|
---|
881 | for (i=0;i<NDIM;i++) {
|
---|
882 | Tubevector[axis[0]][i] = (double)chiral[0] * Vector[axis[0]][i] + (double)chiral[1] * Vector[axis[1]][i];
|
---|
883 | //Tubevector[axis[0]][i] = chiral[0] * Vector[axis[0]][i] + chiral[1] * Vector[axis[1]][i];
|
---|
884 | //Tubevector[axis[0]][i] = (2.*chiral[0]+chiral[1])/(double)ggT * Vector[axis[0]][i] + (-chiral[0]-2.*chiral[1])/(double)ggT * Vector[axis[1]][i];
|
---|
885 | //Tubevector[axis[1]][i] = -chiral[1] * Vector[axis[0]][i] + chiral[0] * Vector[axis[1]][i];
|
---|
886 | Tubevector[axis[1]][i] = (double)chiral[0] * OrthoVector[axis[0]][i] - (double)chiral[1] * OrthoVector[axis[1]][i];
|
---|
887 | //Tubevector[axis[1]][i] = (-chiral[0]-2.*chiral[1])/(double)ggT * Vector[axis[0]][i] + (2.*chiral[0]+chiral[1])/(double)ggT * Vector[axis[1]][i];
|
---|
888 | // fprintf(stderr, "Tubevector[axis[0]][i] = (double)chiral[0] * Vector[axis[0]][i] + (double)chiral[1] * Vector[axis[1]][i]\n = %lg * %lg + %lg * %lg = %lg + %lg = %lg\n\n",
|
---|
889 | // (double)chiral[0], Vector[axis[0]][i], (double)chiral[1], Vector[axis[1]][i],
|
---|
890 | // (double)chiral[0] * Vector[axis[0]][i], (double)chiral[1] * Vector[axis[1]][i],
|
---|
891 | // Tubevector[axis[0]][i]);
|
---|
892 | Tubevector[axis[2]][i] = Vector[axis[2]][i];
|
---|
893 | }
|
---|
894 | // here we assume, that Vector[axis[2]] is along z direction!
|
---|
895 | gsl_matrix *M = gsl_matrix_alloc(2,2);
|
---|
896 | gsl_matrix *C = gsl_matrix_alloc(2,2);
|
---|
897 | gsl_matrix *evec = gsl_matrix_alloc(2,2);
|
---|
898 | gsl_vector *eval = gsl_vector_alloc(2);
|
---|
899 | gsl_vector *v = gsl_vector_alloc(2);
|
---|
900 | gsl_vector *u = gsl_vector_alloc(2);
|
---|
901 | gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc(2);
|
---|
902 | gsl_matrix_set(C, 0,0, Vector[axis[0]][0]);
|
---|
903 | gsl_matrix_set(C, 1,0, Vector[axis[0]][1]);
|
---|
904 | gsl_matrix_set(C, 0,1, Vector[axis[1]][0]);
|
---|
905 | gsl_matrix_set(C, 1,1, Vector[axis[1]][1]);
|
---|
906 | gsl_blas_dgemm(CblasTrans,CblasNoTrans, 1.0, C, C, 0.0, M);
|
---|
907 | fprintf(stdout, "M: \t%lg\t%lg\n\t%lg\t%lg\n", gsl_matrix_get(M,0,0), gsl_matrix_get(M,0,1), gsl_matrix_get(M,1,0), gsl_matrix_get(M,1,1));
|
---|
908 | gsl_eigen_symmv(M, eval, evec, w);
|
---|
909 | gsl_eigen_symmv_sort(eval,evec,GSL_EIGEN_SORT_ABS_DESC);
|
---|
910 | fprintf(stdout, "Eigenvalues: %lg\t%lg\n", gsl_vector_get(eval,0), gsl_vector_get(eval,1));
|
---|
911 | fprintf(stdout, "Eigenvectors: \t%lg\t%lg\n\t\t%lg\t%lg\n", gsl_matrix_get(evec,0,0), gsl_matrix_get(evec,0,1), gsl_matrix_get(evec,1,0), gsl_matrix_get(evec,1,1));
|
---|
912 | gsl_matrix_set(M, 0,0, 0.);
|
---|
913 | gsl_matrix_set(M, 1,0, 1.);
|
---|
914 | gsl_matrix_set(M, 0,1, -gsl_vector_get(eval,1)/gsl_vector_get(eval,0));
|
---|
915 | gsl_matrix_set(M, 1,1, 0.);
|
---|
916 | gsl_vector_set(v,0,(double)chiral[0]);
|
---|
917 | gsl_vector_set(v,1,(double)chiral[1]);
|
---|
918 | gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0, evec, M, 0.0, C);
|
---|
919 | gsl_blas_dgemm(CblasNoTrans,CblasTrans, 1.0, C, evec, 0.0, M);
|
---|
920 | fprintf(stdout, "M: \t%lg\t%lg\n\t%lg\t%lg\n", gsl_matrix_get(M,0,0), gsl_matrix_get(M,0,1), gsl_matrix_get(M,1,0), gsl_matrix_get(M,1,1));
|
---|
921 | gsl_blas_dgemv(CblasNoTrans, 1.0, M, v, 0.0, u);
|
---|
922 | fprintf(stdout, "Looking for factor to integer...\n");
|
---|
923 | for(i=1;i<(chiral[0]+chiral[1])*(chiral[0]+chiral[1]);i++) {
|
---|
924 | x1 = gsl_vector_get(u,0)*(double)i;
|
---|
925 | x2 = gsl_vector_get(u,1)*(double)i;
|
---|
926 | x3 =
|
---|
927 | fprintf(stdout, "%d: %d\t%d vs. %lg\t%lg\n",i, ((int)(x1+x1/fabs(x1)*.5)), ((int)(x2+x2/fabs(x2)*.5)), (x1), (x2));
|
---|
928 | if (( fabs( ((int)(x1+x1/fabs(x1)*.5)) - (x1) ) < 1e-6) && ( fabs( ((int)(x2+x2/fabs(x2)*.5)) - (x2) ) < 1e-6 )) {
|
---|
929 | gsl_blas_dscal((double)i, u);
|
---|
930 | break;
|
---|
931 | }
|
---|
932 | }
|
---|
933 | fprintf(stdout, "(c,d) = (%lg,%lg)\n",gsl_vector_get(u,0), gsl_vector_get(u,1));
|
---|
934 |
|
---|
935 | // get length
|
---|
936 | double x[NDIM];
|
---|
937 | for (i=0;i<NDIM;i++)
|
---|
938 | x[i] = gsl_vector_get(u,0) * Vector[axis[0]][i] + gsl_vector_get(u,1) * Vector[axis[1]][i];
|
---|
939 | angle = Norm(x)/Norm(Tubevector[axis[1]]) ;//ScalarProduct(x,Tubevector[axis[1]])/Norm(Tubevector[axis[1]]);
|
---|
940 | fprintf(stdout, "angle is %lg\n", angle);
|
---|
941 | for (i=0;i<NDIM;i++) {
|
---|
942 | Tubevector[axis[1]][i] = gsl_vector_get(u,0) * Vector[axis[0]][i] + gsl_vector_get(u,1) * Vector[axis[1]][i];
|
---|
943 | }
|
---|
944 |
|
---|
945 | // Probe
|
---|
946 | gsl_matrix_set(M, 0,0, Vector[axis[0]][0]);
|
---|
947 | gsl_matrix_set(M, 1,0, Vector[axis[0]][1]);
|
---|
948 | gsl_matrix_set(M, 0,1, Vector[axis[1]][0]);
|
---|
949 | gsl_matrix_set(M, 1,1, Vector[axis[1]][1]);
|
---|
950 | gsl_vector_set(v,0,(double)chiral[0]);
|
---|
951 | gsl_vector_set(v,1,(double)chiral[1]);
|
---|
952 | gsl_blas_dgemv(CblasNoTrans, 1.0, M, u, 0.0, eval);
|
---|
953 | gsl_blas_dgemv(CblasNoTrans, 1.0, M, v, 0.0, u);
|
---|
954 | x1=1.;
|
---|
955 | gsl_blas_ddot(u,eval,&x1);
|
---|
956 | fprintf(stdout, "Testing (c,d): (a,b) M^t M (c,d)^t = 0 ? : %lg\n", x1);
|
---|
957 |
|
---|
958 | gsl_matrix_free(M);
|
---|
959 | gsl_matrix_free(C);
|
---|
960 | gsl_matrix_free(evec);
|
---|
961 | gsl_vector_free(eval);
|
---|
962 | gsl_vector_free(v);
|
---|
963 | gsl_vector_free(u);
|
---|
964 | gsl_eigen_symmv_free(w);
|
---|
965 |
|
---|
966 | if (fabs(x1) > 1e-6) {
|
---|
967 | fprintf(stderr,"Resulting TubeVectors of axis %d and %d and not orthogonal, aborting.\n", axis[0], axis[1]);
|
---|
968 | return(128);
|
---|
969 | }
|
---|
970 |
|
---|
971 |
|
---|
972 | angle = Projection(Tubevector[axis[1]], Vector[axis[0]]);
|
---|
973 | fprintf(stdout, "Projection Tubevector1 axis[0] %lg %lg\n", angle, 1./angle);
|
---|
974 | angle = Projection(Tubevector[axis[1]], Vector[axis[1]]);
|
---|
975 | fprintf(stdout, "Projection Tubevector1 axis[1] %lg %lg\n", angle, 1./angle);
|
---|
976 |
|
---|
977 | /* fprintf(stdout, "Vector\n");
|
---|
978 | PrintMatrix(stdout, Vector);
|
---|
979 | fprintf(stdout, "Tubevector\n");
|
---|
980 | PrintMatrix(stdout, Tubevector);
|
---|
981 | for (i=0;i<NDIM;i++) {
|
---|
982 | fprintf(stdout, "Tubevector %d in Unit cell vectors:\t", axis[i]);
|
---|
983 | tempvector = MatrixTrafoInverse(Tubevector[axis[i]], Recivector);
|
---|
984 | PrintVector(stdout, tempvector);
|
---|
985 | Free(tempvector, "Main:tempvector");
|
---|
986 | }*/
|
---|
987 |
|
---|
988 | // Give info for length and radius factors
|
---|
989 | fprintf(stdout, "\nThe chiral angle then is %lg degrees with tube radius %5.5f A and length %5.5f A, i.e. torus radius of %5.5f A.\n",
|
---|
990 | acos(Projection(Vector[axis[0]], Tubevector[axis[0]]))/M_PI*180.,
|
---|
991 | Norm(Tubevector[axis[0]])/(2.*M_PI),
|
---|
992 | Norm(Tubevector[axis[1]]),
|
---|
993 | Norm(Tubevector[axis[1]])/(2.*M_PI)
|
---|
994 | );
|
---|
995 | fprintf(stdout, "\nGive integer factors for length and radius of tube (multiple of %d suggested) : ", ggT);
|
---|
996 | fscanf(stdin, "%d %d", &factors[1], &factors[0]);
|
---|
997 | fprintf(stdout, "\nThe chiral angle then is %5.5f degrees with tube radius %5.5f A and length %5.5f A, i.e. torus radius of %5.5f A.\n",
|
---|
998 | acos(Projection(Vector[axis[0]], Tubevector[axis[0]]))/M_PI*180.,
|
---|
999 | (double)factors[0]*Norm(Tubevector[axis[0]])/(2.*M_PI),
|
---|
1000 | (double)factors[1]*Norm(Tubevector[axis[1]]),
|
---|
1001 | (double)factors[1]*Norm(Tubevector[axis[1]])/(2.*M_PI)
|
---|
1002 | );
|
---|
1003 | fprintf(stdout, "Satisfied? [yn] ");
|
---|
1004 | fscanf(stdin, "%c", &flag);
|
---|
1005 | fscanf(stdin, "%c", &flag);
|
---|
1006 | } while (flag != 'y');
|
---|
1007 | } else {
|
---|
1008 | char *ptr = NULL;
|
---|
1009 | char dummy[10];
|
---|
1010 | double dummydouble;
|
---|
1011 |
|
---|
1012 | SheetBuffer = bufptr = ReadBuffer(SheetFilename, &length);
|
---|
1013 | bufptr += (GetNextline(bufptr, line))*sizeof(char);
|
---|
1014 | sscanf(line, "%d", &numbersheet);
|
---|
1015 |
|
---|
1016 | // retrieve axis permutation
|
---|
1017 | sprintf(dummy, "Axis");
|
---|
1018 | fprintf(stdout, "%s ", dummy);
|
---|
1019 | while ((length = GetNextline(bufptr, line)) != 0) {
|
---|
1020 | bufptr += (length)*sizeof(char);
|
---|
1021 | if ((ptr = strstr(line, dummy)) != NULL)
|
---|
1022 | break;
|
---|
1023 | }
|
---|
1024 | if (length == 0) {
|
---|
1025 | fprintf(stderr, "ERROR: Main at stage Sheet - could not find %s in %s\n", dummy, SheetFilename);
|
---|
1026 | exit(255);
|
---|
1027 | }
|
---|
1028 | ptr += strlen(dummy);
|
---|
1029 | sscanf(ptr, "%d %d %d", &axis[0], &axis[1], &axis[2]);
|
---|
1030 | fprintf(stdout, "%d %d %d\n", axis[0], axis[1], axis[2]);
|
---|
1031 |
|
---|
1032 | // retrieve chiral numbers
|
---|
1033 | sprintf(dummy, "(n,m)");
|
---|
1034 | fprintf(stdout, "%s ", dummy);
|
---|
1035 | while ((length = GetNextline(bufptr, line)) != 0) {
|
---|
1036 | bufptr += (length)*sizeof(char);
|
---|
1037 | if ((ptr = strstr(line, dummy)) != NULL)
|
---|
1038 | break;
|
---|
1039 | }
|
---|
1040 | if (length == 0) {
|
---|
1041 | fprintf(stderr, "ERROR: Main at stage Sheet - could not find %s in %s\n", dummy, SheetFilename);
|
---|
1042 | exit(255);
|
---|
1043 | }
|
---|
1044 | ptr += strlen(dummy);
|
---|
1045 | sscanf(ptr, "%d %d", &chiral[0], &chiral[1]);
|
---|
1046 | fprintf(stdout, "%d %d\n", chiral[0], chiral[1]);
|
---|
1047 | ggT = GCD(2*chiral[1]+chiral[0],2*chiral[0]+chiral[1]);
|
---|
1048 | fprintf(stdout, "Greatest Common Denominator of (2n+m, 2m+n) is %d\n", ggT);
|
---|
1049 |
|
---|
1050 | // retrieve length and radius factors
|
---|
1051 | sprintf(dummy, "factors");
|
---|
1052 | fprintf(stdout, "%s ", dummy);
|
---|
1053 | while ((length = GetNextline(bufptr, line)) != 0) {
|
---|
1054 | bufptr += (length)*sizeof(char);
|
---|
1055 | if ((ptr = strstr(line, dummy)) != NULL)
|
---|
1056 | break;
|
---|
1057 | }
|
---|
1058 | if (length == 0) {
|
---|
1059 | fprintf(stderr, "ERROR: Main at stage Sheet - could not find %s in %s\n", dummy, SheetFilename);
|
---|
1060 | exit(255);
|
---|
1061 | }
|
---|
1062 | ptr += strlen(dummy);
|
---|
1063 | sscanf(ptr, "%d %d %d", &factors[0], &factors[1], &factors[2]);
|
---|
1064 | fprintf(stdout, "%d %d %d\n", factors[0], factors[1], factors[2]);
|
---|
1065 |
|
---|
1066 | // create orthogonal vectors individually for each unit cell vector
|
---|
1067 | CreateOrthogonalAxisVectors(OrthoVector, Vector, axis);
|
---|
1068 | fprintf(stdout, "Orthogonal vector axis[0] %lg", Projection(Vector[axis[0]], OrthoVector[axis[0]]));
|
---|
1069 | fprintf(stdout, "Orthogonal vector axis[1] %lg", Projection(Vector[axis[1]], OrthoVector[axis[1]]));
|
---|
1070 | // create Tubevectors
|
---|
1071 | for (i=0;i<NDIM;i++) {
|
---|
1072 | Tubevector[axis[0]][i] = chiral[0] * Vector[axis[0]][i] + chiral[1] * Vector[axis[1]][i];
|
---|
1073 | //Tubevector[axis[0]][i] = (2.*chiral[0]+chiral[1])/(double)ggT * Vector[axis[0]][i] + (-chiral[0]-2.*chiral[1])/(double)ggT * Vector[axis[1]][i];
|
---|
1074 | //Tubevector[axis[1]][i] = -chiral[1] * Vector[axis[0]][i] + chiral[0] * Vector[axis[1]][i];
|
---|
1075 | Tubevector[axis[1]][i] = chiral[0] * OrthoVector[axis[0]][i] + chiral[1] * OrthoVector[axis[1]][i];
|
---|
1076 | //Tubevector[axis[1]][i] = (-chiral[0]-2.*chiral[1])/(double)ggT * Vector[axis[0]][i] + (2.*chiral[0]+chiral[1])/(double)ggT * Vector[axis[1]][i];
|
---|
1077 | Tubevector[axis[2]][i] = Vector[axis[2]][i];
|
---|
1078 | }
|
---|
1079 | // here we assume, that Vector[axis[2]] is along z direction!
|
---|
1080 | gsl_matrix *M = gsl_matrix_alloc(2,2);
|
---|
1081 | gsl_matrix *C = gsl_matrix_alloc(2,2);
|
---|
1082 | gsl_matrix *evec = gsl_matrix_alloc(2,2);
|
---|
1083 | gsl_vector *eval = gsl_vector_alloc(2);
|
---|
1084 | gsl_vector *v = gsl_vector_alloc(2);
|
---|
1085 | gsl_vector *u = gsl_vector_alloc(2);
|
---|
1086 | gsl_eigen_symmv_workspace *w = gsl_eigen_symmv_alloc(2);
|
---|
1087 | gsl_matrix_set(C, 0,0, Vector[axis[0]][0]);
|
---|
1088 | gsl_matrix_set(C, 1,0, Vector[axis[0]][1]);
|
---|
1089 | gsl_matrix_set(C, 0,1, Vector[axis[1]][0]);
|
---|
1090 | gsl_matrix_set(C, 1,1, Vector[axis[1]][1]);
|
---|
1091 | gsl_blas_dgemm(CblasTrans,CblasNoTrans, 1.0, C, C, 0.0, M);
|
---|
1092 | fprintf(stdout, "M: \t%lg\t%lg\n\t%lg\t%lg\n", gsl_matrix_get(M,0,0), gsl_matrix_get(M,0,1), gsl_matrix_get(M,1,0), gsl_matrix_get(M,1,1));
|
---|
1093 | gsl_eigen_symmv(M, eval, evec, w);
|
---|
1094 | gsl_eigen_symmv_sort(eval,evec,GSL_EIGEN_SORT_ABS_DESC);
|
---|
1095 | fprintf(stdout, "Eigenvalues: %lg\t%lg\n", gsl_vector_get(eval,0), gsl_vector_get(eval,1));
|
---|
1096 | fprintf(stdout, "Eigenvectors: \t%lg\t%lg\n\t\t%lg\t%lg\n", gsl_matrix_get(evec,0,0), gsl_matrix_get(evec,0,1), gsl_matrix_get(evec,1,0), gsl_matrix_get(evec,1,1));
|
---|
1097 | gsl_matrix_set(M, 0,0, 0.);
|
---|
1098 | gsl_matrix_set(M, 1,0, 1.);
|
---|
1099 | gsl_matrix_set(M, 0,1, -gsl_vector_get(eval,1)/gsl_vector_get(eval,0));
|
---|
1100 | gsl_matrix_set(M, 1,1, 0.);
|
---|
1101 | gsl_vector_set(v,0,(double)chiral[0]);
|
---|
1102 | gsl_vector_set(v,1,(double)chiral[1]);
|
---|
1103 | gsl_blas_dgemm(CblasNoTrans,CblasNoTrans, 1.0, evec, M, 0.0, C);
|
---|
1104 | gsl_blas_dgemm(CblasNoTrans,CblasTrans, 1.0, C, evec, 0.0, M);
|
---|
1105 | fprintf(stdout, "M: \t%lg\t%lg\n\t%lg\t%lg\n", gsl_matrix_get(M,0,0), gsl_matrix_get(M,0,1), gsl_matrix_get(M,1,0), gsl_matrix_get(M,1,1));
|
---|
1106 | gsl_blas_dgemv(CblasNoTrans, 1.0, M, v, 0.0, u);
|
---|
1107 | fprintf(stdout, "Looking for factor to integer...\n");
|
---|
1108 | for(i=1;i<(chiral[0]+chiral[1])*(chiral[0]+chiral[1]);i++) {
|
---|
1109 | x1 = gsl_vector_get(u,0)*(double)i;
|
---|
1110 | x2 = gsl_vector_get(u,1)*(double)i;
|
---|
1111 | x3 =
|
---|
1112 | fprintf(stdout, "%d: %d\t%d vs. %lg\t%lg\n",i, ((int)(x1+x1/fabs(x1)*.5)), ((int)(x2+x2/fabs(x2)*.5)), (x1), (x2));
|
---|
1113 | if (( fabs( ((int)(x1+x1/fabs(x1)*.5)) - (x1) ) < 1e-6) && ( fabs( ((int)(x2+x2/fabs(x2)*.5)) - (x2) ) < 1e-6 )) {
|
---|
1114 | gsl_blas_dscal((double)i, u);
|
---|
1115 | break;
|
---|
1116 | }
|
---|
1117 | }
|
---|
1118 | fprintf(stdout, "(c,d) = (%lg,%lg)\n",gsl_vector_get(u,0), gsl_vector_get(u,1));
|
---|
1119 |
|
---|
1120 | // get length
|
---|
1121 | double x[NDIM];
|
---|
1122 | for (i=0;i<NDIM;i++)
|
---|
1123 | x[i] = gsl_vector_get(u,0) * Vector[axis[0]][i] + gsl_vector_get(u,1) * Vector[axis[1]][i];
|
---|
1124 | angle = Norm(x)/Norm(Tubevector[axis[1]]) ;//ScalarProduct(x,Tubevector[axis[1]])/Norm(Tubevector[axis[1]]);
|
---|
1125 | fprintf(stdout, "angle is %lg\n", angle);
|
---|
1126 | for (i=0;i<NDIM;i++) {
|
---|
1127 | Tubevector[axis[1]][i] = gsl_vector_get(u,0) * Vector[axis[0]][i] + gsl_vector_get(u,1) * Vector[axis[1]][i];
|
---|
1128 | }
|
---|
1129 |
|
---|
1130 | // Probe
|
---|
1131 | gsl_matrix_set(M, 0,0, Vector[axis[0]][0]);
|
---|
1132 | gsl_matrix_set(M, 1,0, Vector[axis[0]][1]);
|
---|
1133 | gsl_matrix_set(M, 0,1, Vector[axis[1]][0]);
|
---|
1134 | gsl_matrix_set(M, 1,1, Vector[axis[1]][1]);
|
---|
1135 | gsl_vector_set(v,0,(double)chiral[0]);
|
---|
1136 | gsl_vector_set(v,1,(double)chiral[1]);
|
---|
1137 | gsl_blas_dgemv(CblasNoTrans, 1.0, M, u, 0.0, eval);
|
---|
1138 | gsl_blas_dgemv(CblasNoTrans, 1.0, M, v, 0.0, u);
|
---|
1139 | x1=1.;
|
---|
1140 | gsl_blas_ddot(u,eval,&x1);
|
---|
1141 | fprintf(stdout, "Testing (c,d): (a,b) M^t M (c,d)^t = 0 ? : %lg\n", x1);
|
---|
1142 |
|
---|
1143 | gsl_matrix_free(M);
|
---|
1144 | gsl_matrix_free(C);
|
---|
1145 | gsl_matrix_free(evec);
|
---|
1146 | gsl_vector_free(eval);
|
---|
1147 | gsl_vector_free(v);
|
---|
1148 | gsl_vector_free(u);
|
---|
1149 | gsl_eigen_symmv_free(w);
|
---|
1150 |
|
---|
1151 | if (fabs(x1) > 1e-6) {
|
---|
1152 | fprintf(stderr,"Resulting TubeVectors of axis %d and %d and not orthogonal, aborting.\n", axis[0], axis[1]);
|
---|
1153 | return(128);
|
---|
1154 | }
|
---|
1155 |
|
---|
1156 | // retrieve seed ...
|
---|
1157 | randomness = (double *) Calloc(sizeof(double)*numbercell, 0., "Main: at sheet - randomness");
|
---|
1158 | sprintf(dummy, "seed");
|
---|
1159 | fprintf(stdout, "%s ", dummy);
|
---|
1160 | while ((length = GetNextline(bufptr, line)) != 0) {
|
---|
1161 | bufptr += (length)*sizeof(char);
|
---|
1162 | if ((ptr = strstr(line, dummy)) != NULL)
|
---|
1163 | break;
|
---|
1164 | }
|
---|
1165 | if (length == 0) {
|
---|
1166 | fprintf(stderr, "ERROR: Main at stage Sheet - could not find %s in %s\n", dummy, SheetFilename);
|
---|
1167 | exit(255);
|
---|
1168 | }
|
---|
1169 | ptr += strlen(dummy);
|
---|
1170 | sscanf(ptr, "%d", &seed);
|
---|
1171 | fprintf(stdout, "%d\n", seed);
|
---|
1172 |
|
---|
1173 | // ... and randomness
|
---|
1174 | if (seed != 0) { // only parse for values if a seed, i.e. randomness wanted, was specified
|
---|
1175 | sprintf(dummy, "Randomness");
|
---|
1176 | fprintf(stdout, "%s\n", dummy);
|
---|
1177 | while ((length = GetNextline(bufptr, line)) != 0) {
|
---|
1178 | bufptr += (length)*sizeof(char);
|
---|
1179 | if ((ptr = strstr(line, dummy)) != NULL)
|
---|
1180 | break;
|
---|
1181 | }
|
---|
1182 | if (length == 0) {
|
---|
1183 | fprintf(stderr, "ERROR: Main at stage Sheet - could not find %s in %s\n", dummy, SheetFilename);
|
---|
1184 | exit(255);
|
---|
1185 | }
|
---|
1186 | sprintf(dummy, "probability values");
|
---|
1187 | for (i=0;i<numbercell;i++) {
|
---|
1188 | length = GetNextline(bufptr, line);
|
---|
1189 | if (length == 0) {
|
---|
1190 | fprintf(stderr, "ERROR: Main at stage Sheet - could not find %s in %s\n", dummy, SheetFilename);
|
---|
1191 | exit(255);
|
---|
1192 | }
|
---|
1193 | bufptr += (length)*sizeof(char);
|
---|
1194 | sscanf(line, "%d %lg", &j, &dummydouble);
|
---|
1195 | randomness[j] = dummydouble;
|
---|
1196 | fprintf(stdout, "%d %g\n", j, randomness[j]);
|
---|
1197 | }
|
---|
1198 | }
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | //int OrthOrder[NDIM] = { axis[2], axis[0], axis[1] };
|
---|
1202 | //Orthogonalize(Tubevector,OrthOrder);
|
---|
1203 | angle = acos(Projection(Vector[axis[0]], Vector[axis[1]])); // calcs angle between shanks in unit cell
|
---|
1204 | fprintf(stdout, "The basic angle between the two shanks of the unit cell is %lg %lg\n", angle/M_PI*180., Projection(Vector[axis[0]], Vector[axis[1]]));
|
---|
1205 | if ( angle/M_PI*180. > 90 ) {
|
---|
1206 | fprintf(stderr, "There seems to be something wrong with the unit cell! for nanotube the angle should be 60 degrees for example!\n");
|
---|
1207 | return 1;
|
---|
1208 | }
|
---|
1209 | angle = acos(Projection(Tubevector[axis[0]], Tubevector[axis[1]])); // calcs angle between shanks in unit cell
|
---|
1210 | fprintf(stdout, "The basic angle between the two shanks of the tube unit cell is %lg %lg\n", angle/M_PI*180., Projection(Tubevector[axis[0]], Tubevector[axis[1]]));
|
---|
1211 | //angle -= acos(Projection(Vector[axis[0]], Tubevector[axis[0]]));
|
---|
1212 | //angle = 30./180.*M_PI - acos(Projection(Vector[axis[0]], Tubevector[axis[0]]));
|
---|
1213 | //angle = acos(Projection(Tubevector[axis[0]], Vector[axis[0]]));
|
---|
1214 | fprintf(stdout, "The relative alignment rotation angle then is %lg\n", angle/M_PI*180.);
|
---|
1215 | if (fabs(Tubevector[axis[0]][0]) > MYEPSILON)
|
---|
1216 | angle = -M_PI/2. + acos(Tubevector[axis[0]][0]/Norm(Tubevector[axis[0]]));
|
---|
1217 | else
|
---|
1218 | angle = 0.;
|
---|
1219 | fprintf(stdout, "The absolute alignment rotation angle then is %lg %lg\n", angle/M_PI*180., Tubevector[axis[0]][0]/Norm(Tubevector[axis[0]]));
|
---|
1220 | fprintf(stdout, "\nThe chiral angle then is %5.5f degrees with tube radius %5.5f A and length %5.5f A, i.e. final torus radius of %5.5f A.\n",
|
---|
1221 | acos(Projection(Vector[axis[0]], Tubevector[axis[0]]))/M_PI*180.,
|
---|
1222 | (double)factors[0]*Norm(Tubevector[axis[0]])/(2.*M_PI),
|
---|
1223 | (double)factors[1]*Norm(Tubevector[axis[1]]),
|
---|
1224 | (double)factors[1]*Norm(Tubevector[axis[1]])/(2.*M_PI)
|
---|
1225 | );
|
---|
1226 | Orthogonalize(Tubevector, axis); // with correct translational vector, not needed anymore (? what's been done here. Hence, re-inserted)
|
---|
1227 | fprintf(stdout, "Tubevector magnitudes: %5.5lg %5.5lg %5.5lg\n", Norm(Tubevector[0]), Norm(Tubevector[1]), Norm(Tubevector[2]));
|
---|
1228 | fprintf(stdout, "Tubevectors are \n");
|
---|
1229 | PrintMatrix(stdout, Tubevector);
|
---|
1230 | MatrixInversion(Tubevector, TubevectorInverse);
|
---|
1231 | //Transpose(TubevectorInverse);
|
---|
1232 | fprintf(stdout, "Vector\n");
|
---|
1233 | PrintMatrix(stdout, Vector);
|
---|
1234 | fprintf(stdout, "TubevectorInverse\n");
|
---|
1235 | PrintMatrix(stdout, TubevectorInverse);
|
---|
1236 | for (i=0;i<NDIM;i++) {
|
---|
1237 | fprintf(stdout, "Vector %d in TubeVectorInverse vectors:\t", axis[i]);
|
---|
1238 | tempvector = MatrixTrafoInverse(Vector[axis[i]], TubevectorInverse);
|
---|
1239 | PrintVector(stdout, tempvector);
|
---|
1240 | Free(tempvector, "Main:tempvector");
|
---|
1241 | }
|
---|
1242 | fprintf(stdout, "Reciprocal Tubebvectors are \n");
|
---|
1243 | PrintMatrix(stdout, TubevectorInverse);
|
---|
1244 | fprintf(stdout, "Tubevector magnitudes: %5.5lg %5.5lg %5.5lg\n", Norm(Tubevector[0]), Norm(Tubevector[1]), Norm(Tubevector[2]));
|
---|
1245 |
|
---|
1246 | biggestdiameter = DetermineBiggestDiameter(Tubevector, axis, factors);
|
---|
1247 | for (i=0;i<NDIM;i++) {
|
---|
1248 | sheetnr[i] = 0;
|
---|
1249 | }
|
---|
1250 | for (i=0;i<NDIM;i++) {
|
---|
1251 | for (j=0;j<NDIM;j++) {
|
---|
1252 | // sheetnr[j] = ceil(biggestdiameter/Norm(Vector[j]));
|
---|
1253 | if (fabs(Vector[i][j]) > MYEPSILON) {
|
---|
1254 | tmp = ceil(biggestdiameter/fabs(Vector[i][j]));
|
---|
1255 | } else {
|
---|
1256 | tmp = 0;
|
---|
1257 | }
|
---|
1258 | sheetnr[j] = sheetnr[j] > tmp ? sheetnr[j] : tmp;
|
---|
1259 | }
|
---|
1260 | }
|
---|
1261 | fprintf(stdout, "Maximum indices to regard: %d %d %d\n", sheetnr[0], sheetnr[1], sheetnr[2]);
|
---|
1262 | for (i=0;i<NDIM;i++) {
|
---|
1263 | fprintf(stdout, "For axis %d: (%5.5lg\t%5.5lg\t%5.5lg) with %5.5lg\n", i, (Vector[i][0]*sheetnr[i]), (Vector[i][1]*sheetnr[i]), (Vector[i][2]*sheetnr[i]), Norm(Vector[i]));
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 | //if (!strncmp(stage, "Cell", 4)) {
|
---|
1267 | // parse in atoms for quicker processing
|
---|
1268 | struct Atoms *atombuffer = malloc(sizeof(struct Atoms)*numbercell);
|
---|
1269 | bufptr = CellBuffer;
|
---|
1270 | bufptr += GetNextline(bufptr, line)*sizeof(char);
|
---|
1271 | bufptr += GetNextline(bufptr, line)*sizeof(char);
|
---|
1272 | for (i=0;i<numbercell;i++) {
|
---|
1273 | if ((length = GetNextline(bufptr, line)) != 0) {
|
---|
1274 | bufptr += length*sizeof(char);
|
---|
1275 | sscanf(line, "%s %lg %lg %lg", atombuffer[i].name, &(atombuffer[i].x[0]), &(atombuffer[i].x[1]), &(atombuffer[i].x[2]));
|
---|
1276 | fprintf(stdout, "Read Atombuffer Nr %i: %s %5.5lg %5.5lg %5.5lg\n", i+1, atombuffer[i].name, atombuffer[i].x[0], atombuffer[i].x[1], atombuffer[i].x[2]);
|
---|
1277 | } else {
|
---|
1278 | fprintf(stdout, "Error reading Atom Nr. %i\n", i+1);
|
---|
1279 | break;
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 | SheetFile = fopen(SheetFilename, "w");
|
---|
1283 | if (SheetFile == NULL) {
|
---|
1284 | fprintf(stderr, "ERROR: main - can't open %s for writing\n", SheetFilename);
|
---|
1285 | exit(255);
|
---|
1286 | }
|
---|
1287 | SheetFileAligned = fopen(SheetFilenameAligned, "w");
|
---|
1288 | if (SheetFile == NULL) {
|
---|
1289 | fprintf(stderr, "ERROR: main - can't open %s for writing\n", SheetFilenameAligned);
|
---|
1290 | exit(255);
|
---|
1291 | }
|
---|
1292 | // Now create the sheet
|
---|
1293 | double index[NDIM];
|
---|
1294 | int nr;//, nummer = 0;
|
---|
1295 | numbersheet = 0;
|
---|
1296 | index[axis[2]] = 0;
|
---|
1297 | // initialise pseudo random number generator with given seed
|
---|
1298 | fprintf(stdout, "Initialising pseudo random number generator with given seed %d.\n", seed);
|
---|
1299 | srand(seed);
|
---|
1300 | //for (index[axis[0]] = 0; index[axis[0]] <= sheetnr[axis[0]]; index[axis[0]]++) { // NOTE: minor axis may start from 0! Check on this later ...
|
---|
1301 | for (index[axis[0]] = -sheetnr[axis[0]]+1; index[axis[0]] < sheetnr[axis[0]]; index[axis[0]]++) { // NOTE: minor axis may start from 0! Check on this later ...
|
---|
1302 | //for (index[axis[1]] = 0; index[axis[1]] <= sheetnr[axis[1]]; index[axis[1]]++) { // These are all the cells that need be checked on
|
---|
1303 | for (index[axis[1]] = -sheetnr[axis[1]]+1; index[axis[1]] < sheetnr[axis[1]]; index[axis[1]]++) { // These are all the cells that need be checked on
|
---|
1304 | // Calculate offset in cartesian coordinates
|
---|
1305 | offset = MatrixTrafo(Vector, index);
|
---|
1306 |
|
---|
1307 | //fprintf(stdout, "Now dealing with numbercell atoms in unit cell at R = (%lg,%lg,%lg)\n", offset[0], offset[1], offset[2]);
|
---|
1308 | for (nr = 0; nr < numbercell; nr++) {
|
---|
1309 | percentage = rand()/(RAND_MAX+1.0);
|
---|
1310 | //fprintf(stdout, "Lucky number for %d is %lg >? %lg\n", nr, percentage, randomness[nr]);
|
---|
1311 | if (percentage >= randomness[nr]) {
|
---|
1312 | // Create coordinates at atom site
|
---|
1313 | coord = VectorAdd(atombuffer[nr].x, offset);
|
---|
1314 | //fprintf(stdout, "Atom Nr. %i: ", (numbersheet+1));
|
---|
1315 | //PrintVector(stdout, coord);
|
---|
1316 | // project down on major and minor Tubevectors and check for length if out of sheet
|
---|
1317 | tempvector = MatrixTrafoInverse(coord, TubevectorInverse);
|
---|
1318 | if (((tempvector[axis[0]] + MYEPSILON) > 0) && ((factors[0] - tempvector[axis[0]]) > MYEPSILON) &&
|
---|
1319 | ((tempvector[axis[1]] + MYEPSILON) > 0) && ((factors[1] - tempvector[axis[1]]) > MYEPSILON) &&
|
---|
1320 | ((tempvector[axis[2]] + MYEPSILON) > 0) && ((factors[2] - tempvector[axis[2]]) > MYEPSILON)) { // check if within rotated cell numbersheet++;
|
---|
1321 | //if (nummer >= 2) strcpy(atombuffer[nr].name, "O");
|
---|
1322 | //nummer++;
|
---|
1323 | fprintf(SheetFile, "%s\t%5.5lg\t%5.5lg\t%5.5lg\n", atombuffer[nr].name, coord[0], coord[1], coord[2]);
|
---|
1324 | // rotate to align the sheet in xy plane
|
---|
1325 | x1 = coord[0]*cos(-angle) + coord[1] * sin(-angle);
|
---|
1326 | x2 = coord[0]*(-sin(-angle)) + coord[1] * cos(-angle);
|
---|
1327 | x3 = coord[2];
|
---|
1328 | fprintf(SheetFileAligned, "%s\t%5.5lg\t%5.5lg\t%5.5lg\n", atombuffer[nr].name, x1, x2, x3);
|
---|
1329 | //fprintf(SheetFile, "O\t%5.5lg\t%5.5lg\t%5.5lg\n", coord[0], coord[1], coord[2]);
|
---|
1330 | //fprintf(stdout, "%s/%d\t(%lg\t%lg\t%lg)\t", atombuffer[nr].name, numbersheet+1, coord[0], coord[1], coord[2]);
|
---|
1331 | //PrintVector(stdout, tempvector);
|
---|
1332 | numbersheet++;
|
---|
1333 | //fprintf(stdout, "%i,", nr);
|
---|
1334 | } //else {
|
---|
1335 | //numbersheet++;
|
---|
1336 | //fprintf(SheetFile, "B\t%lg\t%lg\t%lg\n", coord[0], coord[1], coord[2]);
|
---|
1337 | //fprintf(stdout, "O \t(%lg\t%lg\t%lg)\n", coord[0], coord[1], coord[2]);
|
---|
1338 | //fprintf(stdout, "!!%i!!, ", nr);
|
---|
1339 | //}
|
---|
1340 | Free(tempvector, "Main: At stage Sheet - tempvector");
|
---|
1341 | Free(coord, "Main: At stage Sheet - coord");
|
---|
1342 | }
|
---|
1343 | }
|
---|
1344 | Free(offset, "Main: At stage Sheet - offset");
|
---|
1345 | }
|
---|
1346 | //fprintf(stdout, "\n";
|
---|
1347 | }
|
---|
1348 |
|
---|
1349 | fclose(SheetFile);
|
---|
1350 | fclose(SheetFileAligned);
|
---|
1351 | AddAtomicNumber(SheetFilename,numbersheet, Vector, Recivector); // prepend atomic number and comment
|
---|
1352 | AddAtomicNumber(SheetFilenameAligned,numbersheet, Vector, Recivector); // prepend atomic number and comment
|
---|
1353 | AddSheetInfo(SheetFilename,axis,chiral, factors, seed, numbercell, randomness);
|
---|
1354 | fprintf(stdout, "\nThere are %i atoms in the created sheet.\n", numbersheet);
|
---|
1355 |
|
---|
1356 | strncpy(stage, "Sheet", 5);
|
---|
1357 | //}
|
---|
1358 | SheetBuffer = ReadBuffer(SheetFilename, &length);
|
---|
1359 |
|
---|
1360 |
|
---|
1361 | // ======================== STAGE: Tube ==============================
|
---|
1362 | // The tube starts with the rectangular (due to the orthogonalization) sheet
|
---|
1363 | // just created (or read). Along the minor axis it is rolled up, i.e. projected
|
---|
1364 | // from a 2d surface onto a cylindrical surface (x,y,z <-> r,alpha,z). The only
|
---|
1365 | // thing that's a bit complex is that the sheet it not aligned along the cartesian
|
---|
1366 | // axis but along major and minor. That's why we have to transform the atomic
|
---|
1367 | // cartesian coordinates into the orthogonal tubevector base, do the rolling up
|
---|
1368 | // there (and regard that minor and major axis must not necessarily be of equal
|
---|
1369 | // length) and afterwards transform back again (where we need the $halfaxis due to
|
---|
1370 | // the above possible inequality).
|
---|
1371 |
|
---|
1372 | FILE *TubeFile = NULL;
|
---|
1373 | FILE *TubeFileAligned = NULL;
|
---|
1374 |
|
---|
1375 | Debug ("STAGE: Tube\n");
|
---|
1376 | if (!strncmp(stage, "Sheet", 4)) {
|
---|
1377 | TubeFile = fopen(TubeFilename, "w");
|
---|
1378 | if (TubeFile == NULL) {
|
---|
1379 | fprintf(stderr, "ERROR: Main - can't open %s for writing\n", TubeFilename);
|
---|
1380 | exit(255);
|
---|
1381 | }
|
---|
1382 | TubeFileAligned = fopen(TubeFilenameAligned, "w");
|
---|
1383 | if (TubeFile == NULL) {
|
---|
1384 | fprintf(stderr, "ERROR: Main - can't open %s for writing\n", TubeFilenameAligned);
|
---|
1385 | exit(255);
|
---|
1386 | }
|
---|
1387 | bufptr = SheetBuffer;
|
---|
1388 | bufptr += GetNextline(bufptr, line); // write numbers to file
|
---|
1389 | bufptr += GetNextline(bufptr, line); // write comment to file
|
---|
1390 |
|
---|
1391 | //cog = CenterOfGravity(bufptr, numbersheet);
|
---|
1392 | //cog_projected = MatrixTrafoInverse(cog, TubevectorInverse);
|
---|
1393 | //fprintf(stdout, "\nCenter of Gravity at (%5.5lg\t%5.5lg\t%5.5lg) and projected at (%5.5lg\t%5.5lg\t%5.5lg)\n", cog[0], cog[1], cog[2], cog_projected[0], cog_projected[1], cog_projected[2]);
|
---|
1394 |
|
---|
1395 | // restart
|
---|
1396 | bufptr = SheetBuffer;
|
---|
1397 | bufptr += GetNextline(bufptr, line); // write numbers to file
|
---|
1398 | bufptr += GetNextline(bufptr, line); // write numbers to file
|
---|
1399 |
|
---|
1400 | // determine half axis as tube vector not necessarily have the same length
|
---|
1401 | double halfaxis[NDIM];
|
---|
1402 | for (i=0;i<NDIM;i++)
|
---|
1403 | halfaxis[i] = factors[0]*Norm(Tubevector[axis[0]])/Norm(Tubevector[i]);
|
---|
1404 |
|
---|
1405 | double arg, radius;
|
---|
1406 | for (i=0;i<numbersheet;i++) {
|
---|
1407 | // scan next atom
|
---|
1408 | bufptr += GetNextline(bufptr, line);
|
---|
1409 | sscanf(line, "%s %lg %lg %lg", name, &atom[0], &atom[1], &atom[2]);
|
---|
1410 |
|
---|
1411 | // transform atom coordinates in cartesian system to the axis eigensystem
|
---|
1412 | x = MatrixTrafoInverse(atom, TubevectorInverse);
|
---|
1413 | //x = VectorAdd(y, cog_projected);
|
---|
1414 | //free(y);
|
---|
1415 |
|
---|
1416 | // roll up (project (x,y,z) on cylindrical coordinates (radius,arg,z))
|
---|
1417 | arg = 2.*M_PI*x[axis[0]]/(factors[0]) - M_PI; // is angle
|
---|
1418 | radius = 1./(2.*M_PI); // is length of sheet in units of axis vector, divide by pi to get radius (from circumference)
|
---|
1419 | // fprintf(stdout, "arg: %5.2f (c%2.2f,s%2.2f)\t",$arg, cos($arg), sin($arg));
|
---|
1420 | x[axis[0]] = cos(arg)*halfaxis[axis[0]]*(radius+x[axis[2]]/halfaxis[axis[2]]); // as both vectors are not normalized additional betrag has to be taken into account!
|
---|
1421 | x[axis[2]] = sin(arg)*halfaxis[axis[2]]*(radius+x[axis[2]]/halfaxis[axis[2]]); // due to the back-transformation from eigensystem to cartesian one
|
---|
1422 | //fprintf(stdout, "rotated: (%5.2f,%5.2f,%5.2f)\n",x[0],x[1],x[2]);
|
---|
1423 | atom_transformed = MatrixTrafo(Tubevector, x);
|
---|
1424 | fprintf(TubeFile, "%s\t%lg\t%lg\t%lg\n", name, atom_transformed[0], atom_transformed[1], atom_transformed[2]);
|
---|
1425 | // rotate and flip to align tube in z-direction
|
---|
1426 | x1 = atom_transformed[0]*cos(-angle) + atom_transformed[1] * sin(-angle);
|
---|
1427 | x2 = atom_transformed[0]*(-sin(-angle)) + atom_transformed[1] * cos(-angle);
|
---|
1428 | x3 = atom_transformed[2];
|
---|
1429 | fprintf(TubeFileAligned, "%s\t%lg\t%lg\t%lg\n", name, x3, x2, x1); // order so that symmetry is along z axis
|
---|
1430 | //fprintf(stdout, "%s\t%5.5lg\t%5.5lg\t%5.5lg\n", name, atom_transformed[0], atom_transformed[1] ,atom_transformed[2]);
|
---|
1431 |
|
---|
1432 | Free(x, "Main: at stage Tube - x");
|
---|
1433 | Free(atom_transformed, "Main: at stage Tube - atom_transformed");
|
---|
1434 | }
|
---|
1435 |
|
---|
1436 |
|
---|
1437 | fclose(TubeFile);
|
---|
1438 | fclose(TubeFileAligned);
|
---|
1439 | //free(cog);
|
---|
1440 | //free(cog_projected);
|
---|
1441 | AddAtomicNumber(TubeFilename,numbersheet, Vector, Recivector); // prepend atomic number and comment
|
---|
1442 | AddAtomicNumber(TubeFilenameAligned,numbersheet, Vector, Recivector); // prepend atomic number and comment
|
---|
1443 | AddSheetInfo(TubeFilename,axis,chiral, factors, seed, numbercell, randomness);
|
---|
1444 | fprintf(stdout, "\nThere are %i atoms in the created tube.\n", numbersheet);
|
---|
1445 |
|
---|
1446 | strncpy(stage, "Tube", 4);
|
---|
1447 | } else {
|
---|
1448 | }
|
---|
1449 |
|
---|
1450 | TubeBuffer = ReadBuffer(TubeFilename, &length);
|
---|
1451 |
|
---|
1452 | // ======================== STAGE: Torus =============================
|
---|
1453 | // The procedure for the torus is very much alike to the one used to make the
|
---|
1454 | // tube. Only the projection is not from 2d surface onto a cylindrical one but
|
---|
1455 | // from a cylindrial onto a torus surface
|
---|
1456 | // (x,y,z) <-> (cos(s)*(R+r*cos(t)), sin(s)*(R+rcos(t)), r*sin(t)).
|
---|
1457 | // Here t is the angle within the tube with radius r, s is the torus angle with
|
---|
1458 | // radius R. We get R from the tubelength (that's why we need lengthfactor to
|
---|
1459 | // make it long enough). And due to fact that we have it already upon a cylindrical
|
---|
1460 | // surface, r*cos(t) and r*sin(t) already reside in $minoraxis and $noaxis.
|
---|
1461 |
|
---|
1462 | FILE *TorusFile;
|
---|
1463 |
|
---|
1464 | Debug ("STAGE: Torus\n");
|
---|
1465 | if (!strncmp(stage, "Tube", 4)) {
|
---|
1466 | TorusFile = fopen(TorusFilename, "w");
|
---|
1467 | if (TorusFile == NULL) {
|
---|
1468 | fprintf(stderr, "ERROR: main - can't open %s for writing\n", TorusFilename);
|
---|
1469 | exit(255);
|
---|
1470 | }
|
---|
1471 | bufptr = TubeBuffer;
|
---|
1472 | bufptr += GetNextline(bufptr, line); // write numbers to file
|
---|
1473 | bufptr += GetNextline(bufptr, line); // write comment to file
|
---|
1474 |
|
---|
1475 | //cog = CenterOfGravity(bufptr, numbersheet);
|
---|
1476 | //cog_projected = MatrixTrafoInverse(cog, TubevectorInverse);
|
---|
1477 | //fprintf(stdout, "\nCenter of Gravity at (%5.5lg\t%5.5lg\t%5.5lg) and projected at (%5.5lg\t%5.5lg\t%5.5lg)\n", cog[0], cog[1], cog[2], cog_projected[0], cog_projected[1], cog_projected[2]);
|
---|
1478 |
|
---|
1479 | // determine half axis as tube vectors not necessarily have same length
|
---|
1480 | double halfaxis[NDIM];
|
---|
1481 | for (i=0;i<NDIM;i++)
|
---|
1482 | halfaxis[i] = Norm(Tubevector[axis[1]])/Norm(Tubevector[i]);
|
---|
1483 |
|
---|
1484 | double arg, radius;
|
---|
1485 | for (i=0;i<numbersheet;i++) {
|
---|
1486 | // scan next atom
|
---|
1487 | bufptr += GetNextline(bufptr, line);
|
---|
1488 | sscanf(line, "%s %lg %lg %lg", name, &atom[0], &atom[1], &atom[2]);
|
---|
1489 |
|
---|
1490 | // transform atom coordinates in cartesian system to the axis eigensystem
|
---|
1491 | x = MatrixTrafoInverse(atom, TubevectorInverse);
|
---|
1492 | //x = VectorAdd(y, cog_projected);
|
---|
1493 | //free(y);
|
---|
1494 |
|
---|
1495 | // roll up (project (x,y,z) on cylindrical coordinates (radius,arg,z))
|
---|
1496 | arg = 2.*M_PI*x[axis[1]]/(factors[1]) - M_PI; // is angle
|
---|
1497 | radius = (factors[1])/(2.*M_PI) + x[axis[0]]/halfaxis[axis[0]]; // is length of sheet in units of axis vector, divide by pi to get radius (from circumference)
|
---|
1498 | // fprintf(stdout, "arg: %5.2f (c%2.2f,s%2.2f)\t",$arg, cos($arg), sin($arg));
|
---|
1499 | x[axis[0]] = cos(arg)*halfaxis[axis[0]]*radius; // as both vectors are not normalized additional betrag has to be taken into account!
|
---|
1500 | x[axis[1]] = sin(arg)*halfaxis[axis[1]]*radius; // due to the back-transformation from eigensystem to cartesian one
|
---|
1501 | //fprintf(stdout, "rotated: (%5.2f,%5.2f,%5.2f)\n",x[0],x[1],x[2]);
|
---|
1502 | atom_transformed = MatrixTrafo(Tubevector, x);
|
---|
1503 | fprintf(TorusFile, "%s\t%lg\t%lg\t%lg\n", name, atom_transformed[0], atom_transformed[1] ,atom_transformed[2]);
|
---|
1504 | //fprintf(stdout, "%s\t%5.5lg\t%5.5lg\t%5.5lg\n", name, atom_transformed[0], atom_transformed[1] ,atom_transformed[2]);
|
---|
1505 |
|
---|
1506 | Free(x, "Main: at stage Torus - x");
|
---|
1507 | Free(atom_transformed, "Main: at stage Torus - atom_transformed");
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | fclose(TorusFile);
|
---|
1511 | //free(cog);
|
---|
1512 | //free(cog_projected);
|
---|
1513 | AddAtomicNumber(TorusFilename,numbersheet, Vector, Recivector); // prepend atomic number and comment
|
---|
1514 | AddSheetInfo(TorusFilename,axis,chiral, factors, seed, numbercell, randomness);
|
---|
1515 | fprintf(stdout, "\nThere are %i atoms in the created torus.\n", numbersheet);
|
---|
1516 |
|
---|
1517 | strncpy(stage, "Torus", 5);
|
---|
1518 | } else {
|
---|
1519 | }
|
---|
1520 |
|
---|
1521 | // Free memory
|
---|
1522 | for (i=0; i<NDIM; i++ ) {
|
---|
1523 | Free(Vector[i], "Main: end of stages - *Vector");
|
---|
1524 | Free(Recivector[i], "Main: end of stages - *Recivector");
|
---|
1525 | Free(Tubevector[i], "Main: end of stages - *Tubevector");
|
---|
1526 | Free(TubevectorInverse[i], "Main: end of stages - *TubevectorInverse");
|
---|
1527 | }
|
---|
1528 | Free(atom, "Main: end of stages - atom");
|
---|
1529 | Free(Vector, "Main: end of stages - Vector");
|
---|
1530 | Free(Recivector, "Main: end of stages - Recivector");
|
---|
1531 | Free(Tubevector, "Main: end of stages - Tubevector");
|
---|
1532 | Free(TubevectorInverse, "Main: end of stages - TubevectorInverse");
|
---|
1533 | Free(randomness, "Main: at stage Sheet - randomness");
|
---|
1534 |
|
---|
1535 | if (CellBuffer != NULL) Free(CellBuffer, "Main: end of stages - CellBuffer");
|
---|
1536 | if (SheetBuffer != NULL) Free(SheetBuffer, "Main: end of stages - SheetBuffer");
|
---|
1537 | if (TubeBuffer != NULL) Free(TubeBuffer, "Main: end of stages - TubeBuffer");
|
---|
1538 |
|
---|
1539 | Free(CellFilename, "Main: end of stafes - CellFilename");
|
---|
1540 | Free(SheetFilename, "Main: end of stafes - CellFilename");
|
---|
1541 | Free(TubeFilename, "Main: end of stafes - CellFilename");
|
---|
1542 | Free(TorusFilename, "Main: end of stafes - CellFilename");
|
---|
1543 | Free(SheetFilenameAligned, "Main: end of stafes - CellFilename");
|
---|
1544 | Free(TubeFilenameAligned, "Main: end of stafes - CellFilename");
|
---|
1545 |
|
---|
1546 | // exit
|
---|
1547 | exit(0);
|
---|
1548 | }
|
---|