void accumulate_onepdm(Matrix& onepdm) { #ifndef SERIAL Matrix tmp_recv; mpi::communicator world; if (world.size() == 1) return; if (!mpigetrank()) { for(int i=1;i<world.size();++i) { world.recv(i, i, tmp_recv); for(int k=0;k<onepdm.Nrows();++k) for(int l=0;l<onepdm.Ncols();++l) if(tmp_recv(k+1,l+1) != 0.) { onepdm(k+1,l+1) = tmp_recv(k+1,l+1); } } } else { world.send(0, mpigetrank(), onepdm); } #endif }
void Twopdm_container::accumulate_spatial_npdm() { #ifndef SERIAL mpi::communicator world; #ifdef NDEBUG if( mpigetrank() == 0) MPI_Reduce(MPI_IN_PLACE, spatial_twopdm.data(), spatial_twopdm.size(), MPI_DOUBLE, MPI_SUM, 0, world); else MPI_Reduce(spatial_twopdm.data(), spatial_twopdm.data(), spatial_twopdm.size(), MPI_DOUBLE, MPI_SUM, 0, world); #else array_4d<double> tmp_recv; if( mpigetrank() == 0) { for(int i=1;i<world.size();++i) { world.recv(i, i, tmp_recv); for(int k=0;k<spatial_twopdm.dim1();++k) for(int l=0;l<spatial_twopdm.dim2();++l) for(int m=0;m<spatial_twopdm.dim3();++m) for(int n=0;n<spatial_twopdm.dim4();++n) if ( abs(tmp_recv(k,l,m,n)) > NUMERICAL_ZERO) { // Test for duplicates assert(abs(spatial_twopdm(k,l,m,n)) < NUMERICAL_ZERO ) ; spatial_twopdm(k,l,m,n) = tmp_recv(k,l,m,n); } } } else { world.send(0, mpigetrank(), spatial_twopdm); } #endif #endif }
void SpinBlock::addAdditionalCompOps() { #ifndef SERIAL boost::mpi::communicator world; if (world.size() == 1) return; //there is no need to have additional compops int length = dmrginp.last_site(); int dotopindex = (sites[0] == 0) ? complementary_sites[0] : complementary_sites[complementary_sites.size()-1]; if (!ops[CRE]->is_local()) { for(int i=0; i<get_sites().size(); i++) { if (ops[CRE]->has(sites[i])) { if (processorindex(sites[i]) != mpigetrank()) ops[CRE]->add_local_indices(sites[i]); mpi::broadcast(world, *(ops[CRE]->get_element(sites[i])[0]), processorindex(sites[i])); } } } for (int i=0; i<complementary_sites.size(); i++) { int compsite = complementary_sites[i]; if (compsite == dotopindex) continue; int I = (compsite > dotopindex) ? compsite : dotopindex; int J = (compsite > dotopindex) ? dotopindex : compsite; if (processorindex(compsite) == processorindex(trimap(I, J, length))) continue; if (processorindex(compsite) == mpigetrank()) { bool other_proc_has_ops = true; world.recv(processorindex(trimap(I, J, length)), 0, other_proc_has_ops); //this will potentially receive some ops if (other_proc_has_ops) { ops[CRE_DESCOMP]->add_local_indices(I, J); recvcompOps(*ops[CRE_DESCOMP], I, J, CRE_DESCOMP); ops[DES_DESCOMP]->add_local_indices(I, J); recvcompOps(*ops[DES_DESCOMP], I, J, DES_DESCOMP); } } else { //this will potentially send some ops if (processorindex(trimap(I, J, length)) == mpigetrank()) { bool this_proc_has_ops = ops[CRE_DESCOMP]->has_local_index(I, J); world.send(processorindex(compsite), 0, this_proc_has_ops); if (this_proc_has_ops) { sendcompOps(*ops[CRE_DESCOMP], I, J, CRE_DESCOMP, compsite); sendcompOps(*ops[DES_DESCOMP], I, J, DES_DESCOMP, compsite); } } else continue; } //dmrginp.datatransfer.stop(); //dmrginp.datatransfer -> stop(); //ROA } #endif }
void SpinBlock::store (bool forward, const vector<int>& sites, SpinBlock& b, int left, int right, char *name) { Timer disktimer; std::string file; if(dmrginp.spinAdapted()) { if (forward) file = str(boost::format("%s%s%d%s%d%s%d%s%d%s%d%s%d%s") % dmrginp.save_prefix() % "/SpinBlock-forward-"% sites[0] % "-" % sites[sites.size()-1] % "." % left % "." % right % "." %b.integralIndex % "." % mpigetrank() % ".tmp" ); else file = str(boost::format("%s%s%d%s%d%s%d%s%d%s%d%s%d%s") % dmrginp.save_prefix() % "/SpinBlock-backward-"% sites[0] % "-" % sites[sites.size()-1] % "." % left % "." % right % "." %b.integralIndex % "." % mpigetrank() % ".tmp" ); } else { if (forward) file = str(boost::format("%s%s%d%s%d%s%d%s%d%s%d%s%d%s") % dmrginp.save_prefix() % "/SpinBlock-forward-"% (sites[0]/2) % "-" % (sites[sites.size()-1]/2) % "." % left % "." % right % "." %b.integralIndex % "." % mpigetrank() % ".tmp" ); else file = str(boost::format("%s%s%d%s%d%s%d%s%d%s%d%s%d%s") % dmrginp.save_prefix() % "/SpinBlock-backward-"% (sites[0]/2) % "-" % (sites[sites.size()-1]/2) % "." % left % "." % right % "." %b.integralIndex % "." % mpigetrank() % ".tmp" ); } p1out << "\t\t\t Saving block file :: " << file << endl; std::ofstream ofs(file.c_str(), std::ios::binary); int lstate = left; int rstate = right; if (mpigetrank()==0) { StateInfo::store(forward, sites, b.braStateInfo, lstate); StateInfo::store(forward, sites, b.ketStateInfo, rstate); } b.Save (ofs); ofs.close(); //p1out << "\t\t\t block save disk time " << disktimer.elapsedwalltime() << " " << disktimer.elapsedcputime() << endl; }
void save_onepdm_text(const Matrix& onepdm, const int &i, const int &j) { //the spatial has a factor of 1/2 in front of it if(!mpigetrank()) { std::vector<int> reorder; reorder.resize(onepdm.Nrows()/2); for (int k=0; k<onepdm.Nrows()/2; k++) { reorder.at(dmrginp.reorder_vector()[k]) = k; } char file[5000]; sprintf (file, "%s%s%d.%d", dmrginp.save_prefix().c_str(),"/onepdm.", i, j); ofstream ofs(file); ofs << onepdm.Nrows() << endl; for(int k=0;k<onepdm.Nrows()/2;++k) for(int l=0;l<onepdm.Ncols()/2;++l) { int K = reorder.at(k), L = reorder.at(l); double opdm = onepdm(2*K+1, 2*L+1) ; ofs << boost::format("%d %d %20.14e\n") % (2*k) % (2*l) % opdm; opdm = onepdm(2*K+2, 2*L+2); ofs << boost::format("%d %d %20.14e\n") % (2*k+1) % (2*l+1) % opdm; } ofs.close(); } }
void Onepdm_container::accumulate_spatial_npdm() { #ifndef SERIAL array_2d<double> tmp_recv; mpi::communicator world; if( mpigetrank() == 0) { for(int i=1;i<calc.size();++i) { calc.recv(i, i, tmp_recv); for(int k=0;k<spatial_onepdm.dim1();++k) for(int l=0;l<spatial_onepdm.dim2();++l) if( abs(tmp_recv(k,l)) > NUMERICAL_ZERO ) spatial_onepdm(k,l) = tmp_recv(k,l); } } else { calc.send(0, mpigetrank(), spatial_onepdm); } #endif }
void Pairpdm_container::calculate_spatial_npdm() { //const std::vector<int>& ro = dmrginp.reorder_vector(); //mpi::communicator world; if( mpigetrank() == 0) { for(int k=0;k<spatial_pairpdm.dim1();++k) for(int l=0;l<spatial_pairpdm.dim2();++l) spatial_pairpdm(k,l)= pairpdm(2*k,2*l+1); } }
void load_onepdm_binary(Matrix& onepdm, const int &i, const int &j) { if(!mpigetrank()) { char file[5000]; sprintf (file, "%s%s%d.%d", dmrginp.save_prefix().c_str(), "/onepdm.", i, j); std::ifstream ifs(file, std::ios::binary); boost::archive::binary_iarchive load(ifs); load >> onepdm; ifs.close(); }
void save_onepdm_binary(const Matrix& onepdm, const int &i, const int &j) { if(!mpigetrank()) { char file[5000]; sprintf (file, "%s%s%d.%d", dmrginp.save_prefix().c_str(),"/onepdm.", i, j); std::ofstream ofs(file, std::ios::binary); boost::archive::binary_oarchive save(ofs); save << onepdm; ofs.close(); } }
void Onepdm_container::save_spatial_npdm_binary(const int &i, const int &j) { if( mpigetrank() == 0) { char file[5000]; sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/spatial_onepdm.", i, j,".bin"); std::ofstream ofs(file, std::ios::binary); boost::archive::binary_oarchive save(ofs); save << spatial_onepdm; ofs.close(); } }
void Twopdm_container::calculate_spatial_npdm() { double factor = 0.5; //mpi::communicator world; if( mpigetrank() == 0) { for(int k=0;k<spatial_twopdm.dim1();++k) for(int l=0;l<spatial_twopdm.dim2();++l) for(int m=0;m<spatial_twopdm.dim3();++m) for(int n=0;n<spatial_twopdm.dim4();++n) spatial_twopdm(k,l,m,n)= factor*(twopdm(2*k,2*l,2*m,2*n)+ twopdm(2*k+1,2*l,2*m,2*n+1)+ twopdm(2*k,2*l+1,2*m+1,2*n)+ twopdm(2*k+1,2*l+1,2*m+1,2*n+1)); } }
void Nevpt2_npdm::compute_A22_matrix( array_6d<double>& eee, array_8d<double>& eeee ) { if( mpigetrank() == 0 ) { std::cout << "Building NEVPT2 A22 matrix\n"; int dim = eee.dim1(); const TwoElectronArray& twoInt = v_2; // Output text file char file[5000]; sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/A22_matrix.", 0, 0,".txt"); ofstream ofs(file); ofs << dim << endl; for(int ap=0; ap<dim; ++ap) { for(int bp=0; bp<dim; ++bp) { for(int cp=0; cp<dim; ++cp) { for(int a=0; a<dim; ++a) { for(int b=0; b<dim; ++b) { int d_bpb = ( bp == b ); for(int c=0; c<dim; ++c) { double val = 0.0; for(int d=0; d<dim; ++d) { for(int e=0; e<dim; ++e) { int d_bpe = ( bp == e ); for(int f=0; f<dim; ++f) { // Factor of 2 on indices to recover spatial two-electron integrals val += twoInt(2*d,2*e,2*f,2*a) * ( 2.0 * d_bpb * eee(cp,ap,d,f,e,c) - eeee(cp,ap,b,bp,d,f,e,c) ); val -= twoInt(2*d,2*c,2*f,2*e) * ( 2.0 * d_bpb * eee(cp,ap,d,f,a,e) - eeee(cp,ap,b,bp,d,f,a,e) ); val += twoInt(2*d,2*e,2*f,2*b) * ( 2.0 * d_bpe * eee(cp,ap,d,f,a,c) - eeee(cp,ap,e,bp,d,f,a,c) ); } } } if ( abs(val) > 1e-14 ) ofs << boost::format("%d %d %d %d %d %d %20.14e\n") % ap % bp % cp % a % b % c % val; } } } } } } ofs.close(); } }
void SpinAdapted::Wavefunction::SaveWavefunctionInfo (const StateInfo &waveInfo, const std::vector<int>& sites, const int wave_num) { char file [5000]; sprintf (file, "%s%s%d%s%d%s%d%s%d%s", dmrginp.save_prefix().c_str(), "/wave-", sites [0], "-", *(sites.rbegin()), ".", mpigetrank(), ".", wave_num, ".tmp"); if (dmrginp.outputlevel() > 0) pout << "\t\t\t Saving Wavefunction " << file << endl; if (mpigetrank() == 0) { std::ofstream ofs(file, std::ios::binary); boost::archive::binary_oarchive save_wave(ofs); save_wave << onedot << waveInfo << *waveInfo.leftStateInfo << *(waveInfo.leftStateInfo->leftStateInfo); save_wave << *(waveInfo.leftStateInfo->rightStateInfo) << *waveInfo.rightStateInfo; if(!onedot) save_wave << *(waveInfo.rightStateInfo->leftStateInfo) << *(waveInfo.rightStateInfo->rightStateInfo); this->Save (ofs); ofs.close(); } }
void SpinAdapted::Wavefunction::LoadWavefunctionInfo (StateInfo &waveInfo, const std::vector<int>& sites, const int wave_num) { char file [5000]; sprintf (file, "%s%s%d%s%d%s%d%s%d%s", dmrginp.load_prefix().c_str(), "/wave-", sites [0], "-", *(sites.rbegin()), ".", mpigetrank(), ".", wave_num, ".tmp"); if (dmrginp.outputlevel() > 0) pout << "\t\t\t Loading Wavefunction " << file << endl; waveInfo.Allocate (); if (mpigetrank() == 0) { std::ifstream ifs(file, std::ios::binary); boost::archive::binary_iarchive load_wave(ifs); load_wave >> onedot >> waveInfo >> *waveInfo.leftStateInfo >> *(waveInfo.leftStateInfo->leftStateInfo) >> *(waveInfo.leftStateInfo->rightStateInfo) >> *waveInfo.rightStateInfo; if(!onedot) load_wave >> *(waveInfo.rightStateInfo->leftStateInfo) >> *(waveInfo.rightStateInfo->rightStateInfo); this->Load (ifs); ifs.close(); }
void Onepdm_container::save_spatial_npdm_text(const int &i, const int &j) { if( mpigetrank() == 0) { char file[5000]; sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/spatial_onepdm.", i, j,".txt"); ofstream ofs(file); ofs << spatial_onepdm.dim1() << endl; double trace = 0.0; for(int k=0;k<spatial_onepdm.dim1();++k) for(int l=0;l<spatial_onepdm.dim2();++l) { ofs << boost::format("%d %d %20.14e\n") % k % l % spatial_onepdm(k,l); if ( k==l ) trace += spatial_onepdm(k,l); } ofs.close(); pout << "Spatial 1PDM trace = " << trace << "\n"; } }
void save_onepdm_spatial_binary(const Matrix& onepdm, const int &i, const int &j) { //the spatial has a factor of 1/2 in front of it if(!mpigetrank()) { char file[5000]; sprintf (file, "%s%s%d.%d", dmrginp.save_prefix().c_str(),"/spatial_onepdm_bin.", i, j); FILE* f = fopen(file, "wb"); int rows = onepdm.Nrows()/2; Matrix spatonepdm(rows, rows); for (int i=0; i<rows; i++) for (int j=0; j<rows; j++) spatonepdm(i+1,j+1) = onepdm(2*i+1, 2*j+1)+onepdm(2*i+2, 2*j+2); int result = fwrite(&rows, 1, sizeof(int), f); result = fwrite(&rows, 1, sizeof(int), f); result = fwrite(spatonepdm.Store(), rows*rows, sizeof(double), f); fclose(f); } }
void Twopdm_container::save_spatial_npdm_text(const int &i, const int &j) { if( mpigetrank() == 0) { char file[5000]; sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/spatial_twopdm.", i, j,".txt"); ofstream ofs(file); ofs << spatial_twopdm.dim1() << endl; double trace = 0.0; for(int k=0; k<spatial_twopdm.dim1(); ++k) for(int l=0; l<spatial_twopdm.dim2(); ++l) for(int m=0; m<spatial_twopdm.dim3(); ++m) for(int n=0; n<spatial_twopdm.dim4(); ++n) { ofs << boost::format("%d %d %d %d %20.14e\n") % k % l % m % n % spatial_twopdm(k,l,m,n); if ( (k==n) && (l==m) ) trace += spatial_twopdm(k,l,m,n); } ofs.close(); pout << "Spatial 2PDM trace = " << trace << "\n"; } }
void SweepOnepdm::BlockAndDecimate (SweepParams &sweepParams, SpinBlock& system, SpinBlock& newSystem, const bool &useSlater, const bool& dot_with_sys, int state) { //mcheck("at the start of block and decimate"); // figure out if we are going forward or backwards dmrginp.guessgenT -> start(); bool forward = (system.get_sites() [0] == 0); SpinBlock systemDot; SpinBlock envDot; int systemDotStart, systemDotEnd; int systemDotSize = sweepParams.get_sys_add() - 1; if (forward) { systemDotStart = dmrginp.spinAdapted() ? *system.get_sites().rbegin () + 1 : (*system.get_sites().rbegin ())/2 + 1 ; systemDotEnd = systemDotStart + systemDotSize; } else { systemDotStart = dmrginp.spinAdapted() ? system.get_sites()[0] - 1 : (system.get_sites()[0])/2 - 1 ; systemDotEnd = systemDotStart - systemDotSize; } vector<int> spindotsites(2); spindotsites[0] = systemDotStart; spindotsites[1] = systemDotEnd; systemDot = SpinBlock(systemDotStart, systemDotEnd, system.get_integralIndex(), true); SpinBlock environment, environmentDot, newEnvironment; int environmentDotStart, environmentDotEnd, environmentStart, environmentEnd; const int nexact = forward ? sweepParams.get_forward_starting_size() : sweepParams.get_backward_starting_size(); newSystem.set_integralIndex() = system.get_integralIndex(); newSystem.default_op_components(dmrginp.direct(), system, systemDot, false, false, true); newSystem.erase(CRE_CRE_DESCOMP); newSystem.erase(CRE_CRE); newSystem.erase(HAM); newSystem.setstoragetype(DISTRIBUTED_STORAGE_FOR_ONEPDM); newSystem.BuildSumBlock (NO_PARTICLE_SPIN_NUMBER_CONSTRAINT, system, systemDot); if (dmrginp.outputlevel() > 0) { pout << "\t\t\t NewSystem block " << endl << newSystem << endl; newSystem.printOperatorSummary(); } InitBlocks::InitNewEnvironmentBlock(environment, systemDot, newEnvironment, system, systemDot, sweepParams.current_root(), sweepParams.current_root(), sweepParams.get_sys_add(), sweepParams.get_env_add(), forward, dmrginp.direct(), sweepParams.get_onedot(), nexact, useSlater, system.get_integralIndex(), false, false, true); SpinBlock big; newSystem.set_loopblock(true); system.set_loopblock(false); newEnvironment.set_loopblock(false); InitBlocks::InitBigBlock(newSystem, newEnvironment, big); const int nroots = dmrginp.nroots(); std::vector<Wavefunction> solution(1); DiagonalMatrix e; GuessWave::guess_wavefunctions(solution[0], e, big, sweepParams.get_guesstype(), true, state, true, 0.0); #ifndef SERIAL mpi::communicator world; mpi::broadcast(world, solution, 0); #endif std::vector<Matrix> rotateMatrix; DensityMatrix tracedMatrix(newSystem.get_stateInfo()); tracedMatrix.allocate(newSystem.get_stateInfo()); tracedMatrix.makedensitymatrix(solution, big, std::vector<double>(1,1.0), 0.0, 0.0, false); rotateMatrix.clear(); if (!mpigetrank()) double error = makeRotateMatrix(tracedMatrix, rotateMatrix, sweepParams.get_keep_states(), sweepParams.get_keep_qstates()); #ifndef SERIAL mpi::broadcast(world,rotateMatrix,0); #endif #ifdef SERIAL const int numprocs = 1; #endif #ifndef SERIAL const int numprocs = world.size(); #endif Matrix onepdm; load_onepdm_binary(onepdm, state ,state); Matrix pairmat; if (dmrginp.hamiltonian() == BCS) load_pairmat_binary(pairmat, state ,state); if (sweepParams.get_block_iter() == 0) { //this is inface a combination of 2_0_0, 1_1_0 and 0_2_0 p2out << "\t\t\t compute 2_0_0"<<endl; compute_one_pdm_2_0_0(solution[0], solution[0], big, onepdm); if (dmrginp.hamiltonian() == BCS) compute_pair_2_0_0(solution[0], solution[0], big, pairmat); p2out << "\t\t\t compute 1_1_0"<<endl; compute_one_pdm_1_1_0(solution[0], solution[0], big, onepdm); if (dmrginp.hamiltonian() == BCS) compute_pair_1_1_0(solution[0], solution[0], big, pairmat); } p2out << "\t\t\t compute 0_2_0"<<endl; compute_one_pdm_0_2_0(solution[0], solution[0], big, onepdm); if (dmrginp.hamiltonian() == BCS) compute_pair_0_2_0(solution[0], solution[0], big, pairmat); p2out << "\t\t\t compute 1_1"<<endl; compute_one_pdm_1_1(solution[0], solution[0], big, onepdm); if (dmrginp.hamiltonian() == BCS) compute_pair_1_1(solution[0], solution[0], big, pairmat); if (sweepParams.get_block_iter() == sweepParams.get_n_iters() - 1) { p2out << "\t\t\t compute 0_2"<<endl; compute_one_pdm_0_2(solution[0], solution[0], big, onepdm); if (dmrginp.hamiltonian() == BCS) compute_pair_0_2(solution[0], solution[0], big, pairmat); } accumulate_onepdm(onepdm); save_onepdm_binary(onepdm, state, state); if (dmrginp.hamiltonian() == BCS) { accumulate_onepdm(pairmat); save_pairmat_binary(pairmat, state, state); } SaveRotationMatrix (newSystem.get_sites(), rotateMatrix, state); solution[0].SaveWavefunctionInfo (big.get_stateInfo(), big.get_leftBlock()->get_sites(), state); newSystem.transform_operators(rotateMatrix); }
void SpinBlock::addAdditionalCompOps() { #ifndef SERIAL boost::mpi::communicator world; if (world.size() == 1) return; //there is no need to have additional compops int length = dmrginp.last_site(); if (!ops[CRE]->is_local()) { for(int i=0; i<get_sites().size(); i++) { if (ops[CRE]->has(sites[i])) { if (processorindex(sites[i]) != mpigetrank()) ops[CRE]->add_local_indices(sites[i]); ops[CRE]->set_local() = true; mpi::broadcast(world, *(ops[CRE]->get_element(sites[i])[0]), processorindex(sites[i])); } } } if (has(DES)) { if (!ops[DES]->is_local()) { for(int i=0; i<get_sites().size(); i++) { if (ops[DES]->has(sites[i])) { if (processorindex(sites[i]) != mpigetrank()) ops[DES]->add_local_indices(sites[i]); ops[DES]->set_local() = true; mpi::broadcast(world, *(ops[DES]->get_element(sites[i])[0]), processorindex(sites[i])); } } } } if(dmrginp.calc_type() == MPS_NEVPT) { // if ( has(CDD_CRE_DESCOMP)) // if (!ops[CDD_CRE_DESCOMP]->is_local) { // for(int i=0; i<get_sites().size(); i++) { // if (ops[CDD_CRE_DESCOMP]->has(sites[i])) { // if (processorindex(sites[i]) != mpigetrank()) ops[CDD_CRE_DESCOMP]->add_local_indices(sites[i]); // ops[CDD_CRE_DESCOMP]->set_local() = true; // mpi::broadcast(world, *(ops[CDD_CRE_DESCOMP]->get_element(sites[i])[0]), processorindex(sites[i])); // } // } // } return ; } vector<int> dotindice; dotindice.push_back((sites[0] == 0) ? complementary_sites[0] : complementary_sites[complementary_sites.size()-1]); if (!dmrginp.spinAdapted()) { // when non-spinadapted, sites are spin orbitals dotindice.push_back((sites[0] == 0) ? complementary_sites[1] : complementary_sites[complementary_sites.size()-2]); } for (int idx = 0; idx < dotindice.size(); ++idx) { int dotopindex = dotindice[idx]; for (int i=0; i<complementary_sites.size(); i++) { int compsite = complementary_sites[i]; if (std::find(dotindice.begin(), dotindice.end(), compsite) != dotindice.end()) continue; int I = (compsite > dotopindex) ? compsite : dotopindex; int J = (compsite > dotopindex) ? dotopindex : compsite; if (processorindex(compsite) == processorindex(trimap_2d(I, J, length))) continue; if (processorindex(compsite) == mpigetrank()) { //this will potentially receive some ops bool other_proc_has_ops = true; world.recv(processorindex(trimap_2d(I, J, length)), 0, other_proc_has_ops); if (other_proc_has_ops) { ops[CRE_DESCOMP]->add_local_indices(I, J); recvcompOps(*ops[CRE_DESCOMP], I, J, CRE_DESCOMP); } other_proc_has_ops = true; world.recv(processorindex(trimap_2d(I, J, length)), 0, other_proc_has_ops); if (other_proc_has_ops) { ops[DES_DESCOMP]->add_local_indices(I, J); recvcompOps(*ops[DES_DESCOMP], I, J, DES_DESCOMP); } if (has(DES)) { other_proc_has_ops = true; world.recv(processorindex(trimap_2d(I, J, length)), 0, other_proc_has_ops); if (other_proc_has_ops) { ops[CRE_CRECOMP]->add_local_indices(I, J); recvcompOps(*ops[CRE_CRECOMP], I, J, CRE_CRECOMP); } other_proc_has_ops = true; world.recv(processorindex(trimap_2d(I, J, length)), 0, other_proc_has_ops); if (other_proc_has_ops) { ops[DES_CRECOMP]->add_local_indices(I, J); recvcompOps(*ops[DES_CRECOMP], I, J, DES_CRECOMP); } } } else { //this will potentially send some ops if (processorindex(trimap_2d(I, J, length)) == mpigetrank()) { bool this_proc_has_ops = ops[CRE_DESCOMP]->has_local_index(I, J); world.send(processorindex(compsite), 0, this_proc_has_ops); if (this_proc_has_ops) { sendcompOps(*ops[CRE_DESCOMP], I, J, CRE_DESCOMP, compsite); } this_proc_has_ops = ops[DES_DESCOMP]->has_local_index(I, J); world.send(processorindex(compsite), 0, this_proc_has_ops); if (this_proc_has_ops) { sendcompOps(*ops[DES_DESCOMP], I, J, DES_DESCOMP, compsite); } if (has(DES)) { this_proc_has_ops = ops[CRE_CRECOMP]->has_local_index(I, J); world.send(processorindex(compsite), 0, this_proc_has_ops); if (this_proc_has_ops) { sendcompOps(*ops[CRE_CRECOMP], I, J, CRE_CRECOMP, compsite); } this_proc_has_ops = ops[DES_CRECOMP]->has_local_index(I, J); world.send(processorindex(compsite), 0, this_proc_has_ops); if (this_proc_has_ops) { sendcompOps(*ops[DES_CRECOMP], I, J, DES_CRECOMP, compsite); } } } else continue; } } } #endif }
void SpinBlock::store (bool forward, const vector<int>& sites, SpinBlock& b) { Timer disktimer; std::string file; if (forward) file = str(boost::format("%s%s%d%s%d%s%d%s") % dmrginp.save_prefix() % "/SpinBlock-forward-"% sites[0] % "-" % sites[sites.size()-1] % "." % mpigetrank() % ".tmp" ); else file = str(boost::format("%s%s%d%s%d%s%d%s") % dmrginp.save_prefix() % "/SpinBlock-backward-"% sites[0] % "-" % sites[sites.size()-1] % "." % mpigetrank() % ".tmp" ); if (dmrginp.outputlevel() > 0) pout << "\t\t\t Saving block file :: " << file << endl; std::ofstream ofs(file.c_str(), std::ios::binary); b.Save (ofs); ofs.close(); //pout << "\t\t\t block save disk time " << disktimer.elapsedwalltime() << " " << disktimer.elapsedcputime() << endl; }
array_8d<double> Nevpt2_npdm::compute_EEEE_matrix(array_2d<double>& onepdm, array_4d<double>& twopdm, array_6d<double>& threepdm, array_8d<double>& fourpdm) { if( mpigetrank() == 0 ) { std::cout << "Building spatial <0|EEEE|0>\n"; int dim = threepdm.dim1(); assert( onepdm.dim1() == twopdm.dim1() ); array_8d<double> eeee_matrix(dim,dim,dim,dim,dim,dim,dim,dim); eeee_matrix.Clear(); // Output text file double factor = 1.0; char file[5000]; sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/EEEE_matrix.", 0, 0,".txt"); ofstream ofs(file); ofs << dim << endl; for(int i=0; i<dim; ++i) { for(int j=0; j<dim; ++j) { for(int k=0; k<dim; ++k) { int d_jk = ( j == k ); for(int l=0; l<dim; ++l) { for(int m=0; m<dim; ++m) { int d_lm = ( l == m ); int d_jm = ( j == m ); for(int n=0; n<dim; ++n) { for(int p=0; p<dim; ++p) { int d_np = ( n == p ); int d_lp = ( l == p ); int d_jp = ( j == p ); for(int q=0; q<dim; ++q) { double val = 0.0; // 1PDM terms val += d_jk * d_lm * d_np * onepdm(i,q); // 2PDM terms val += d_jk * d_lm * 2.0*twopdm(i,p,q,n); // Note factor of two difference between Block 2PDMs and other codes val += d_jk * d_np * 2.0*twopdm(i,m,q,l); val += d_jk * d_lp * 2.0*twopdm(i,m,n,q); val += d_jm * d_np * 2.0*twopdm(i,k,l,q); val += d_jm * d_lp * 2.0*twopdm(i,k,q,n); val += d_lm * d_np * 2.0*twopdm(i,k,q,j); val += d_lm * d_jp * 2.0*twopdm(i,k,n,q); // 3PDM terms val += d_jk * threepdm(i,m,p,q,n,l); val += d_jm * threepdm(i,k,p,q,l,n); val += d_lm * threepdm(i,k,p,q,n,j); val += d_np * threepdm(i,k,m,q,l,j); val += d_lp * threepdm(i,k,m,n,q,j); val += d_jp * threepdm(i,k,m,n,l,q); // 4PDM terms val += fourpdm(i,k,m,p,j,l,n,q); if ( abs(val) > 1e-14 ) { ofs << boost::format("%d %d %d %d %d %d %d %d %20.14e\n") % i % j % k % l % m % n % p % q % val; eeee_matrix(i,j,k,l,m,n,p,q) = val; } } } } } } } } } ofs.close(); // Save binary matrix sprintf (file, "%s%s%d.%d%s", dmrginp.save_prefix().c_str(),"/EEEE_matrix.", 0, 0,".bin"); std::ofstream ofs2(file, std::ios::binary); boost::archive::binary_oarchive save(ofs2); save << eeee_matrix; ofs2.close(); return eeee_matrix; } }
void SweepTwopdm::BlockAndDecimate (SweepParams &sweepParams, SpinBlock& system, SpinBlock& newSystem, const bool &useSlater, const bool& dot_with_sys, int state) { //mcheck("at the start of block and decimate"); // figure out if we are going forward or backwards dmrginp.guessgenT -> start(); bool forward = (system.get_sites() [0] == 0); SpinBlock systemDot; SpinBlock envDot; int systemDotStart, systemDotEnd; int systemDotSize = sweepParams.get_sys_add() - 1; if (forward) { systemDotStart = dmrginp.spinAdapted() ? *system.get_sites().rbegin () + 1 : (*system.get_sites().rbegin ())/2 + 1 ; systemDotEnd = systemDotStart + systemDotSize; } else { systemDotStart = dmrginp.spinAdapted() ? system.get_sites()[0] - 1 : (system.get_sites()[0])/2 - 1 ; systemDotEnd = systemDotStart - systemDotSize; } vector<int> spindotsites(2); spindotsites[0] = systemDotStart; spindotsites[1] = systemDotEnd; //if (useSlater) { systemDot = SpinBlock(systemDotStart, systemDotEnd, system.get_integralIndex(), true); //SpinBlock::store(true, systemDot.get_sites(), systemDot); //} //else //SpinBlock::restore(true, spindotsites, systemDot); SpinBlock environment, environmentDot, newEnvironment; int environmentDotStart, environmentDotEnd, environmentStart, environmentEnd; const int nexact = forward ? sweepParams.get_forward_starting_size() : sweepParams.get_backward_starting_size(); system.addAdditionalCompOps(); InitBlocks::InitNewSystemBlock(system, systemDot, newSystem, sweepParams.current_root(), sweepParams.current_root(), sweepParams.get_sys_add(), dmrginp.direct(), system.get_integralIndex(), DISTRIBUTED_STORAGE, true, true); InitBlocks::InitNewEnvironmentBlock(environment, systemDot, newEnvironment, system, systemDot, sweepParams.current_root(), sweepParams.current_root(), sweepParams.get_sys_add(), sweepParams.get_env_add(), forward, dmrginp.direct(), sweepParams.get_onedot(), nexact, useSlater, system.get_integralIndex(), true, true, true); SpinBlock big; newSystem.set_loopblock(true); system.set_loopblock(false); newEnvironment.set_loopblock(false); InitBlocks::InitBigBlock(newSystem, newEnvironment, big); const int nroots = dmrginp.nroots(); std::vector<Wavefunction> solution(1); DiagonalMatrix e; GuessWave::guess_wavefunctions(solution[0], e, big, sweepParams.get_guesstype(), true, state, true, 0.0); #ifndef SERIAL mpi::communicator world; mpi::broadcast(world, solution, 0); #endif std::vector<Matrix> rotateMatrix; DensityMatrix tracedMatrix(newSystem.get_stateInfo()); tracedMatrix.allocate(newSystem.get_stateInfo()); tracedMatrix.makedensitymatrix(solution, big, std::vector<double>(1,1.0), 0.0, 0.0, false); rotateMatrix.clear(); if (!mpigetrank()) double error = makeRotateMatrix(tracedMatrix, rotateMatrix, sweepParams.get_keep_states(), sweepParams.get_keep_qstates()); #ifndef SERIAL mpi::broadcast(world,rotateMatrix,0); #endif #ifdef SERIAL const int numprocs = 1; #endif #ifndef SERIAL const int numprocs = world.size(); #endif if (sweepParams.get_block_iter() == 0) compute_twopdm_initial(solution, system, systemDot, newSystem, newEnvironment, big, numprocs, state); compute_twopdm_sweep(solution, system, systemDot, newSystem, newEnvironment, big, numprocs, state); if (sweepParams.get_block_iter() == sweepParams.get_n_iters() - 1) compute_twopdm_final(solution, system, systemDot, newSystem, newEnvironment, big, numprocs, state); SaveRotationMatrix (newSystem.get_sites(), rotateMatrix, state); //for(int i=0;i<dmrginp.nroots();++i) solution[0].SaveWavefunctionInfo (big.get_stateInfo(), big.get_leftBlock()->get_sites(), state); newSystem.transform_operators(rotateMatrix); }
void SweepGenblock::BlockAndDecimate (SweepParams &sweepParams, SpinBlock& system, SpinBlock& newSystem, const bool &useSlater, const bool& dot_with_sys, int state) { if (dmrginp.outputlevel() > 0) mcheck("at the start of block and decimate"); // figure out if we are going forward or backwards pout << "\t\t\t Performing Blocking"<<endl; dmrginp.guessgenT -> start(); bool forward = (system.get_sites() [0] == 0); SpinBlock systemDot; int systemDotStart, systemDotEnd; int systemDotSize = sweepParams.get_sys_add() - 1; if (forward) { systemDotStart = *system.get_sites().rbegin () + 1; systemDotEnd = systemDotStart + systemDotSize; } else { systemDotStart = system.get_sites() [0] - 1; systemDotEnd = systemDotStart - systemDotSize; } vector<int> spindotsites(2); spindotsites[0] = systemDotStart; spindotsites[1] = systemDotEnd; systemDot = SpinBlock(systemDotStart, systemDotEnd); const int nexact = forward ? sweepParams.get_forward_starting_size() : sweepParams.get_backward_starting_size(); system.addAdditionalCompOps(); InitBlocks::InitNewSystemBlock(system, systemDot, newSystem, sweepParams.get_sys_add(), dmrginp.direct(), DISTRIBUTED_STORAGE, dot_with_sys, true); pout << "\t\t\t System Block"<<newSystem; if (dmrginp.outputlevel() > 0) newSystem.printOperatorSummary(); std::vector<Matrix> rotateMatrix; if (!dmrginp.get_fullrestart()) { //this should be done when we actually have wavefunctions stored, otherwise not!! SpinBlock environment, environmentDot, newEnvironment; int environmentDotStart, environmentDotEnd, environmentStart, environmentEnd; InitBlocks::InitNewEnvironmentBlock(environment, systemDot, newEnvironment, system, systemDot, sweepParams.get_sys_add(), sweepParams.get_env_add(), forward, dmrginp.direct(), sweepParams.get_onedot(), nexact, useSlater, true, true, true); SpinBlock big; InitBlocks::InitBigBlock(newSystem, newEnvironment, big); DiagonalMatrix e; std::vector<Wavefunction> solution(1); GuessWave::guess_wavefunctions(solution[0], e, big, sweepParams.get_guesstype(), true, state, true, 0.0); solution[0].SaveWavefunctionInfo (big.get_stateInfo(), big.get_leftBlock()->get_sites(), state); DensityMatrix tracedMatrix; tracedMatrix.allocate(newSystem.get_stateInfo()); tracedMatrix.makedensitymatrix(solution, big, std::vector<double>(1, 1.0), 0.0, 0.0, false); rotateMatrix.clear(); if (!mpigetrank()) double error = newSystem.makeRotateMatrix(tracedMatrix, rotateMatrix, sweepParams.get_keep_states(), sweepParams.get_keep_qstates()); } else LoadRotationMatrix (newSystem.get_sites(), rotateMatrix, state); #ifndef SERIAL mpi::communicator world; broadcast(world, rotateMatrix, 0); #endif if (!dmrginp.get_fullrestart()) SaveRotationMatrix (newSystem.get_sites(), rotateMatrix, state); pout <<"\t\t\t Performing Renormalization "<<endl<<endl; newSystem.transform_operators(rotateMatrix); if (dmrginp.outputlevel() > 0) mcheck("after rotation and transformation of block"); if (dmrginp.outputlevel() > 0) pout <<newSystem<<endl; if (dmrginp.outputlevel() > 0) newSystem.printOperatorSummary(); //mcheck("After renorm transform"); }