atomes 1.2.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_npt.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_npt.c'
24*
25* Contains:
26*
27
28 - The functions to read NPT data
29
30*
31* List of functions:
32
33 int cell_get_lattice (int format);
34 int open_cell_file (int format, gchar * filename);
35
36 void add_cell_info (gchar * info);
37
38*/
39
40#include "global.h"
41#include "bind.h"
42#include "interface.h"
43#include "project.h"
44#include <ctype.h>
45#include "cbuild_edit.h"
46#include "readers.h"
47
48#ifdef G_OS_WIN32
49 typedef intptr_t ssize_t;
50 extern ssize_t getline(char **lineptr, size_t *n, FILE *stream);
51#endif
52
53FILE * cellp;
54
55typedef struct cell_file cell_file;
57{
59 gchar * info;
60};
61
63
71void add_cell_info (gchar * info)
72{
73 this_cell -> info = (this_cell -> info) ? g_strdup_printf ("%s\n%s", this_cell -> info, info) : g_strdup_printf ("%s", info);
74}
75
83int cell_get_lattice (int format)
84{
85 size_t length = 0;
86 gchar * line = NULL;
87 int i, j, k;
88 char c;
89 i = 0;
90 for (c = getc(cellp); c != EOF; c = getc(cellp)) if (c == '\n') i ++;
91 j = (format == 0 || format == 2) ? 1 : (format == 1) ? 2 : 3;
92 k = i / j;
93 i = i - k * j;
94 if (i) return 0;
95 if (k != active_project -> steps)
96 {
97 add_cell_info (g_strdup_printf ("Wrong file format - %d steps in coordinates file", active_project -> steps));
98 add_cell_info (g_strdup_printf ("Wrong file format - %d steps found in cell file", k));
99 return 0;
100 }
101 rewind (cellp);
102 this_cell -> lattice.box = g_malloc0(k*sizeof*this_cell -> lattice.box);
103 for (i=0; i<active_project -> steps; i++)
104 {
105 if (format < 2)
106 {
107 for (j=0; j<2; j++)
108 {
109 if (! j || format == 1)
110 {
111 getline (& line, & length, cellp);
112 this_line = g_strdup_printf ("%s", line);
113 this_word = strtok (this_line, " ");
114 }
115 else
116 {
117 this_word = strtok (NULL, " ");
118 }
119 k = 0;
120 if (! this_word)
121 {
122 add_cell_info (g_strdup_printf ("Wrong file format - error at step %d !", i+1));
123 return 0;
124 }
125 this_cell -> lattice.box[i].param[j][k] = string_to_double ((gpointer)this_word);
126 k ++;
127 this_word = strtok (NULL, " ");
128 if (! this_word)
129 {
130 add_cell_info (g_strdup_printf ("Wrong file format - error at step %d !", i+1));
131 return 0;
132 }
133 this_cell -> lattice.box[i].param[j][k] = string_to_double ((gpointer)this_word);
134 k ++;
135 this_word = strtok (NULL, " ");
136 if (! this_word)
137 {
138 add_cell_info (g_strdup_printf ("Wrong file format - error at step %d !", i+1));
139 return 0;
140 }
141 this_cell -> lattice.box[i].param[j][k] = string_to_double ((gpointer)this_word);
142 }
143 }
144 else
145 {
146 for (j=0; j<3; j++)
147 {
148 if (! j || format == 3)
149 {
150 getline (& line, & length, cellp);
151 this_line = g_strdup_printf ("%s", line);
152 this_word = strtok (this_line, " ");
153 }
154 else
155 {
156 this_word = strtok (NULL, " ");
157 }
158 k = 0;
159 if (! this_word)
160 {
161 add_cell_info (g_strdup_printf ("Wrong file format - error at step %d !", i+1));
162 return 0;
163 }
164 this_cell -> lattice.box[i].vect[j][k] = string_to_double ((gpointer)this_word);
165 k ++;
166 this_word = strtok (NULL, " ");
167 if (! this_word)
168 {
169 add_cell_info (g_strdup_printf ("Wrong file format - error at step %d !", i+1));
170 return 0;
171 }
172 this_cell -> lattice.box[i].vect[j][k] = string_to_double ((gpointer)this_word);
173 k ++;
174 this_word = strtok (NULL, " ");
175 if (! this_word)
176 {
177 add_cell_info (g_strdup_printf ("Wrong file format - error at step %d !", i+1));
178 return 0;
179 }
180 this_cell -> lattice.box[i].vect[j][k] = string_to_double ((gpointer)this_word);
181 }
182 }
183 }
184 this_cell -> lattice.npt = FALSE;
185 for (i=1; i<active_project -> steps; i++)
186 {
187 if (format < 2)
188 {
189 for (j=0; j<2; j++)
190 {
191 for (k=0; k<3; k++)
192 {
193 if (this_cell -> lattice.box[i].param[j][k] != this_cell -> lattice.box[0].param[j][k])
194 {
195 this_cell -> lattice.npt = TRUE;
196 break;
197 }
198 }
199 if (this_cell -> lattice.npt) break;
200 }
201 if (this_cell -> lattice.npt) break;
202 }
203 else
204 {
205 for (j=0; j<3; j++)
206 {
207 for (k=0; k<3; k++)
208 {
209 if (this_cell -> lattice.box[i].vect[j][k] != this_cell -> lattice.box[0].vect[j][k])
210 {
211 this_cell -> lattice.npt = TRUE;
212 break;
213 }
214 }
215 if (this_cell -> lattice.npt) break;
216 }
217 if (this_cell -> lattice.npt) break;
218 }
219 }
220 return 1;
221}
222
231int open_cell_file (int format, gchar * filename)
232{
233 int res;
234 int i, j, k, l;
235 cellp = fopen (filename, "r");
236 if (cellp)
237 {
238 this_cell = g_malloc0(sizeof*this_cell);
239 if (cell_get_lattice(format))
240 {
241 active_cell -> ltype = format/2 + 1;
242 active_cell -> pbc = TRUE;
243 active_cell -> npt = this_cell -> lattice.npt;
244 i = (active_cell -> npt) ? active_project -> steps : 1;
245 if (active_cell -> npt)
246 {
247 g_free (active_cell -> box);
248 active_cell -> box = g_malloc0(i*sizeof*active_cell -> box);
249 active_box = & active_cell -> box[0];
250 }
251 for (j=0; j<i; j++)
252 {
253 if (format < 2)
254 {
255 for (k=0; k<2; k++)
256 {
257 for (l=0; l<3; l++)
258 {
259 active_cell -> box[j].param[k][l] = this_cell -> lattice.box[j].param[k][l];
260 }
261 }
262 }
263 else
264 {
265 for (k=0; k<3; k++)
266 {
267 for (l=0; l<3; l++)
268 {
269 active_cell -> box[j].vect[k][l] = this_cell -> lattice.box[j].vect[k][l];
270 }
271 }
272 }
273 }
274 active_cell -> has_a_box = TRUE;
275 active_cell -> crystal = FALSE;
276 res = 0;
277 }
278 else
279 {
280 // No or wrong cell data found
281#ifdef DEBUG
282 g_debug ("CELL-NPT:: Impossible to retrieve cell parameters !");
283#endif
284 res = 2;
285 }
286 }
287 else
288 {
289 // Cannot open file
290 res = 1;
291 }
292 if (this_cell)
293 {
294 if (this_cell -> info)
295 {
296 show_error (this_cell -> info, 0, MainWindow);
297 }
298 if (this_cell)
299 {
300 if (this_cell -> info) g_free (this_cell -> info);
301 if (this_cell -> lattice.box) g_free (this_cell -> lattice.box);
302 g_free (this_cell);
303 this_cell = NULL;
304 }
305 }
306 return res;
307}
Binding to the Fortran90 subroutines.
Function declarations for the crystal builder.
GtkWidget * MainWindow
Definition global.c:201
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...
cell_info * active_cell
Definition project.c:50
box_info * active_box
Definition project.c:51
project * active_project
Definition project.c:47
void show_error(char *error, int val, GtkWidget *win)
show error message
Definition interface.c:293
Messaging function declarations.
integer(kind=c_int) function lattice(totl, lid, vectors, vmod, angles, lat, cfrac, apbc)
Definition lattice.F90:162
Function declarations for reading atomes project file Function declarations for saving atomes proje...
char * this_word
Definition read_coord.c:76
gchar * this_line
Definition read_coord.c:75
FILE * cellp
Definition read_npt.c:53
int cell_get_lattice(int format)
get the lattice parameters format from the file
Definition read_npt.c:83
cell_file * this_cell
Definition read_npt.c:62
void add_cell_info(gchar *info)
append information message to the cell information
Definition read_npt.c:71
int open_cell_file(int format, gchar *filename)
open the file that contains the cell parameters
Definition read_npt.c:231
Functions declaration to read atomic coordinates.
Definition glwin.h:351
cell_info lattice
Definition read_npt.c:58
gchar * info
Definition read_npt.c:59
int c
Definition tab-1.c:95
GtkWidget * res[2]
Definition w_encode.c:212