atomes 1.2.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_trj.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_trj.c'
24*
25* Contains:
26*
27
28 - The functions to read CPMD atomic coordinates
29
30*
31* List of functions:
32
33 int trj_get_atom_coordinates ();
34 int open_trj_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, l;
57 gchar * lia[4] = {"a", "b", "c", "d"};
59#ifdef OPENMP
60 int res;
61 int numth = omp_get_max_threads ();
62 gboolean doatoms = FALSE;
63 gchar * saved_line;
64 if (active_project -> steps < numth)
65 {
66 if (numth >= 2*(active_project -> steps-1))
67 {
68 doatoms = TRUE;
69 }
70 else
71 {
72 numth = active_project -> steps;
73 }
74 }
75 if (doatoms)
76 {
77 // OpenMP on atoms
78 res = 0;
79 for (i=0; i<active_project -> steps; i++)
80 {
81 k = i*active_project -> natomes;
82 #pragma omp parallel for num_threads(numth) private(j,this_line,saved_line,this_word) shared(i,k,lia,coord_line,active_project,res)
83 for (j=0; j<active_project -> natomes; j++)
84 {
85 if (res == 2) goto enda;
86 this_line = g_strdup_printf ("%s", coord_line[k+j]);
87 this_word = strtok_r (this_line, " ", & saved_line);
88 if (! this_word)
89 {
90 format_error (i+1, j+1, lia[0], k+j);
91 res = 2;
92 goto enda;
93 }
94 this_word = strtok_r (NULL, " ", & saved_line);
95 if (! this_word)
96 {
97 format_error (i+1, j+1, lia[1], k+j);
98 res = 2;
99 goto enda;
100 }
101 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word);
102 this_word = strtok_r (NULL, " ", & saved_line);
103 if (! this_word)
104 {
105 format_error (i+1, j+1, lia[2], k+j);
106 res = 2;
107 goto enda;
108 }
109 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word);
110 this_word = strtok_r (NULL, " ", & saved_line);
111 if (! this_word)
112 {
113 format_error (i+1, j+1, lia[3], k+j);
114 res = 2;
115 goto enda;
116 }
117 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word);
118 g_free (this_line);
119 enda:;
120 }
121 if (res == 2) break;
122 }
123 }
124 else
125 {
126 res = 0;
127 #pragma omp parallel for num_threads(numth) private(i,j,k,this_line,saved_line,this_word) shared(lia,coord_line,active_project,res)
128 for (i=0; i<active_project -> steps; i++)
129 {
130 if (res == 2) goto ends;
131 k = i*active_project -> natomes;
132 for (j=0; j<active_project -> natomes; j++)
133 {
134 this_line = g_strdup_printf ("%s", coord_line[k+j]);
135 this_word = strtok_r (this_line, " ", & saved_line);
136 if (! this_word)
137 {
138 format_error (i+1, j+1, lia[0], k+j);
139 res = 2;
140 goto ends;
141 }
142 this_word = strtok_r (NULL, " ", & saved_line);
143 if (! this_word)
144 {
145 format_error (i+1, j+1, lia[1], k+j);
146 res = 2;
147 goto ends;
148 }
149 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word) * 0.52917721;
150 this_word = strtok_r (NULL, " ", & saved_line);
151 if (! this_word)
152 {
153 format_error (i+1, j+1, lia[2], k+j);
154 res = 2;
155 goto ends;
156 }
157 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word) * 0.52917721;
158 this_word = strtok_r (NULL, " ", & saved_line);
159 if (! this_word)
160 {
161 format_error (i+1, j+1, lia[3], k+j);
162 res = 2;
163 goto ends;
164 }
165 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word) * 0.52917721;
166 g_free (this_line);
167 }
168 ends:;
169 }
170 }
171 g_free (coord_line);
172 if (res == 2) return 2;
173 for (i=1; i<active_project -> steps; i++)
174 {
175 for (j=0; j<active_project -> natomes; j++)
176 {
177 if (active_project -> atoms[i-1][j].sp != active_project -> atoms[i][j].sp)
178 {
179 add_reader_info (g_strdup_printf ("Error - chemical species changes between steps %d and %d, for atom %d !", i, i+1, j+1), 0);
180 return 2;
181 }
182 }
183 }
184#else
185 line_node * tmp_line;
186 tail = head;
187 k = 0;
188 for (i=0; i<active_project -> steps; i++)
189 {
190 for (j=0; j<active_project -> natomes; j++)
191 {
192 this_line = g_strdup_printf ("%s", tail -> line);
193 this_word = strtok (this_line, " ");
194 if (! this_word)
195 {
196 format_error (i+1, j+1, lia[0], k);
197 return 2;
198 }
199 this_word = strtok (NULL, " ");
200 if (! this_word)
201 {
202 format_error (i+1, j+1, lia[1], k);
203 return 2;
204 }
205 active_project -> atoms[i][j].x = string_to_double ((gpointer)this_word) * 0.52917721;
206 this_word = strtok (NULL, " ");
207 if (! this_word)
208 {
209 format_error (i+1, j+1, lia[2], k);
210 return 2;
211 }
212 active_project -> atoms[i][j].y = string_to_double ((gpointer)this_word) * 0.52917721;
213 this_word = strtok (NULL, " ");
214 if (! this_word)
215 {
216 format_error (i+1, j+1, lia[3], k);
217 return 2;
218 }
219 active_project -> atoms[i][j].z = string_to_double ((gpointer)this_word) * 0.52917721;
220 tmp_line = tail;
221 tail = tail -> next;
222 g_free (tmp_line);
223 k ++;
224 }
225 }
226#endif
227 i = 0;
228 for (j=0; j<this_reader -> nspec; j++)
229 {
230 for (k=0; k<this_reader -> nsps[j]; k++)
231 {
232 for (l=0; l<active_project -> steps; l++)
233 {
234 active_project -> atoms[l][i].sp = j;
235 }
236 i ++;
237 }
238 }
239 return 0;
240}
241
249int open_trj_file (int linec)
250{
251 if (linec%(this_reader -> natomes) != 0) return 2;
252 reader_info ("trj", "Number of atoms", this_reader -> natomes);
253 active_project -> steps = linec / this_reader -> natomes;
254 reader_info ("trj", "Number of steps", active_project -> steps);
255 active_project -> natomes = this_reader -> natomes;
256 return trj_get_atom_coordinates ();
257}
Binding to the Fortran90 subroutines.
Callback declarations for main window.
void allocatoms(project *this_proj)
allocate project data
Definition open_p.c:163
int atoms[NUM_STYLES][2]
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.
Function declarations for reading atomes project file Function declarations for saving atomes proje...
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 open_trj_file(int linec)
open CPMD file
Definition read_trj.c:249
int trj_get_atom_coordinates()
get the atomic coordinates from the CPMD file
Definition read_trj.c:54
Functions declaration to read atomic coordinates.
GtkWidget * res[2]
Definition w_encode.c:212