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