atomes 1.1.14
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_xyz.c
Go to the documentation of this file.
1/* This file is part of the 'atomes' software
2
3'atomes' is free software: you can redistribute it and/or modify it under the terms
4of the GNU Affero General Public License as published by the Free Software Foundation,
5either version 3 of the License, or (at your option) any later version.
6
7'atomes' is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
8without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9See the GNU General Public License for more details.
10
11You should have received a copy of the GNU Affero General Public License along with 'atomes'.
12If not, see <https://www.gnu.org/licenses/>
13
14Copyright (C) 2022-2024 by CNRS and University of Strasbourg */
15
22/*
23* This file: 'read_xyz.c'
24*
25* Contains:
26*
27
28 - The functions to read XYZ atomic coordinates
29
30*
31* List of functions:
32
33 int xyz_get_atom_coordinates ();
34 int open_xyz_file (int linec);
35
36*/
37
38#include "global.h"
39#include "glview.h"
40#include "callbacks.h"
41#include "interface.h"
42#include "project.h"
43#include "bind.h"
44#include <omp.h>
45#include "readers.h"
46
53{
54 int i, j, k;
55 double v;
56 gchar * lia[4] = {"a", "b", "c", "d"};
57 this_reader -> nspec = 0;
58 active_project -> steps = this_reader -> steps;
59 active_project -> natomes = this_reader -> natomes;
61 this_reader -> z = allocdouble (1);
62 this_reader -> nsps = allocint (1);
63#ifdef OPENMP
64 int v_dummy;
65 int res;
66 int numth = omp_get_max_threads ();
67 gboolean doatoms = FALSE;
68 gchar * saved_line;
69 if (this_reader -> steps < numth)
70 {
71 if (numth >= 2*(this_reader -> steps-1))
72 {
73 doatoms = TRUE;
74 }
75 else
76 {
77 numth = this_reader -> steps;
78 }
79 }
80
81 if (doatoms)
82 {
83 // OpenMP on atoms
84 res = 0;
85 for (i=0; i<this_reader -> steps; i++)
86 {
87 k = i*(this_reader -> natomes + 2) + 2;
88 #pragma omp parallel for num_threads(numth) private(j,v,v_dummy,this_line,saved_line,this_word) shared(i,k,lia,coord_line,this_reader,active_project,res)
89 for (j=0; j<this_reader -> natomes; j++)
90 {
91 if (res == 2) goto enda;
92 this_line = g_strdup_printf ("%s", coord_line[k+j]);
93 saved_line = g_strdup_printf ("%s", this_line);
94 this_word = strtok_r (this_line, " ", & saved_line);
95 if (! this_word)
96 {
97 format_error (i+1, j+1, lia[0], k+j);
98 res = 2;
99 goto enda;
100 }
102 v_dummy = 0;
103 if (! v)
104 {
105 #pragma omp critical
106 v_dummy = set_v_dummy (this_word);
107 }
108 if (v || v_dummy)
109 {
110 if (! i)
111 {
112 v = v + v_dummy * 0.1;
113 #pragma omp critical
114 check_for_species (v, j);
115 }
116 this_word = strtok_r (NULL, " ", & saved_line);
117 if (! this_word)
118 {
119 format_error (i+1, j+1, lia[1], k+j);
120 res = 2;
121 goto enda;
122 }
123 active_project -> atoms[i][j].x = atof(this_word);
124 this_word = strtok_r (NULL, " ", & saved_line);
125 if (! this_word)
126 {
127 format_error (i+1, j+1, lia[2], k+j);
128 res = 2;
129 goto enda;
130 }
131 active_project -> atoms[i][j].y = atof(this_word);
132 this_word = strtok_r (NULL, " ", & saved_line);
133 if (! this_word)
134 {
135 format_error (i+1, j+1, lia[3], k+j);
136 res = 2;
137 goto enda;
138 }
139 active_project -> atoms[i][j].z = atof(this_word);
140 }
141 else
142 {
143 format_error (i+1, j+1, lia[0], k+j);
144 res = 2;
145 goto enda;
146 }
147 g_free (this_line);
148 enda:;
149 }
150 if (res == 2) break;
151 }
152 }
153 else
154 {
155 res = 0;
156 #pragma omp parallel for num_threads(numth) private(i,j,k,v,v_dummy,this_line,saved_line,this_word) shared(lia,coord_line,this_reader,active_project,res)
157 for (i=0; i<this_reader -> steps; i++)
158 {
159 if (res == 2) goto ends;
160 k = i*(this_reader -> natomes + 2) + 2;
161 for (j=0; j<this_reader -> natomes; j++)
162 {
163 this_line = g_strdup_printf ("%s", coord_line[k+j]);
164 saved_line = g_strdup_printf ("%s", this_line);
165 this_word = strtok_r (this_line, " ", & saved_line);
166 if (! this_word)
167 {
168 format_error (i+1, j+1, lia[0], k+j);
169 res = 2;
170 goto ends;
171 }
173 v_dummy = 0;
174 if (! v)
175 {
176 #pragma omp critical
177 v_dummy = set_v_dummy (this_word);
178 }
179 if (v || v_dummy)
180 {
181 if (! i)
182 {
183 v = v + v_dummy * 0.1;
184 check_for_species (v, j);
185 }
186 this_word = strtok_r (NULL, " ", & saved_line);
187 if (! this_word)
188 {
189 format_error (i+1, j+1, lia[1], k+j);
190 res = 2;
191 goto ends;
192 }
193 active_project -> atoms[i][j].x = atof(this_word);
194 this_word = strtok_r (NULL, " ", & saved_line);
195 if (! this_word)
196 {
197 format_error (i+1, j+1, lia[1], k+j);
198 res = 2;
199 goto ends;
200 }
201 active_project -> atoms[i][j].y = atof(this_word);
202 this_word = strtok_r (NULL, " ", & saved_line);
203 if (! this_word)
204 {
205 format_error (i+1, j+1, lia[2], k+j);
206 res = 2;
207 goto ends;
208 }
209 active_project -> atoms[i][j].z = atof(this_word);
210 }
211 else
212 {
213 format_error (i+1, j+1, lia[3], k+j);
214 res = 2;
215 goto ends;
216 }
217 g_free (this_line);
218 }
219 ends:;
220 }
221 }
222 g_free (coord_line);
223 if (res == 2) return 2;
224#else
225 line_node * tmp_line;
226 tail = head;
227 k = 0;
228 for (i=0; i<active_project -> steps; i++)
229 {
230 for (j=0; j<2; j++)
231 {
232 tmp_line = tail;
233 tail = tail -> next;
234 g_free (tmp_line);
235 k ++;
236 }
237 for (j=0; j<active_project -> natomes; j++)
238 {
239 this_line = g_strdup_printf ("%s", tail -> line);
240 this_word = strtok (this_line, " ");
241 if (! this_word)
242 {
243 format_error (i+1, j+1, lia[0], k+j);
244 return 2;
245 }
247 if (v)
248 {
249 if (! i) check_for_species (v, j);
250 this_word = strtok (NULL, " ");
251 if (! this_word)
252 {
253 format_error (i+1, j+1, lia[1], k+j);
254 return 2;
255 }
256 active_project -> atoms[i][j].x = atof(this_word);
257 this_word = strtok (NULL, " ");
258 if (! this_word)
259 {
260 format_error (i+1, j+1, lia[2], k+j);
261 return 2;
262 }
263 active_project -> atoms[i][j].y = atof(this_word);
264 this_word = strtok (NULL, " ");
265 if (! this_word)
266 {
267 format_error (i+1, j+1, lia[3], k+j);
268 return 2;
269 }
270 active_project -> atoms[i][j].z = atof(this_word);
271 }
272 else
273 {
274 format_error (i+1, j+1, lia[0], k+j);
275 return 2;
276 }
277 tmp_line = tail;
278 tail = tail -> next;
279 g_free (tmp_line);
280 k ++;
281 }
282 }
283#endif
284 for (i=1; i<active_project -> steps; i++)
285 {
286 for (j=0; j<active_project -> natomes; j++)
287 {
288 active_project -> atoms[i][j].sp = active_project -> atoms[0][j].sp;
289 }
290 }
291 return 0;
292}
293
301int open_xyz_file (int linec)
302{
303 int res;
304#ifdef OPENMP
305 this_line = g_strdup_printf ("%s", coord_line[0]);
306 this_word = strtok (this_line, " ");
307 if (! this_word)
308 {
309 add_reader_info ("Wrong file format - cannot find the number of atoms !", 0);
310 add_reader_info ("Wrong file format - first line is corrupted !", 0);
311 res = 2;
312 goto end;
313 }
314 this_reader -> natomes = (int)atof(this_word);
315 reader_info ("xyz", "Number of atoms", this_reader -> natomes);
316 g_free (this_line);
317 if (linec%(this_reader -> natomes + 2) != 0)
318 {
319 res = 2;
320 }
321 else
322 {
323 this_reader -> steps = linec / (this_reader -> natomes + 2);
324 reader_info ("xyz", "Number of steps", this_reader -> steps);
326 }
327#else
328 this_line = g_strdup_printf ("%s", head -> line);
329 this_word = strtok (this_line, " ");
330 if (! this_word)
331 {
332 add_reader_info ("Wrong file format - cannot find the number of atoms !", 0);
333 add_reader_info ("Wrong file format - the first line is corrupted !", 0);
334 res = 2;
335 goto end;
336 }
337 this_reader -> natomes = (int)atof(this_word);
338 reader_info ("xyz", "Number of atoms", this_reader -> natomes);
339 g_free (this_line);
340 if (linec%(this_reader -> natomes + 2) != 0)
341 {
342 res = 2;
343 }
344 else
345 {
346 this_reader -> steps = linec / (this_reader -> natomes + 2);
347 reader_info ("xyz", "Number of steps", this_reader -> steps);
349 }
350#endif
351 end:
352 return res;
353}
Binding to the Fortran90 subroutines.
double get_z_from_periodic_table(gchar *lab)
get Z from atom label
Definition w_library.c:304
Callback declarations for main window.
void allocatoms(project *this_proj)
allocate project data
Definition open_p.c:160
int atoms[NUM_STYLES][2]
double * allocdouble(int val)
allocate a double * pointer
Definition global.c:471
int * allocint(int val)
allocate an int * pointer
Definition global.c:326
Global variable declarations Global convenience function declarations Global data structure defin...
project * active_project
Definition project.c:47
Variable declarations related to the OpenGL window Function declarations related to the OpenGL wind...
Messaging function declarations.
double z
Definition ogl_draw.c:57
Function declarations for reading atomes project file Function declarations for saving atomes proje...
void check_for_species(double v, int ato)
Fill the species for each atom and the associated data.
Definition read_coord.c:194
line_node * head
Definition read_coord.c:73
gchar ** coord_line
Definition read_coord.c:70
coord_file * this_reader
Definition read_coord.c:69
void reader_info(gchar *type, gchar *sinf, int val)
display reader information
Definition read_coord.c:99
line_node * tail
Definition read_coord.c:74
void add_reader_info(gchar *info, int mid)
append information message to the reader information
Definition read_coord.c:84
char * this_word
Definition read_coord.c:72
void format_error(int stp, int ato, gchar *mot, int line)
Message to display an error message.
Definition read_coord.c:114
gchar * this_line
Definition read_coord.c:71
int set_v_dummy(gchar *this_word)
check if dummy is used for unknown species, if not then ask what to do
Definition read_coord.c:140
int xyz_get_atom_coordinates()
get the atomic coordinates from the XYZ file
Definition read_xyz.c:52
int open_xyz_file(int linec)
open XYZ file
Definition read_xyz.c:301
Functions declaration to read atomic coordinates.
GtkWidget * res[2]
Definition w_encode.c:212