Page MenuHomec4science

xml_parser_restart.cc
No OneTemporary

File Metadata

Created
Mon, Oct 28, 14:49

xml_parser_restart.cc

/**
* @file xml_parser_restart.cc
*
* @author Guillaume Anciaux <guillaume.anciaux@epfl.ch>
*
* @date Wed Aug 20 16:58:08 2014
*
* @brief XML parser for restart files
*
* @section LICENSE
*
* Copyright INRIA and CEA
*
* The LibMultiScale is a C++ parallel framework for the multiscale
* coupling methods dedicated to material simulations. This framework
* provides an API which makes it possible to program coupled simulations
* and integration of already existing codes.
*
* This Project was initiated in a collaboration between INRIA Futurs Bordeaux
* within ScAlApplix team and CEA/DPTA Ile de France.
* The project is now continued at the Ecole Polytechnique Fédérale de Lausanne
* within the LSMS/ENAC laboratory.
*
* This software is governed by the CeCILL-C license under French law and
* abiding by the rules of distribution of free software. You can use,
* modify and/ or redistribute the software under the terms of the CeCILL-C
* license as circulated by CEA, CNRS and INRIA at the following URL
* "http://www.cecill.info".
*
* As a counterpart to the access to the source code and rights to copy,
* modify and redistribute granted by the license, users are provided only
* with a limited warranty and the software's author, the holder of the
* economic rights, and the successive licensors have only limited
* liability.
*
* In this respect, the user's attention is drawn to the risks associated
* with loading, using, modifying and/or developing or reproducing the
* software by the user in light of its specific status of free software,
* that may mean that it is complicated to manipulate, and that also
* therefore means that it is reserved for developers and experienced
* professionals having in-depth computer knowledge. Users are therefore
* encouraged to load and test the software's suitability as regards their
* requirements in conditions enabling the security of their systems and/or
* data to be ensured and, more generally, to use and operate it in the
* same conditions as regards security.
*
* The fact that you are presently reading this means that you have had
* knowledge of the CeCILL-C license and that you accept its terms.
*
*/
#include "xml_parser_restart.hh"
#include "lm_common.hh"
#include "trace_atom.hh"
#include <sstream>
/* -------------------------------------------------------------------------- */
__BEGIN_LIBMULTISCALE__
/* -------------------------------------------------------------------------- */
template <UInt Dim>
void XMLRestartParser<Dim>::charHandler(const XML_Char *str, int len) {
char *buf;
DUMP("read a line of size " << len, DBG_ALL);
if (len == 0)
return;
buf = new char[len + 1];
strncpy(buf, str, len);
if (type == TEXT)
buf[len] = '\0';
else
b64.SetStream(buf, len);
if (buf[0] == '\n') {
delete[] buf;
return;
}
switch (current_field) {
case P0S:
manageP0(buf);
break;
case DEP:
manageU(buf);
break;
case VEL:
manageV(buf);
break;
case ACC:
manageA(buf);
break;
case NONE:
break;
}
delete[] buf;
}
/* -------------------------------------------------------------------------- */
template <UInt Dim>
void XMLRestartParser<Dim>::startElement(const char *name, const char **atts) {
UInt j;
cpt = 0;
b64.reset();
if (strcmp("SimulationData", name) == 0) {
for (j = 0; (j < 10 && atts[j] != NULL); ++j) {
if (strcmp("dim", atts[j]) == 0) {
UInt dim = atoi(atts[j + 1]);
if (dim != Dim)
LM_FATAL("reloading data of a different dimension");
++j;
}
if (strcmp("dataType", atts[j]) == 0) {
if (strcmp("TEXT", atts[j + 1]) == 0) {
type = TEXT;
} else
type = BINARY;
++j;
}
if (strcmp("nbDofs", atts[j]) == 0) {
nbDofs = atoi(atts[j + 1]);
++j;
}
if (strcmp("timestep", atts[j]) == 0) {
current_step = atoi(atts[j + 1]);
DUMP("Changing current timestep from reload to " << current_step,
DBG_WARNING);
++j;
}
}
}
if (strcmp("P0", name) == 0) {
current_field = P0S;
DUMP("reading intial positions", DBG_INFO);
}
if (strcmp("U", name) == 0) {
current_field = DEP;
DUMP("reading displacements", DBG_INFO);
}
if (strcmp("V", name) == 0) {
current_field = VEL;
DUMP("reading velocities", DBG_INFO);
}
if (strcmp("A", name) == 0) {
current_field = ACC;
DUMP("reading accelerations", DBG_INFO);
}
}
/* -------------------------------------------------------------------------- */
template <UInt Dim> void XMLRestartParser<Dim>::endElement(const char *name) {
std::string message = name;
switch (current_field) {
case P0S:
message += " : in vector P0s ";
break;
case DEP:
message += " : in vector Us ";
break;
case VEL:
message += " : in vector Vs ";
break;
case ACC:
message += " : in vector As ";
break;
case NONE:
break;
}
current_field = NONE;
if (cpt != 0 && cpt != nbDofs)
LM_FATAL(message << ": values number do not concord (read " << cpt
<< " should be " << nbDofs << ")");
}
/* -------------------------------------------------------------------------- */
template <UInt Dim>
UInt XMLRestartParser<Dim>::readTriplet(Triplet<Dim> &C,
std::stringstream &sstr) {
if (type == TEXT) {
Real tmp;
while (triplet.getRead() < Dim) {
UInt i = triplet.getRead();
if (sstr >> tmp) {
triplet(i) = tmp;
triplet.getRead()++;
} else
return 0;
}
}
if (type == BINARY) {
for (UInt i = triplet.getRead(); i < Dim; ++i) {
DUMP("read " << triplet.getRead() << " " << i, DBG_ALL);
triplet.getRead() += b64.PopRealInBase64(triplet(i));
DUMP("read " << triplet.getRead() << " " << i, DBG_ALL);
if (i + 1 != triplet.getRead()) {
DUMP("already red " << triplet.getRead()
<< " components of the triplet",
DBG_ALL);
return 0;
}
}
}
for (UInt i = 0; i < Dim; ++i)
if (std::isnan(triplet(i)) || std::isinf(triplet(i)))
triplet(i) = 1;
C = triplet;
triplet.getRead() = 0;
return 1;
}
/* -------------------------------------------------------------------------- */
template <UInt Dim> void XMLRestartParser<Dim>::manageP0(char *s) {
Triplet<Dim> C;
std::stringstream sstr(s);
while (readTriplet(C, sstr) && cpt < nbDofs) {
IF_TRACED(C, "traced atom (P0) has been read from restart file :"
<< " position is " << mapP0.size());
mapP0.push_back(C);
++cpt;
}
}
/* -------------------------------------------------------------------------- */
template <UInt Dim> void XMLRestartParser<Dim>::manageU(char *s) {
Triplet<Dim> u;
std::stringstream sstr(s);
while (readTriplet(u, sstr) && cpt < nbDofs) {
Triplet<Dim> index = mapP0[cpt];
IF_TRACED(index, "traced atom (U) has been read from restart file :"
<< " position is " << index);
mapU[index] = u;
DUMP("mapU size = " << mapU.size() << " : " << index << " " << u << " + "
<< cpt,
DBG_DETAIL);
++cpt;
}
}
/* -------------------------------------------------------------------------- */
template <UInt Dim> void XMLRestartParser<Dim>::manageV(char *s) {
Triplet<Dim> v;
std::stringstream sstr(s);
while (readTriplet(v, sstr) && cpt < nbDofs) {
Triplet<Dim> index = mapP0[cpt];
mapV[index] = v;
DUMP(v << " + " << cpt, DBG_ALL);
++cpt;
}
}
/* -------------------------------------------------------------------------- */
template <UInt Dim> void XMLRestartParser<Dim>::manageA(char *s) {
Triplet<Dim> a;
std::stringstream sstr(s);
while (readTriplet(a, sstr) && cpt < nbDofs) {
Triplet<Dim> index = mapP0[cpt];
mapA[index] = a;
DUMP(a << " + " << cpt, DBG_ALL);
++cpt;
}
}
/* -------------------------------------------------------------------------- */
template class XMLRestartParser<1>;
template class XMLRestartParser<2>;
template class XMLRestartParser<3>;
/* -------------------------------------------------------------------------- */
__END_LIBMULTISCALE__

Event Timeline