atomes 1.3.2
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-2026 by CNRS and University of Strasbourg */
15
21
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);
64extern void allocatoms (project * this_proj);
65extern chemical_data * alloc_chem_data (int spec);
66extern int build_crystal (gboolean visible, project * this_proj, int c_step, gboolean to_wrap, gboolean show_clones, cell_info * cell, GtkWidget * widg);
67extern const gchar * dfi[2];
68
69extern atom_search * cif_search;
71extern gboolean cif_multiple;
72
73FILE * coordf;
75gchar ** coord_line = NULL;
76gchar * this_line = NULL;
77char * this_word;
78line_node * head = NULL;
79line_node * tail = NULL;
80
89void add_reader_info (gchar * info, int mid)
90{
91 int i;
92 gboolean append = TRUE;
93 for (i=0; i<this_reader -> msg; i++)
94 {
95 if (g_strcmp0(this_reader -> info[i], info) == 0)
96 {
97 append = FALSE;
98 break;
99 }
100 }
101 if (append)
102 {
103 if (! this_reader -> msg)
104 {
105 this_reader -> info = g_malloc0(sizeof*this_reader -> info);
106 }
107 else
108 {
109 this_reader -> info = g_realloc (this_reader -> info, (this_reader -> msg+1)*sizeof*this_reader -> info);
110 }
111 this_reader -> info[this_reader -> msg] = g_strdup_printf ("%s", info);
112 this_reader -> msg ++;
113 }
114 if (! mid) this_reader -> mid = 0;
115}
116
126void reader_info (gchar * type, gchar * sinf, int val)
127{
128 g_print ("Reading coordinates [%s]: %s = %d\n", type, sinf, val);
129}
130
141void format_error (int stp, int ato, gchar * mot, int line)
142{
143 gchar * str;
144 if (ato < 0)
145 {
146 str = g_strdup_printf (_("Wrong file format: error at step %d !\n"
147 "Wrong file format: record <b>%s</b> on line <b>%d</b> is corrupted !"),
148 stp, mot, line);
149 }
150 else
151 {
152 str = g_strdup_printf (_("Wrong file format: error at step %d, atom %d !\n"
153 "Wrong file format: record <b>%s</b> on line <b>%d</b> is corrupted !"),
154 stp, ato, mot, line+1);
155 }
156 add_reader_info (str, 0);
157 g_free (str);
158}
159
168{
169 int i;
170 for (i=0; i<this_reader -> ndummy; i++)
171 {
172 if (g_strcmp0(this_reader -> dummy[i], this_word) == 0)
173 {
174 return i+1;
175 }
176 }
177 gchar ** dummy = NULL;
178 if (this_reader -> ndummy)
179 {
181 g_free (this_reader -> dummy);
182 }
183 this_reader -> ndummy ++;
184 this_reader -> dummy = g_malloc0(this_reader -> ndummy*sizeof*this_reader -> dummy);
185 if (dummy)
186 {
187 for (i=0; i<this_reader -> ndummy-1; i++)
188 {
189 this_reader -> dummy[i] = g_strdup_printf ("%s", dummy[i]);
190 }
191 }
192 this_reader -> dummy[this_reader -> ndummy-1] = g_strdup_printf ("%s", this_word);
193 // Dummy added, then do we use this dummy ?
194 gchar * str = g_strdup_printf (_("Use dummy atom(s) for unknown %s species ?"), this_word);
195 gboolean use_dummy = ask_yes_no (_("Use dummy atom(s) ?"), str, GTK_MESSAGE_QUESTION, atomes_main_window);
196 g_free (str);
197 if (use_dummy)
198 {
199 str = g_strdup_printf (_("Using dummy atom(s) for unknown %s species"), this_word);
200 add_reader_info (str, 1);
201 g_free (str);
202 return this_reader -> ndummy;
203 }
204 else
205 {
206 str = g_strdup_printf (_("No dummy atom(s) for unknown %s species"), this_word);
207 add_reader_info (str, 1);
208 g_free (str);
209 return 0;
210 }
211}
212
221void check_for_species (double v, int ato)
222{
223 int i;
224 gboolean add_spec;
225 add_spec = TRUE;
226 if (this_reader -> nspec)
227 {
228 for (i=0; i<this_reader -> nspec; i++)
229 {
230 if (this_reader -> z[i] == v)
231 {
232 if (this_reader -> cartesian)
233 {
234 active_project -> atoms[0][ato].sp = i;
235 }
236 else
237 {
238 this_reader -> lot[ato] = i;
239 }
240 this_reader -> nsps[i] ++;
241 add_spec = FALSE;
242 break;
243 }
244 }
245 }
246 if (add_spec)
247 {
248 if (this_reader -> nspec)
249 {
250 this_reader -> z = g_realloc (this_reader -> z, (this_reader -> nspec+1)*sizeof*this_reader -> z);
251 this_reader -> nsps = g_realloc (this_reader -> nsps, (this_reader -> nspec+1)*sizeof*this_reader -> nsps);
252 }
253 if (this_reader -> cartesian)
254 {
255 active_project -> atoms[0][ato].sp = this_reader -> nspec;
256 }
257 else
258 {
259 this_reader -> lot[ato] = this_reader -> nspec;
260 }
261 this_reader -> nsps[this_reader -> nspec] = 1;
262 this_reader -> z[this_reader -> nspec] = v;
263 this_reader -> nspec ++;
264 }
265}
266
275int open_coord_file (gchar * filename, int fti)
276{
277 int i, j, k, l;
278 int res = 0;
279#ifdef OPENMP
280 struct stat status;
281 res = stat (filename, & status);
282 if (res == -1)
283 {
284 add_reader_info (_("Error - cannot get file statistics !\n"), 0);
285 return 1;
286 }
287 int fsize = status.st_size;
288#endif
289
290 if (fti == 13)
291 {
292 i = 1;
293 }
294 else
295 {
296 coordf = fopen (filename, dfi[0]);
297 if (! coordf)
298 {
299 add_reader_info (_("Error - cannot open coordinates file !\n"), 0);
300 return 1;
301 }
302#ifdef OPENMP
303 gchar * coord_content = g_malloc0(fsize*sizeof*coord_content);
304 fread (coord_content, fsize, 1, coordf);
305 fclose (coordf);
306 int linecount = 0;
307 for (j=0; j<fsize; j++) if (coord_content[j] == '\n') linecount ++;
308 coord_line = g_malloc0(linecount*sizeof*coord_line);
309 coord_line[0] = & coord_content[0];
310 i = 1;
311 for (j=0; j<fsize; j++)
312 {
313 if (coord_content[j] == '\n')
314 {
315 coord_content[j] = '\0';
316 if (i < linecount)
317 {
318 coord_line[i] = & coord_content[j+1];
319 i ++;
320 }
321 }
322 }
323#else
324 gchar * buf = g_malloc0(LINE_SIZE*sizeof*buf);
325 head = NULL;
326 tail = NULL;
327 i = 0;
328 while (fgets(buf, LINE_SIZE, coordf))
329 {
330 if (head == NULL)
331 {
332 head = g_malloc0(sizeof*head);
333 tail = g_malloc0(sizeof*tail);
334 tail = head;
335 }
336 else
337 {
338 tail -> next = g_malloc0(sizeof*tail -> next);
339 if (fti == 9 || fti == 10)
340 {
341 tail -> next -> prev = g_malloc0(sizeof*tail -> next -> prev);
342 tail -> next -> prev = tail;
343 }
344 tail = tail -> next;
345 }
346 tail -> line = g_strdup_printf ("%s", buf);
347 tail -> line = substitute_string (tail -> line, "\n", "\0");
348 i ++;
349 }
350 g_free (buf);
351 fclose (coordf);
352#endif
353 }
354
355 if (i)
356 {
357 this_reader -> cartesian = TRUE;
358 if (fti < 2)
359 {
360 res = open_xyz_file (i);
361 }
362 else if (fti == 2)
363 {
364 res = open_c3d_file (i);
365 }
366 else if (fti < 5)
367 {
368 res = open_trj_file (i);
369 }
370 else if (fti < 7)
371 {
372 res = open_vas_file (i);
373 }
374 else if (fti > 6 && fti < 9)
375 {
376 res = open_pdb_file (i);
377 }
378 else if (fti > 8 && fti < 12)
379 {
380 if (fti == 11) cif_use_symmetry_positions = TRUE;
381 this_reader -> cartesian = FALSE;
382 if (fti == 10)
383 {
384 res = open_cif_file (i);
385 }
386 else
387 {
388 active_project -> steps = this_reader -> steps = 1;
389 this_reader -> rounding = -1;
390 cif_multiple = FALSE;
392 }
393 }
394 else if (fti == 12)
395 {
396 res = open_hist_file (i);
397 }
398 else if (fti == 13)
399 {
401 }
402 }
403 else
404 {
405 res = 1;
406 }
407#ifndef OPENMP
408 if (tail) g_free (tail);
409#endif
410 if (! res)
411 {
412 if (fti == 9 && ! this_reader -> cartesian)
413 {
414 // this_reader -> lattice.sp_group -> sid = 2;
415 // get_origin (this_reader -> lattice.sp_group);
417 {
418 // Test for all configurations, do build each:
419 // - a single trajectory ?
420 // - each in a single project ?
421 i = 1;
422 crystal_dist_chk = TRUE;
423 crystal_crowded = FALSE;
424 crystal_low_warning = TRUE;
425 for (j=0; j<active_project -> steps; j++)
426 {
427 k = build_crystal (FALSE, active_project, j, TRUE, FALSE, & this_reader -> lattice, atomes_main_window);
428 if (! k)
429 {
430 add_reader_info (_("Error(s) trying to build crystal using the CIF file parameters !\n"
431 "This usually comes from: \n"
432 "\t - incorrect space group description\n"
433 "\t - incomplete space group description\n"
434 "\t - missing space group setting\n"
435 "\t - incorrect space group setting\n"), 0);
436 res = 3;
437 goto end;
438 }
439 else if (k < 0)
440 {
441 add_reader_info (_("Error(s) trying to build crystal using the CIF file parameters !\n"
442 "Information lead to change(s) between each configuration\n"), 0);
443 res = 3;
444 goto end;
445 }
446 else if (k > 1 && i)
447 {
448 add_reader_info (_("Potential issue(s) when building crystal !\n"
449 "This usually comes from: \n"
450 "\t - incorrect space group description\n"
451 "\t - incomplete space group description\n"
452 "\t - missing space group setting\n"
453 "\t - incorrect space group setting\n"), 1);
454 if (this_reader -> num_sym_pos && active_project -> steps == 1)
455 {
456 add_reader_info (_("\nAnother model will be built using included symmetry positions\n"), 1);
458 }
459 i = 0;
460 }
461 }
462 }
463 }
464 for (i=0; i<active_project -> steps; i++)
465 {
466 for (j=0; j<active_project -> natomes; j++)
467 {
468 active_project -> atoms[i][j].id = j;
469 active_project -> atoms[i][j].show[0] = TRUE;
470 active_project -> atoms[i][j].show[1] = TRUE;
471 active_project -> atoms[i][j].label[0] = FALSE;
472 active_project -> atoms[i][j].label[1] = FALSE;
473 active_project -> atoms[i][j].pick[0] = FALSE;
474 active_project -> atoms[i][j].cloned = FALSE;
475 }
476 }
477 if (active_project -> steps > 1)
478 {
479 active_project -> skt_corr_threshold = (active_project -> steps < 20) ? 1 : 10;
480 active_project -> skt_n_data_sets = min (5, active_project -> steps);
481 active_project -> sqw_n_data_sets = 5;
482 }
483 if (fti != 9 || this_reader -> cartesian)
484 {
485 active_project -> nspec = this_reader -> nspec;
488 k = l = 0;
489 reader_info (coord_files_ext[fti], _("Number of species"), active_project -> nspec);
490 for (i=0; i<active_project -> nspec; i++)
491 {
492 active_chem -> chem_prop[CHEM_Z][i] = this_reader -> z[i];
493 j = (int)this_reader -> z[i];
494 if (this_reader -> z[i] < 1.0)
495 {
496 active_chem -> label[i] = g_strdup_printf ("%s", this_reader -> dummy[l]);
497 active_chem -> element[i] = g_strdup_printf (_("Dummy %s"), this_reader -> dummy[l]);
498 active_chem -> chem_prop[CHEM_M][i] = 1.0;
499 active_chem -> chem_prop[CHEM_R][i] = 0.5;
500 l ++;
501 }
502 else
503 {
504 active_chem -> label[i] = g_strdup_printf ("%s", periodic_table_info[j].lab);
505 active_chem -> element[i] = g_strdup_printf ("%s", periodic_table_info[j].name);
506 active_chem -> chem_prop[CHEM_M][i] = set_mass_ (& j);
507 active_chem -> chem_prop[CHEM_R][i] = set_radius_ (& j, & k);
508 if (! active_chem -> chem_prop[CHEM_R][i])
509 {
510 gchar * str = g_strdup_printf (_("For species %s, radius is equal to 0.0 !\n"), active_chem -> label[i]);
511 add_reader_info (str, 1);
512 g_free (str);
513 }
514 active_chem -> chem_prop[CHEM_N][i] = set_neutron_ (& j);
515 active_chem -> chem_prop[CHEM_X][i] = active_chem -> chem_prop[CHEM_Z][i];
516 }
517 active_chem -> nsps[i] = this_reader -> nsps[i];
518 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]);
519 }
520 }
521 else
522 {
523 reader_info (coord_files_ext[fti], _("Number of species"), active_project -> nspec);
524 for (i=0; i<active_project -> nspec; i++)
525 {
526 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]);
527 }
528 }
529 }
530 end:;
531 if (! (fti == 9 && cif_use_symmetry_positions) || res)
532 {
533 if (cif_search)
534 {
535 g_free (cif_search);
536 cif_search = NULL;
537 }
538 if (cif_object)
539 {
540 g_free (cif_object);
541 cif_object = NULL;
542 }
544 }
545 return res;
546}
gchar * mot[2][2]
atomic_object * cif_object
Definition read_cif.c:342
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:378
const gchar * dfi[2]
Definition main.c:105
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
int atoms[NUM_STYLES][2]
float val
Definition dlp_init.c:117
GtkWidget * atomes_main_window
Definition global.c:208
int activep
Definition global.c:162
gchar ** duplicate_strings(int num, gchar **old_val)
copy a list of strings
Definition global.c:532
gboolean cif_use_symmetry_positions
Definition global.c:193
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:321
chemical_data * active_chem
Definition project.c:48
#define CHEM_R
Definition global.h:320
#define CHEM_M
Definition global.h:319
char * coord_files_ext[NCFORMATS+1]
Definition callbacks.c:104
#define LINE_SIZE
Definition global.h:501
#define min(a, b)
Definition global.h:94
#define CHEM_X
Definition global.h:322
#define CHEM_Z
Definition global.h:318
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:420
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:63
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:220
gboolean cif_multiple
Definition read_cif.c:103
atom_search * cif_search
Definition read_cif.c:341
int open_sml_file_out_of_library()
read 'Simple chemical library XML' file outside of atomes library
Definition read_sml.c:262
int open_coord_file(gchar *filename, int fti)
open atomic coordinates file
Definition read_coord.c:275
int open_trj_file(int linec)
open CPMD file
Definition read_trj.c:249
FILE * coordf
Definition read_coord.c:73
line_node * head
Definition read_coord.c:78
gchar ** coord_line
Definition read_coord.c:75
coord_file * this_reader
Definition read_coord.c:74
void reader_info(gchar *type, gchar *sinf, int val)
display reader information
Definition read_coord.c:126
int open_c3d_file(int linec)
open C3D file
Definition read_c3d.c:320
line_node * tail
Definition read_coord.c:79
void add_reader_info(gchar *info, int mid)
append information message to the reader information
Definition read_coord.c:89
chemical_data * alloc_chem_data(int spec)
allocate chemistry data
Definition open_p.c:203
int open_hist_file(int linec)
open DL-POLY history file
Definition read_hist.c:434
char * this_word
Definition read_coord.c:77
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:141
void allocatoms(project *this_proj)
allocate project data
Definition open_p.c:177
int open_cif_file(int linec)
open CIF file
Definition read_cif.c:2738
gchar * this_line
Definition read_coord.c:76
void check_for_species(double v, int ato)
Fill the species for each atom and the associated data.
Definition read_coord.c:221
int open_cif_configuration(int linec, int conf)
Definition read_cif.c:2248
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:167
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
int status
Definition w_advance.c:173
GtkWidget * res[2]
Definition w_encode.c:342
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