SpeciesHandler.C 7.77 KB
Newer Older
Francois Gygi committed
1 2
////////////////////////////////////////////////////////////////////////////////
//
3
// Copyright (c) 2008-2012 The Regents of the University of California
Francois Gygi committed
4 5 6
//
// This file is part of Qbox
//
Francois Gygi committed
7 8
// Qbox is distributed under the terms of the GNU General Public License
// as published by the Free Software Foundation, either version 2 of
Francois Gygi committed
9 10 11 12 13 14
// 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/>.
//
////////////////////////////////////////////////////////////////////////////////
//
Francois Gygi committed
15 16 17 18 19 20 21 22 23 24 25 26 27 28
// SpeciesHandler.C
//
////////////////////////////////////////////////////////////////////////////////

#include "SpeciesHandler.h"
#include "Species.h"
#include "StrX.h"
using namespace xercesc;
#include <iostream>
#include <sstream>
#include <cassert>
using namespace std;

////////////////////////////////////////////////////////////////////////////////
29 30
SpeciesHandler::SpeciesHandler(Species& sp) :
  sp_(sp), d_ij_alloc(false) {}
Francois Gygi committed
31 32 33 34

////////////////////////////////////////////////////////////////////////////////
SpeciesHandler::~SpeciesHandler() {}

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
////////////////////////////////////////////////////////////////////////////////
// read attributes
void SpeciesHandler::read(const Attributes& attributes)
{
  unsigned int len = attributes.getLength();
  for ( unsigned int index = 0; index < len; index++ )
  {
    string attrname(XMLString::transcode(attributes.getLocalName(index)));
    if ( attrname == "l" )
    {
      current_l = atoi(StrX(attributes.getValue(index)).localForm());
    }
    else if ( attrname == "size" )
    {
      current_size = atoi(StrX(attributes.getValue(index)).localForm());
    }
    else if ( attrname == "i" )
    {
      current_i = atoi(StrX(attributes.getValue(index)).localForm()) - 1;
    }
    else if ( attrname == "j" )
    {
      current_j = atoi(StrX(attributes.getValue(index)).localForm()) - 1;
    }
  }
}

////////////////////////////////////////////////////////////////////////////////
// allocate array for d_ij matrix
void SpeciesHandler::alloc_d_ij()
{
  // allocate for every l
  sp_.d_.resize(sp_.proj_.size());
  for ( int l = 0; l < sp_.d_.size(); l++ )
  {
    const int size = sp_.proj_[l].size();
    sp_.d_[l].resize(size);
    for ( int i = 0; i < size; i++ )
    {
      sp_.d_[l][i].resize(size);
    }
  }
  d_ij_alloc = true;
}

Francois Gygi committed
80 81 82 83
////////////////////////////////////////////////////////////////////////////////
void SpeciesHandler::startElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname,
  const Attributes& attributes)
84
{
85
//  cout << " SpeciesHandler::startElement " << StrX(qname) << endl;
86

Francois Gygi committed
87
  string locname(XMLString::transcode(localname));
88

Francois Gygi committed
89 90 91 92
  if ( locname == "species" )
  {
    // check for the case where the species is a link to another uri
    unsigned int len = attributes.getLength();
93
    for ( unsigned int index = 0; index < len; index++ )
Francois Gygi committed
94 95
    {
      string attrname(XMLString::transcode(attributes.getLocalName(index)));
96
      if ( attrname == "name" )
Francois Gygi committed
97 98 99 100 101 102 103 104 105 106 107
      {
        current_name = StrX(attributes.getValue(index)).localForm();
        sp_.name_ = current_name;
      }
      else if ( attrname == "href" )
      {
        current_href = StrX(attributes.getValue(index)).localForm();
        sp_.uri_ = current_href;
      }
    }
  }
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
  else if ( locname == "norm_conserving_pseudopotential" )
  {
    sp_.type_ = Species::NCPP;
  }
  else if ( locname == "norm_conserving_semilocal_pseudopotential" )
  {
    sp_.type_ = Species::SLPP;
  }
  else if ( locname == "core_density" )
  {
    read(attributes);
  }
  else if ( locname == "local_potential" )
  {
    read(attributes);
  }
Francois Gygi committed
124 125
  else if ( locname == "projector" )
  {
126 127 128 129 130
    read(attributes);
  }
  else if ( locname == "d_ij" )
  {
    read(attributes);
Francois Gygi committed
131 132 133 134 135 136 137 138 139
  }
}

