<commit_before>/*
 *            Copyright 2009-2016 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 "igwbse.h"

#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/algorithm/string/split.hpp>
#include <votca/xtp/logger.h>
#include <votca/tools/constants.h>
#include <votca/xtp/qmpackagefactory.h>

using boost::format;
using namespace boost::filesystem;
using namespace votca::tools;

namespace ub = boost::numeric::ublas;
    
namespace votca { namespace xtp {
    
// +++++++++++++++++++++++++++++ //
// IGWBSE MEMBER FUNCTIONS         //
// +++++++++++++++++++++++++++++ //

void IGWBSE::Initialize(votca::tools::Property* options ) {

 
    
    
    // tasks to be done by IBSE: dft_input, dft_run, dft_parse, mgbft, bse_coupling
    _do_dft_input = false;
    _do_dft_run   = false;
    _do_dft_parse = false;
    _do_gwbse     = false;
    _do_coupling  = false;
    _do_trim      = false;
    
    _store_dft = false;
    _store_singlets = false;
    _store_triplets = false;
    _store_ehint = false;
    _write_orbfile=false;


    // update options with the VOTCASHARE defaults   
    //UpdateWithDefaults( options, "xtp" );
    ParseOptionsXML( options  );
    
    // register all QM packages (Gaussian, turbomole, etc))
    QMPackageFactory::RegisterAll();

}

void IGWBSE::ParseOptionsXML( votca::tools::Property *opt ) {
   
    
    // parsing general ibse options
    string key = "options." + Identify();
   // _energy_difference = opt->get( key + ".degeneracy" ).as< double > ();
    
 
    // job tasks
    string _tasks_string = opt->get(key+".tasks").as<string> ();
    if (_tasks_string.find("input")    != std::string::npos) _do_dft_input = true;
    if (_tasks_string.find("dft")      != std::string::npos) _do_dft_run   = true;
    if (_tasks_string.find("parse")    != std::string::npos) _do_dft_parse = true;
    if (_tasks_string.find("gwbse")    != std::string::npos) _do_gwbse     = true;
    if (_tasks_string.find("coupling") != std::string::npos) _do_coupling  = true;

    // storage options
    string _store_string = opt->get(key+".store").as<string> ();
    if (_store_string.find("dft") != std::string::npos) _store_dft = true;
    if (_store_string.find("singlets") != std::string::npos) _store_singlets = true;
    if (_store_string.find("triplets") != std::string::npos) _store_triplets = true;
    if (_store_string.find("ehint") != std::string::npos) _store_ehint = true;
    if (_store_dft || _store_singlets || _store_triplets || _store_ehint){
        _write_orbfile=true;
    }
    // options for gwbse
    string _gwbse_xml = opt->get(key+".gwbse_options").as<string> ();
    //cout << endl << "... ... Parsing " << _package_xml << endl ;
    load_property_from_xml( _gwbse_options, _gwbse_xml.c_str() );
    string _coupling_xml=opt->get(key + ".bsecoupling_options").as<string>();
    load_property_from_xml(_coupling_options, _coupling_xml.c_str());
    
    // options for dft package
    string _package_xml = opt->get(key+".dftpackage").as<string> ();
    //cout << endl << "... ... Parsing " << _package_xml << endl ;
    load_property_from_xml( _package_options, _package_xml.c_str() );    
    key = "package";
    _package = _package_options.get(key+".name").as<string> ();
    
    // job file specification
     key = "options."+Identify();

        if ( opt->exists(key+".job_file")) {
            _jobfile = opt->get(key+".job_file").as<string>();
        }
        else {
            throw std::runtime_error("Job-file not set. Abort.");
        }
    
    //options for parsing data into sql file   
    key ="options." + Identify()+".readjobfile";
    if ( opt->exists(key+".singlets")) {
            string _parse_string_s = opt->get(key+".singlets").as<string> ();
            _singlet_levels=FillParseMaps(_parse_string_s);       
        }
    if ( opt->exists(key+".triplets")) {
            string _parse_string_t = opt->get(key+".triplets").as<string> ();
            _triplet_levels=FillParseMaps(_parse_string_t);       
        }

     
    

    
}

std::map<std::string, int> IGWBSE::FillParseMaps(string Mapstring){
    std::vector<string> strings_vec;
    boost::algorithm::split( strings_vec, Mapstring, boost::is_any_of("\t,\n"),boost::token_compress_on );
    std::vector<string>::iterator sit;
    std::map<std::string, int> type2level;
    for(sit=strings_vec.begin();sit<strings_vec.end();++sit){
        //std::vector<string>temp;
        std::vector<string> segmentpnumber;
        boost::algorithm::split(segmentpnumber , *sit, boost::is_any_of(": "),boost::token_compress_on );
        if (segmentpnumber.size()!=2){
            cout << segmentpnumber.size()<<endl;
            for (unsigned i=0;i<segmentpnumber.size();i++){
                cout << segmentpnumber[i]<<endl;
            }
            throw runtime_error("Parser igwbse: Segment and exciton labels are not separated properly");
        }
        if (segmentpnumber[1].size()!=2){
            throw runtime_error("State identifier "+segmentpnumber[1]+" unknown, right now only states up to number 9 are parsed. s1,s2,t1, etc..");
        }
         
        //boost::algorithm::split( temp, (*sit), boost::is_any_of(""),boost::token_compress_on );
        int number=boost::lexical_cast<int>(segmentpnumber[1].at(1));
        string type=boost::lexical_cast<string>(segmentpnumber[0]);
        type2level[type]=number; 
    }
    return type2level;
}

void IGWBSE::LoadOrbitals(string file_name, Orbitals* orbitals, Logger *log ) {

    LOG(logDEBUG, *log) << "Loading " << file_name << flush; 
    std::ifstream ifs( file_name.c_str() );
    boost::archive::binary_iarchive ia( ifs );
    try {
        ia >> *orbitals;
    } catch(std::exception &err) {
        LOG(logDEBUG, *log) << "Could not load orbitals from " << file_name << flush; 
        std::cerr << "An error occurred:\n" << err.what() << endl;
    } 
    ifs.close();

}<commit_msg>debugging via ssh<commit_after>/*
 *            Copyright 2009-2016 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 "igwbse.h"

#include <boost/format.hpp>
#include <boost/filesystem.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/algorithm/string/split.hpp>
#include <votca/xtp/logger.h>
#include <votca/tools/constants.h>
#include <votca/xtp/qmpackagefactory.h>

using boost::format;
using namespace boost::filesystem;
using namespace votca::tools;

namespace ub = boost::numeric::ublas;
    
namespace votca { namespace xtp {
    
// +++++++++++++++++++++++++++++ //
// IGWBSE MEMBER FUNCTIONS         //
// +++++++++++++++++++++++++++++ //

void IGWBSE::Initialize(votca::tools::Property* options ) {

 
    
    
    // tasks to be done by IBSE: dft_input, dft_run, dft_parse, mgbft, bse_coupling
    _do_dft_input = false;
    _do_dft_run   = false;
    _do_dft_parse = false;
    _do_gwbse     = false;
    _do_coupling  = false;
    _do_trim      = false;
    
    _store_dft = false;
    _store_singlets = false;
    _store_triplets = false;
    _store_ehint = false;
    _write_orbfile=false;


    // update options with the VOTCASHARE defaults   
    //UpdateWithDefaults( options, "xtp" );
    ParseOptionsXML( options  );
    
    // register all QM packages (Gaussian, turbomole, etc))
    QMPackageFactory::RegisterAll();

}

void IGWBSE::ParseOptionsXML( votca::tools::Property *opt ) {
   
    
    // parsing general ibse options
    string key = "options." + Identify();
   // _energy_difference = opt->get( key + ".degeneracy" ).as< double > ();
    
 
    // job tasks
    string _tasks_string = opt->get(key+".tasks").as<string> ();
    if (_tasks_string.find("input")    != std::string::npos) _do_dft_input = true;
    if (_tasks_string.find("dft")      != std::string::npos) _do_dft_run   = true;
    if (_tasks_string.find("parse")    != std::string::npos) _do_dft_parse = true;
    if (_tasks_string.find("gwbse")    != std::string::npos) _do_gwbse     = true;
    if (_tasks_string.find("coupling") != std::string::npos) _do_coupling  = true;

    // storage options
    string _store_string = opt->get(key+".store").as<string> ();
    if (_store_string.find("dft") != std::string::npos) _store_dft = true;
    if (_store_string.find("singlets") != std::string::npos) _store_singlets = true;
    if (_store_string.find("triplets") != std::string::npos) _store_triplets = true;
    if (_store_string.find("ehint") != std::string::npos) _store_ehint = true;
    if (_store_dft || _store_singlets || _store_triplets || _store_ehint){
        _write_orbfile=true;
    }
    // options for gwbse
    string _gwbse_xml = opt->get(key+".gwbse_options").as<string> ();
    //cout << endl << "... ... Parsing " << _package_xml << endl ;
    load_property_from_xml( _gwbse_options, _gwbse_xml.c_str() );
    string _coupling_xml=opt->get(key + ".bsecoupling_options").as<string>();
    load_property_from_xml(_coupling_options, _coupling_xml.c_str());
    
    // options for dft package
    string _package_xml = opt->get(key+".dftpackage").as<string> ();
    //cout << endl << "... ... Parsing " << _package_xml << endl ;
    load_property_from_xml( _package_options, _package_xml.c_str() );    
    key = "package";
    _package = _package_options.get(key+".name").as<string> ();
    
    // job file specification
     key = "options."+Identify();

        if ( opt->exists(key+".job_file")) {
            _jobfile = opt->get(key+".job_file").as<string>();
        }
        else {
            throw std::runtime_error("Job-file not set. Abort.");
        }
    
    //options for parsing data into sql file   
    key ="options." + Identify()+".readjobfile";
    if ( opt->exists(key+".singlets")) {
            string _parse_string_s = opt->get(key+".singlets").as<string> ();
            _singlet_levels=FillParseMaps(_parse_string_s);       
        }
    if ( opt->exists(key+".triplets")) {
            string _parse_string_t = opt->get(key+".triplets").as<string> ();
            _triplet_levels=FillParseMaps(_parse_string_t);       
        }

     
    

    
}

std::map<std::string, int> IGWBSE::FillParseMaps(string Mapstring){
    std::vector<string> strings_vec;
    boost::algorithm::split( strings_vec, Mapstring, boost::is_any_of("\t,\n"),boost::token_compress_on );
    std::vector<string>::iterator sit;
    std::map<std::string, int> type2level;
    for(sit=strings_vec.begin();sit<strings_vec.end();++sit){
        //std::vector<string>temp;
        std::vector<string> segmentpnumber;
        boost::algorithm::split(segmentpnumber , *sit, boost::is_any_of(": "),boost::token_compress_on );
        if (segmentpnumber.size()!=2){
            cout << segmentpnumber.size()<<endl;
            for (unsigned i=0;i<segmentpnumber.size();i++){
                cout << i<<":"<< segmentpnumber[i]<<endl;
            }
            throw runtime_error("Parser igwbse: Segment and exciton labels are not separated properly");
        }
        if (segmentpnumber[1].size()!=2){
            throw runtime_error("State identifier "+segmentpnumber[1]+" unknown, right now only states up to number 9 are parsed. s1,s2,t1, etc..");
        }
         
        //boost::algorithm::split( temp, (*sit), boost::is_any_of(""),boost::token_compress_on );
        int number=boost::lexical_cast<int>(segmentpnumber[1].at(1));
        string type=boost::lexical_cast<string>(segmentpnumber[0]);
        type2level[type]=number; 
    }
    return type2level;
}

void IGWBSE::LoadOrbitals(string file_name, Orbitals* orbitals, Logger *log ) {

    LOG(logDEBUG, *log) << "Loading " << file_name << flush; 
    std::ifstream ifs( file_name.c_str() );
    boost::archive::binary_iarchive ia( ifs );
    try {
        ia >> *orbitals;
    } catch(std::exception &err) {
        LOG(logDEBUG, *log) << "Could not load orbitals from " << file_name << flush; 
        std::cerr << "An error occurred:\n" << err.what() << endl;
    } 
    ifs.close();

}
{'fraction_non_alphanumeric': 0.1566283179186405, 'fraction_numerical': 0.004060455673358899, 'mean_word_length': 3.1330018645121194, 'pattern_counts': {'":': 3, '<': 147, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 22}, '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': '5268578', 'n_tokens_mistral': 4461, 'n_tokens_neox': 4314, 'n_words': 887}