atomes 1.3.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
save_p.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: 'save_p.c'
24*
25* Contains:
26*
27
28 - The functions to start saving an atomes project file
29
30*
31* List of functions:
32
33 int save_this_string (FILE * fp, gchar * string);
34 int save_analysis (FILE * fp, project * this_proj, atomes_analysis * this_analysis, int wid);
35 int save_project (FILE * fp, project * this_proj, int wid);
36
37*/
38
39#include "global.h"
40#include "project.h"
41
50int save_this_string (FILE * fp, gchar * string)
51{
52 int i;
53 if (string)
54 {
55 i = strlen (string);
56 if (i > 0)
57 {
58 i ++;
59 if (fwrite (& i, sizeof(int), 1, fp) != 1) return ERROR_RW;
60 if (fwrite (string, sizeof(char), i, fp) != i) return ERROR_RW;
61 }
62 else
63 {
64 i = 0;
65 if (fwrite (& i, sizeof(int), 1, fp) != 1) return ERROR_RW;
66 }
67 }
68 else
69 {
70 i = 0;
71 if (fwrite (& i, sizeof(int), 1, fp) != 1) return ERROR_RW;
72 }
73 return OK;
74}
75
86int save_analysis (FILE * fp, project * this_proj, atomes_analysis * this_analysis, int wid)
87{
88 int i, j;
89 if (fwrite (& this_analysis -> aid, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
90 if (save_this_string (fp, this_analysis -> name) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
91 if (fwrite (& this_analysis -> avail_ok, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
92 if (fwrite (& this_analysis -> init_ok, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
93 if (fwrite (& this_analysis -> calc_ok, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
94 if (fwrite (& this_analysis -> requires_md, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
95 if (fwrite (& this_analysis -> num_delta, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
96 if (fwrite (& this_analysis -> delta, sizeof(double), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
97 if (fwrite (& this_analysis -> min, sizeof(double), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
98 if (fwrite (& this_analysis -> max, sizeof(double), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
99 if (fwrite (& this_analysis -> fact, sizeof(double), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
100 if (fwrite (& this_analysis -> graph_res, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
101 if (this_analysis -> graph_res)
102 {
103 if (fwrite (& this_analysis -> numc, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
104 if (fwrite (& this_analysis -> c_sets, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
105 if (fwrite (this_analysis -> compat_id, sizeof(int), this_analysis -> c_sets, fp) != this_analysis -> c_sets) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
106 i = (this_analysis -> x_title) ? 1 : 0;
107 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
108 if (this_analysis -> x_title)
109 {
110 if (save_this_string (fp, this_analysis -> x_title) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
111 }
112 if (this_analysis -> curves)
113 {
114 i = 0;
115 for (j=0; j<this_analysis -> numc; j++)
116 {
117 if (this_analysis -> curves[j] -> ndata) i ++;
118 }
119 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
120 if (i)
121 {
122 for (j=0; j<this_analysis -> numc; j++)
123 {
124 if (this_analysis -> curves[j] -> ndata)
125 {
126 if (save_project_curve (fp, this_proj, wid, this_analysis -> aid, j) != OK)
127 {
128 update_error_trace (__FILE__, __func__, __LINE__-2);
129 return ERROR_CURVE;
130 }
131 }
132 }
133 }
134 }
135 else
136 {
137 i = 0;
138 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_ANA);
139 }
140 }
141 return OK;
142}
143
153int save_project (FILE * fp, project * this_proj, int wid)
154{
155 int i, j, k;
156 gchar * ver;
157
158 // First 2 lines for compatibility issues
159 i = 2;
160 j = 9;
161 ver = g_strdup_printf ("%%\n%% project file v-%1d.%1d\n%%\n", i, j);
162 if (save_this_string (fp, ver) != OK)
163 {
164 g_free (ver);
165 return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
166 }
167 g_free (ver);
168 if (save_this_string (fp, this_proj -> name) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
169 if (fwrite (& this_proj -> tfile, sizeof(this_proj -> tfile), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
170 if (this_proj -> tfile > -1)
171 {
172 if (save_this_string (fp, this_proj -> coordfile) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
173 }
174 if (this_proj -> bondfile != NULL)
175 {
176 if (save_this_string (fp, this_proj -> bondfile) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
177 }
178 else
179 {
180 i = -1;
181 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
182 }
183 //Note: we now save the number of analysis available at this version of the project file
184 i = NCALCS;
185 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
186 if (fwrite (& this_proj -> nspec, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
187 if (fwrite (& this_proj -> natomes, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
188 if (fwrite (& this_proj -> steps, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
189 if (fwrite (& this_proj -> cell.pbc, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
190 if (fwrite (& this_proj -> cell.frac, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
191 if (fwrite (& this_proj -> cell.ltype, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
192 i = (this_proj -> cell.npt) ? this_proj -> steps : 1;
193 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
194 for (j=0; j<i; j++)
195 {
196 for (k=0; k<3; k++)
197 {
198 if (fwrite (this_proj -> cell.box[j].vect[k], sizeof(double), 3, fp) != 3) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
199 }
200 if (fwrite (this_proj -> cell.box[j].param[0], sizeof(double), 3, fp) != 3) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
201 if (fwrite (this_proj -> cell.box[j].param[1], sizeof(double), 3, fp) != 3) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
202 }
203 if (fwrite (& this_proj -> cell.crystal, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
204 if (this_proj -> cell.sp_group)
205 {
206 if (fwrite (& this_proj -> cell.sp_group -> id, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
207 if (save_this_string (fp, this_proj -> cell.sp_group -> bravais) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
208 if (save_this_string (fp, this_proj -> cell.sp_group -> hms) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
209 if (save_this_string (fp, this_proj -> cell.sp_group -> setting) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
210 }
211 else
212 {
213 i = 0;
214 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
215 }
216
217 if (fwrite (& this_proj -> run, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
218 if (fwrite (& this_proj -> initgl, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
219 if (fwrite (this_proj -> modelgl -> pixels, sizeof(int), 2, fp) != 2) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
220 // Analysis data for k-points sampling
221 for (i=0; i<2; i++)
222 {
223 if (fwrite (this_proj -> sk_advanced[i], sizeof(double), 2, fp) != 2) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
224 }
225 // Next lines are calculation data related to rings and chains statistics
226 if (fwrite (this_proj -> rsearch, sizeof(int), 2, fp) != 2) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
227 for (i=0; i<5; i++)
228 {
229 if (fwrite (this_proj -> rsparam[i], sizeof(int), 6, fp) != 6) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
230 if (fwrite (this_proj -> rsdata[i], sizeof(double), 5, fp) != 5) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
231 }
232 if (fwrite (& this_proj -> csearch, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
233 if (fwrite (this_proj -> csparam, sizeof(int), 7, fp) != 7) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
234 if (fwrite (this_proj -> csdata, sizeof(double), 2, fp) != 2) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
235 // Time unit for dynamical calculations, delta t and steps are stored in MSD analysis
236 if (fwrite (& this_proj -> tunit, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
237 if (this_proj -> steps)
238 {
239 if (fwrite (& this_proj -> skt_corr_threshold, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
240 if (fwrite (& this_proj -> skt_all_sets, sizeof(gboolean), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
241 if (! this_proj -> skt_all_sets)
242 {
243 i = (this_proj -> skt_step_id) ? this_proj -> skt_n_data_sets : 0;
244 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
245 if (i)
246 {
247 if (fwrite (this_proj -> skt_step_id, sizeof(int), this_proj -> skt_n_data_sets, fp) != this_proj -> skt_n_data_sets) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
248 }
249 }
250 i = (this_proj -> sqw_q_id) ? this_proj -> sqw_n_data_sets : 0;
251 if (fwrite (& i, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
252 if (i)
253 {
254 if (fwrite (this_proj -> sqw_q_id, sizeof(double), this_proj -> sqw_n_data_sets, fp) != this_proj -> sqw_n_data_sets) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
255 }
256 if (fwrite (& this_proj -> sqw_freq, sizeof(int), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
257 }
258 if (this_proj -> natomes == 0 || this_proj -> nspec == 0)
259 {
260 // error
261 return signal_error (__FILE__, __func__, __LINE__, ERROR_NO_WAY);
262 }
263 else
264 {
265 for (i=0; i<this_proj -> nspec; i++)
266 {
267 if (save_this_string (fp, this_proj -> chemistry -> label[i]) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
268 if (save_this_string (fp, this_proj -> chemistry -> element[i]) != OK) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
269 }
270 if (fwrite (this_proj -> chemistry -> nsps, sizeof(int), this_proj -> nspec, fp) != this_proj -> nspec) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
271 if (fwrite (this_proj -> chemistry -> formula, sizeof(int), this_proj -> nspec, fp) != this_proj -> nspec) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
272 for (i=0; i<CHEM_PARAMS; i++)
273 {
274 if (fwrite (this_proj -> chemistry -> chem_prop[i], sizeof(double), this_proj -> nspec, fp) != this_proj -> nspec) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
275 }
276 if (fwrite (& this_proj -> chemistry -> grtotcutoff, sizeof(double), 1, fp) != 1) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
277 for (i=0; i<this_proj -> nspec; i++)
278 {
279 if (fwrite (this_proj -> chemistry -> cutoffs[i], sizeof(double), this_proj -> nspec, fp) != this_proj -> nspec) return signal_error (__FILE__, __func__, __LINE__, ERROR_PROJECT);
280 }
281 for (i=0; i<this_proj -> steps; i++)
282 {
283 for (j=0; j<this_proj -> natomes; j++)
284 {
285 if (save_atom_a (fp, this_proj, i, j) != OK)
286 {
287 update_error_trace (__FILE__, __func__, __LINE__-2);
288 return ERROR_ATOM_A;
289 }
290 }
291 }
292 if (this_proj -> run)
293 {
294 if (this_proj -> analysis)
295 {
296 for (i=0; i<NCALCS; i++)
297 {
298 if (this_proj -> analysis[i])
299 {
300 if (save_analysis (fp, this_proj, this_proj -> analysis[i], wid) != OK)
301 {
302 update_error_trace (__FILE__, __func__, __LINE__-2);
303 return ERROR_ANA;
304 }
305 }
306 }
307 }
308 if (this_proj -> initgl)
309 {
310 if (fwrite (& this_proj -> modelgl -> bonding, sizeof(gboolean), 1, fp) != 1) return ERROR_COORD;
311 if (fwrite (this_proj -> modelgl -> adv_bonding, sizeof(gboolean), 2, fp) != 2) return ERROR_COORD;
312 // for (i=0; i<10; i++) g_debug ("SAVING :: i= %d, this_proj -> coord -> totcoord[%d]= %d", i, i, this_proj -> coord -> totcoord[i]);
313 if (fwrite (this_proj -> coord -> totcoord, sizeof(int), 10, fp) != 10) return ERROR_COORD;
314
315 // Save molecule
316 if ((this_proj -> natomes > ATOM_LIMIT || this_proj -> steps > STEP_LIMIT) && this_proj -> modelgl -> adv_bonding[1])
317 {
318 if (save_mol (fp, this_proj) != OK)
319 {
320 update_error_trace (__FILE__, __func__, __LINE__-2);
321 return ERROR_MOL;
322 }
323 }
324 // saving bonding info
325 if (save_bonding (fp, this_proj) != OK)
326 {
327 update_error_trace (__FILE__, __func__, __LINE__-2);
328 return ERROR_COORD;
329 }
330 // saving glwin info
331 if (save_opengl_image (fp, this_proj, this_proj -> modelgl -> anim -> last -> img, this_proj -> nspec) != OK)
332 {
333 update_error_trace (__FILE__, __func__, __LINE__-2);
334 return ERROR_IMAGE;
335 }
336
337 if (save_dlp_field_data (fp, this_proj) != OK)
338 {
339 update_error_trace (__FILE__, __func__, __LINE__-2);
340 return ERROR_FIELD;
341 }
342 if (save_lmp_field_data (fp, this_proj) != OK)
343 {
344 update_error_trace (__FILE__, __func__, __LINE__-2);
345 return ERROR_FIELD;
346 }
347
348 for (i=0; i<2; i++)
349 {
350 if (save_cpmd_data (fp, i, this_proj) != OK)
351 {
352 update_error_trace (__FILE__, __func__, __LINE__-2);
353 return ERROR_QM;
354 }
355 }
356 for (i=0; i<2; i++)
357 {
358 if (save_cp2k_data (fp, i, this_proj) != OK)
359 {
360 update_error_trace (__FILE__, __func__, __LINE__-2);
361 return ERROR_QM;
362 }
363 }
364 }
365 }
366 }
367#ifdef DEBUG
368// debugioproj (this_proj, "WRITE");
369#endif
370 return OK;
371}
integer(kind=c_int) function bonding(scf, sbf, adv, bdist, bmin, delt_ij, sfil)
Definition bonds.F90:22
integer(kind=c_int) function chemistry()
Definition chemistry.F90:22
FILE * fp
Global variable declarations Global convenience function declarations Global data structure defin...
int signal_error(const char *file, const char *func, int error_line, int error_id)
Definition callbacks.c:182
#define ATOM_LIMIT
atom number limit to compute fragment(s) and molecule(s) analysis automatically
#define ERROR_RW
Definition global.h:296
#define ERROR_CURVE
Definition global.h:298
#define STEP_LIMIT
Definition global.h:293
#define ERROR_PROJECT
Definition global.h:297
#define ERROR_FIELD
#define CHEM_PARAMS
Definition global.h:315
#define ERROR_IMAGE
Definition global.h:299
#define ERROR_ATOM_A
Definition global.h:300
#define OK
Definition global.h:295
#define min(a, b)
Definition global.h:93
#define ERROR_COORD
Definition global.h:304
#define ERROR_MOL
Definition global.h:307
#define ERROR_ANA
Definition global.h:308
void update_error_trace(const char *file, const char *func, int trace_line)
Definition callbacks.c:200
#define NCALCS
#define ERROR_QM
Definition global.h:309
#define ERROR_NO_WAY
Definition global.h:303
#define max(a, b)
Definition global.h:92
Function declarations for reading atomes project file Function declarations for saving atomes proje...
int save_lmp_field_data(FILE *fp, project *this_proj)
save LAMMPS force field data to file
Definition save_field.c:540
int save_bonding(FILE *fp, project *this_proj)
save bonding information to file
Definition save_bond.c:50
int save_atom_a(FILE *fp, project *this_proj, int s, int a)
save atom data to file (a)
Definition save_opengl.c:57
int save_this_string(FILE *fp, gchar *string)
save string to file
Definition save_p.c:50
int save_opengl_image(FILE *fp, project *this_proj, image *img, int sid)
save OpenGL image properties to file
int save_cp2k_data(FILE *fp, int cid, project *this_proj)
save CP2K data to file
Definition save_qm.c:189
int save_project_curve(FILE *fp, project *this_proj, int wid, int rid, int cid)
save project curve to file
Definition save_curve.c:75
int save_mol(FILE *fp, project *this_proj)
save molecule information to file
Definition save_mol.c:106
int save_dlp_field_data(FILE *fp, project *this_proj)
save force field data to file
Definition save_field.c:447
int save_cpmd_data(FILE *fp, int cid, project *this_proj)
save CPMD data to file
Definition save_qm.c:109
int save_project(FILE *fp, project *this_proj, int wid)
save project to file
Definition save_p.c:153
int save_analysis(FILE *fp, project *this_proj, atomes_analysis *this_analysis, int wid)
saving analysis parameter(s) and result(s) to project file
Definition save_p.c:86
int save_this_string(FILE *fp, gchar *string)
save string to file
Definition save_p.c:50
gboolean skt_all_sets
int element
Definition w_periodic.c:61
atom_search * csearch
Definition w_search.c:2719
GtkWidget * img
Definition workspace.c:70