<commit_before>/*
 *            Copyright 2009-2018 The VOTCA Development Team
 *                       (http://www.votca.org)
 *
 *      Licensed under the Apache License, Version 2.0 (the "License")
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */



#include <votca/xtp/numerical_integrations.h>
#include <boost/math/constants/constants.hpp>
#include <votca/xtp/radial_euler_maclaurin_rule.h>
#include <votca/xtp/sphere_lebedev_rule.h>
#include <votca/xtp/aoshell.h>
#include <votca/tools/constants.h>

#include <votca/xtp/aomatrix.h>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <cmath>
#include <iterator>
#include <string>




namespace votca {
    namespace xtp {

    NumericalIntegration::~NumericalIntegration(){
      if (_setXC) {
        xc_func_end(&xfunc);
        if (_use_separate) {
          xc_func_end(&cfunc);
        }
      }
    }

    

    double NumericalIntegration::getExactExchange(const std::string& functional) {      

      double exactexchange = 0.0;
      Vxc_Functionals map;
      std::vector<std::string> strs;

      boost::split(strs, functional, boost::is_any_of(" "));
      if (strs.size() > 2) {
        throw std::runtime_error("Too many functional names");
      } else if (strs.size() < 1) {
        throw std::runtime_error("Specify at least one functional");
      }

      for (unsigned i = 0; i < strs.size(); i++) {

        int func_id = map.getID(strs[i]);
        if (func_id < 0) {
          exactexchange = 0.0;
          break;
        }
        xc_func_type func;
        if (xc_func_init(&func, func_id, XC_UNPOLARIZED) != 0) {
         throw std::runtime_error((boost::format("Functional %s not found\n") %strs[i]).str());
        }
        if (exactexchange > 0 && func.cam_alpha > 0) {
          throw std::runtime_error("You have specified two functionals with exact exchange");
        }
        exactexchange += func.cam_alpha;
        xc_func_end(&func);
      }
      
      return exactexchange;

    }
    
    
   void NumericalIntegration::setXCfunctional(const std::string& functional) {

      Vxc_Functionals map;
      std::vector<std::string> strs;
      boost::split(strs, functional, boost::is_any_of(" "));
      xfunc_id = 0;
      _use_separate = false;
      cfunc_id = 0;
      if (strs.size() == 1) {
        xfunc_id = map.getID(strs[0]);
      } else if (strs.size() == 2) {
        xfunc_id = map.getID(strs[0]);
        cfunc_id = map.getID(strs[1]);
        _use_separate = true;
      } else {
        std::cout << "LIBXC " << strs.size() << std::endl;
        throw std::runtime_error("LIBXC. Please specify one combined or an exchange and a correlation functionals");
      }

      if (xc_func_init(&xfunc, xfunc_id, XC_UNPOLARIZED) != 0) {
        throw std::runtime_error((boost::format("Functional %s not found\n") %strs[0]).str());
      }
      xc_func_init(&xfunc, xfunc_id, XC_UNPOLARIZED);
      if (xfunc.info->kind != 2 && !_use_separate) {
        throw std::runtime_error("Your functional misses either correlation or exchange, please specify another functional, separated by whitespace");
      }
      if (_use_separate) {
        if (xc_func_init(&cfunc, cfunc_id, XC_UNPOLARIZED) != 0) {
          throw std::runtime_error((boost::format("Functional %s not found\n") %strs[1]).str());
        }
        xc_func_init(&cfunc, cfunc_id, XC_UNPOLARIZED);
        xc_func_init(&xfunc, xfunc_id, XC_UNPOLARIZED);
        if ((xfunc.info->kind + cfunc.info->kind) != 1) {
          throw std::runtime_error("Your functionals are not one exchange and one correlation");
        }
      }
      _setXC = true;
      return;
    }

        
        void NumericalIntegration::EvaluateXC(const double rho, const Eigen::Vector3d& grad_rho, double& f_xc, double& df_drho, double& df_dsigma) {

        double sigma = (grad_rho.transpose()*grad_rho).value();
        double exc[1];
        double vsigma[1]; // libxc 
        double vrho[1]; // libxc df/drho
        switch (xfunc.info->family) {
          case XC_FAMILY_LDA:
            xc_lda_exc_vxc(&xfunc, 1, &rho, exc, vrho);<commit_msg>fixed mem error, at least attempt<commit_after>/*
 *            Copyright 2009-2018 The VOTCA Development Team
 *                       (http://www.votca.org)
 *
 *      Licensed under the Apache License, Version 2.0 (the "License")
 *
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *              http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */



#include <votca/xtp/numerical_integrations.h>
#include <boost/math/constants/constants.hpp>
#include <votca/xtp/radial_euler_maclaurin_rule.h>
#include <votca/xtp/sphere_lebedev_rule.h>
#include <votca/xtp/aoshell.h>
#include <votca/tools/constants.h>

#include <votca/xtp/aomatrix.h>
#include <fstream>
#include <boost/algorithm/string.hpp>
#include <cmath>
#include <iterator>
#include <string>




namespace votca {
    namespace xtp {

    NumericalIntegration::~NumericalIntegration(){
      if (_setXC) {
        xc_func_end(&xfunc);
        if (_use_separate) {
          xc_func_end(&cfunc);
        }
      }
    }

    

    double NumericalIntegration::getExactExchange(const std::string& functional) {      

      double exactexchange = 0.0;
      Vxc_Functionals map;
      std::vector<std::string> strs;

      boost::split(strs, functional, boost::is_any_of(" "));
      if (strs.size() > 2) {
        throw std::runtime_error("Too many functional names");
      } else if (strs.size() < 1) {
        throw std::runtime_error("Specify at least one functional");
      }

      for (unsigned i = 0; i < strs.size(); i++) {

        int func_id = map.getID(strs[i]);
        if (func_id < 0) {
          exactexchange = 0.0;
          break;
        }
        xc_func_type func;
        if (xc_func_init(&func, func_id, XC_UNPOLARIZED) != 0) {
         throw std::runtime_error((boost::format("Functional %s not found\n") %strs[i]).str());
        }
        if (exactexchange > 0 && func.cam_alpha > 0) {
          throw std::runtime_error("You have specified two functionals with exact exchange");
        }
        exactexchange += func.cam_alpha;
        xc_func_end(&func);
      }
      
      return exactexchange;

    }
    
    
   void NumericalIntegration::setXCfunctional(const std::string& functional) {

      Vxc_Functionals map;
      std::vector<std::string> strs;
      tools::Tokenizer tok(functional," ,\n\t");
      tok.ToVector(strs);
      xfunc_id = 0;
      _use_separate = false;
      cfunc_id = 0;
      if (strs.size() == 1) {
        xfunc_id = map.getID(strs[0]);
      } else if (strs.size() == 2) {
        xfunc_id = map.getID(strs[0]);
        cfunc_id = map.getID(strs[1]);
        _use_separate = true;
      } else {
        std::cout << "LIBXC " << strs.size() << std::endl;
        throw std::runtime_error("LIBXC. Please specify one combined or an exchange and a correlation functionals");
      }

      if (xc_func_init(&xfunc, xfunc_id, XC_UNPOLARIZED) != 0) {
        throw std::runtime_error((boost::format("Functional %s not found\n") %strs[0]).str());
      }
      if (xfunc.info->kind != 2 && !_use_separate) {
        throw std::runtime_error("Your functional misses either correlation or exchange, please specify another functional, separated by whitespace");
      }
      if (_use_separate) {
        if (xc_func_init(&cfunc, cfunc_id, XC_UNPOLARIZED) != 0) {
          throw std::runtime_error((boost::format("Functional %s not found\n") %strs[1]).str());
        }
        if ((xfunc.info->kind + cfunc.info->kind) != 1) {
          throw std::runtime_error("Your functionals are not one exchange and one correlation");
        }
      }
      _setXC = true;
      return;
    }

        
        void NumericalIntegration::EvaluateXC(const double rho, const Eigen::Vector3d& grad_rho, double& f_xc, double& df_drho, double& df_dsigma) {

        double sigma = (grad_rho.transpose()*grad_rho).value();
        double exc[1];
        double vsigma[1]; // libxc 
        double vrho[1]; // libxc df/drho
        switch (xfunc.info->family) {
          case XC_FAMILY_LDA:
            xc_lda_exc_vxc(&xfunc, 1, &rho, exc, vrho);
{'fraction_non_alphanumeric': 0.12730299667036626, 'fraction_numerical': 0.009100998890122086, 'mean_word_length': 2.6600324939073925, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5524785', 'n_tokens_mistral': 3055, 'n_tokens_neox': 2917, 'n_words': 704}