Exemplo n.º 1
0
void Average_ekt::evaluate_obdm(Wavefunction_data * wfdata, Wavefunction * wf,
    System * sys, Sample_point * sample, Average_return & avg) { 


  wf->updateVal(wfdata,sample);
  Wf_return wfval_base(wf->nfunc(),2);
  wf->getVal(wfdata,0,wfval_base);
  int nup=sys->nelectrons(0);
  int ndown=sys->nelectrons(1);
  int nelectrons=nup+ndown;

  Array1 <Array2 <dcomplex> > movals1_base(nelectrons);
  for(int e=0; e< nelectrons; e++) { 
    movals1_base(e).Resize(nmo,1);
    calc_mos(sample,e,movals1_base(e));
    /*! Huihuo
      Here, permutation symmetry has been applied, the real evaluation quantity is 
      sum_(e=1)^N phi_i* (r_e) phi_j(r') psi(r_1,... r',...r_N)/psi(r_1, ..., r_n, ..., r_N)
      Therefore, movals1_base[e, i] = phi_i(r_e), where e is the index of the electron. 
      !!!One need to be careful about the off-gamma point 1 RDM 
      */
  }
  //  avg.vals.Resize(nmo+4*nmo*nmo);
  //  avg.vals=0;

  Array2 <dcomplex> movals1(nmo,1);
  Array1 <Wf_return> wfs(nelectrons);

  Wavefunction_storage * store;
  wf->generateStorage(store);
  for(int i=0; i< npoints_eval; i++) { 
    Array1 <doublevar> oldpos(3);
    sample->getElectronPos(0,oldpos);
    sample->setElectronPosNoNotify(0,saved_r(i));
    calc_mos(sample,0,movals1);
    /*!
      This is simply to calculate phi_j(r'): Noted that r' has been given, therefore, movals1 is a one dimentional array, with matrix element to be 
      phi_j(r') -- j is the index 
      */
    sample->setElectronPosNoNotify(0,oldpos);

    Array1 <Wf_return> wf_eval;
    wf->evalTestPos(saved_r(i),sample,wfs);

    //Testing the evalTestPos
    //for(int e=0; e< nelectrons; e++) { 
    //  Wf_return test_wf(wf->nfunc(),2);
    //  sample->getElectronPos(e,oldpos);
    //  wf->saveUpdate(sample,e,store);
    //  sample->setElectronPos(e,saved_r(i));
    //  wf->updateVal(wfdata,sample);
    //  wf->getVal(wfdata,e,test_wf);
    //  sample->setElectronPos(e,oldpos);
    //  wf->restoreUpdate(sample,e,store);
    //  cout << "e " << e << " test " << test_wf.amp(0,0) << " evalTestPos " << wfs(e).amp(0,0) << endl;
    //}

    doublevar dist1=0;
    for(int m=0; m < nmo; m++) 
      dist1+=norm(movals1(m,0));

    for(int orbnum=0; orbnum < nmo; orbnum++) { 
      avg.vals(orbnum)+=norm(movals1(orbnum,0))/(dist1*npoints_eval);
    }

    for(int e=0; e< nelectrons; e++) { 
      dcomplex psiratio_1b=exp(dcomplex(wfs(e).amp(0,0)-wfval_base.amp(0,0),
            wfs(e).phase(0,0)-wfval_base.phase(0,0)));
      /*!
        psi(r_1,...,r',...,r_N)/psi(r_1,...,r_e,...,r_N), the ratio of the new with respect to the old if one change the position of the e-th electron from 
        r_e to r'
        */
      int which_obdm=0;
      if(e >= nup) { which_obdm=1;  } 
      dcomplex tmp;
      int place=0;
      dcomplex prefactor=psiratio_1b/(dist1*npoints_eval);
      for(int orbnum=0; orbnum < nmo; orbnum++) { 
        for(int orbnum2=0; orbnum2 < nmo; orbnum2++) { 
          //tmp=movals1(orbnum,0)*conj(movals1_base(e)(orbnum2,0))*prefactor;
          /*! Huihuo Zheng
            This is to based on my new definition: 
            \rho(r, r') = N\int dR psi*(r, R) psi(r', R) = \sum rho_{ij} phi_i*(r) phi_j(r')
            in this way, one will find that, the phase factor is cancelled
            */
          tmp=conj(movals1(orbnum,0))*movals1_base(e)(orbnum2,0)*prefactor;
          avg.vals(nmo+2*which_obdm*nmo*nmo+place)+=tmp.real();
          avg.vals(nmo+2*which_obdm*nmo*nmo+place+1)+=tmp.imag();

          place+=2;
        }
      }

    }
  }
  delete store;
}
Exemplo n.º 2
0
int test_main( int, char*[] )
{
    {   // basic_filebuf runtime results are ignored; as long as they don't crash
        // or throw we are satisfied
        fs::basic_filebuf<char> bfb;
        fs::filebuf cfb;

        bfb.open( "fstream_test_bffoo", std::ios_base::in );
        cfb.open( "fstream_test_bffoo", std::ios_base::in );

#   ifndef BOOST_NO_STD_WSTRING
        fs::wfilebuf wfb;
        wfb.open( "fstream_test_bffoo", std::ios_base::in );
#   endif
    }

    std::remove( "fstream_test_bfoo" );
    std::remove( "fstream_test_cfoo" );
# ifndef BOOST_NO_STD_WSTRING
    std::remove( "fstream_test_wfoo" );
# endif
    {
        fs::basic_ofstream<char> bofs( "fstream_test_bfoo" );
        fs::ofstream cofs( "fstream_test_cfoo" );

        BOOST_CHECK( bofs.is_open() );
        BOOST_CHECK( cofs.is_open() );

        bofs << "fstream_test_bfoo";
        cofs << "fstream_test_cfoo";

        // these will fail, but they still test the interface
        bofs.open( "fstream_test_bfoo" );
        cofs.open( "fstream_test_cfoo" );

#   ifndef BOOST_NO_STD_WSTRING
        fs::wofstream wofs( "fstream_test_wfoo" );
        BOOST_CHECK( wofs.is_open() );
        wofs << L"fstream_test_wfoo";
        wofs.open( "fstream_test_wfoo" ); // expected to fail
#   endif
    }

    {
        fs::basic_ifstream<char> bifs( "fstream_test_bfoo" );
        fs::ifstream cifs( "fstream_test_cfoo" );

        BOOST_CHECK( bifs.is_open() );
        BOOST_CHECK( cifs.is_open() );

        std::string b;
        std::string c;

        bifs >> b;
        cifs >> c;

        BOOST_CHECK( b == "fstream_test_bfoo" );
        BOOST_CHECK( c == "fstream_test_cfoo" );

        // these will fail, but they still test the interface
        bifs.open( "fstream_test_bfoo" );
        cifs.open( "fstream_test_cfoo" );

#   ifndef BOOST_NO_STD_WSTRING
        fs::wifstream wifs( "fstream_test_wfoo" );
        BOOST_CHECK( wifs.is_open() );
        std::wstring w;
        wifs >> w;
        BOOST_CHECK( w == L"fstream_test_wfoo" );
        wifs.open( "fstream_test_wfoo" ); // expected to fail
#   endif
    }

    {
        fs::basic_fstream<char> bfs( "fstream_test_bfoo" );
        fs::fstream cfs( "fstream_test_cfoo" );

        BOOST_CHECK( bfs.is_open() );
        BOOST_CHECK( cfs.is_open() );

        std::string b;
        std::string c;

        bfs >> b;
        cfs >> c;

        BOOST_CHECK( b == "fstream_test_bfoo" );
        BOOST_CHECK( c == "fstream_test_cfoo" );

        // these will fail, but they still test the interface
        bfs.open( "fstream_test_bfoo" );
        cfs.open( "fstream_test_cfoo" );

#   ifndef BOOST_NO_STD_WSTRING
        fs::wfstream wfs( "fstream_test_wfoo" );
        BOOST_CHECK( wfs.is_open() );
        std::wstring w;
        wfs >> w;
        BOOST_CHECK( w == L"fstream_test_wfoo" );
        wfs.open( "fstream_test_wfoo" ); // expected to fail
#   endif
    }

//  std::remove( "fstream_test_bfoo" );
//  std::remove( "fstream_test_cfoo" );
//  #   ifndef BOOST_NO_STD_WSTRING
//  std::remove( "fstream_test_wfoo" );
//  #   endif
    return 0;
}
Exemplo n.º 3
0
void Average_ekt::evaluate_valence(Wavefunction_data * wfdata, Wavefunction * wf,
    System * sys, Pseudopotential *psp, Sample_point * sample, Average_return & avg) { 
  /*
     This is to evaluate the v_ij in extended Koopmans' theorem -- for valence bands
     */
  wf->updateVal(wfdata,sample);
  Wf_return wfval_base(wf->nfunc(),2);
  wf->getVal(wfdata,0,wfval_base);
  int nup=sys->nelectrons(0);
  int ndown=sys->nelectrons(1);
  int nelectrons=nup+ndown;

  Array1 <doublevar> rn(3), rnp(3), rnpp(3); 
  Array1 <Array2 <dcomplex> > movals1_base(nelectrons);
  Array2 <dcomplex> movals_p(nmo, 1); 
  Array1 <doublevar> VLoc(nelectrons); 
  Array1 <doublevar> VLoc0(nelectrons);
  Array1 <doublevar> Vtest(nelectrons+1);
  Array1 <dcomplex> pseudo_t(nmo); 
  /***************************************
    Routines to get Kin and Vloc
   ****************************************/
  Array2<doublevar> Kin(nelectrons, wf->nfunc());
  //Array2<doublevar> Kin0(nelectrons, wf->nfunc());
  Array2<dcomplex> movals_lap(nmo, 5);
  for(int e=0; e< nelectrons; e++) { 
    movals1_base(e).Resize(nmo,1);
    //Kin(e).Resize(1); //The Kinetic energy 
    calc_mos(sample, e, movals1_base(e));
  }
//  sys->calcKineticSeparated(sample,saved_r(i),Kin);

  //***** calculate kinetic energy and potential energy
  sys->calcKineticSeparated(wfdata, sample, wf, Kin); 
  sys->calcLocSeparated(sample, VLoc);

  Array1 <Wf_return> wfs(nelectrons);

  Wavefunction_storage * store;
  wf->generateStorage(store);
  for(int i=0; i< npoints_eval; i++) {
    Array1 <doublevar> oldpos(3);
    sample->getElectronPos(0,oldpos);
    sample->setElectronPosNoNotify(0, saved_r(i));
    calc_mosLap(sample, 0, movals_lap);
    int nrandvar=psp->nTest();
    Array1 <doublevar> rand_num(nrandvar);
    for(int l=0; l< nrandvar; l++) 
      rand_num(l)=rng.ulec();
    calc_mos(sample,0,movals_p);

    calcPseudoMo(sys, sample, psp, rand_num, pseudo_t);
    sample->setElectronPosNoNotify(0,oldpos);
    Array1 <Wf_return> wf_eval;
    wf->evalTestPos(saved_r(i),sample,wfs);
    sys->calcLocWithTestPos(sample, saved_r(i), Vtest);

    doublevar vtot = 0.0;
    for (int e=0; e<nelectrons; e++) {
      vtot += Vtest(e);
    }
    doublevar dist1=0;
    for(int m=0; m < nmo; m++)
      dist1+=norm(movals_p(m,0));
    
    for(int orbnum=0; orbnum < nmo; orbnum++) {
      avg.vals(orbnum)+=norm(movals_p(orbnum,0))/(dist1*npoints_eval);//this is the normalization
    }
    
    Array2<doublevar> totalv(nelectrons, wf->nfunc());
    totalv = 0.0;
    psp->calcNonlocSeparated(wfdata, sys, sample, wf, totalv);
    
    int place = 0;
    for (int orbnum = 0; orbnum < nmo; orbnum++ ) {
      for (int orbnum2 = 0; orbnum2 < nmo; orbnum2++ ) {
        dcomplex tmp3=conj(movals_p(orbnum, 0))*
                    (pseudo_t(orbnum2) +
                     Vtest(nelectrons)*movals_p(orbnum2, 0)
                      -0.5*movals_lap(orbnum2, 4)
                     +movals_p(orbnum2, 0)*vtot)
                /(dist1*npoints_eval);
        //	tmp3=conj(movals_p(orbnum, 0))*(movals_p(orbnum2, 0))/(dist1*npoints_eval);
        //dcomplex tmp3=conj(movals_p(orbnum, 0))*(pseudo_t(orbnum2))/(dist1*npoints_eval);
        int which_obdm = 0;
        avg.vals(nmo+8*nmo*nmo + 2*which_obdm*nmo*nmo+place) += tmp3.real();
        avg.vals(nmo+8*nmo*nmo + 2*which_obdm*nmo*nmo+place+1) += tmp3.imag();
        
        which_obdm = 1;
        //tmp3=conj(movals_p(orbnum, 0))*(-0.5*movals_lap(orbnum2, 4))/(dist1*npoints_eval);
        avg.vals(nmo+8*nmo*nmo + 2*which_obdm*nmo*nmo+place) += tmp3.real();
        avg.vals(nmo+8*nmo*nmo + 2*which_obdm*nmo*nmo+place+1) += tmp3.imag();
        place += 2;
      }
    }
    ofstream dump;
    if(dump_data) {
      dump.open("EKT_DUMP",ios::app);
    }
    for(int e=0; e< nelectrons; e++) {
      dcomplex psiratio_1b=conj(exp(dcomplex(wfs(e).amp(0,0)
                                             -wfval_base.amp(0,0),
                                             wfs(e).phase(0,0)
                                             -wfval_base.phase(0,0))));
      int which_obdm=0;
      if(e >= nup) { which_obdm=1;  }
      dcomplex tmp, tmp2, tmp3;
      int place=0;
      dcomplex prefactor=psiratio_1b/(dist1*npoints_eval);
      //cout << "Local potential" << "   Kinetic" << "   Pseudo" << endl;
      //      cout << VLoc(e) << "  " << Kin(e, 0) << "  " << totalv(e, 0) << endl;
      
      for(int orbnum=0; orbnum < nmo; orbnum++) {
        for(int orbnum2=0; orbnum2 < nmo; orbnum2++) {
          //	  assert(wf->nfunc()==1);
          tmp = 0.5*(movals_p(orbnum,0)*conj(movals1_base(e)(orbnum2,0))*prefactor
                     + movals1_base(e)(orbnum, 0)*conj(movals_p(orbnum2, 0))*conj(prefactor));
          tmp2 = tmp*(VLoc(e) + Kin(e, 0) + totalv(e, 0));
          //rho_ij part the one body reduced density matrix
          doublevar tmp2_mag=abs(tmp2);
          if(tmp2_mag > ekt_cutoff) {
            tmp2*=ekt_cutoff/tmp2_mag;
          }
          avg.vals(nmo+2*which_obdm*nmo*nmo+place) += tmp.real();
          avg.vals(nmo+2*which_obdm*nmo*nmo+place+1) += tmp.imag();
          //v_ij^v part
          //tmp3 = 0.0;
          avg.vals(nmo+4*nmo*nmo + 2*which_obdm*nmo*nmo+place)+=tmp2.real();
          avg.vals(nmo+4*nmo*nmo + 2*which_obdm*nmo*nmo+place+1)+=tmp2.imag();
          if(dump_data) {
            if(orbnum==orbnum2 and orbnum==0) {
              sample->getElectronPos(e,oldpos);
              dump << which_obdm << "," << tmp2.real() << "," << tmp.real() << ","
                 << VLoc(e) << "," << Kin(e,0) << "," << totalv(e,0) <<
               "," << oldpos(0) << "," << oldpos(1) << "," << oldpos(2) << endl;
            }
          }
          if (eval_conduction) {
            //tmp3 = -1.0*psiratio_1b*conj(movals1_base(e)(orbnum, 0))*(vtot*movals_lap(orbnum2, 0)
            //						 + pseudo_t(orbnum2) - 0.5*movals_lap(orbnum2, 4) + Vtest(nelectrons)*movals_lap(orbnum2, 0))/(dist1*npoints_eval);
            tmp3 = -1.0*conj(movals1_base(e)(orbnum, 0))*movals_p(orbnum2, 0)*prefactor*(VLoc(e) + Kin(e, 0) + totalv(e, 0) + Vtest(e));
            doublevar tmp3_mag=abs(tmp3);
            if(tmp3_mag > ekt_cutoff) {
              tmp3*=ekt_cutoff/tmp3_mag;
            }
            avg.vals(nmo+8*nmo*nmo + 2*which_obdm*nmo*nmo+place) += tmp3.real();
            avg.vals(nmo+8*nmo*nmo + 2*which_obdm*nmo*nmo+place+1) += tmp3.imag();
          }
          place+=2;
        }
      }
    }
  }
  delete store;
}
Exemplo n.º 4
0
Arquivo: ows.c Projeto: Ezio47/tinyows
int main(int argc, char *argv[])
{
  ows *o;
  char *query;

  o = ows_init();
  o->config_file = buffer_init();

  /* Config Files */
  if (getenv("TINYOWS_CONFIG_FILE"))
    buffer_add_str(o->config_file, getenv("TINYOWS_CONFIG_FILE"));
  else if (getenv("TINYOWS_MAPFILE")) {
    buffer_add_str(o->config_file, getenv("TINYOWS_MAPFILE"));
    o->mapfile = true;
  } else
    buffer_add_str(o->config_file, OWS_CONFIG_FILE_PATH);

  LIBXML_TEST_VERSION
  xmlInitParser();

  /* Parse the configuration file and initialize ows struct */
  if (!o->exit) ows_parse_config(o, o->config_file->buf);
  if (!o->exit) ows_log(o, 2, "== TINYOWS STARTUP ==");

  /* Connect the ows to the database */
  if (!o->exit) ows_pg(o, o->pg_dsn->buf);
  if (!o->exit) ows_log(o, 2, "== Connection PostGIS ==");

  /* Fill layers storage metadata */
  if (!o->exit) ows_layers_storage_fill(o);
  if (!o->exit) ows_log(o, 2, "== Filling Storage ==");

  o->init = false;

#if TINYOWS_FCGI
  if (!o->exit) ows_log(o, 2, "== FCGI START ==");
  while (FCGI_Accept() >= 0) {
#endif

    query=NULL;
    if (!o->exit) query = cgi_getback_query(o);  /* Retrieve safely query string */
    if (!o->exit) ows_log(o, 4, query);          /* Log input query if asked */

    if (!o->exit && (!query || !strlen(query))) {
      /* Usage or Version command line options */
      if (argc > 1) {
        if (    !strncmp(argv[1], "--help", 6)
                || !strncmp(argv[1], "-h", 2)
                || !strncmp(argv[1], "--check", 7)) ows_usage(o);

        else if (    !strncmp(argv[1], "--version", 9)
                     || !strncmp(argv[1], "-v", 2))
          fprintf(stdout, "%s\n", TINYOWS_VERSION);

        else ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service");

      } else ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service");

      o->exit=true;  /* Have done what we have to */
    }

    if (!o->exit) o->request = ows_request_init();
    if (!o->exit) ows_kvp_or_xml(o, query);  /* Method is KVP or XML ? */

    if (!o->exit) {

      switch (o->request->method) {
        case OWS_METHOD_KVP:
          o->cgi = cgi_parse_kvp(o, query);
          break;
        case OWS_METHOD_XML:
          o->cgi = cgi_parse_xml(o, query);
          break;

        default:
          ows_error(o, OWS_ERROR_REQUEST_HTTP, "Wrong HTTP request Method", "http");
      }
    }

    if (!o->exit) o->psql_requests = list_init();
    if (!o->exit) ows_metadata_fill(o, o->cgi);                    /* Fill service's metadata */
    if (!o->exit) ows_request_check(o, o->request, o->cgi, query); /* Process service request */

    /* Run the right OWS service */
    if (!o->exit) {
      switch (o->request->service) {
        case WFS:
          o->request->request.wfs = wfs_request_init();
          wfs_request_check(o, o->request->request.wfs, o->cgi);
          if (!o->exit) wfs(o, o->request->request.wfs);
          break;
        default:
          ows_error(o, OWS_ERROR_INVALID_PARAMETER_VALUE, "Service Unknown", "service");
      }
    }

    if (o->request) {
      ows_request_free(o->request);
      o->request=NULL;
    }

    /* We allocated memory only on post case */
    if (cgi_method_post() && query) free(query);

#if TINYOWS_FCGI
    fflush(stdout);
    o->exit = false;
  }
  ows_log(o, 2, "== FCGI SHUTDOWN ==");
  OS_LibShutdown();
#endif
  ows_log(o, 2, "== TINYOWS SHUTDOWN ==");
  ows_free(o);

  xmlCleanupParser();

  return EXIT_SUCCESS;
}