atomes 1.1.16
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_c3d.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_c3d.c'
24*
25* Contains:
26*
27
28 - The functions to read Chem3d atomic coordinates
29
30*
31* List of functions:
32
33 int c3d_get_atom_coordinates ();
34 int open_c3d_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
49extern void check_for_species (double v, int ato);
50
57{
58 int i, j, k;
59 double v;
60 gchar * lia[5] = {"a", "b", "c", "d", "e"};
61 this_reader -> nspec = 0;
62 active_project -> steps = this_reader -> steps;
63 active_project -> natomes = this_reader -> natomes;
65 this_reader -> z = allocdouble (1);
66 this_reader -> nsps = allocint (1);
67#ifdef OPENMP
68 int v_dummy;
69 int res;
70 int numth = omp_get_max_threads ();
71 gboolean doatoms = FALSE;
72 gchar * saved_line;
73 if (this_reader -> steps < numth)
74 {
75 if (numth >= 2*(this_reader -> steps-1))
76 {
77 doatoms = TRUE;
78 }
79 else
80 {
81 numth = this_reader -> steps;
82 }
83 }
84
85 if (doatoms)
86 {
87 // OpenMP on atoms
88 res = 0;
89 for (i=0; i<this_reader -> steps; i++)
90 {
91 k = i*(this_reader -> natomes + 1) + 1;
92 #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)
93 for (j=0; j<this_reader -> natomes; j++)
94 {
95 if (res == 2) goto enda;
96 this_line = g_strdup_printf ("%s", coord_line[k+j]);
97 saved_line = g_strdup_printf ("%s", this_line);
98 this_word = strtok_r (this_line, " ", & saved_line);
99 if (! this_word)
100 {
101 format_error (i+1, j+1, lia[0], k+j);
102 res = 2;
103 goto enda;
104 }
106 v_dummy = 0;
107 if (! v)
108 {
109 #pragma omp critical
110 v_dummy = set_v_dummy (this_word);
111 }
112 if (v || v_dummy)
113 {
114 if (! i)
115 {
116 v = v + v_dummy * 0.1;
117 #pragma omp critical
118 check_for_species (v, j);
119 }
120 this_word = strtok_r (NULL, " ", & saved_line);
121 if (! this_word)
122 {
123 format_error (i+1, j+1, lia[1], k+j);
124 res = 2;
125 goto enda;
126 }
127 this_word = strtok_r (NULL, " ", & saved_line);
128 if (! this_word)
129 {
130 format_error (i+1, j+1, lia[2], k+j);
131 res = 2;
132 goto enda;
133 }
134 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word);
135 this_word = strtok_r (NULL, " ", & saved_line);
136 if (! this_word)
137 {
138 format_error (i+1, j+1, lia[3], k+j);
139 res = 2;
140 goto enda;
141 }
142 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word);
143 this_word = strtok_r (NULL, " ", & saved_line);
144 if (! this_word)
145 {
146 format_error (i+1, j+1, lia[4], k+j);
147 res = 2;
148 goto enda;
149 }
150 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word);
151 }
152 else
153 {
154 format_error (i+1, j+1, lia[0], k+j);
155 res = 2;
156 goto enda;
157 }
158 g_free (this_line);
159 enda:;
160 }
161 if (res == 2) break;
162 }
163 }
164 else
165 {
166 res = 0;
167 #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)
168 for (i=0; i<this_reader -> steps; i++)
169 {
170 if (res == 2) goto ends;
171 k = i*(this_reader -> natomes + 1) + 1;
172 for (j=0; j<this_reader -> natomes; j++)
173 {
174 this_line = g_strdup_printf ("%s", coord_line[k+j]);
175 saved_line = g_strdup_printf ("%s", this_line);
176 this_word = strtok_r (this_line, " ", & saved_line);
177 if (! this_word)
178 {
179 format_error (i+1, j+1, lia[0], k+j);
180 res = 2;
181 goto ends;
182 }
184 v_dummy = 0;
185 if (! v)
186 {
187 #pragma omp critical
188 v_dummy = set_v_dummy (this_word);
189 }
190 if (v || v_dummy)
191 {
192 if (! i)
193 {
194 v = v + v_dummy * 0.1;
195 check_for_species (v, j);
196 }
197 this_word = strtok_r (NULL, " ", & saved_line);
198 if (! this_word)
199 {
200 format_error (i+1, j+1, lia[1], k+j);
201 res = 2;
202 goto ends;
203 }
204 this_word = strtok_r (NULL, " ", & saved_line);
205 if (! this_word)
206 {
207 format_error (i+1, j+1, lia[2], k+j);
208 res = 2;
209 goto ends;
210 }
211 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word);
212 this_word = strtok_r (NULL, " ", & saved_line);
213 if (! this_word)
214 {
215 format_error (i+1, j+1, lia[3], k+j);
216 res = 2;
217 goto ends;
218 }
219 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word);
220 this_word = strtok_r (NULL, " ", & saved_line);
221 if (! this_word)
222 {
223 format_error (i+1, j+1, lia[4], k+j);
224 res = 2;
225 goto ends;
226 }
227 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word);
228 }
229 else
230 {
231 format_error (i+1, j+1, lia[0], k+j);
232 res = 2;
233 goto ends;
234 }
235 g_free (this_line);
236 }
237 ends:;
238 }
239 }
240 g_free (coord_line);
241 if (res == 2) return 2;
242#else
243 line_node * tmp_line;
244 tail = head;
245 k = 0;
246 for (i=0; i<active_project -> steps; i++)
247 {
248 tmp_line = tail;
249 tail = tail -> next;
250 g_free (tmp_line);
251 k ++;
252 for (j=0; j<active_project -> natomes; j++)
253 {
254 this_line = g_strdup_printf ("%s", tail -> line);
255 this_word = strtok (this_line, " ");
256 if (! this_word)
257 {
258 format_error (i+1, j+1, lia[0], k+j);
259 return 2;
260 }
262 if (v)
263 {
264 if (! i) check_for_species (v, j);
265 this_word = strtok (NULL, " ");
266 if (! this_word)
267 {
268 format_error (i+1, j+1, lia[1], k+j);
269 return 2;
270 }
271 this_word = strtok (NULL, " ");
272 if (! this_word)
273 {
274 format_error (i+1, j+1, lia[2], k+j);
275 return 2;
276 }
277 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word);
278 this_word = strtok (NULL, " ");
279 if (! this_word)
280 {
281 format_error (i+1, j+1, lia[3], k+j);
282 return 2;
283 }
284 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word);
285 this_word = strtok (NULL, " ");
286 if (! this_word)
287 {
288 format_error (i+1, j+1, lia[4], k+j);
289 return 2;
290 }
291 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word);
292 }
293 else
294 {
295 format_error (i+1, j+1, lia[0], k+j);
296 return 2;
297 }
298 tmp_line = tail;
299 tail = tail -> next;
300 g_free (tmp_line);
301 k ++;
302 }
303 }
304#endif
305 for (i=1; i<active_project -> steps; i++)
306 {
307 for (j=0; j<active_project -> natomes; j++)
308 {
309 active_project -> atoms[i][j].sp = active_project -> atoms[0][j].sp;
310 }
311 }
312 return 0;
313}
314
322int open_c3d_file (int linec)
323{
324 int res;
325#ifdef OPENMP
326 this_line = g_strdup_printf ("%s", coord_line[0]);
327 this_word = strtok (this_line, " ");
328 if (! this_word)
329 {
330 add_reader_info ("Wrong file format - cannot find the number of atoms !", 0);
331 add_reader_info ("Wrong file format - first line is corrupted !", 0);
332 res = 2;
333 goto end;
334 }
335 this_reader -> natomes = (int)string_to_double ((gpointer)this_word);
336 reader_info ("c3d", "Number of atoms", this_reader -> natomes);
337 g_free (this_line);
338 if (linec%(this_reader -> natomes + 1) != 0)
339 {
340 res = 2;
341 }
342 else
343 {
344 this_reader -> steps = linec / (this_reader -> natomes + 1);
345 reader_info ("c3d", "Number of steps", this_reader -> steps);
346 res = (this_reader -> steps > 1) ? 2 : c3d_get_atom_coordinates ();
347 }
348#else
349 this_line = g_strdup_printf ("%s", head -> line);
350 this_word = strtok (this_line, " ");
351 if (! this_word)
352 {
353 add_reader_info ("Wrong file format - cannot find the number of atoms !", 0);
354 add_reader_info ("Wrong file format - the first line is corrupted !", 0);
355 res = 2;
356 goto end;
357 }
358 this_reader -> natomes = (int)string_to_double ((gpointer)this_word);
359 reader_info ("chem3d", "Number of atoms", this_reader -> natomes);
360 g_free (this_line);
361 if (linec%(this_reader -> natomes + 1) != 0)
362 {
363 res = 2;
364 }
365 else
366 {
367 this_reader -> steps = linec / (this_reader -> natomes + 1);
368 reader_info ("chem3d", "Number of steps", this_reader -> steps);
369 res = (this_reader -> steps > 1) ? 2 : c3d_get_atom_coordinates ();
370 }
371#endif
372 end:
373 return res;
374}
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: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:57
Function declarations for reading atomes project file Function declarations for saving atomes proje...
int c3d_get_atom_coordinates()
get the atomic coordinates from the C3D file
Definition read_c3d.c:56
int open_c3d_file(int linec)
open C3D file
Definition read_c3d.c:322
void check_for_species(double v, int ato)
Fill the species for each atom and the associated data.
Definition read_coord.c:196
line_node * head
Definition read_coord.c:75
gchar ** coord_line
Definition read_coord.c:72
coord_file * this_reader
Definition read_coord.c:71
void reader_info(gchar *type, gchar *sinf, int val)
display reader information
Definition read_coord.c:101
line_node * tail
Definition read_coord.c:76
void add_reader_info(gchar *info, int mid)
append information message to the reader information
Definition read_coord.c:86
char * this_word
Definition read_coord.c:74
void format_error(int stp, int ato, gchar *mot, int line)
Message to display an error message.
Definition read_coord.c:116
gchar * this_line
Definition read_coord.c:73
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:142
Functions declaration to read atomic coordinates.
GtkWidget * res[2]
Definition w_encode.c:212