void Molecular_system::calcLocWithTestPos(Sample_point * sample, Array1 <doublevar> & tpos, Array1 <doublevar> & Vtest) { int nions=sample->ionSize(); int nelectrons=sample->electronSize(); Vtest.Resize(nelectrons + 1); Vtest = 0; Array1 <doublevar> oldpos(3); //cout << "Calculating local energy\n"; sample->getElectronPos(0, oldpos); sample->setElectronPosNoNotify(0, tpos); Array1 <doublevar> R(5); sample->updateEIDist(); sample->updateEEDist(); for(int i=0; i < nions; i++) { sample->getEIDist(0, i, R); Vtest(nelectrons)+=-sample->getIonCharge(i)/R(0); } doublevar dist = 0.0; for(int d=0; d<3; d++) { dist += (oldpos(d) - tpos(d))*(oldpos(d) - tpos(d)); } dist = sqrt(dist); Vtest(0) = 1/dist; Array1 <doublevar> R2(5); for(int i=1; i< nelectrons; i++) { sample->getEEDist(0,i,R2); Vtest(i)= 1/R2(0); } sample->setElectronPosNoNotify(0, oldpos); sample->updateEIDist(); sample->updateEEDist(); //cout << "elec-elec: " << elecElec << endl; //cout << "pot " << pot << endl; for(int d=0; d< 3; d++) Vtest(nelectrons) -=electric_field(d)*tpos(d); }
void wxWindowDFB::DoMoveWindow(int x, int y, int width, int height) { // NB: [x,y] arguments are in (parent's) window coordinates, while // m_rect.{x,y} are in (parent's) client coordinates. That's why we // offset by parentOrigin in some places below wxPoint parentOrigin(0, 0); AdjustForParentClientOrigin(parentOrigin.x, parentOrigin.y); wxRect oldpos(m_rect); oldpos.Offset(parentOrigin); wxRect newpos(x, y, width, height); // input [x,y] is in window coords, but we store client coords in m_rect: m_rect = newpos; m_rect.Offset(-parentOrigin); // window's position+size changed and so did the subsurface that covers it InvalidateDfbSurface(); if ( IsShown() ) { // queue both former and new position of the window for repainting: wxWindow *parent = GetParent(); // only refresh the visible parts: if ( !CanBeOutsideClientArea() ) { wxRect parentClient(parent->GetClientSize()); oldpos.Intersect(parentClient); newpos.Intersect(parentClient); } parent->RefreshRect(oldpos); parent->RefreshRect(newpos); } }
// Do the actual move int psLinearMovement::MoveV (float delta) { if (velBody < SMALL_EPSILON && velWorld < SMALL_EPSILON && (!colldet || colldet->IsOnGround ())) return PS_MOVE_DONTMOVE; // didn't move anywhere int ret = PS_MOVE_SUCCEED; iMovable* movable = mesh->GetMovable (); if (movable->GetSectors ()->GetCount () <= 0) return PS_MOVE_DONTMOVE; // didn't move anywhere csMatrix3 mat; // To test collision detection we use absolute position and transformation // (this is relevant if we are anchored). Later on we will correct that. csReversibleTransform fulltransf = movable->GetFullTransform (); mat = fulltransf.GetT2O (); csVector3 worldVel (fulltransf.This2OtherRelative (velBody) + velWorld); csVector3 oldpos (fulltransf.GetOrigin ()); csVector3 newpos (worldVel*delta + oldpos); csVector3 bufpos = newpos; float dist = (newpos - oldpos).Norm (); // @@@ Magodra: In some cases the newpos seams to be invalid. Not sure about // the reason, but the FollowSegment function will not work later // in this function. So check for that condidtion, give warning, // and halt the movement. if (CS::IsNaN(newpos.x) || CS::IsNaN(newpos.y) || CS::IsNaN(newpos.z)) { printf("From old position %s ",toString(oldpos,movable->GetSectors()->Get(0)).GetDataSafe()); StackTrace("LinearMovement to a NAN position."); return PS_MOVE_DONTMOVE; // didn't move anywhere } // Check for collisions and adjust position if (colldet) { if (!colldet->AdjustForCollisions (oldpos, newpos, worldVel, delta, movable)) { ret = PS_MOVE_FAIL; newpos = oldpos; } else { // check if we collided, did move less than 9/10 of the distance if ((newpos - bufpos).Norm () > dist/10.0) { ret = PS_MOVE_PARTIAL; } } } csVector3 origNewpos = newpos; bool mirror = false; // Update position to account for portals iSector* new_sector = movable->GetSectors ()->Get (0); iSector* old_sector = new_sector; // @@@ Jorrit: had to do this add! // We need to measure slightly above the position of the actor or else // we won't really cross a portal. float height5 = (bottomSize.y + topSize.y) / 10.0f; newpos.y += height5; csMatrix3 id; csOrthoTransform transform_oldpos (id, oldpos + csVector3 (0.0f, height5, 0.0f)); new_sector = new_sector->FollowSegment (transform_oldpos, newpos, mirror, PS_LINMOVE_FOLLOW_ONLY_PORTALS); newpos.y -= height5; if (new_sector != old_sector) movable->SetSector (new_sector); portalDisplaced += newpos - origNewpos; if(!IsOnGround ()) { //printf("Applying gravity: velY: %g.\n", velWorld.y); // gravity! move down! velWorld.y -= gravity * delta; /* * Terminal velocity * ((120 miles/hour / 3600 second/hour) * 5280 feet/mile) * / 3.28 feet/meter = 53.65 m/s */ // The body velocity is figured in here too. if (velWorld.y < 0) { if (fulltransf.This2OtherRelative (velBody).y + velWorld.y < -(ABS_MAX_FREEFALL_VELOCITY)) velWorld.y = -(ABS_MAX_FREEFALL_VELOCITY) - fulltransf.This2OtherRelative (velBody).y; if (velWorld.y > 0) { // printf("Reset other y %g\n", fulltransf.This2OtherRelative (velBody).y); velWorld.y = 0; } } } else { if(velWorld.y < 0) { velWorld.y = 0; } if (hugGround) HugGround (newpos, new_sector); } // Move to the new position. If we have an anchor we have to convert // the new position from absolute to relative. movable->GetTransform ().SetOrigin (newpos); movable->GetTransform ().SetT2O( movable->GetTransform ().GetT2O () * transform_oldpos.GetT2O ()); if (colldet) { // Part 4: Add us to all nearby sectors. mesh->PlaceMesh (); } movable->UpdateMove (); return ret; }
int psLinearMovement::MoveSprite (float delta) { int ret = PS_MOVE_SUCCEED; csReversibleTransform fulltransf = mesh->GetMovable () ->GetFullTransform (); // const csMatrix3& transf = fulltransf.GetT2O (); // Calculate the total velocity (body and world) in OBJECT space. csVector3 bodyVel (fulltransf.Other2ThisRelative (velWorld) + velBody); float local_max_interval = csMin(csMin((bodyVel.y==0.0f) ? MAX_CD_INTERVAL : ABS (intervalSize.y/bodyVel.y), (bodyVel.x==0.0f) ? MAX_CD_INTERVAL : ABS (intervalSize.x/bodyVel.x) ),(bodyVel.z==0.0f) ? MAX_CD_INTERVAL : ABS (intervalSize.z/bodyVel.z) ); // Err on the side of safety (95% error margin) local_max_interval *= 0.95f; //printf("local_max_interval=%f, bodyVel is %1.2f, %1.2f, %1.2f\n",local_max_interval, bodyVel.x, bodyVel.y, bodyVel.z); //printf("velWorld is %1.2f, %1.2f, %1.2f\n", velWorld.x, velWorld.y, velWorld.z); //printf("velBody is %1.2f, %1.2f, %1.2f\n", velBody.x, velBody.y, velBody.z); // Sanity check on time interval here. Something is messing it up. -KWF //local_max_interval = csMax(local_max_interval, 0.1F); if (colldet) { while (delta > local_max_interval) { csVector3 oldpos(mesh->GetMovable ()->GetFullTransform ().GetOrigin()); // Perform brutal optimisation here for falling with no obstacles if(velWorld.y < -20.0f && mesh->GetMovable()->GetSectors()->GetCount() > 0) { csVector3 worldVel (fulltransf.This2OtherRelative (velBody) + velWorld); bool hit = false; // We check for other meshes at the start and end of the box with radius * 2 to be on the safe side { csRef<iMeshWrapperIterator> objectIter = engine->GetNearbyMeshes(mesh->GetMovable()->GetSectors()->Get(0), oldpos + boundingBox.GetCenter(), boundingBox.GetSize().Norm() * 2); if(objectIter->HasNext()) hit = true; } if(!hit) { csRef<iMeshWrapperIterator> objectIter = engine->GetNearbyMeshes(mesh->GetMovable()->GetSectors()->Get(0), oldpos + worldVel * delta + boundingBox.GetCenter(), boundingBox.GetSize().Norm() * 2); if(objectIter->HasNext()) hit = true; } if(!hit) { csOrthoTransform transform_newpos(csMatrix3(), oldpos + worldVel * delta); csBox3 fullBox(fulltransf.This2OtherRelative(boundingBox.Min()), fulltransf.This2OtherRelative(boundingBox.Max())); csBox3 newBox( transform_newpos.This2OtherRelative(boundingBox.Min()), transform_newpos.This2OtherRelative(boundingBox.Max())); fullBox.AddBoundingBox(newBox); csRef<iMeshWrapperIterator> objectIter = engine->GetNearbyMeshes(mesh->GetMovable()->GetSectors()->Get(0), fullBox); if(objectIter->HasNext()) hit = true; } if(!hit) local_max_interval = delta; } ret = MoveV (local_max_interval); csVector3 displacement(fulltransf.GetOrigin() - oldpos); displacement = fulltransf.Other2ThisRelative(displacement); // Check the invariants still hold otherwise we may jump walls if(!(fabs(displacement.x) <= intervalSize.x)) { printf("X (%g) out of bounds when performing CD > %g!\n", fabs(displacement.x), intervalSize.x); CS_ASSERT(false); } if(!(fabs(displacement.z) <= intervalSize.z)) { printf("Z (%g) out of bounds when performing CD > %g!\n", fabs(displacement.y), intervalSize.y); CS_ASSERT(false); } if(!(fabs(displacement.y) <= intervalSize.y)) { printf("Y (%g) out of bounds when performing CD > %g!\n", fabs(displacement.z), intervalSize.z); CS_ASSERT(false); } RotateV (local_max_interval); // We must update the transform after every rotation! fulltransf = mesh->GetMovable ()->GetFullTransform (); if (ret == PS_MOVE_FAIL) return ret; // The velocity may have changed by now bodyVel = fulltransf.Other2ThisRelative(velWorld) + velBody; delta -= local_max_interval; local_max_interval = csMin(csMin((bodyVel.y==0.0f) ? MAX_CD_INTERVAL : ABS (intervalSize.y/bodyVel.y), (bodyVel.x==0.0f) ? MAX_CD_INTERVAL : ABS (intervalSize.x/bodyVel.x)), (bodyVel.z==0.0f) ? MAX_CD_INTERVAL : ABS (intervalSize.z/bodyVel.z)); // Err on the side of safety (95% error margin) local_max_interval *= 0.95f; // Sanity check on time interval here. Something is messing it up. -KWF // local_max_interval = csMax(local_max_interval, 0.1F); } } if (!colldet || delta) { ret = MoveV (delta); RotateV(delta); } return ret; }
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; }
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; }
void Average_ekt::calcPseudoMo(System * sys, Sample_point * sample, Pseudopotential *psp, const Array1 <doublevar> & accept_var, Array1 <dcomplex> & totalv)//, { int natoms=sample->ionSize(); assert(accept_var.GetDim(0) >= nTest()); //assert(totalv.GetDim(0) >= nwf); assert(nelectrons == sample->electronSize()); Array1 <doublevar> ionpos(3), oldpos(3), newpos(3); Array1 <doublevar> newdist(5), olddist(5); int accept_counter=0; //deriv.Resize(natoms, 3); //deriv=0; dcomplex nonlocal; Array1 <doublevar> rDotR(psp->getMaxAIP()); Array2 <dcomplex> movals_t(nmo, 1); Array2 <dcomplex> movals(nmo, 1); sample->getElectronPos(0, oldpos); calc_mos(sample, 0, movals); dcomplex local; for (int jmo = 0; jmo < nmo; jmo ++) { nonlocal=0.0; local = 0.0; for(int at = 0; at< natoms; at++){ if(psp->getNumL(at) != 0) { Array1 <doublevar> v_l(psp->getNumL(at)); sample->getIonPos(at, ionpos); sample->updateEIDist(); sample->getEIDist(0, at, olddist); int spin=1; // if(e < sys->nelectrons(0)) spin=0; spin = 0; psp->getRadialOut(at, spin, sample, olddist, v_l); //---------------------------------------- //Start integral int accept; if(psp->getDeterministic()) { accept= olddist(0) < psp->getCutoff(at); } else { doublevar strength=0; const doublevar calculate_threshold=10; for(int l=0; l<psp->getNumL(at)-1; l++) { strength+=calculate_threshold*(2*l+1)*fabs(v_l(l)); } strength=min((doublevar) 1.0, strength); doublevar rand=accept_var(accept_counter++); //cout << at <<" random number " << rand // << " p_eval " << strength << endl; if ( strength > 0.0 ) { for(int l=0; l<psp->getNumL(at)-1; l++) v_l(l)/=strength; } accept=strength>rand; } //bool localonly = true; if(accept) { for(int i=0; i< psp->getAIP(at); i++) { for(int d=0; d < 3; d++) newpos(d)=psp->getIntegralPt(at,i,d)*olddist(0)-olddist(d+2); sample->translateElectron(0, newpos); sample->updateEIDist(); sample->getEIDist(0,at,newdist); rDotR(i)=0; for(int d=0; d < 3; d++) rDotR(i)+=newdist(d+2)*olddist(d+2); rDotR(i)/=(newdist(0)*olddist(0)); //divide by the magnitudes calc_mos(sample, 0, movals_t);//update value //---- //cout << "signs " << base_sign << " " << new_sign << endl;; for(int l=0; l< psp->getNumL(at)-1; l++) { nonlocal+=(2*l+1)*v_l(l)*legendre(rDotR(i), l)*psp->getIntegralWeight(at, i)*movals_t(jmo, 0); } sample->setElectronPos(0, oldpos); } } int localL=psp->getNumL(at)-1; //The l-value of the local part is local += v_l(localL); } // if atom has psp } //atom loop // cout << "Local:" << local << endl; //cout << "Nonlocal: " << nonlocal/movals(jmo, 0) << endl; totalv(jmo) = local*movals(jmo, 0)+nonlocal; //totalv(jmo) =0.0; } //nmo loop //cout << "psp: local part " << accum_local // << " nonlocal part " << accum_nonlocal << endl; }
void Actor::update() { if(!validRender()) return; Game *gptr = NULL; gptr = Game::getInstance(); float movespeed = 0.1; //handle key presses if(sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { m_Velocity = sf::Vector2f(movespeed, 0); m_CurrentDirection = FACE_EAST; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { m_Velocity = sf::Vector2f(-movespeed, 0); m_CurrentDirection = FACE_WEST; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { m_Velocity = sf::Vector2f(0, -movespeed); m_CurrentDirection = FACE_NORTH; } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { m_Velocity = sf::Vector2f(0, movespeed); m_CurrentDirection = FACE_SOUTH; } else if(sf::Mouse::isButtonPressed(sf::Mouse::Right) && !gptr->isEditingMap()) { float angle = getAngle(gptr->isoPositionToScreenPosition(m_Position), gptr->mousePosGlobal, false); if(angle >=0 && angle < 90) { m_Velocity = sf::Vector2f(0, -movespeed); m_CurrentDirection = FACE_NORTH; } else if(angle >= 90 && angle < 180) { m_Velocity = sf::Vector2f(-movespeed, 0); m_CurrentDirection = FACE_WEST; } else if(angle >= 180 && angle < 270) { m_Velocity = sf::Vector2f(0, movespeed); m_CurrentDirection = FACE_SOUTH; } else { m_Velocity = sf::Vector2f(movespeed, 0); m_CurrentDirection = FACE_EAST; } } else { m_Velocity = sf::Vector2f(0,0); } sf::Vector3f oldpos(m_Position); //update player position based on velocity m_Position.x += m_Velocity.x; m_Position.y += m_Velocity.y; //if new player position is not valid, set back to original position if(!gptr->validPosition(m_Position)) { //is position not valid because it is not within current map? if(gptr->positionOutsideOfCurrentMap(m_Position)) { sf::Vector2f mapsize = gptr->getCurrentMapSize(); sf::Vector2f shiftdir; //which direction to shift? if(m_Position.x < 0) shiftdir.x = -1; else if(m_Position.x >= mapsize.x ) shiftdir.x = 1; if(m_Position.y < 0) shiftdir.y = -1; else if(m_Position.y >= mapsize.y ) shiftdir.y = 1; if(gptr->shiftMapChunkInDirection(shiftdir)) { //adjust current position m_Position = sf::Vector3f( m_Position.x + (-shiftdir.x*mapsize.x), m_Position.y + (-shiftdir.y*mapsize.y), m_Position.z); //std::cout << "mposition:" << m_Position.x << "," << m_Position.y << "," << m_Position.z << std::endl; } else m_Position = oldpos; } else m_Position = oldpos; } if(m_AnimationClock.getElapsedTime().asMilliseconds() >= m_Animations[m_CurrentAnimation]->getFrameTime(m_AnimationFrame)) { m_AnimationFrame++; m_AnimationClock.restart(); if(m_AnimationFrame >= m_Animations[m_CurrentAnimation]->getFrameCount()) m_AnimationFrame = 0; } }
int FileContents::getField(int lineNum, int field, char *dest, int len) { int curfield = field; int curline = lineNum; dest[0] = '\0'; if (lineNum >= numLinesInFile) { return 0; } if (printfFile) { curline %= realLinesInFile; } const string & line = fileLines[curline]; size_t pos(0), oldpos(0); do { oldpos = pos; size_t localpos = line.find(';', oldpos); if (localpos != string::npos) { pos = localpos + 1; } else { pos = localpos; break; } if (curfield == 0) { break; } curfield --; } while (oldpos != string::npos); if (curfield) { WARNING("Field %d not found in the file %s", field, fileName); return 0; } if (string::npos == oldpos) { return 0; } if (string::npos != pos) { // should not be decremented for fieldN pos -= (oldpos + 1); } string x = line.substr(oldpos, pos); if (x.length()) { if (printfFile) { const char *s = x.c_str(); size_t l = strlen(s); int copied = 0; if (l>INT_MAX) WARNING("length of string exceeds INT_MAX: %ld",l); for (int i = 0; i < (int)l; i++) { if (s[i] == '%') { if (s[i + 1] == '%') { dest[copied++] = s[i]; } else { const char *format = s + i; i++; while (s[i] != 'd') { if (i == (int)l) { REPORT_ERROR("Invalid printf injection field (ran off end of line): %s", s); } if (!(isdigit(s[i]) || s[i] == '.' || s[i] == '-')) { REPORT_ERROR("Invalid printf injection field (only decimal values allowed '%c'): %s", s[i], s); } i++; } assert(s[i] == 'd'); char *tmp = (char *)malloc(s + i + 2 - format); if (!tmp) { REPORT_ERROR("Out of memory!\n"); } memcpy(tmp, format, s + i + 1 - format); tmp[s + i + 1 - format] = '\0'; copied += sprintf(dest + copied, tmp, printfOffset + (lineNum * printfMultiple)); free(tmp); } } else { dest[copied++] = s[i]; } } dest[copied] = '\0'; return copied; } else { return snprintf(dest, len, "%s", x.c_str()); } } else { return 0; } }
void Nodes_method::run(Program_options & options, ostream & output) { ofstream os; //for writing to *.plt files string pltfile; //name of plotfile being written string confile; //name of configuration of electrons to be being written double max_value,min_value; int count; Array1 <doublevar> xyz(3), xyz2(3), resolution_array(3); //position of electron "in" MO Array1 <int> D_array1(3); //dummy array1 Array2 <doublevar> oldpos(mywalker->electronSize(),3); Array1 <doublevar> tmp(3); D_array1=0; //sets all 3 components to 0. use as counter for gridpoints D_array1(0)=roundoff((minmax(1)-minmax(0))/resolution); D_array1(1)=roundoff((minmax(3)-minmax(2))/resolution); D_array1(2)=roundoff((minmax(5)-minmax(4))/resolution); resolution_array(0)=(minmax(1)-minmax(0))/(D_array1(0)-1); resolution_array(1)=(minmax(3)-minmax(2))/(D_array1(1)-1); resolution_array(2)=(minmax(5)-minmax(4))/(D_array1(2)-1); Array2 <doublevar> grid(plots.GetSize(),D_array1(0)*D_array1(1)*D_array1(2)); Wf_return wfvals(wf->nfunc(), 2); //where wfval fist index labels //wf number ie. ground state // excited state and so on, and second label is for two values, sign and log(wf). //scan electron positions cout << "Using these electron positions:"<<endl; for(int i=0; i<mywalker->electronSize(); i++){ mywalker->getElectronPos(i, tmp); cout.precision(5); cout.width(8); cout <<i+1<<" "<<tmp(0)<<" "<<tmp(1)<<" "<<tmp(2)<<endl; for (int d=0;d<3;d++) oldpos(i,d)=tmp(d); // cout << "pos " << oldpos(i,0) << " " << oldpos(i,1) << " " << oldpos(i,2) // << endl; } //generate .xyz file for gOpenMol to view coordinates pltfile=options.runid + ".xyz"; os.open(pltfile.c_str()); cout<<"writing to "<<pltfile<<endl; vector <string> atomlabels; sysprop->getAtomicLabels(atomlabels); os<<atomlabels.size()+mywalker->electronSize()<<endl; os<<endl; int spin_up=sysprop->nelectrons(0); //cout << "Up electrons "<<spin_up<<endl; for(unsigned int i=0; i<atomlabels.size(); i++){ Array1 <doublevar> pos(3); mywalker->getIonPos(i, pos); os<<atomlabels[i] <<" "<< pos(0) <<" "<<pos(1) <<" "<< pos(2)<<endl; } for(int j=0; j<mywalker->electronSize(); j++){ if (j<spin_up) os<<"Eu"; else os<<"Ed"; // mywalker->getElectronPos(j, pos); os<<" "<< oldpos(j,0)<<" "<<oldpos(j,1) <<" "<< oldpos(j,2)<<endl; } os.close(); if (!doublemove) { //calculate value of each molecular orbital at each grid point and store in an Array1 // grid values with x=fastest running variable, and z=slowest cout<<"calculating "<<D_array1(0)*D_array1(1)*D_array1(2) <<" grid points"<<endl; cout<<"for "<< plots.GetDim(0) <<" 3D projections of wavefunction"<<endl; count=0; xyz(0)=minmax(0); xyz(1)=minmax(2); xyz(2)=minmax(4); //init elec probe to xmin ymin zmin //Array1 <doublevar> oldpos(3) for(int i=0; i<plots.GetSize(); i++){ cout.width(3); cout <<"============================="<<plots(i) <<"==============================" <<endl; count=0; for(int xx=0; xx<D_array1(0);xx++){ xyz(0)=minmax(0)+xx*resolution_array(0); //move forward on x axis one resolution unit max_value=min_value=0.0; cout << "x "; cout.precision(5); cout.width(8); cout<< xyz(0); for(int yy=0; yy<D_array1(1);yy++){ xyz(1)=minmax(2)+yy*resolution_array(1); //move forward on y axis one resolution unit for(int zz=0;zz<D_array1(2);zz++){ xyz(2)=minmax(4)+zz*resolution_array(2); //move forward on z axis one resolution unit // mywalker->getElectronPos(plots(i), oldpos); mywalker->setElectronPos(plots(i)-1,xyz); //move elec#plots(i) to point specified by xyz wf->updateVal(wfdata, mywalker); //update wfdata wf->getVal(wfdata, 0, wfvals); //get wf value const doublevar cutoff=15; if(wfvals.amp(0,0)<cutoff) { grid(i,count)=wfvals.sign(0)*exp(wfvals.amp(0,0)); } else { //cut off the maximum value output so that there aren't overflow errors grid(i,count)=wfvals.sign(0)*exp(cutoff); } //grid(i,count)=exp(2.0*wfvals(0,1));//!square of wavefunction if (grid(i,count)>max_value) max_value=grid(i,count); if (grid(i,count)<min_value) min_value=grid(i,count); // cout << "grid " << grid(i,count)<< " " << xyz(0) << endl; count++; //index for cycling through grid points } } cout.setf(ios::scientific| ios:: showpos); cout <<", max. value "<<max_value<<", min. value "<<min_value<< endl; cout.unsetf(ios::scientific| ios:: showpos); } mywalker->setElectronPos(plots(i)-1, oldpos(plots(i)-1)); } //Loop through and generate plot files for each orbital requested if(plots.GetSize()<=0) error("Number of requested plots is not a positive number"); cout<<"saving data for "<<plots.GetSize()<<" 3D projections of wavefunction"<<endl; for(int i=0; i<plots.GetSize(); i++) { //output to file with orbital number in it char strbuff[40]; sprintf(strbuff, "%d", plots(i)); confile=pltfile = options.runid; confile += ".orb."; pltfile += ".orb."; pltfile += strbuff; confile += strbuff; pltfile += ".cube"; /*FIGURE OUT HOW TO CONVERT INT TO STRING*/ confile += ".xyz"; os.open(pltfile.c_str()); cout<<"writing to "<<pltfile<<endl; os << "QWalk nodes output\n"; os << "Wavefunction single scan with " << plots(i) <<" electron"<<endl; int natoms=sysprop->nIons(); os << " " << natoms+ mywalker->electronSize()-1 << " " << minmax(0) << " " << minmax(2) << " " << minmax(4) << endl; os << D_array1(0) << " " << resolution_array(0) << " 0.0000 0.0000" << endl; os << D_array1(1) << " 0.0000 " << resolution_array(1) << " 0.0000" << endl; os << D_array1(2) << " 0.0000 0.0000 " << resolution_array(2) << endl; Array1 <doublevar> pos(3); for(int at=0; at< natoms; at++) { mywalker->getIonPos(at,pos); os << " " << mywalker->getIonCharge(at) << " 0.0000 " << pos(0) <<" " << pos(1) << " " << pos(2) << endl; } for(int j=0; j<mywalker->electronSize(); j++){ if (j!=plots(i)-1){ if (j<spin_up) os<<" 3 0.0000 "; else os<<" 3 0.0000 "; os<< oldpos(j,0)<<" "<<oldpos(j,1)<<" "<< oldpos(j,2)<<endl; } } os.setf(ios::scientific); for(int j=0; j< D_array1(0)*D_array1(1)*D_array1(2); j++) { os <<setw(16)<<setprecision(8)<<grid(i,j); if(j%6 ==5) os << endl; } os << endl; os.unsetf(ios::scientific); os<<setprecision(6); os.close(); /* old plt file plots // http://www.csc.fi/gopenmol/developers/plt_format.phtml os<<"3 "; //rank=3 always os<<"2\n"; //dummy variable => "Orbital/density surface" //number of grid points for x, y, & z direction os <<D_array1(2)<<" "<<D_array1(1)<<" "<<D_array1(0)<<endl; os <<minmax(4)<<" "<<minmax(5)<<" "<<minmax(2)<<" "<<minmax(3) <<" "<<minmax(0)<<" "<<minmax(1)<<endl; for(int j=0; j<(D_array1(0)*D_array1(1)*D_array1(2)); j++) os<<grid(i,j)<<endl; os.close(); os.open(confile.c_str()); cout<<"writing to "<<confile<<endl; Array1 <doublevar> pos(3); sysprop->getAtomicLabels(atomlabels); os<<atomlabels.size()+ mywalker->electronSize()-1 <<endl; os << endl; for(unsigned int j=0; j<atomlabels.size();j++){ mywalker->getIonPos(j, pos); os<<atomlabels[j] <<" "<< pos(0) <<" "<<pos(1) <<" "<< pos(2)<<endl; } for(int j=0; j<mywalker->electronSize(); j++){ if (j!=plots(i)-1){ if (j<spin_up) os<<"Eu"; else os<<"Ed"; // mywalker->getElectronPos(j, pos); os<<" "<< oldpos(j,0)<<" "<<oldpos(j,1) <<" "<< oldpos(j,2)<<endl; } } os.close(); */ } } else { cout << "Using translation vector(s): "<<endl; for(int i=0;i<dxyz.GetDim(0);i++) cout<<"( "<<dxyz(i,0)<<" , "<<dxyz(i,1)<<" , "<<dxyz(i,2)<<" )"<<endl; for(int i=0; i<plots.GetSize(); i=i+2){ count=0; xyz(0)=minmax(0); xyz(1)=minmax(2); xyz(2)=minmax(4); //init elec probe to xmin ymin zmin //Array1 <doublevar> oldpos(3) cout.width(3); cout <<"============================="<<plots(i)<<" and "<<plots(i+1) <<"==============================" <<endl; for(int xx=0; xx<D_array1(0);xx++){ max_value=min_value=0.0; xyz(0)=minmax(0)+xx*resolution_array(0); xyz2(0)=xyz(0)+dxyz(i/2,0); cout << "x "; cout.precision(5); cout.width(8); cout<< xyz(0); for(int yy=0; yy<D_array1(1);yy++){ xyz(1)=minmax(2)+yy*resolution_array(1); xyz2(1)=xyz(1)+dxyz(i/2,1); for(int zz=0;zz<D_array1(2);zz++){ xyz(2)=minmax(4)+zz*resolution_array(2); xyz2(2)=xyz(2)+dxyz(i/2,2); // mywalker->getElectronPos(plots(i), oldpos); mywalker->setElectronPos(plots(i)-1,xyz);//move elec#plots(i) //to point specified by xyz mywalker->setElectronPos(plots(i+1)-1,xyz2);//move elec#plots(i) //to point specified by xyz wf->updateVal(wfdata, mywalker); //update wfdata wf->getVal(wfdata, 0, wfvals); //get wf value grid(i,count)=wfvals.sign(0)*exp(wfvals.amp(0,0)); //grid(i,count)=exp(2.0*wfvals(0,1));//!square of wavefunction // cout << "grid " << grid(i,count)<< " " << xyz(0) << endl; if (grid(i,count)>max_value) max_value=grid(i,count); if (grid(i,count)<min_value) min_value=grid(i,count); count++; //index for cycling through grid points } } cout.setf(ios::scientific| ios:: showpos); cout <<", max. value "<<max_value<<", min. value "<<min_value<< endl; cout.unsetf(ios::scientific| ios:: showpos); } mywalker->setElectronPos(plots(i)-1, oldpos(plots(i)-1)); mywalker->setElectronPos(plots(i+1)-1, oldpos(plots(i+1)-1)); } //Loop through and generate plot files for each orbital requested if(plots.GetSize()<=0) error("Number of requested plots is not a positive number"); cout<<"saving data for "<<plots.GetSize()<<" 3D projections of wavefunction"<<endl; for(int i=0; i<plots.GetSize(); i=i+2) { char strbuff2[40],strbuff[40]; //output to file with orbital number in it sprintf(strbuff, "%d", plots(i)); sprintf(strbuff2, "%d", plots(i+1)); confile=pltfile = options.runid; confile += ".orb."; pltfile += ".orb."; pltfile += strbuff; confile += strbuff; pltfile += "with"; confile += "with"; pltfile += strbuff2; confile += strbuff2; pltfile += ".cube"; /*FIGURE OUT HOW TO CONVERT INT TO STRING*/ confile += ".xyz"; os.open(pltfile.c_str()); cout<<"writing to "<<pltfile<<endl; os << "GOS nodes output\n"; os << "Wave function double scan with "<< plots(i) <<" & "<<plots(i+1)<<" electrons"<< endl; int natoms=sysprop->nIons(); os << " " << natoms + mywalker->electronSize()-2 << " " << minmax(0) << " " << minmax(2) << " " << minmax(4) << endl; os << D_array1(0) << " " << resolution_array(0) << " 0.0000 0.0000" << endl; os << D_array1(1) << " 0.0000 " << resolution_array(1) << " 0.0000" << endl; os << D_array1(2) << " 0.0000 0.0000 " << resolution_array(2) << endl; Array1 <doublevar> pos(3); for(int at=0; at< natoms; at++) { mywalker->getIonPos(at,pos); os << " " << mywalker->getIonCharge(at) << " 0.0000 " << pos(0) <<" " << pos(1) << " " << pos(2) << endl; } for(int j=0; j<mywalker->electronSize(); j++){ if ((j!=plots(i)-1)&&(j!=plots(i+1)-1)){ if (j<spin_up) os<<" 3 0.0000 "; else os<<" 3 0.0000 "; os<< oldpos(j,0)<<" "<<oldpos(j,1)<<" "<< oldpos(j,2)<<endl; } } os.setf(ios::scientific); for(int j=0; j< D_array1(0)*D_array1(1)*D_array1(2); j++) { os << setw(16) << setprecision(8) << grid(i,j); if(j%6 ==5) os << endl; } os << endl; os.unsetf(ios::scientific); os<<setprecision(6); os.close(); /* old plot to plt file version // http://www.csc.fi/gopenmol/developers/plt_format.phtml os<<"3 "; //rank=3 always os<<"2\n"; //dummy variable => "Orbital/density surface" //number of grid points for x, y, & z direction os <<D_array1(2)<<" "<<D_array1(1)<<" "<<D_array1(0)<<endl; os <<minmax(4)<<" "<<minmax(5)<<" "<<minmax(2)<<" "<<minmax(3) <<" "<<minmax(0)<<" "<<minmax(1)<<endl; for(int j=0; j<(D_array1(0)*D_array1(1)*D_array1(2)); j++) os<<grid(i,j)<<endl; os.close(); os.open(confile.c_str()); cout<<"writing to "<<confile<<endl; Array1 <doublevar> pos(3); sysprop->getAtomicLabels(atomlabels); os<<atomlabels.size()+ mywalker->electronSize()-2 <<endl; os << endl; for(unsigned int j=0; j<atomlabels.size();j++){ mywalker->getIonPos(j, pos); os<<atomlabels[j] <<" "<< pos(0) <<" "<<pos(1) <<" "<< pos(2)<<endl; } for(int j=0; j<mywalker->electronSize(); j++){ if ((j!=plots(i)-1)&&(j!=plots(i+1)-1)){ if (j<spin_up) os<<"Eu"; else os<<"Ed"; // mywalker->getElectronPos(j, pos); os<<" "<< oldpos(j,0)<<" "<<oldpos(j,1) <<" "<< oldpos(j,2)<<endl; } } os.close(); */ } } cout <<"End of Nodes Method"<<endl; }