atomes 1.2.1
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-2025 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 "readers.h"
45#ifdef OPENMP
46# include <omp.h>
47#endif
48
55{
56 int i, j, k;
57 double v;
58 gchar * lia[4] = {"a", "b", "c", "d"};
59 this_reader -> nspec = 0;
60 active_project -> steps = this_reader -> steps;
61 active_project -> natomes = this_reader -> natomes;
63 this_reader -> z = allocdouble (1);
64 this_reader -> nsps = allocint (1);
65#ifdef OPENMP
66 int v_dummy;
67 int res;
68 int numth = omp_get_max_threads ();
69 gboolean doatoms = FALSE;
70 gchar * saved_line;
71 if (this_reader -> steps < numth)
72 {
73 if (numth >= 2*(this_reader -> steps-1))
74 {
75 doatoms = TRUE;
76 }
77 else
78 {
79 numth = this_reader -> steps;
80 }
81 }
82
83 if (doatoms)
84 {
85 // OpenMP on atoms
86 res = 0;
87 for (i=0; i<this_reader -> steps; i++)
88 {
89 k = i*(this_reader -> natomes + 2) + 2;
90 #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)
91 for (j=0; j<this_reader -> natomes; j++)
92 {
93 if (res == 2) goto enda;
94 this_line = g_strdup_printf ("%s", coord_line[k+j]);
95 this_word = strtok_r (this_line, " ", & saved_line);
96 if (! this_word)
97 {
98 format_error (i+1, j+1, lia[0], k+j);
99 res = 2;
100 goto enda;
101 }
103 v_dummy = 0;
104 if (! v)
105 {
106 #pragma omp critical
107 v_dummy = set_v_dummy (this_word);
108 }
109 if (v || v_dummy)
110 {
111 if (! i)
112 {
113 v = v + v_dummy * 0.1;
114 #pragma omp critical
115 check_for_species (v, j);
116 }
117 this_word = strtok_r (NULL, " ", & saved_line);
118 if (! this_word)
119 {
120 format_error (i+1, j+1, lia[1], k+j);
121 res = 2;
122 goto enda;
123 }
124 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word);
125 this_word = strtok_r (NULL, " ", & saved_line);
126 if (! this_word)
127 {
128 format_error (i+1, j+1, lia[2], k+j);
129 res = 2;
130 goto enda;
131 }
132 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word);
133 this_word = strtok_r (NULL, " ", & saved_line);
134 if (! this_word)
135 {
136 format_error (i+1, j+1, lia[3], k+j);
137 res = 2;
138 goto enda;
139 }
140 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word);
141 }
142 else
143 {
144 format_error (i+1, j+1, lia[0], k+j);
145 res = 2;
146 goto enda;
147 }
148 g_free (this_line);
149 enda:;
150 }
151 if (res == 2) break;
152 }
153 }
154 else
155 {
156 res = 0;
157 #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)
158 for (i=0; i<this_reader -> steps; i++)
159 {
160 if (res == 2) goto ends;
161 k = i*(this_reader -> natomes + 2) + 2;
162 for (j=0; j<this_reader -> natomes; j++)
163 {
164 this_line = g_strdup_printf ("%s", coord_line[k+j]);
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 = string_to_double ((gpointer)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 = string_to_double ((gpointer)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 = string_to_double ((gpointer)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 = string_to_double ((gpointer)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 = string_to_double ((gpointer)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 = string_to_double ((gpointer)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)string_to_double ((gpointer)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)string_to_double ((gpointer)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:163
int atoms[NUM_STYLES][2]
double * allocdouble(int val)
allocate a double * pointer
Definition global.c:459
int * allocint(int val)
allocate an int * pointer
Definition global.c:314
double string_to_double(gpointer string)
convert string to double
Definition global.c:624
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:61
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:220
line_node * head
Definition read_coord.c:77
gchar ** coord_line
Definition read_coord.c:74
coord_file * this_reader
Definition read_coord.c:73
void reader_info(gchar *type, gchar *sinf, int val)
display reader information
Definition read_coord.c:125
line_node * tail
Definition read_coord.c:78
void add_reader_info(gchar *info, int mid)
append information message to the reader information
Definition read_coord.c:88
char * this_word
Definition read_coord.c:76
void format_error(int stp, int ato, gchar *mot, int line)
Message to display an error message.
Definition read_coord.c:140
gchar * this_line
Definition read_coord.c:75
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:166
int xyz_get_atom_coordinates()
get the atomic coordinates from the XYZ file
Definition read_xyz.c:54
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