atomes 1.2.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_coord.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_coord.c'
24*
25* Contains:
26*
27
28 - The general functions to import atomic coordinates
29
30*
31* List of functions:
32
33 gboolean set_dummy_in_use (gchar * this_word);
34
35 int open_coord_file (gchar * filename, int fti);
36
37 void add_reader_info (gchar * info, int mid);
38 void reader_info (gchar * type, gchar * sinf, int val);
39 void format_error (int stp, int ato, gchar * mot, int line);
40 void check_for_species (double v, int ato);
41
42*/
43
44#include "global.h"
45#include "glview.h"
46#include "callbacks.h"
47#include "interface.h"
48#include "project.h"
49#include "bind.h"
50#include "cbuild_edit.h"
51#ifdef OPENMP
52# include <omp.h>
53#endif
54
55extern int open_xyz_file (int linec);
56extern int open_c3d_file (int linec);
57extern int open_pdb_file (int linec);
58extern int open_trj_file (int linec);
59extern int open_vas_file (int linec);
60extern int open_cif_configuration (int linec, int conf);
61extern int open_cif_file (int linec);
62extern int open_hist_file (int linec);
63extern void allocatoms (project * this_proj);
64extern chemical_data * alloc_chem_data (int spec);
65extern int build_crystal (gboolean visible, project * this_proj, int c_step, gboolean to_wrap, gboolean show_clones, cell_info * cell, GtkWidget * widg);
66extern const gchar * dfi[2];
67
68extern atom_search * cif_search;
70extern gboolean cif_multiple;
71
72FILE * coordf;
74gchar ** coord_line = NULL;
75gchar * this_line = NULL;
76char * this_word;
77line_node * head = NULL;
78line_node * tail = NULL;
79
88void add_reader_info (gchar * info, int mid)
89{
90 int i;
91 gboolean append = TRUE;
92 for (i=0; i<this_reader -> msg; i++)
93 {
94 if (g_strcmp0(this_reader -> info[i], info) == 0)
95 {
96 append = FALSE;
97 break;
98 }
99 }
100 if (append)
101 {
102 if (! this_reader -> msg)
103 {
104 this_reader -> info = g_malloc0 (sizeof*this_reader -> info);
105 }
106 else
107 {
108 this_reader -> info = g_realloc (this_reader -> info, (this_reader -> msg+1)*sizeof*this_reader -> info);
109 }
110 this_reader -> info[this_reader -> msg] = g_strdup_printf ("%s", info);
111 this_reader -> msg ++;
112 }
113 if (! mid) this_reader -> mid = 0;
114}
115
125void reader_info (gchar * type, gchar * sinf, int val)
126{
127 g_print ("Reading coordinates [%s]: %s = %d\n", type, sinf, val);
128}
129
140void format_error (int stp, int ato, gchar * mot, int line)
141{
142 gchar * str;
143 if (ato < 0)
144 {
145 str = g_strdup_printf ("Wrong file format: error at step %d !\n"
146 "Wrong file format: record <b>%s</b> on line <b>%d</b> is corrupted !",
147 stp, mot, line);
148 }
149 else
150 {
151 str = g_strdup_printf ("Wrong file format: error at step %d, atom %d !\n"
152 "Wrong file format: record <b>%s</b> on line <b>%d</b> is corrupted !",
153 stp, ato, mot, line+1);
154 }
155 add_reader_info (str, 0);
156 g_free (str);
157}
158
167{
168 int i;
169 for (i=0; i<this_reader -> ndummy; i++)
170 {
171 if (g_strcmp0(this_reader -> dummy[i], this_word) == 0)
172 {
173 return i+1;
174 }
175 }
176 gchar ** dummy = NULL;
177 if (this_reader -> ndummy)
178 {
180 g_free (this_reader -> dummy);
181 }
182 this_reader -> ndummy ++;
183 this_reader -> dummy = g_malloc0(this_reader -> ndummy*sizeof*this_reader -> dummy);
184 if (dummy)
185 {
186 for (i=0; i<this_reader -> ndummy-1; i++)
187 {
188 this_reader -> dummy[i] = g_strdup_printf ("%s", dummy[i]);
189 }
190 }
191 this_reader -> dummy[this_reader -> ndummy-1] = g_strdup_printf ("%s", this_word);
192 // Dummy added, then do we use this dummy ?
193 gchar * str = g_strdup_printf ("Use dummy atom(s) for unknown %s species ?", this_word);
194 gboolean use_dummy = ask_yes_no ("Use dummy atom(s) ?", str, GTK_MESSAGE_QUESTION, MainWindow);
195 g_free (str);
196 if (use_dummy)
197 {
198 str = g_strdup_printf ("Using dummy atom(s) for unknown %s species", this_word);
199 add_reader_info (str, 1);
200 g_free (str);
201 return this_reader -> ndummy;
202 }
203 else
204 {
205 str = g_strdup_printf ("No dummy atom(s) for unknown %s species", this_word);
206 add_reader_info (str, 1);
207 g_free (str);
208 return 0;
209 }
210}
211
220void check_for_species (double v, int ato)
221{
222 int i;
223 gboolean add_spec;
224 add_spec = TRUE;
225 if (this_reader -> nspec)
226 {
227 for (i=0; i<this_reader -> nspec; i++)
228 {
229 if (this_reader -> z[i] == v)
230 {
231 if (this_reader -> cartesian)
232 {
233 active_project -> atoms[0][ato].sp = i;
234 }
235 else
236 {
237 this_reader -> lot[ato] = i;
238 }
239 this_reader -> nsps[i] ++;
240 add_spec = FALSE;
241 break;
242 }
243 }
244 }
245 if (add_spec)
246 {
247 if (this_reader -> nspec)
248 {
249 this_reader -> z = g_realloc (this_reader -> z, (this_reader -> nspec+1)*sizeof*this_reader -> z);
250 this_reader -> nsps = g_realloc (this_reader -> nsps, (this_reader -> nspec+1)*sizeof*this_reader -> nsps);
251 }
252 if (this_reader -> cartesian)
253 {
254 active_project -> atoms[0][ato].sp = this_reader -> nspec;
255 }
256 else
257 {
258 this_reader -> lot[ato] = this_reader -> nspec;
259 }
260 this_reader -> nsps[this_reader -> nspec] = 1;
261 this_reader -> z[this_reader -> nspec] = v;
262 this_reader -> nspec ++;
263 }
264}
265
274int open_coord_file (gchar * filename, int fti)
275{
276 int res = 0;
277#ifdef OPENMP
278 struct stat status;
279 res = stat (filename, & status);
280 if (res == -1)
281 {
282 add_reader_info ("Error - cannot get file statistics !\n", 0);
283 return 1;
284 }
285 int fsize = status.st_size;
286#endif
287 coordf = fopen (filename, dfi[0]);
288 if (! coordf)
289 {
290 add_reader_info ("Error - cannot open coordinates file !\n", 0);
291 return 1;
292 }
293 int i, j, k, l;
294#ifdef OPENMP
295 gchar * coord_content = g_malloc0(fsize*sizeof*coord_content);
296 fread (coord_content, fsize, 1, coordf);
297 fclose (coordf);
298 int linecount = 0;
299 for (j=0; j<fsize; j++) if (coord_content[j] == '\n') linecount ++;
300 coord_line = g_malloc0 (linecount*sizeof*coord_line);
301 coord_line[0] = & coord_content[0];
302 i = 1;
303 for (j=0; j<fsize; j++)
304 {
305 if (coord_content[j] == '\n')
306 {
307 coord_content[j] = '\0';
308 if (i < linecount)
309 {
310 coord_line[i] = & coord_content[j+1];
311 i ++;
312 }
313 }
314 }
315#else
316 gchar * buf = g_malloc0(LINE_SIZE*sizeof*buf);
317 head = NULL;
318 tail = NULL;
319 i = 0;
320 while (fgets(buf, LINE_SIZE, coordf))
321 {
322 if (head == NULL)
323 {
324 head = g_malloc0 (sizeof*head);
325 tail = g_malloc0 (sizeof*tail);
326 tail = head;
327 }
328 else
329 {
330 tail -> next = g_malloc0 (sizeof*tail -> next);
331 if (fti == 9 || fti == 10)
332 {
333 tail -> next -> prev = g_malloc0 (sizeof*tail -> next -> prev);
334 tail -> next -> prev = tail;
335 }
336 tail = tail -> next;
337 }
338 tail -> line = g_strdup_printf ("%s", buf);
339 tail -> line = substitute_string (tail -> line, "\n", "\0");
340 i ++;
341 }
342 g_free (buf);
343 fclose (coordf);
344#endif
345 if (i)
346 {
347 this_reader -> cartesian = TRUE;
348 if (fti < 2)
349 {
350 res = open_xyz_file (i);
351 }
352 else if (fti == 2)
353 {
354 res = open_c3d_file (i);
355 }
356 else if (fti < 5)
357 {
358 res = open_trj_file (i);
359 }
360 else if (fti < 7)
361 {
362 res = open_vas_file (i);
363 }
364 else if (fti > 6 && fti < 9)
365 {
366 res = open_pdb_file (i);
367 }
368 else if (fti > 8 && fti < 12)
369 {
370 if (fti == 11) cif_use_symmetry_positions = TRUE;
371 this_reader -> cartesian = FALSE;
372 if (fti == 10)
373 {
374 res = open_cif_file (i);
375 }
376 else
377 {
378 active_project -> steps = this_reader -> steps = 1;
379 this_reader -> rounding = -1;
380 cif_multiple = FALSE;
382 }
383 }
384 else if (fti == 12)
385 {
386 res = open_hist_file (i);
387 }
388 }
389 else
390 {
391 res = 1;
392 }
393#ifndef OPENMP
394 if (tail) g_free (tail);
395#endif
396 if (! res)
397 {
398 if (fti == 9 && ! this_reader -> cartesian)
399 {
400 // this_reader -> lattice.sp_group -> sid = 2;
401 // get_origin (this_reader -> lattice.sp_group);
403 {
404 // Test for all configurations, do build each:
405 // - a single trajectory ?
406 // - each in a single project ?
407 i = 1;
408 crystal_dist_chk = TRUE;
409 crystal_crowded = FALSE;
410 crystal_low_warning = TRUE;
411 for (j=0; j<active_project -> steps; j++)
412 {
413 k = build_crystal (FALSE, active_project, j, TRUE, FALSE, & this_reader -> lattice, MainWindow);
414 if (! k)
415 {
416 add_reader_info ("Error(s) trying to build crystal using the CIF file parameters !\n"
417 "This usually comes from: \n"
418 "\t - incorrect space group description\n"
419 "\t - incomplete space group description\n"
420 "\t - missing space group setting\n"
421 "\t - incorrect space group setting\n", 0);
422 res = 3;
423 goto end;
424 }
425 else if (k < 0)
426 {
427 add_reader_info ("Error(s) trying to build crystal using the CIF file parameters !\n"
428 "Information lead to change(s) between each configuration\n", 0);
429 res = 3;
430 goto end;
431 }
432 else if (k > 1 && i)
433 {
434 add_reader_info ("Potential issue(s) when building crystal !\n"
435 "This usually comes from: \n"
436 "\t - incorrect space group description\n"
437 "\t - incomplete space group description\n"
438 "\t - missing space group setting\n"
439 "\t - incorrect space group setting\n", 1);
440 if (this_reader -> num_sym_pos && active_project -> steps == 1)
441 {
442 add_reader_info ("\nAnother model will be built using included symmetry positions\n", 1);
444 }
445 i = 0;
446 }
447 }
448 }
449 }
450 for (i=0; i<active_project -> steps; i++)
451 {
452 for (j=0; j<active_project -> natomes; j++)
453 {
454 active_project -> atoms[i][j].id = j;
455 active_project -> atoms[i][j].show[0] = TRUE;
456 active_project -> atoms[i][j].show[1] = TRUE;
457 active_project -> atoms[i][j].label[0] = FALSE;
458 active_project -> atoms[i][j].label[1] = FALSE;
459 active_project -> atoms[i][j].pick[0] = FALSE;
460 active_project -> atoms[i][j].cloned = FALSE;
461 }
462 }
463 if (fti != 9 || this_reader -> cartesian)
464 {
465 active_project -> nspec = this_reader -> nspec;
468 k = l = 0;
469 reader_info (coord_files_ext[fti], "Number of species", active_project -> nspec);
470 for (i=0; i<active_project -> nspec; i++)
471 {
472 active_chem -> chem_prop[CHEM_Z][i] = this_reader -> z[i];
473 j = (int)this_reader -> z[i];
474 if (this_reader -> z[i] < 1.0)
475 {
476 active_chem -> label[i] = g_strdup_printf ("%s", this_reader -> dummy[l]);
477 active_chem -> element[i] = g_strdup_printf ("Dummy %s", this_reader -> dummy[l]);
478 active_chem -> chem_prop[CHEM_M][i] = 1.0;
479 active_chem -> chem_prop[CHEM_R][i] = 0.5;
480 l ++;
481 }
482 else
483 {
484 active_chem -> label[i] = g_strdup_printf ("%s", periodic_table_info[j].lab);
485 active_chem -> element[i] = g_strdup_printf ("%s", periodic_table_info[j].name);
486 active_chem -> chem_prop[CHEM_M][i] = set_mass_ (& j);
487 active_chem -> chem_prop[CHEM_R][i] = set_radius_ (& j, & k);
488 if (! active_chem -> chem_prop[CHEM_R][i])
489 {
490 gchar * str = g_strdup_printf ("For species %s, radius is equal to 0.0 !\n", active_chem -> label[i]);
491 add_reader_info (str, 1);
492 g_free (str);
493 }
494 active_chem -> chem_prop[CHEM_N][i] = set_neutron_ (& j);
495 active_chem -> chem_prop[CHEM_X][i] = active_chem -> chem_prop[CHEM_Z][i];
496 }
497 active_chem -> nsps[i] = this_reader -> nsps[i];
498 g_print ("Reading coordinates [%s]:\t %s, nsps[%d]= %d\n", coord_files_ext[fti], active_chem -> label[i], i+1, active_chem -> nsps[i]);
499 }
500 }
501 else
502 {
503 reader_info (coord_files_ext[fti], "Number of species", active_project -> nspec);
504 for (i=0; i<active_project -> nspec; i++)
505 {
506 g_print ("Reading coordinates [%s]:\t %s, nsps[%d]= %d\n", coord_files_ext[fti], active_chem -> label[i], i+1, active_chem -> nsps[i]);
507 }
508 }
509 }
510 end:;
511 if (! (fti == 9 && cif_use_symmetry_positions) || res)
512 {
513 if (cif_search)
514 {
515 g_free (cif_search);
516 cif_search = NULL;
517 }
518 if (cif_object)
519 {
520 g_free (cif_object);
521 cif_object = NULL;
522 }
524 }
525 return res;
526}
gchar * mot[2][2]
Definition popup.c:3298
Binding to the Fortran90 subroutines.
double set_radius_(int *, int *)
double set_neutron_(int *)
double set_mass_(int *)
gchar * substitute_string(gchar *init, gchar *o_motif, gchar *n_motif)
substitute all patterns in string
Definition w_library.c:372
Callback declarations for main window.
gboolean crystal_low_warning
gboolean crystal_crowded
gboolean crystal_dist_chk
Function declarations for the crystal builder.
integer(kind=c_int) function chemistry()
Definition chemistry.F90:22
dummy_atom * dummy
Definition cpmd_atoms.c:73
void label(cairo_t *cr, double val, int axe, int p, project *this_proj)
draw axis label
Definition labels.c:56
int atoms[NUM_STYLES][2]
float val
Definition dlp_init.c:117
int activep
Definition global.c:159
GtkWidget * MainWindow
Definition global.c:201
gchar ** duplicate_strings(int num, gchar **old_val)
copy a list of strings
Definition global.c:544
gboolean cif_use_symmetry_positions
Definition global.c:189
Global variable declarations Global convenience function declarations Global data structure defin...
element_data periodic_table_info[]
Definition w_library.c:71
#define CHEM_N
Definition global.h:302
chemical_data * active_chem
Definition project.c:48
#define CHEM_R
Definition global.h:301
#define CHEM_M
Definition global.h:300
char * coord_files_ext[NCFORMATS+1]
Definition callbacks.c:101
#define LINE_SIZE
Definition global.h:487
#define CHEM_X
Definition global.h:303
#define CHEM_Z
Definition global.h:299
project * active_project
Definition project.c:47
Variable declarations related to the OpenGL window Function declarations related to the OpenGL wind...
gboolean ask_yes_no(gchar *title, gchar *text, int type, GtkWidget *widg)
ask yes or no for something: prepare dialog
Definition interface.c:356
Messaging function declarations.
integer(kind=c_int) function lattice(totl, lid, vectors, vmod, angles, lat, cfrac, apbc)
Definition lattice.F90:162
double z
Definition ogl_draw.c:61
Function declarations for reading atomes project file Function declarations for saving atomes proje...
void active_project_changed(int id)
change the active project
Definition update_p.c:175
int open_coord_file(gchar *filename, int fti)
open atomic coordinates file
Definition read_coord.c:274
int open_trj_file(int linec)
open CPMD file
Definition read_trj.c:249
FILE * coordf
Definition read_coord.c:72
gboolean cif_multiple
Definition read_cif.c:103
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
int open_c3d_file(int linec)
open C3D file
Definition read_c3d.c:320
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
chemical_data * alloc_chem_data(int spec)
allocate chemistry data
Definition open_p.c:189
int open_hist_file(int linec)
open DL-POLY history file
Definition read_hist.c:434
char * this_word
Definition read_coord.c:76
int build_crystal(gboolean visible, project *this_proj, int c_step, gboolean to_wrap, gboolean show_clones, cell_info *cell, GtkWidget *widg)
build crystal
void format_error(int stp, int ato, gchar *mot, int line)
Message to display an error message.
Definition read_coord.c:140
void allocatoms(project *this_proj)
allocate project data
Definition open_p.c:163
int open_cif_file(int linec)
open CIF file
Definition read_cif.c:2547
gchar * this_line
Definition read_coord.c:75
void check_for_species(double v, int ato)
Fill the species for each atom and the associated data.
Definition read_coord.c:220
const gchar * dfi[2]
Definition main.c:76
int open_cif_configuration(int linec, int conf)
Definition read_cif.c:2082
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
atom_search * cif_search
Definition read_cif.c:312
int open_xyz_file(int linec)
open XYZ file
Definition read_xyz.c:301
int open_vas_file(int linec)
open VASP file
Definition read_vas.c:242
int open_pdb_file(int linec)
open PDB file
Definition read_pdb.c:216
atomic_object * cif_object
Definition read_cif.c:313
int id
Definition global.h:946
int status
Definition w_advance.c:178
GtkWidget * res[2]
Definition w_encode.c:212
int element
Definition w_periodic.c:61
gboolean append(atom_search *asearch, project *this_proj, int i, int j)
test if the atom 'i' of species 'j' must be added to the tree store or not
Definition w_search.c:756
GtkWidget * lab
Definition workspace.c:73