void TestMain() { boost::shared_ptr<A> spA(new A); boost::shared_ptr<B> spB(new B); spA->m_spB = spB; spB->m_spA = spA; }
bool NJAABuilder::putInFunc(xmlNodePtr cur) { string corr_tag("correlation"); string jastfunction("pade"); const xmlChar *ftype = xmlGetProp(cur, (const xmlChar *)"function"); if(ftype) jastfunction = (const char*) ftype; int ng = targetPtcl.groups(); ///happends only once if(InFunc.size() != ng*ng) { InFunc.resize(ng*ng,0); } int ia=0, ib=0, iab=0; cur = cur->children; while(cur != NULL) { string cname((const char*)(cur->name)); if(cname == "grid") { gridPtr=cur; //save the pointer } else if(cname ==corr_tag) { string spA((const char*)(xmlGetProp(cur,(const xmlChar *)"speciesA"))); string spB((const char*)(xmlGetProp(cur,(const xmlChar *)"speciesB"))); const xmlChar* refptr=xmlGetProp(cur,(const xmlChar *)"ref"); const xmlChar* idptr=xmlGetProp(cur,(const xmlChar *)"id"); if(!IgnoreSpin) { //get the species ia = targetPtcl.getSpeciesSet().findSpecies(spA); ib = targetPtcl.getSpeciesSet().findSpecies(spB); iab = ia*ng+ib; } if(!(InFunc[ia*ng+ib])) { InFuncType *j2=createInFunc(jastfunction,ia,ib); j2->put(cur,targetPsi.VarList); InFunc[ia*ng+ib]= j2; XMLReport("Added Jastrow Correlation between "<<spA<<" and "<<spB) } else {
/** Create a two-body Jatrow function with a template *@param cur the current xmlNode *@param dummy null pointer used to identify FN * *The template class JeeType is a functor which handles the *evaluation of the function value, gradients and laplacians using *distance tables. This is a specialized builder function for *spin-dependent Jastrow function,e.g., for electrons, two functions *are created for uu(dd) and ud(du). */ template <class FN> TwoBodyJastrowOrbital<FN>* JAABuilder::createJAA(xmlNodePtr cur, const string& jname) { string corr_tag("correlation"); int ng = targetPtcl.groups(); int ia=0, ib=0, iab=0; xmlNodePtr gridPtr=NULL; cur = cur->children; const SpeciesSet& species(targetPtcl.getSpeciesSet()); typedef TwoBodyJastrowOrbital<FN> JeeType; JeeType *J2 = new JeeType(targetPtcl,targetPsi.is_manager()); typedef DiffTwoBodyJastrowOrbital<FN> dJ2Type; dJ2Type *dJ2 = new dJ2Type(targetPtcl); RealType rc=targetPtcl.Lattice.WignerSeitzRadius; int pairs=0; while (cur != NULL) { string cname((const char*)(cur->name)); if (cname == corr_tag) { string spA("u"); string spB("u"); OhmmsAttributeSet rAttrib; rAttrib.add(spA, "speciesA"); rAttrib.add(spA, "species1"); rAttrib.add(spB, "speciesB"); rAttrib.add(spB, "species2"); rAttrib.put(cur); if (spA==targetPsi.getName()) //could have used the particle name { spA=species.speciesName[0]; spB=species.speciesName[0]; } int ia = species.findSpecies(spA); int ib = species.findSpecies(spB); if (ia==species.size() || ia == species.size()) { APP_ABORT("JAABuilder::createJAA is trying to use invalid species"); } string pairID=spA+spB; FN *j= new FN; j->cutoff_radius=rc; j->put(cur); J2->addFunc(pairID,ia,ib,j); dJ2->addFunc(pairID,ia,ib,j); ++pairs; } cur = cur->next; } // while cur if (pairs) { J2->dPsi=dJ2; string j2name="J2_"+jname; targetPsi.addOrbital(J2,j2name); return J2; } else {//clean up and delete the twobody orbitals APP_ABORT("JAABuilder::put Failed to create Two-Body with "+jname); return 0; } }
bool JastrowBuilder::createTwoBodySpin(xmlNodePtr cur, JeeType* J2) { /**\typedef The type of a simple function,e.g., PadeJastrow<double> */ typedef typename JeeType::FuncType FuncType; int cur_var = targetPsi.VarList.size(); DistanceTableData* d_table = DistanceTable::getTable(DistanceTable::add(targetPtcl)); int ng = targetPtcl.groups(); map<string,FuncType*> jastrowMap; vector<FuncType*> jastrow(ng*ng); for(int i=0; i<ng*ng; i++) jastrow[i]=0; int nj = 0; cur = cur->children; while(cur != NULL) { string cname((const char*)(cur->name)); //if(cname == dtable_tag) { // string source_name((const char*)(xmlGetProp(cur,(const xmlChar *)"source"))); // //int iptcl = 0; // map<string,ParticleSet*>::iterator pit(ptclPool.find(source_name)); // if(pit == ptclPool.end()) return false; // ParticleSet* a = (*pit).second; // d_table = DistanceTable::getTable(DistanceTable::add(*a)); // ng = a->groups(); // //create a Jastrow function for each pair type // //for spin 1/2 particles (up-up, down-up = up-down, // //down-down) // for(int i=0; i<ng*ng; i++) jastrow.push_back(NULL); //} else if(cname ==corr_tag) { if(cname ==corr_tag) { string spA((const char*)(xmlGetProp(cur,(const xmlChar *)"speciesA"))); string spB((const char*)(xmlGetProp(cur,(const xmlChar *)"speciesB"))); const xmlChar* refptr=xmlGetProp(cur,(const xmlChar *)"ref"); const xmlChar* idptr=xmlGetProp(cur,(const xmlChar *)"id"); int ia = targetPtcl.getSpeciesSet().findSpecies(spA); int ib = targetPtcl.getSpeciesSet().findSpecies(spB); int iab = ia*ng+ib; if(!(jastrow[iab])) { //create the new Jastrow function FuncType *j2=NULL; if(refptr == NULL) { j2 = new FuncType; } else { typename map<string,FuncType*>::iterator it(jastrowMap.find((const char*)refptr)); if(it != jastrowMap.end()) { j2 = new FuncType((*it).second); } else { j2 = new FuncType; } } if(idptr == NULL) { ostringstream idassigned; idassigned << "j2"<<iab; jastrowMap[idassigned.str()]=j2; } else { jastrowMap[(const char*)idptr]=j2; } //initialize j2->put(cur,targetPsi.VarList); jastrow[iab]= j2; if(ia != ib) {//up-down-type pair, treat down-up the same way jastrow[ib*ng+ia] = j2; } else { for(int iaa=0; iaa<ng; iaa++) if(iaa != ia) jastrow[iaa*ng+iaa] = j2; } XMLReport("Added Jastrow Correlation between "<<spA<<" and "<<spB) nj++; } else { ERRORMSG("Using an existing Jastrow Correlation "<<spA<<" and "<<spB) } } cur = cur->next; } // while cur
template<typename Scalar> void sparse_llt(int rows, int cols) { double density = std::max(8./(rows*cols), 0.01); typedef Matrix<Scalar,Dynamic,Dynamic> DenseMatrix; typedef Matrix<Scalar,Dynamic,1> DenseVector; // TODO fix the issue with complex (see SparseLLT::solveInPlace) SparseMatrix<Scalar> m2(rows, cols); DenseMatrix refMat2(rows, cols); DenseVector b = DenseVector::Random(cols); DenseVector ref_x(cols), x(cols); DenseMatrix B = DenseMatrix::Random(rows,cols); DenseMatrix ref_X(rows,cols), X(rows,cols); initSparse<Scalar>(density, refMat2, m2, ForceNonZeroDiag|MakeLowerTriangular, 0, 0); for(int i=0; i<rows; ++i) m2.coeffRef(i,i) = refMat2(i,i) = internal::abs(internal::real(refMat2(i,i))); ref_x = refMat2.template selfadjointView<Lower>().llt().solve(b); if (!NumTraits<Scalar>::IsComplex) { x = b; SparseLLT<SparseMatrix<Scalar> > (m2).solveInPlace(x); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: default"); } #ifdef EIGEN_CHOLMOD_SUPPORT // legacy API { // Cholmod, as configured in CholmodSupport.h, only supports self-adjoint matrices SparseMatrix<Scalar> m3 = m2.adjoint()*m2; DenseMatrix refMat3 = refMat2.adjoint()*refMat2; ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b); x = b; SparseLLT<SparseMatrix<Scalar>, Cholmod>(m3).solveInPlace(x); VERIFY((m3*x).isApprox(b,test_precision<Scalar>()) && "LLT legacy: cholmod solveInPlace"); x = SparseLLT<SparseMatrix<Scalar>, Cholmod>(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT legacy: cholmod solve"); } // new API { // Cholmod, as configured in CholmodSupport.h, only supports self-adjoint matrices SparseMatrix<Scalar> m3 = m2 * m2.adjoint(), m3_lo(rows,rows), m3_up(rows,rows); DenseMatrix refMat3 = refMat2 * refMat2.adjoint(); m3_lo.template selfadjointView<Lower>().rankUpdate(m2,0); m3_up.template selfadjointView<Upper>().rankUpdate(m2,0); // with a single vector as the rhs ref_x = refMat3.template selfadjointView<Lower>().llt().solve(b); x = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); x = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3).solve(b); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); x = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3_lo).solve(b); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); x = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3_up).solve(b); VERIFY(ref_x.isApprox(x,test_precision<Scalar>()) && "LLT: cholmod solve, single dense rhs"); // with multiple rhs ref_X = refMat3.template selfadjointView<Lower>().llt().solve(B); #ifndef EIGEN_DEFAULT_TO_ROW_MAJOR // TODO make sure the API is properly documented about this fact X = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3).solve(B); VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "LLT: cholmod solve, multiple dense rhs"); X = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3).solve(B); VERIFY(ref_X.isApprox(X,test_precision<Scalar>()) && "LLT: cholmod solve, multiple dense rhs"); #endif // with a sparse rhs SparseMatrix<Scalar> spB(rows,cols), spX(rows,cols); B.diagonal().array() += 1; spB = B.sparseView(0.5,1); ref_X = refMat3.template selfadjointView<Lower>().llt().solve(DenseMatrix(spB)); spX = CholmodDecomposition<SparseMatrix<Scalar>, Lower>(m3).solve(spB); VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs"); spX = CholmodDecomposition<SparseMatrix<Scalar>, Upper>(m3).solve(spB); VERIFY(ref_X.isApprox(spX.toDense(),test_precision<Scalar>()) && "LLT: cholmod solve, multiple sparse rhs"); } #endif }
bool BsplineJastrowBuilder::put(xmlNodePtr cur) { ReportEngine PRE(ClassName,"put(xmlNodePtr)"); bool PrintTables=false; typedef BsplineFunctor<RealType> RadFuncType; // Create a one-body Jastrow if (sourcePtcl) { string j1spin("no"); OhmmsAttributeSet jAttrib; jAttrib.add(j1spin,"spin"); jAttrib.put(cur); #ifdef QMC_CUDA return createOneBodyJastrow<OneBodyJastrowOrbitalBspline,DiffOneBodySpinJastrowOrbital<RadFuncType> >(cur); #else //if(sourcePtcl->IsGrouped) //{ // app_log() << "Creating OneBodySpinJastrowOrbital<T> " << endl; // return createOneBodyJastrow<OneBodySpinJastrowOrbital<RadFuncType>,DiffOneBodySpinJastrowOrbital<RadFuncType> >(cur); //} //else //{ // app_log() << "Creating OneBodyJastrowOrbital<T> " << endl; // return createOneBodyJastrow<OneBodyJastrowOrbital<RadFuncType>,DiffOneBodyJastrowOrbital<RadFuncType> >(cur); //} if(j1spin=="yes") return createOneBodyJastrow<OneBodySpinJastrowOrbital<RadFuncType>,DiffOneBodySpinJastrowOrbital<RadFuncType> >(cur); else return createOneBodyJastrow<OneBodyJastrowOrbital<RadFuncType>,DiffOneBodyJastrowOrbital<RadFuncType> >(cur); #endif } else // Create a two-body Jastrow { string init_mode("0"); { OhmmsAttributeSet hAttrib; hAttrib.add(init_mode,"init"); hAttrib.put(cur); } BsplineInitializer<RealType> j2Initializer; xmlNodePtr kids = cur->xmlChildrenNode; #ifdef QMC_CUDA typedef TwoBodyJastrowOrbitalBspline J2Type; #else typedef TwoBodyJastrowOrbital<BsplineFunctor<RealType> > J2Type; #endif typedef DiffTwoBodyJastrowOrbital<BsplineFunctor<RealType> > dJ2Type; int taskid=(targetPsi.is_manager())?targetPsi.getGroupID():-1; J2Type *J2 = new J2Type(targetPtcl,taskid); dJ2Type *dJ2 = new dJ2Type(targetPtcl); SpeciesSet& species(targetPtcl.getSpeciesSet()); int chargeInd=species.addAttribute("charge"); //std::map<std::string,RadFuncType*> functorMap; bool Opt(false); while (kids != NULL) { std::string kidsname((const char*)kids->name); if (kidsname == "correlation") { OhmmsAttributeSet rAttrib; RealType cusp=-1e10; string pairType("0"); string spA(species.speciesName[0]); string spB(species.speciesName[0]); rAttrib.add(spA,"speciesA"); rAttrib.add(spB,"speciesB"); rAttrib.add(pairType,"pairType"); rAttrib.add(cusp,"cusp"); rAttrib.put(kids); if(pairType[0]=='0') { pairType=spA+spB; } else { PRE.warning("pairType is deprecated. Use speciesA/speciesB"); //overwrite the species spA=pairType[0]; spB=pairType[1]; } int ia = species.findSpecies(spA); int ib = species.findSpecies(spB); if(ia==species.size() || ib == species.size()) { PRE.error("Failed. Species are incorrect.",true); } if(cusp<-1e6) { RealType qq=species(chargeInd,ia)*species(chargeInd,ib); cusp = (ia==ib)? -0.25*qq:-0.5*qq; } app_log() << " BsplineJastrowBuilder adds a functor with cusp = " << cusp << endl; RadFuncType *functor = new RadFuncType(cusp); functor->cutoff_radius = targetPtcl.Lattice.WignerSeitzRadius; bool initialized_p=functor->put(kids); functor->elementType=pairType; if (functor->cutoff_radius < 1.0e-6) { app_log() << " BsplineFunction rcut is currently zero.\n" << " Setting to Wigner-Seitz radius = " << targetPtcl.Lattice.WignerSeitzRadius << endl; functor->cutoff_radius = targetPtcl.Lattice.WignerSeitzRadius; functor->reset(); } //RPA INIT if(!initialized_p && init_mode =="rpa") { app_log() << " Initializing Two-Body with RPA Jastrow " << endl; j2Initializer.initWithRPA(targetPtcl,*functor,-cusp/0.5); } J2->addFunc(ia,ib,functor); dJ2->addFunc(ia,ib,functor); Opt=(!functor->notOpt or Opt); if(qmc_common.io_node) { char fname[32]; if(qmc_common.mpi_groups>1) sprintf(fname,"J2.%s.g%03d.dat",pairType.c_str(),taskid); else sprintf(fname,"J2.%s.dat",pairType.c_str()); functor->setReportLevel(ReportLevel,fname); functor->print(); } } kids = kids->next; } //dJ2->initialize(); //J2->setDiffOrbital(dJ2); J2->dPsi=dJ2; targetPsi.addOrbital(J2,"J2_bspline"); J2->setOptimizable(Opt); } return true; }