////////////////////////////////////////////////////////////////////////////////
void SpeciesHandler::endElement(const XMLCh* const uri,
  const XMLCh* const localname, const XMLCh* const qname, string& content)
{
  string locname(XMLString::transcode(localname));
  istringstream stst(content);
140 141
//  cout << " SpeciesHandler::endElement " << StrX(qname) << " content="
//    << string(content,0,20) << endl;
Francois Gygi committed
142

143
  if ( locname == "description" )
Francois Gygi committed
144 145 146 147
  {
    // reject ambiguous case where both the href and the definition are given
    if ( current_href != "" )
    {
148 149 150
      cout << " SpeciesHandler: ambiguous definition: uri=" << StrX(uri)
        << endl << " using local definition (href: " << current_href
        << " ignored)" << endl;
Francois Gygi committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
    }
    sp_.description_ = content;
  }
  else if ( locname == "atomic_number" )
  {
    stst >> sp_.atomic_number_;
  }
  else if ( locname == "mass" )
  {
    stst >> sp_.mass_;
  }
  else if ( locname == "symbol" )
  {
    stst >> skipws >> sp_.symbol_;
  }
  else if ( locname == "valence_charge" )
  {
    stst >> sp_.zval_;
  }
  else if ( locname == "lmax" )
  {
    stst >> sp_.lmax_;
  }
  else if ( locname == "llocal" )
  {
    stst >> sp_.llocal_;
  }
  else if ( locname == "nquad" )
  {
    stst >> sp_.nquad_;
  }
  else if ( locname == "rquad" )
  {
    stst >> sp_.rquad_;
  }
  else if ( locname == "mesh_spacing" )
  {
    stst >> sp_.deltar_;
  }
  else if ( locname == "radial_potential" )
  {
192 193
    assert(sp_.type_ == Species::NCPP);
    if ( current_l + 1 > sp_.vps_.size() )
Francois Gygi committed
194
    {
195 196
      sp_.vps_.resize(current_l + 1);
      sp_.phi_.resize(current_l + 1);
Francois Gygi committed
197 198 199 200 201
    }
    sp_.vps_[current_l].resize(current_size);
    for ( int i = 0; i < current_size; i++ )
      stst >> sp_.vps_[current_l][i];
  }
202 203 204 205 206 207 208
  else if ( locname == "local_potential" )
  {
    assert(sp_.type_ == Species::SLPP);
    sp_.vlocal_.resize(current_size);
    for ( int i = 0; i < current_size; i++ )
      stst >> sp_.vlocal_[i];
  }
Francois Gygi committed
209 210
  else if ( locname == "radial_function" )
  {
211
    assert(sp_.type_ == Species::NCPP);
Francois Gygi committed
212 213 214 215
    sp_.phi_[current_l].resize(current_size);
    for ( int i = 0; i < current_size; i++ )
      stst >> sp_.phi_[current_l][i];
  }
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243
  else if ( locname == "core_density" )
  {
    // read charge for nonlinear core correction
    sp_.nlcc_.resize(current_size);
    for ( int i = 0; i < current_size; i++ )
      stst >> sp_.nlcc_[i];
  }
  else if ( locname == "projector" and sp_.type_ == Species::SLPP )
  {
    // read one of the projector with this angular momentum
    // resize vector if necessary
    if ( current_l >= sp_.proj_.size() ) sp_.proj_.resize(current_l + 1);
    if ( current_i >= sp_.proj_[current_l].size() )
      sp_.proj_[current_l].resize(current_i + 1);
    sp_.proj_[current_l][current_i].resize(current_size);
    // read the projector
    for ( int i = 0; i < current_size; i++ )
      stst >> sp_.proj_[current_l][current_i][i];
  }
  else if ( locname == "d_ij" )
  {
    assert(sp_.type_ == Species::SLPP);
    if ( not d_ij_alloc ) alloc_d_ij();
    assert(current_l < sp_.d_.size());
    assert(current_i < sp_.d_[current_l].size());
    assert(current_j < sp_.d_[current_l][current_i].size());
    stst >> sp_.d_[current_l][current_i][current_j];
  }
Francois Gygi committed
244 245 246 247
}

////////////////////////////////////////////////////////////////////////////////
StructureHandler* SpeciesHandler::startSubHandler(const XMLCh* const uri,
248 249
  const XMLCh* const localname, const XMLCh* const qname,
  const Attributes& attributes)
Francois Gygi committed
250 251 252 253 254 255 256 257 258
{
  // check if element qname can be processed by another StructureHandler
  // If it can, return a pointer to the StructureHandler, otherwise return 0
  // cout << " SpeciesHandler::startSubHandler " << StrX(qname) << endl;
  return 0;
}

////////////////////////////////////////////////////////////////////////////////
void SpeciesHandler::endSubHandler(const XMLCh* const uri,
259 260
  const XMLCh* const localname, const XMLCh* const qname,
  const StructureHandler* const subHandler)
Francois Gygi committed
261 262 263 264 265
{
  // cout << " SpeciesHandler::endSubHandler " << StrX(qname) << endl;
  // if any StructureHandler was created by startSubHandler, delete it
  // delete subHandler;
}