Function3dHandler.C 6.28 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//////////////////////////////////////////////////////////////////////////////// //
// Copyright (c) 2018 The Regents of the University of California
//
// This file is part of Qbox
//
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
// the License, or (at your option) any later version.
// See the file COPYING in the root directory of this distribution
// or <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
//
// Function3dHandler.C
//
////////////////////////////////////////////////////////////////////////////////

#include "Function3d.h"
#include "Function3dHandler.h"
20
#include "Base64Transcoder.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include "StrX.h"
#include "Timer.h"
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/sax2/Attributes.hpp>
using namespace xercesc;
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
Function3dHandler::Function3dHandler(Function3d& f) : f_(f) {}

////////////////////////////////////////////////////////////////////////////////
Function3dHandler::~Function3dHandler(void) {}

////////////////////////////////////////////////////////////////////////////////
void Function3dHandler::startElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname,
  const Attributes& attributes)
{
  string locname = StrX(localname).localForm();
  if ( locname == "function3d" )
  {
    unsigned int len = attributes.getLength();
    for ( unsigned int index = 0; index < len; index++ )
    {
      string attrname = StrX(attributes.getLocalName(index)).localForm();
      if ( attrname == "name" )
      {
51
        f_.name = StrX(attributes.getValue(index)).localForm();
52 53 54 55 56 57 58 59 60 61 62 63
      }
    }
  }
  else if ( locname == "domain" )
  {
    unsigned int len = attributes.getLength();
    for ( unsigned int index = 0; index < len; index++ )
    {
      string attrname = StrX(attributes.getLocalName(index)).localForm();
      if ( attrname == "a" )
      {
        istringstream stst(StrX(attributes.getValue(index)).localForm());
64
        stst >> f_.a;
65
      }
66
      else if ( attrname == "b" )
67 68
      {
        istringstream stst(StrX(attributes.getValue(index)).localForm());
69
        stst >> f_.b;
70
      }
71
      else if ( attrname == "c" )
72 73
      {
        istringstream stst(StrX(attributes.getValue(index)).localForm());
74
        stst >> f_.c;
75 76 77 78 79 80 81 82 83 84 85
      }
    }
  }
  else if ( locname == "grid" )
  {
    unsigned int len = attributes.getLength();
    for ( unsigned int index = 0; index < len; index++ )
    {
      string attrname = StrX(attributes.getLocalName(index)).localForm();
      if ( attrname == "nx" )
      {
86
        f_.nx = atoi(StrX(attributes.getValue(index)).localForm());
87
      }
88
      else if ( attrname == "ny" )
89
      {
90
        f_.ny = atoi(StrX(attributes.getValue(index)).localForm());
91
      }
92
      else if ( attrname == "nz" )
93
      {
94
        f_.nz = atoi(StrX(attributes.getValue(index)).localForm());
95 96 97 98 99
      }
    }
  }
  else if ( locname == "grid_function" )
  {
100 101 102
    x0_ = 0;
    y0_ = 0;
    z0_ = 0;
103 104 105 106 107 108 109 110 111 112 113 114 115 116
    unsigned int len = attributes.getLength();
    for ( unsigned int index = 0; index < len; index++ )
    {
      string attrname = StrX(attributes.getLocalName(index)).localForm();
      if ( attrname == "type" )
      {
        string type = StrX(attributes.getValue(index)).localForm();
        if ( type != "double" )
        {
          cerr << "Function3dHandler: grid_function type must be double"
               << endl;
          assert(!"Function3dHandler: incorrect grid_function type");
        }
      }
117 118 119 120 121 122 123 124 125 126 127 128 129
      else if ( attrname == "nx" )
      {
        fnx_ = atoi(StrX(attributes.getValue(index)).localForm());
      }
      else if ( attrname == "ny" )
      {
        fny_ = atoi(StrX(attributes.getValue(index)).localForm());
      }
      else if ( attrname == "nz" )
      {
        fnz_ = atoi(StrX(attributes.getValue(index)).localForm());
      }
      else if ( attrname == "x0" )
130
      {
131
        x0_ = atoi(StrX(attributes.getValue(index)).localForm());
132
      }
133
      else if ( attrname == "y0" )
134
      {
135
        y0_ = atoi(StrX(attributes.getValue(index)).localForm());
136
      }
137
      else if ( attrname == "z0" )
138
      {
139
        z0_ = atoi(StrX(attributes.getValue(index)).localForm());
140
      }
141
      else if ( attrname == "encoding" )
142 143 144 145 146 147 148 149 150 151
      {
        string type = StrX(attributes.getValue(index)).localForm();
        if ( type != "base64" )
        {
          cerr << "Function3dHandler: encoding must be base64"
               << endl;
          assert(!"Function3dHandler: incorrect encoding");
        }
      }
    }
152
    buf_ = "";
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  }
}

////////////////////////////////////////////////////////////////////////////////
#ifndef XERCES_VERSION_MAJOR
#error "XERCES_VERSION_MAJOR not defined"
#endif
#if XERCES_VERSION_MAJOR < 3
#error Xerces-C version should be at least 3
#endif
void Function3dHandler::characters(const XMLCh* const chars,
  const XMLSize_t length)
{
#if TIMING
  Timer tm;
  tm.start();
#endif

  char *str = XMLString::transcode(chars);
172
  buf_ += str;
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  XMLString::release(&str);

#if TIMING
  tm.stop();
  cout << " Function3dHandler::characters: time: " << tm.real() << endl;
#endif
}

////////////////////////////////////////////////////////////////////////////////
void Function3dHandler::endElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname)
{
  string locname = StrX(localname).localForm();
  if ( locname == "grid_function" )
  {
188 189 190 191 192 193
    if ( (fnx_ > f_.nx) || (fny_ > f_.ny) || (fnz_ > f_.nz) )
    {
      assert(!"Function3dHandler:: fragment size > grid size");
    }
    if ( (fnx_ != f_.nx) || (fny_ != f_.ny) || (fnz_ != f_.nz) )
    {
194
      assert(!"Function3dHandler:: fragment processing not implemented");
195 196 197 198 199 200 201 202 203 204 205 206 207
    }
    if ( x0_ != 0 || y0_ != 0 || z0_ != 0 )
    {
      assert(!"Function3dHandler:: fragment offset not implemented");
    }
    // process base64 data in buf
    f_.val.resize(f_.nx*f_.ny*f_.nz);
    Timer tm;
    tm.start();
    Base64Transcoder xcdr;
    size_t nbytes = xcdr.decode(buf_.size(),buf_.data(),(byte*)&f_.val[0]);
    assert(nbytes==f_.val.size()*sizeof(double));
    buf_.clear();
208 209 210
    #if PLT_BIG_ENDIAN
    xcdr.byteswap_double(f_.val.size(),&f_.val[0]);
    #endif
211 212
  }
}