atomes 1.3.1
atomes: an atomic scale modeling tool box
Loading...
Searching...
No Matches
read_isaacs.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
22
23/*
24* This file: 'read_isaacs.c'
25*
26* Contains:
27*
28
29 - The functions to read an ISAACS XML file
30 - The functions to write an ISAACS XML file
31
32*
33* List of functions:
34
35 int XmlwriterFilename (const char *uri);
36 int write_xml (const char * filetosave);
37 int get_spec_from_data (xmlChar * data);
38 int setprop (xmlNodePtr pnode);
39 int testopening (char * tdata, char * tfichier);
40 int setchemistry (xmlNodePtr xsnode);
41 int setbox (xmlNodePtr boxnode);
42 int setpbc (xmlNodePtr pbcnode);
43 int setcutoffs (xmlNodePtr cutnode);
44 int settime(xmlNodePtr timenode);
45 int check_xml (const char * filetocheck);
46
47 size_t strfind (char * ida);
48
49 gboolean file_exists(const char * filename);
50
51 gchar * open_xml (const char * filetoread);
52
53 xmlNodePtr findnode (xmlNodePtr startnode, char * nname);
54
55*/
56
57#include <libxml/encoding.h>
58#include <libxml/xmlwriter.h>
59#include <libxml/xmlreader.h>
60#include <libxml/parser.h>
61
62#include "global.h"
63#include "interface.h"
64#include "callbacks.h"
65
66extern int open_coordinate_file (int id);
67
68#define NFORMATS 7
69
70char * reg_types[NFORMATS] = {"XYZ file",
71 "Chem3D file",
72 "CPMD trajectory",
73 "VASP trajectory",
74 "multiple XYZ file",
75 "multiple Chem3D file",
76 "PDB file"};
77
85size_t strfind (char * ida)
86{
87 size_t a, b, c;
88
89 a = strlen(ida);
90 b = 0;
91 for ( c=0 ; c < a ; c++)
92 {
93 if (ida[c] != ' ')
94 {
95 b ++;
96 }
97 }
98 return b;
99}
100
108int XmlwriterFilename (const char * uri)
109{
110 int rc;
111 int i, j;
112 xmlTextWriterPtr writer;
113
114 char * val;
115 char * ncut;
116 size_t lgt;
117
118 /* Create a new XmlWriter for uri, with no compression. */
119 writer = xmlNewTextWriterFilename(uri, 0);
120 if (writer == NULL) return 0;
121 rc = xmlTextWriterSetIndent(writer, 1);
122 if (rc < 0) return 0;
123 /* Start the document with the xml default for the version,
124 * encoding MY_ENCODING and the default for the standalone
125 * declaration. */
126 rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL);
127 if (rc < 0) return 0;
128
129 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" I.S.A.A.C.S. v1.0 XML file "));
130 if (rc < 0) return 0;
131 rc = xmlTextWriterStartElement(writer, BAD_CAST "isaacs-xml");
132 if (rc < 0) return 0;
133
134 /* Start the "data" element. Since this is the first
135 * element, this will be the root element of the document.
136 <data>
137 <type> </type>
138 <file> <file>
139 </data>*/
140
141 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" Data format and file containing the configuration(s) "));
142 if (rc < 0) return 0;
143 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"data");
144 if (rc < 0) return 0;
145 int reg = 0;
146 if (active_project -> tfile < 2)
147 {
148 reg = (active_project -> steps > 1) ? 4 : 0;
149 }
150 else if (active_project -> tfile == 2)
151 {
152 reg = (active_project -> steps > 1) ? 5 : 1;
153 }
154 else if (active_project -> tfile < 5)
155 {
156 reg = 2;
157 }
158 else if (active_project -> tfile < 6)
159 {
160 reg = 3;
161 }
162 else if (active_project -> tfile < 9)
163 {
164 reg = 6;
165 }
166 else
167 {
168 return 0;
169 }
170
171 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"type", "%s", reg_types[reg]);
172 if (rc < 0) return 0;
173 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"file", "%s", active_project -> coordfile);
174 if (rc < 0) return 0;
175 rc = xmlTextWriterEndElement(writer);
176 if (rc < 0) return 0;
177
178 /* Start the "chemistry" element.
179 <chemistry>
180 <atoms> </atoms>
181 <species number=" ">
182 <active_project -> label id="0"> </active_project -> label>
183 ...
184 </species>
185 <element symbol="">
186 <name> </name>
187 <z> </z>
188 <mass> </mass>
189 <rad> </rad>
190 <radius> </radius>
191 <nscatt> </nscatt>
192 <xscatt> </xscatt>
193 </element>
194 </chemistry> */
195
196 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" Chemistry information "));
197 if (rc < 0) return 0;
198 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"chemistry");
199 if (rc < 0) return 0;
200 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"atoms", "%d", active_project -> natomes);
201 if (rc < 0) return 0;
202 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"species");
203 if (rc < 0) return 0;
204 val = g_strdup_printf("%d",active_project -> nspec);
205 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST (const xmlChar *)"number", BAD_CAST val);
206 g_free (val);
207 if (rc < 0) return 0;
208 for ( i=0 ; i<active_project -> nspec ; i++)
209 {
210 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"label");
211 if (rc < 0) return 0;
212 val = g_strdup_printf("%d",i);
213 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST (const xmlChar *)"id", BAD_CAST val);
214 g_free (val);
215 if (rc < 0) return 0;
216 rc = xmlTextWriterWriteFormatString (writer, "%s", active_chem -> label[i]);
217 if (rc < 0) return 0;
218 rc = xmlTextWriterEndElement(writer);
219 if (rc < 0) return 0;
220 }
221 rc = xmlTextWriterEndElement(writer);
222 if (rc < 0) return 0;
223 for ( i=0 ; i<active_project -> nspec ; i++)
224 {
225 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"element");
226 if (rc < 0) return 0;
227 rc = xmlTextWriterWriteAttribute(writer, BAD_CAST (const xmlChar *)"symbol", BAD_CAST active_chem -> label[i]);
228 if (rc < 0) return 0;
229 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"name", "%s ", active_chem -> element[i]);
230 if (rc < 0) return 0;
231 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"z", "%d", (int)active_chem -> chem_prop[CHEM_Z][i]);
232 if (rc < 0) return 0;
233 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"mass", "%f", active_chem -> chem_prop[CHEM_M][i]);
234 if (rc < 0) return 0;
235 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"rad", "%d", 0);
236 if (rc < 0) return 0;
237 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"radius", "%f", active_chem -> chem_prop[CHEM_R][i]);
238 if (rc < 0) return 0;
239 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"nscatt", "%f", active_chem -> chem_prop[CHEM_N][i]);
240 if (rc < 0) return 0;
241 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"xscatt", "%f", active_chem -> chem_prop[CHEM_Z][i]);
242 if (rc < 0) return 0;
243 rc = xmlTextWriterEndElement(writer);
244 if (rc < 0) return 0;
245 }
246 rc = xmlTextWriterEndElement(writer);
247 if (rc < 0) return 0;
248
249 /* Start the "box" element.
250 <box>
251 <edges>
252 <a></a>
253 <b></b>
254 <c></c>
255 </edges>
256 <angles> </angles>
257 <vectors>
258 <a.x></a.x>
259 <a.y></a.y>
260 <a.z></a.z>
261 <b.x></b.x>
262 <b.y></b.y>
263 <b.z></b.z>
264 <c.x></c.x>
265 <c.y></c.y>
266 <c.z></c.z>
267 </vectors>
268 </box> */
269
270 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" Box information "));
271 if (rc < 0) return 0;
272 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"box");
273 if (rc < 0) return 0;
274 if (active_cell -> ltype == 1)
275 {
276 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"edges");
277 if (rc < 0) return 0;
278 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a", "%f", active_box -> param[0][0]);
279 if (rc < 0) return 0;
280 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b", "%f", active_box -> param[0][1]);
281 if (rc < 0) return 0;
282 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c", "%f", active_box -> param[0][2]);
283 if (rc < 0) return 0;
284 rc = xmlTextWriterEndElement(writer);
285 if (rc < 0) return 0;
286 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"angles");
287 if (rc < 0) return 0;
288 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"alpha", "%f", active_box -> param[1][0]);
289 if (rc < 0) return 0;
290 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"beta", "%f", active_box -> param[1][1]);
291 if (rc < 0) return 0;
292 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"gamma", "%f", active_box -> param[1][2]);
293 if (rc < 0) return 0;
294 rc = xmlTextWriterEndElement(writer);
295 if (rc < 0) return 0;
296 }
297 else
298 {
299 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"edges");
300 if (rc < 0) return 0;
301 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a", "%f", 0.0);
302 if (rc < 0) return 0;
303 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b", "%f", 0.0);
304 if (rc < 0) return 0;
305 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c", "%f", 0.0);
306 if (rc < 0) return 0;
307 rc = xmlTextWriterEndElement(writer);
308 if (rc < 0) return 0;
309 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"angles");
310 if (rc < 0) return 0;
311 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"alpha", "%f", 0.0);
312 if (rc < 0) return 0;
313 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"beta", "%f", 0.0);
314 if (rc < 0) return 0;
315 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"gamma", "%f", 0.0);
316 if (rc < 0) return 0;
317 rc = xmlTextWriterEndElement(writer);
318 if (rc < 0) return 0;
319 }
320 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"vectors");
321 if (rc < 0) return 0;
322 if (active_cell -> ltype == 2)
323 {
324 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a.x", "%f", active_box -> vect[0][0]);
325 if (rc < 0) return 0;
326 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a.y", "%f", active_box -> vect[0][1]);
327 if (rc < 0) return 0;
328 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a.z", "%f", active_box -> vect[0][2]);
329 if (rc < 0) return 0;
330 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b.x", "%f", active_box -> vect[1][0]);
331 if (rc < 0) return 0;
332 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b.y", "%f", active_box -> vect[1][1]);
333 if (rc < 0) return 0;
334 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b.z", "%f", active_box -> vect[1][2]);
335 if (rc < 0) return 0;
336 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c.x", "%f", active_box -> vect[2][0]);
337 if (rc < 0) return 0;
338 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c.y", "%f", active_box -> vect[2][1]);
339 if (rc < 0) return 0;
340 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c.z", "%f", active_box -> vect[2][2]);
341 if (rc < 0) return 0;
342 }
343 else
344 {
345 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a.x", "%f", 0.0);
346 if (rc < 0) return 0;
347 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a.y", "%f", 0.0);
348 if (rc < 0) return 0;
349 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"a.z", "%f", 0.0);
350 if (rc < 0) return 0;
351 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b.x", "%f", 0.0);
352 if (rc < 0) return 0;
353 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b.y", "%f", 0.0);
354 if (rc < 0) return 0;
355 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"b.z", "%f", 0.0);
356 if (rc < 0) return 0;
357 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c.x", "%f", 0.0);
358 if (rc < 0) return 0;
359 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c.y", "%f", 0.0);
360 if (rc < 0) return 0;
361 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"c.z", "%f", 0.0);
362 if (rc < 0) return 0;
363 }
364 rc = xmlTextWriterEndElement(writer);
365 if (rc < 0) return 0;
366 rc = xmlTextWriterEndElement(writer);
367 if (rc < 0) return 0;
368 /* Start the "pbc" element.
369 <pbc>
370 <apply></apply>
371 <fractional></fractional>
372 <fractype></fractype>
373 </pbc>
374 */
375
376 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" PBC information "));
377 if (rc < 0) return 0;
378 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"pbc");
379 if (rc < 0) return 0;
380 if (active_cell -> pbc)
381 {
382 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"apply", "%s", "TRUE");
383 }
384 else
385 {
386 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"apply", "%s", "FALSE");
387 }
388 if (rc < 0) return 0;
389 if (active_cell -> frac)
390 {
391 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"fractional", "%s", "TRUE");
392 }
393 else
394 {
395 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"fractional", "%s", "FALSE");
396 }
397 if (rc < 0) return 0;
398 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"fractype", "%d", active_cell -> frac);
399 if (rc < 0) return 0;
400 rc = xmlTextWriterEndElement(writer);
401 if (rc < 0) return 0;
402
403 /* Start the "cutoffs" element.
404 <cutoffs>
405 <total></total>
406 <partials>
407 <active_chem -> label[spi]-active_chem -> label[spj]></active_chem -> label[spi]-active_chem -> label[spj]>
408 </partials>
409 </cutoffs>
410 */
411
412 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" Cutoffs information "));
413 if (rc < 0) return 0;
414 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"cutoffs");
415 if (rc < 0) return 0;
416 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"total", "%f", active_chem -> grtotcutoff);
417 if (rc < 0) return 0;
418 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"partials");
419 if (rc < 0) return 0;
420 for ( i=0 ; i<active_project -> nspec ; i++ )
421 {
422 for ( j=0 ; j<active_project -> nspec ; j++ )
423 {
424 lgt = 10;
425 lgt += strfind (active_chem -> label[i]) + strlen("-") + strfind(active_chem -> label[j]);
426 ncut = g_malloc0(lgt*sizeof*ncut);
427 strncpy (ncut, active_chem -> label[i], strfind(active_chem -> label[i]));
428 strcat (ncut, "-");
429 strncat (ncut, active_chem -> label[j], strfind(active_chem -> label[j]));
430 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST ncut, "%f", active_chem -> cutoffs[i][j]);
431 if (rc < 0) return 0;
432 g_free (ncut);
433 ncut=NULL;
434 }
435 }
436 rc = xmlTextWriterEndElement(writer);
437 if (rc < 0) return 0;
438 rc = xmlTextWriterEndElement(writer);
439 if (rc < 0) return 0;
440
441 /* Start the "time series" element.
442 <time-series>
443 <dt></dt>
444 <unit></unit>
445 <ndt></ndt>
446 </time-series>
447 */
448
449 if (active_project -> steps > 1)
450 {
451 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" Time series "));
452 if (rc < 0) return 0;
453 rc = xmlTextWriterStartElement(writer, BAD_CAST (const xmlChar *)"time-series");
454 if (rc < 0) return 0;
455 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"dt", "%f", 0.0);
456 if (rc < 0) return 0;
457 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"unit", "t [%s]", untime[active_project -> tunit]);
458 if (rc < 0) return 0;
459 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"ndt", "%d", 1);
460 if (rc < 0) return 0;
461 rc = xmlTextWriterEndElement(writer);
462 if (rc < 0) return 0;
463 }
464
465 /* Start the "project" element.
466 <project></project>
467 */
468
469 rc = xmlTextWriterWriteComment(writer, (const xmlChar *)_(" Apply project "));
470 if (rc < 0) return 0;
471 if (active_project -> run)
472 {
473 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"project", "%s", "TRUE");
474 }
475 else
476 {
477 rc = xmlTextWriterWriteFormatElement(writer, BAD_CAST (const xmlChar *)"project", "%s", "FALSE");
478 }
479 if (rc < 0) return 0;
480// Closing "</isaacs-xml>"
481 rc = xmlTextWriterEndElement(writer);
482 if (rc < 0) return 0;
483
484 rc = xmlTextWriterEndDocument(writer);
485 if (rc < 0) return 0;
486
487 xmlFreeTextWriter(writer);
488 return 1;
489}
490
498int write_xml (const char * filetosave)
499{
500 /* first, the file version */
501 int res=XmlwriterFilename(filetosave);
502 /*
503 * Cleanup function for the XML library.
504 */
505 xmlCleanupParser();
506 /*
507 * this is to debug memory for regression tests
508 */
509 xmlMemoryDump();
510 return res;
511}
512
520gboolean file_exists(const char * filename)
521{
522 FILE * file;
523 if ((file = fopen(filename, "r")))
524 {
525 fclose(file);
526 return TRUE;
527 }
528 return FALSE;
529}
530
539xmlNodePtr findnode (xmlNodePtr startnode, char * nname)
540{
541 xmlNodePtr tmp;
542
543 tmp = startnode;
544 while (g_strcmp0 ((char *)(tmp -> name), nname) != 0)
545 {
546 if (tmp -> next == NULL)
547 {
548 return NULL;
549 }
550 tmp = tmp -> next;
551 }
552 return tmp;
553}
554
562int get_spec_from_data (xmlChar * data)
563{
564 int i;
565 for ( i=0 ; i<active_project -> nspec ; i++)
566 {
567 if (g_strcmp0 (exact_name((char *)data), active_chem -> label[i]) == 0)
568 {
569 return i;
570 }
571 }
572 return -1;
573}
574
582int setprop (xmlNodePtr pnode)
583{
584 int i, res;
585 xmlAttrPtr pspec;
586 xmlNodePtr idn, ptnode, ptn;
587 xmlChar * data;
588
589 ptnode=pnode;
590 for ( i=0 ; i<active_project -> nspec ; i++)
591 {
592 ptnode = findnode(ptnode, "element");
593 if (ptnode == NULL)
594 {
595 res=6;
596 goto pend;
597 }
598 pspec = ptnode -> properties;
599 idn = pspec -> children;
600 data = xmlNodeGetContent(idn);
601 res = get_spec_from_data(data);
602 xmlFree (data);
603 if (res < 0)
604 {
605 res=6;
606 goto pend;
607 }
608 ptnode = ptnode -> children;
609 ptn = findnode(ptnode, "name");
610 if (ptn == NULL)
611 {
612 res=6;
613 goto pend;
614 }
615 data = xmlNodeGetContent(ptn);
616 if (g_strcmp0 (exact_name((char *)data), active_chem -> element[res]) != 0)
617 {
618 xmlFree (data);
619 res=6;
620 goto pend;
621 }
622 xmlFree (data);
623 ptn = findnode(ptnode, "z");
624 if (ptn == NULL)
625 {
626 res=6;
627 goto pend;
628 }
629 data = xmlNodeGetContent(ptn);
630 if ((int)active_chem -> chem_prop[CHEM_Z][res] != (int)string_to_double ((gpointer)data))
631 {
632 xmlFree (data);
633 res=6;
634 goto pend;
635 }
636 xmlFree (data);
637 ptn = findnode(ptnode, "mass");
638 if (ptn == NULL)
639 {
640 res=6;
641 goto pend;
642 }
643 data = xmlNodeGetContent(ptn);
644 active_chem -> chem_prop[CHEM_M][res] = string_to_double ((gpointer)data);
645 xmlFree (data);
646 ptn = findnode(ptnode, "rad");
647 if (ptn == NULL)
648 {
649 res=6;
650 goto pend;
651 }
652 data = xmlNodeGetContent(ptn);
653 // val = string_to_double ((gpointer)data);
654 xmlFree (data);
655 ptn = findnode(ptnode, "radius");
656 if (ptn == NULL)
657 {
658 res=6;
659 goto pend;
660 }
661 data = xmlNodeGetContent(ptn);
662 active_chem -> chem_prop[CHEM_R][res] = string_to_double ((gpointer)data);
663 xmlFree (data);
664 ptn = findnode(ptnode, "nscatt");
665 if (ptn == NULL)
666 {
667 res=6;
668 goto pend;
669 }
670 data = xmlNodeGetContent(ptn);
671 active_chem -> chem_prop[CHEM_N][res] = string_to_double ((gpointer)data);
672 xmlFree (data);
673 ptn = findnode(ptnode, "xscatt");
674 if (ptn == NULL)
675 {
676 res=6;
677 goto pend;
678 }
679 data = xmlNodeGetContent(ptn);
680 active_chem -> chem_prop[CHEM_X][res] = string_to_double ((gpointer)data);
681 xmlFree (data);
682 ptnode = ptnode -> parent;
683 ptnode = ptnode -> next;
684 }
685 res = 0;
686pend:
687 return res;
688}
689
698int testopening (char * tdata, char * tfichier)
699{
700 int i, j;
701 char * err;
702
703 j = -1;
704 for ( i=0 ; i<NFORMATS ; i ++ )
705 {
706 if (g_strcmp0 (reg_types[i], tdata) == 0)
707 {
708 j = i;
709 break;
710 }
711 }
712 if (j > -1)
713 {
714 if (file_exists (tfichier))
715 {
716 switch (j)
717 {
718 case 0:
719 active_project -> tfile = 0;
720 break;
721 case 1:
722 active_project -> tfile = 2;
723 break;
724 case 2:
725 active_project -> tfile = 3;
726 break;
727 case 3:
728 active_project -> tfile = 5;
729 break;
730 case 4:
731 active_project -> tfile = 0;
732 break;
733 case 5:
734 active_project -> tfile = 2;
735 break;
736 case 6:
737 active_project -> tfile = 7;
738 break;
739 }
740 active_project -> coordfile = tfichier;
741 if (open_coordinate_file (active_project -> tfile) == 0)
742 {
743 return 0;
744 }
745 else
746 {
747 return 11;
748 }
749 }
750 else
751 {
752 j=4;
753 err=g_strdup_printf(_("The %s\n %s\n from the XML file does not exist\n"), tdata, tfichier);
754 show_error (err, 0, MainWindow);
755 g_free (err);
756 return j;
757 }
758 }
759 else
760 {
761 show_error (_("Unknown data format in XML file\n"), 0, MainWindow);
762 j=4;
763 return j;
764 }
765}
766
774int setchemistry (xmlNodePtr xsnode)
775{
776 xmlNodePtr idn, cs, xnode;
777 xmlAttrPtr xspec;
778 xmlChar * data;
779 int ats, res, i;
780 double val;
781
782 xnode = xsnode -> children;
783 cs = findnode(xnode, "atoms");
784 if (cs == NULL)
785 {
786 res=5;
787 goto xend;
788 }
789 data = xmlNodeGetContent(cs);
790 val = string_to_double ((gpointer)data);
791 ats = val;
792 if (ats != active_project -> natomes)
793 {
794 show_warning (_("The number of atoms in the XML file\n"
795 "is not the same that the number of atoms\n"
796 "in the file containing the coordinates.\n"
797 "Other information from the XML file\n"
798 "will be ignored\n"), MainWindow);
799 res=5;
800 goto xend;
801 }
802 else
803 {
804 cs = findnode(xnode, "species");
805 if (cs == NULL)
806 {
807 res=5;
808 goto xend;
809 }
810 xspec = cs -> properties;
811 idn = xspec -> children;
812 data = xmlNodeGetContent(idn);
813 val = string_to_double ((gpointer)data);
814 ats = val;
815 if (ats != active_project -> nspec)
816 {
817 show_warning (_("The number of chemical species in the XML file\n"
818 "is not the same that the number of chemical species\n"
819 "in the file that contains the atomic coordinates.\n"), MainWindow);
820 res = 5;
821 goto xend;
822 }
823 else
824 {
825 cs = cs -> children;
826 ats = 0;
827 for ( i=0 ; i<active_project -> nspec ; i++)
828 {
829 cs = findnode(cs, "label");
830 if (cs == NULL)
831 {
832 res=5;
833 goto xend;
834 }
835 data = xmlNodeGetContent(cs);
836 res = get_spec_from_data (data);
837 if (res < 0)
838 {
839 res=5;
840 goto xend;
841 }
842 else
843 {
844 ats ++;
845 }
846 }
847 if (ats != active_project -> nspec)
848 {
849 res=5;
850 goto xend;
851 }
852 xnode = xsnode -> children;
853 res = setprop (xnode);
854 }
855 }
856
857xend:
858 return res;
859}
860
868int setbox (xmlNodePtr boxnode)
869{
870 int box;
871 xmlNodePtr bnode, ba, bb;
872 xmlChar * data;
873 double val;
874
875 bnode = boxnode -> children;
876 ba=findnode(bnode, "edges");
877 if (ba == NULL)
878 {
879 box=7;
880 goto bend;
881 }
882 ba = ba -> children;
883 bb=findnode(ba, "a");
884 if (bb == NULL)
885 {
886 box=7;
887 goto bend;
888 }
889 data = xmlNodeGetContent(bb);
890 val = string_to_double ((gpointer)data);
891 active_box -> param[0][0]= val;
892 bb=findnode(ba, "b");
893 if (bb == NULL)
894 {
895 box=7;
896 goto bend;
897 }
898 data = xmlNodeGetContent(bb);
899 val = string_to_double ((gpointer)data);
900 active_box -> param[0][1]= val;
901 bb=findnode(ba, "c");
902 if (bb == NULL)
903 {
904 box=7;
905 goto bend;
906 }
907 data = xmlNodeGetContent(bb);
908 val = string_to_double ((gpointer)data);
909 active_box -> param[0][2]= val;
910 ba=findnode(bnode, "angles");
911 if (ba == NULL)
912 {
913 box=7;
914 goto bend;
915 }
916 ba = ba -> children;
917 bb=findnode(ba, "alpha");
918 if (bb == NULL)
919 {
920 box=7;
921 goto bend;
922 }
923 data = xmlNodeGetContent(bb);
924 val = string_to_double ((gpointer)data);
925 active_box -> param[1][0] = val;
926 bb=findnode(ba, "beta");
927 if (bb == NULL)
928 {
929 box=7;
930 goto bend;
931 }
932 data = xmlNodeGetContent(bb);
933 val = string_to_double ((gpointer)data);
934 active_box -> param[1][1]= val;
935 bb=findnode(ba, "gamma");
936 if (bb == NULL)
937 {
938 box=7;
939 goto bend;
940 }
941 if (active_box -> param[0][0] != 0.0 && active_box -> param[0][1] != 0.0 && active_box -> param[0][2] != 0.0
942 && active_box -> param[1][0] != 0.0 && active_box -> param[1][1] != 0.0 && active_box -> param[1][2] != 0.0)
943 {
944 active_cell -> ltype = 1;
945 }
946 else
947 {
948 active_cell -> ltype = 0;
949 }
950 data = xmlNodeGetContent(bb);
951 val = string_to_double ((gpointer)data);
952 active_box -> param[1][2]= val;
953 ba=findnode(bnode, "vectors");
954 if (ba == NULL)
955 {
956 box=7;
957 goto bend;
958 }
959 ba = ba -> children;
960 bb=findnode(ba, "a.x");
961 if (bb == NULL)
962 {
963 box=7;
964 goto bend;
965 }
966 data = xmlNodeGetContent(bb);
967 val = string_to_double ((gpointer)data);
968 active_box -> vect[0][0]= val;
969 bb=findnode(ba, "a.y");
970 if (bb == NULL)
971 {
972 box=7;
973 goto bend;
974 }
975 data = xmlNodeGetContent(bb);
976 val = string_to_double ((gpointer)data);
977 active_box -> vect[0][1]= val;
978 bb=findnode(ba, "a.z");
979 if (bb == NULL)
980 {
981 box=7;
982 goto bend;
983 }
984 data = xmlNodeGetContent(bb);
985 val = string_to_double ((gpointer)data);
986 active_box -> vect[0][2]= val;
987 bb=findnode(ba, "b.x");
988 if (bb == NULL)
989 {
990 box=7;
991 goto bend;
992 }
993 data = xmlNodeGetContent(bb);
994 val = string_to_double ((gpointer)data);
995 active_box -> vect[1][0]= val;
996 bb=findnode(ba, "b.y");
997 if (bb == NULL)
998 {
999 box=7;
1000 goto bend;
1001 }
1002 data = xmlNodeGetContent(bb);
1003 val = string_to_double ((gpointer)data);
1004 active_box -> vect[1][1]= val;
1005 bb=findnode(ba, "b.z");
1006 if (bb == NULL)
1007 {
1008 box=7;
1009 goto bend;
1010 }
1011 data = xmlNodeGetContent(bb);
1012 val = string_to_double ((gpointer)data);
1013 active_box -> vect[1][2]= val;
1014 bb=findnode(ba, "c.x");
1015 if (bb == NULL)
1016 {
1017 box=7;
1018 goto bend;
1019 }
1020 data = xmlNodeGetContent(bb);
1021 val = string_to_double ((gpointer)data);
1022 active_box -> vect[2][0]= val;
1023 bb=findnode(ba, "c.y");
1024 if (bb == NULL)
1025 {
1026 box=7;
1027 goto bend;
1028 }
1029 data = xmlNodeGetContent(bb);
1030 val = string_to_double ((gpointer)data);
1031 active_box -> vect[2][1]= val;
1032 bb=findnode(ba, "c.z");
1033 if (bb == NULL)
1034 {
1035 box=7;
1036 goto bend;
1037 }
1038 data = xmlNodeGetContent(bb);
1039 val = string_to_double ((gpointer)data);
1040 active_box -> vect[2][2]= val;
1041
1042 if (active_box -> vect[0][0] != 0.0 && active_box -> vect[0][1] != 0.0 && active_box -> vect[0][2] != 0.0
1043 && active_box -> vect[1][0] != 0.0 && active_box -> vect[1][1] != 0.0 && active_box -> vect[1][2] != 0.0
1044 && active_box -> vect[2][0] != 0.0 && active_box -> vect[2][1] != 0.0 && active_box -> vect[2][2] != 0.0)
1045 {
1046 active_cell -> ltype = 2;
1047 }
1048 box = 0;
1049
1050bend:
1051 return box;
1052}
1053
1061int setpbc (xmlNodePtr pbcnode)
1062{
1063 int pbc;//, j;
1064 xmlNodePtr pnode, bnode;
1065 xmlChar * data;
1066 //double val;
1067
1068 pnode = pbcnode -> children;
1069 bnode=findnode(pnode, "apply");
1070 if (bnode == NULL)
1071 {
1072 pbc=8;
1073 goto pend;
1074 }
1075 data = xmlNodeGetContent(bnode);
1076 active_cell -> pbc = 0;
1077 if (g_strcmp0 ((char *)data, (char *)"TRUE") == 0)
1078 {
1079 active_cell -> pbc = 1;
1080 }
1081 xmlFree (data);
1082 bnode=findnode(pnode, "fractional");
1083 if (bnode == NULL)
1084 {
1085 pbc=8;
1086 goto pend;
1087 }
1088 data = xmlNodeGetContent(bnode);
1089 active_cell -> frac = 0;
1090 if (g_strcmp0 ((char *)data, (char *)"TRUE") == 0)
1091 {
1092 xmlFree (data);
1093 bnode=findnode(pnode, "fractype");
1094 if (bnode == NULL)
1095 {
1096 pbc=8;
1097 goto pend;
1098 }
1099 data = xmlNodeGetContent(bnode);
1100 //val=string_to_double ((gpointer)data);
1101 //j = val;
1102 xmlFree (data);
1103 active_cell -> frac = 1;
1104 }
1105 pbc=0;
1106
1107pend:
1108 return pbc;
1109}
1110
1118int setcutoffs (xmlNodePtr cutnode)
1119{
1120 int cut;
1121 int i, j;
1122 xmlNodePtr cnode, cn;
1123 xmlChar * data;
1124 double val;
1125 char * ncut;
1126 size_t lgt;
1127
1128 cnode = cutnode -> children;
1129 cn = findnode(cnode, "total");
1130 if (cn == NULL)
1131 {
1132 cut=9;
1133 goto cend;
1134 }
1135 data = xmlNodeGetContent(cn);
1136 val=string_to_double ((gpointer)data);
1137 xmlFree (data);
1138 active_chem -> grtotcutoff = val;
1139 cn = findnode(cnode, "partials");
1140 if (cn == NULL)
1141 {
1142 cut=9;
1143 goto cend;
1144 }
1145 cnode = cn -> children;
1146 for ( i=0 ; i<active_project -> nspec ; i++ )
1147 {
1148 for ( j=0 ; j<active_project -> nspec ; j++ )
1149 {
1150 lgt=10;
1151 lgt+=strfind(active_chem -> label[i]) + strlen("-") + strfind(active_chem -> label[j]);
1152 ncut = g_malloc0(lgt*sizeof*ncut);
1153 strncpy (ncut, active_chem -> label[i], strfind(active_chem -> label[i]));
1154 strcat (ncut, "-");
1155 strncat (ncut, active_chem -> label[j], strfind(active_chem -> label[j]));
1156 cn = findnode(cnode, ncut);
1157 if (cn == NULL)
1158 {
1159 cut=9;
1160 goto cend;
1161 }
1162 data = xmlNodeGetContent(cn);
1163 val = string_to_double ((gpointer)data);
1164 xmlFree (data);
1165 active_chem -> cutoffs[i][j] = val;
1166 g_free (ncut);
1167 ncut = NULL;
1168 }
1169 }
1170 cut=0;
1171
1172cend:
1173 return cut;
1174}
1175
1183int settime(xmlNodePtr timenode)
1184{
1185 int i, j;
1186 //double val, delta_t;
1187 //int tunit, ndtbs;
1188 int tps;
1189 xmlChar * data;
1190 xmlNodePtr tnode, tn;
1191
1192 tnode = timenode -> children;
1193 tn = findnode(tnode, "dt");
1194 if (tn == NULL)
1195 {
1196 tps=10;
1197 goto tend;
1198 }
1199 data = xmlNodeGetContent(tn);
1200 //val = string_to_double ((gpointer)data);
1201 //delta_t = val;
1202 xmlFree (data);
1203 tn = findnode(tnode, "unit");
1204 if (tn == NULL)
1205 {
1206 tps=10;
1207 goto tend;
1208 }
1209 data = xmlNodeGetContent(tn);
1210 j=-1;
1211 for ( i=0 ; i<5 ; i++)
1212 {
1213 if (g_strcmp0 ((char *)data, g_strdup_printf ("t [%s]", untime[i])) == 0) j=i;
1214 }
1215 xmlFree (data);
1216 if (j == -1)
1217 {
1218 tps=10;
1219 goto tend;
1220 }
1221 if (j < 5)
1222 {
1223 //tunit = j+1;
1224 }
1225 tn = findnode(tnode, "ndt");
1226 if (tn != NULL)
1227 {
1228 data = xmlNodeGetContent(tn);
1229 //val = string_to_double ((gpointer)data);
1230 //ndtbs = val;
1231 xmlFree (data);
1232 }
1233 tps=0;
1234
1235tend:
1236 return tps;
1237}
1238
1246int check_xml (const char * filetocheck)
1247{
1248 int res;
1249 xmlDoc * doc;
1250 xmlTextReaderPtr reader;
1251 xmlChar * cdata, * cfichier;
1252 xmlNodePtr racine, node;
1253 /*
1254 * build an xmlReader for that file
1255 */
1256 reader = xmlReaderForFile(filetocheck, NULL, 0);
1257 if (reader != NULL)
1258 {
1259 res = 0;
1260 doc = xmlParseFile(filetocheck);
1261 if (doc == NULL)
1262 {
1263 res=1;
1264 goto end;
1265 }
1266 else
1267 {
1268 racine = xmlDocGetRootElement(doc);
1269 if (g_strcmp0 ((char *)(racine->name), "isaacs-xml") != 0)
1270 {
1271 res=2;
1272 goto end;
1273 }
1274 else
1275 {
1276 node = racine -> children;
1277 node = findnode(node, "data");
1278 if (node == NULL)
1279 {
1280 res=3;
1281 goto end;
1282 }
1283 node = node -> children;
1284 node = findnode(node, "type");
1285 if (node == NULL)
1286 {
1287 res=3;
1288 goto end;
1289 }
1290 cdata = xmlNodeGetContent(node);
1291 node = findnode(node, "file");
1292 if (node == NULL)
1293 {
1294 xmlFree (cdata);
1295 res=3;
1296 goto end;
1297 }
1298 cfichier = xmlNodeGetContent(node);
1299 res= testopening((char *)cdata, (char *)cfichier);
1300 xmlFree (cdata);
1301 xmlFree (cfichier);
1302 if (res != 0)
1303 {
1304 goto end;
1305 }
1306 else
1307 {
1308 node = racine -> children;
1309 node = findnode(node, "chemistry");
1310 if (node == NULL)
1311 {
1312 res=3;
1313 goto end;
1314 }
1315 res= setchemistry(node);
1316 if (res != 0)
1317 {
1318 goto end;
1319 }
1320 node = racine -> children;
1321 node = findnode(node, "pbc");
1322 if (node == NULL)
1323 {
1324 res=3;
1325 goto end;
1326 }
1327 res=setpbc (node);
1328 if (res != 0)
1329 {
1330 goto end;
1331 }
1332 node = racine -> children;
1333 node = findnode(node, "box");
1334 if (node == NULL)
1335 {
1336 res=3;
1337 goto end;
1338 }
1339 active_cell -> box = g_malloc0(sizeof*active_cell -> box);
1340 active_box = & active_cell -> box[0];
1341 res=setbox (node);
1342 if (res != 0)
1343 {
1344 goto end;
1345 }
1346 node = racine -> children;
1347 node = findnode(node, "cutoffs");
1348 if (node == NULL)
1349 {
1350 res=3;
1351 goto end;
1352 }
1353 active_chem -> cutoffs = allocddouble (active_project -> nspec, active_project -> nspec);
1354 res=setcutoffs (node);
1355 if (res != 0)
1356 {
1357 goto end;
1358 }
1359 if (active_project -> steps > 1)
1360 {
1361 node = racine -> children;
1362 node = findnode(node, "time-series");
1363 if (node == NULL)
1364 {
1365 res=3;
1366 goto end;
1367 }
1368 res=settime (node);
1369 if (res != 0)
1370 {
1371 goto end;
1372 }
1373 }
1374 node = racine -> children;
1375 node = findnode(node, "project");
1376 if (node == NULL)
1377 {
1378 res=3;
1379 goto end;
1380 }
1381 cdata = xmlNodeGetContent(node);
1382 if (g_strcmp0 ((char *)cdata, (char *)"TRUE") == 0)
1383 {
1384 active_project -> run = 1;
1385 }
1386 else
1387 {
1388 active_project -> run = 0;
1389 }
1390 xmlFree (cdata);
1391 }
1392 }
1393 }
1394 }
1395 else
1396 {
1397 res=1;
1398 }
1399
1400end:
1401 /*
1402 * Cleanup function for the XML library.
1403 */
1404 if (reader != NULL) xmlFreeTextReader(reader);
1405 /*
1406 *Free the global variables that may
1407 *have been allocated by the parser.
1408 */
1409 xmlCleanupParser();
1410
1411 return res;
1412}
1413
1421gchar * open_xml (const char * filetoread)
1422{
1423 int oxml;
1424 oxml = check_xml(filetoread);
1425 switch(oxml)
1426 {
1427 case 1:
1428 return (_("Impossible to open the ISAACS project file (XML) file\n"));
1429 break;
1430 case 2:
1431 return (_("The file is not a valid ISAACS project file (XML) file\n"));
1432 break;
1433 case 3:
1434 return (_("The ISAACS project file (XML) file is incomplete\n"));
1435 break;
1436 case 4:
1437 return (_("Problem(s) in the &lt;data&gt; section of the ISAACS project file (XML)\n"));
1438 break;
1439 case 5:
1440 return (_("Problem(s) in the &lt;chemistry&gt; section of the ISAACS project file (XML)\n"));
1441 break;
1442 case 6:
1443 return (_("Problem(s) in the &lt;element&gt; section of the ISAACS project file (XML)\n"));
1444 break;
1445 case 7:
1446 return (_("Problem(s) in the &lt;box&gt; section of the ISAACS project file (XML)\n"));
1447 break;
1448 case 8:
1449 return (_("Problem(s) in the &lt;pbc&gt; section of the ISAACS project file (XML)\n"));
1450 break;
1451 case 9:
1452 return (_("Problem(s) in the &lt;cutoffs&gt; section of the ISAACS project file (XML)\n"));
1453 break;
1454 case 10:
1455 return (_("Problem(s) in the &lt;time-series&gt; section of the ISAACS project file (XML)\n"));
1456 break;
1457 default:
1458 return NULL;
1459 break;
1460 }
1461}
int open_coordinate_file(int id)
try to open coordinate file, type is based of id
Definition callbacks.c:1407
Callback declarations for main window.
gchar * param[2]
float val
Definition dlp_init.c:117
gchar * filetoread
char * untime[5]
Definition global.c:153
double ** allocddouble(int xal, int yal)
allocate a double ** pointer
Definition global.c:462
GtkWidget * MainWindow
Definition global.c:207
double string_to_double(gpointer string)
convert string to double
Definition global.c:611
Global variable declarations Global convenience function declarations Global data structure defin...
cell_info * active_cell
Definition project.c:50
#define CHEM_N
Definition global.h:319
chemical_data * active_chem
Definition project.c:48
#define CHEM_R
Definition global.h:318
box_info * active_box
Definition project.c:51
#define MY_ENCODING
Definition global.h:103
#define CHEM_M
Definition global.h:317
#define CHEM_X
Definition global.h:320
#define CHEM_Z
Definition global.h:316
project * active_project
Definition project.c:47
void show_warning(char *warning, GtkWidget *win)
show warning
Definition interface.c:266
void show_error(char *error, int val, GtkWidget *win)
show error message
Definition interface.c:299
gchar * exact_name(gchar *name)
short cut to print string without spaces
Definition interface.c:434
Messaging function declarations.
int open_coordinate_file(int id)
try to open coordinate file, type is based of id
Definition callbacks.c:1407
int setprop(xmlNodePtr pnode)
read chemical properties from XML node
int setbox(xmlNodePtr boxnode)
read box properties from node
xmlNodePtr findnode(xmlNodePtr startnode, char *nname)
find XML node
int settime(xmlNodePtr timenode)
read MD information from node
gboolean file_exists(const char *filename)
file exists ?
int testopening(char *tdata, char *tfichier)
test atomic coordinates file opening
int write_xml(const char *filetosave)
write XML file
char * reg_types[NFORMATS]
Definition read_isaacs.c:70
int check_xml(const char *filetocheck)
check the opening of ISAACS XML file
size_t strfind(char *ida)
size of a string without spaces
Definition read_isaacs.c:85
#define NFORMATS
Definition read_isaacs.c:68
int XmlwriterFilename(const char *uri)
write ISAACS XML file
int get_spec_from_data(xmlChar *data)
get atomic species from data
int setchemistry(xmlNodePtr xsnode)
read chemistry data from node
int setcutoffs(xmlNodePtr cutnode)
read bond cutoffs from node
gchar * open_xml(const char *filetoread)
Open ISAACS XML file.
int setpbc(xmlNodePtr pbcnode)
read the PBC information from node
Definition glwin.h:350
GtkWidget * res[2]
Definition w_encode.c:342
int element
Definition w_periodic.c:61