void MarkovExpectation::compute(FitContext *fc, const char *what, const char *how) { if (fc) { for (auto c1 : components) { c1->compute(fc, what, how); } } omxRecompute(initial, fc); if (initialV != omxGetMatrixVersion(initial)) { omxCopyMatrix(scaledInitial, initial); EigenVectorAdaptor Ei(scaledInitial); if (scale == SCALE_SOFTMAX) Ei.derived() = Ei.array().exp(); if (scale != SCALE_NONE) { Ei /= Ei.sum(); } if (verbose >= 2) mxPrintMat("initial", Ei); initialV = omxGetMatrixVersion(initial); } if (transition) { omxRecompute(transition, fc); if (transitionV != omxGetMatrixVersion(transition)) { omxCopyMatrix(scaledTransition, transition); EigenArrayAdaptor Et(scaledTransition); if (scale == SCALE_SOFTMAX) Et.derived() = Et.array().exp(); if (scale != SCALE_NONE) { Eigen::ArrayXd v = Et.colwise().sum(); Et.rowwise() /= v.transpose(); } if (verbose >= 2) mxPrintMat("transition", Et); transitionV = omxGetMatrixVersion(transition); } } }
void init_sys(int N, Vars * U){ int i; for(i=0;i<N;++i){ if(i < N/2){ U[i].mass = 1.0; U[i].velocity = 0.0; U[i].press = 1.0; //printf("yes %d\n",i); } else{ U[i].mass = 0.1; U[i].velocity = 0.0; U[i].press = 0.125; U[i].energy = Et(U[i].mass, U[i].velocity, U[i].press); } } for(i=0;i<N;++i) U[i].energy = Et(U[i].mass, U[i].velocity, U[i].press); }
Vars U_R(Vars Ui, Vars Uipo, Vars Uipt){//does U_L for the right state Vars UR; UR.mass = Uipo.mass - 0.5*minmod(thet*(Uipo.mass-Ui.mass),0.5*(Uipt.mass-Ui.mass),thet*(Uipt.mass-Uipo.mass)); UR.xvelocity = Uipo.xvelocity - 0.5*minmod(thet*(Uipo.xvelocity-Ui.xvelocity),.5*(Uipt.xvelocity-Ui.xvelocity),thet*(Uipt.xvelocity-Uipo.xvelocity)); UR.yvelocity = Uipo.yvelocity - 0.5*minmod(thet*(Uipo.yvelocity-Ui.yvelocity),.5*(Uipt.yvelocity-Ui.yvelocity),thet*(Uipt.yvelocity-Uipo.yvelocity)); UR.press = Uipo.press - 0.5*minmod(thet*(Uipo.press-Ui.press), 0.5*(Uipt.press-Ui.press), thet*(Uipt.press-Uipo.press)); UR.energy = Et(UR.mass, UR.xvelocity, UR.yvelocity, UR.press); return(UR); }
Vars U_L(Vars Ui, Vars Uimo, Vars Uipo){//interpolates the conserved variables at interface for the left state Vars UL; UL.mass = Ui.mass + 0.5*minmod(thet*(Ui.mass - Uimo.mass), 0.5*(Uipo.mass - Uimo.mass),thet*(Uipo.mass-Ui.mass)); UL.xvelocity = Ui.xvelocity + 0.5*minmod(thet*(Ui.xvelocity - Uimo.xvelocity), 0.5*(Uipo.xvelocity - Uimo.xvelocity),thet*(Uipo.xvelocity-Ui.xvelocity)); UL.yvelocity = Ui.yvelocity + 0.5*minmod(thet*(Ui.yvelocity - Uimo.yvelocity), 0.5*(Uipo.yvelocity - Uimo.yvelocity),thet*(Uipo.yvelocity-Ui.yvelocity)); UL.press = Ui.press + 0.5*minmod(thet*(Ui.press - Uimo.press), 0.5*(Uipo.press - Uimo.press),thet*(Uipo.press-Ui.press)); UL.energy = Et(UL.mass, UL.xvelocity, UL.yvelocity, UL.press); return(UL); }
void MarkovExpectation::populateAttr(SEXP robj) { compute(0, 0, 0); // needed? TODO MxRList out; EigenVectorAdaptor Ei(scaledInitial); const char *initialName = isMixtureInterface? "weights" : "initial"; out.add(initialName, Rcpp::wrap(Ei)); if (scaledTransition) { EigenMatrixAdaptor Et(scaledTransition); out.add("transition", Rcpp::wrap(Et)); } Rf_setAttrib(robj, Rf_install("output"), out.asR()); }
void ProgValidationTiltPairs::run() { MetaData MD_tilted, MD_untilted, DF1sorted, DF2sorted, DFweights; MD_tilted.read(fntiltimage_In); MD_untilted.read(fnuntiltimage_In); DF1sorted.sort(MD_tilted,MDL_ITEM_ID,true); DF2sorted.sort(MD_untilted,MDL_ITEM_ID,true); MDIterator iter1(DF1sorted), iter2(DF2sorted); std::vector< Matrix1D<double> > ang1, ang2; Matrix1D<double> rotTiltPsi(3), z(3); size_t currentId; bool anotherImageIn2=iter2.hasNext(); size_t id1, id2; bool mirror; Matrix2D<double> Eu, Et, R; double alpha, beta; while (anotherImageIn2) { ang1.clear(); ang2.clear(); // Take current id DF2sorted.getValue(MDL_ITEM_ID,currentId,iter2.objId); // Grab all the angles in DF2 associated to this id bool anotherIteration=false; do { DF2sorted.getValue(MDL_ITEM_ID,id2,iter2.objId); anotherIteration=false; if (id2==currentId) { DF2sorted.getValue(MDL_ANGLE_ROT,XX(rotTiltPsi),iter2.objId); DF2sorted.getValue(MDL_ANGLE_TILT,YY(rotTiltPsi),iter2.objId); DF2sorted.getValue(MDL_ANGLE_PSI,ZZ(rotTiltPsi),iter2.objId); DF2sorted.getValue(MDL_FLIP,mirror,iter2.objId); std::cout << "From DF2:" << XX(rotTiltPsi) << " " << YY(rotTiltPsi) << " " << ZZ(rotTiltPsi) << " " << mirror << std::endl; //LINEA ANTERIOR ORIGINAL if (mirror) { double rotp, tiltp, psip; Euler_mirrorX(XX(rotTiltPsi),YY(rotTiltPsi),ZZ(rotTiltPsi), rotp, tiltp, psip); XX(rotTiltPsi)=rotp; YY(rotTiltPsi)=tiltp; ZZ(rotTiltPsi)=psip; } ang2.push_back(rotTiltPsi); iter2.moveNext(); if (iter2.hasNext()) anotherIteration=true; } } while (anotherIteration); // Advance Iter 1 to catch Iter 2 double N=0, cumulatedDistance=0; size_t newObjId=0; if (iter1.objId>0) { DF1sorted.getValue(MDL_ITEM_ID,id1,iter1.objId); while (id1<currentId && iter1.hasNext()) { iter1.moveNext(); DF1sorted.getValue(MDL_ITEM_ID,id1,iter1.objId); } // If we are at the end of DF1, then we did not find id1 such that id1==currentId if (!iter1.hasNext()) break; // Grab all the angles in DF1 associated to this id anotherIteration=false; do { DF1sorted.getValue(MDL_ITEM_ID,id1,iter1.objId); anotherIteration=false; if (id1==currentId) { DF1sorted.getValue(MDL_ANGLE_ROT,XX(rotTiltPsi),iter1.objId); DF1sorted.getValue(MDL_ANGLE_TILT,YY(rotTiltPsi),iter1.objId); DF1sorted.getValue(MDL_ANGLE_PSI,ZZ(rotTiltPsi),iter1.objId); DF1sorted.getValue(MDL_FLIP,mirror,iter1.objId); std::cout << "From DF1:" << XX(rotTiltPsi) << " " << YY(rotTiltPsi) << " " << ZZ(rotTiltPsi) << " " << mirror << std::endl; //LINEA ANTERIOR ORIGINAL if (mirror) { double rotp, tiltp, psip; Euler_mirrorX(XX(rotTiltPsi),YY(rotTiltPsi),ZZ(rotTiltPsi), rotp, tiltp, psip); XX(rotTiltPsi)=rotp; YY(rotTiltPsi)=tiltp; ZZ(rotTiltPsi)=psip; } ang1.push_back(rotTiltPsi); iter1.moveNext(); if (iter1.hasNext()) anotherIteration=true; } } while (anotherIteration); // Process both sets of angles for (size_t i=0; i<ang2.size(); ++i) { const Matrix1D<double> &anglesi=ang2[i]; double rotu=XX(anglesi); double tiltu=YY(anglesi); double psiu=ZZ(anglesi); Euler_angles2matrix(rotu,tiltu,psiu,Eu,false); /*std::cout << "------UNTILTED MATRIX------" << std::endl; std::cout << Eu << std::endl; std::cout << "vector" << std::endl; std::cout << Eu(2,0) << " " << Eu(2,1) << " " << Eu(2,2) << std::endl;*/ for (size_t j=0; j<ang1.size(); ++j) { const Matrix1D<double> &anglesj=ang1[j]; double rott=XX(anglesj); double tiltt=YY(anglesj); double psit=ZZ(anglesj); double alpha_x, alpha_y; Euler_angles2matrix(rott,tiltt,psit,Et,false); ////////////////////////////////////////////////////////////////// double untilt_angles[3]={rotu, tiltu, psiu}, tilt_angles[3]={rott, tiltt, psit}; angles2tranformation(untilt_angles, tilt_angles, alpha_x, alpha_y); //std::cout << "alpha = " << (alpha_x*alpha_x+alpha_y*alpha_y) << std::endl; ////////////////////////////////////////////////////////////////// /*std::cout << "------TILTED MATRIX------" << std::endl; std::cout << Et << std::endl; std::cout << "vector" << std::endl; std::cout << Et(2,0) << " " << Et(2,1) << " " << Et(2,2) << std::endl; std::cout << "---------------------------" << std::endl; std::cout << "---------------------------" << std::endl;*/ R=Eu*Et.transpose(); double rotTransf, tiltTransf, psiTransf; Euler_matrix2angles(R, rotTransf, tiltTransf, psiTransf); std::cout << "Rot_and_Tilt " << rotTransf << " " << tiltTransf << std::endl; //LINEA ANTERIOR ORIGINAL XX(z) = Eu(2,0) - Et(2,0); YY(z) = Eu(2,1) - Et(2,1); ZZ(z) = Eu(2,2) - Et(2,2); alpha = atan2(YY(z), XX(z)); //Expressed in rad beta = atan2(XX(z)/cos(alpha), ZZ(z)); //Expressed in rad std::cout << "alpha = " << alpha*180/PI << std::endl; std::cout << "beta = " << beta*180/PI << std::endl; } } } else N=0; if (N>0) { double meanDistance=cumulatedDistance/ang2.size(); DFweights.setValue(MDL_ANGLE_DIFF,meanDistance,newObjId); } else if (newObjId>0) DFweights.setValue(MDL_ANGLE_DIFF,-1.0,newObjId); anotherImageIn2=iter2.hasNext(); } std::complex<double> qu[4], qt[4], M[4], Inv_qu[4], test[4], P1[4], P2[4], Inv_quu[4]; double rotu=34*PI/180, tiltu=10*PI/180, psiu=5*PI/180; double rott=25*PI/180, tiltt=15*PI/180, psit=40*PI/180; quaternion2Paulibasis(rotu, tiltu, psiu, qu); /*std::cout << "quaternion2Pauli" << std::endl; std::cout << "Untilted " << qu[0] << " " << qu[1] << " " << qu[2] << " " << qu[3] << std::endl; std::cout << " " << std::endl;*/ Paulibasis2matrix(qu,M); /*std::cout << "Pauli2matrix" << std::endl; std::cout << "Matriz " << M[0] << " " << M[1] << " " << M[2] << " " << M[3] << std::endl; std::cout << " " << std::endl;*/ inverse_matrixSU2(M, Inv_qu); /*std::cout << "inverse_matrixSU(2)" << std::endl; std::cout << "Inversa " << Inv_qu[0] << " " << Inv_qu[1] << " " << Inv_qu[2] << " " << Inv_qu[3] << std::endl; std::cout << " " << std::endl;*/ quaternion2Paulibasis(rott, tiltt, psit, qt); /*std::cout << "quaternion2Pauli" << std::endl; std::cout << "Tilted " << qt[0] << " " << qt[1] << " " << qt[2] << " " << qt[3] << std::endl; std::cout << " " << std::endl;*/ InversefromPaulibasis(qu,Inv_quu); Pauliproduct(qt, Inv_qu, P1); /*std::cout << "Pauliproduct" << std::endl; std::cout << "quaternion qt " << P1[0] << " " << P1[1] << " " << P1[2] << " " << P1[3] << std::endl; std::cout << " " << std::endl; std::cout << "-----------------------------------" << std::endl;*/ //double alpha_x, alpha_y; //extrarotationangles(P1, alpha_x, alpha_y); //std::cout << "alpha_x = " << alpha_x << " " << "alpha_y = " << alpha_y << std::endl; }
void init_sys(int Nx, int Ny, Vars * U, double dx, double dy, int type){//various initial conditions int i, j, randpx, randmx, randpy, randmy; if(type == 0){//Kelvin-Helmholtz instabilities srand(time(NULL)); for(i=0;i<Nx;++i){ for(j=0;j<Ny;++j){ randpx = rand() % 50;//positive random number between 0 and 50 randmx = -1*(rand() % 50);//negative random number between 0 and 50 randpy = rand() % 50;//positive random number between 0 and 50 randmy = -1*(rand() % 50);//negative random number between 0 and 50 if(fabs(-.5+j*dy) < .2){ U[i*Ny+j].mass = 2.; U[i*Ny+j].xvelocity = 0.5*U[i*Ny+j].mass + .01*(randpx + randmx)/50.; U[i*Ny+j].yvelocity = .01*(randpy + randmy)/50.; U[i*Ny+j].press = 2.5; } else{ U[i*Ny+j].mass = 1.; U[i*Ny+j].xvelocity = -0.5*U[i*Ny+j].mass + .01*(randpx + randmx)/50.; U[i*Ny+j].yvelocity = .01*(randpy + randmy)/50.; U[i*Ny+j].press = 2.5; } } } } else if(type == 1){//blast for(i=0;i<Nx;++i){ for(j=0;j<Ny;++j){ if(pow(-.5+i*dx,2) < .005 && pow(-.5+j*dx,2) < .005){ U[i*Ny+j].mass = 1.; U[i*Ny+j].xvelocity = 0.; U[i*Ny+j].yvelocity = 0.; U[i*Ny+j].press = 10.; } else{ U[i*Ny+j].mass = 1.; U[i*Ny+j].xvelocity = 0.; U[i*Ny+j].yvelocity = 0.; U[i*Ny+j].press = .1; } } } } else if(type == 2){//shock tube for(i=0;i<Nx;++i){ for(j=0;j<Ny;++j){ if(j < Ny/2){ U[i*Ny+j].mass = 1.; U[i*Ny+j].xvelocity = 0.; U[i*Ny+j].yvelocity = 0.; U[i*Ny+j].press = 1.; } else{ U[i*Ny+j].mass = .1; U[i*Ny+j].xvelocity = 0.; U[i*Ny+j].yvelocity = 0.; U[i*Ny+j].press = .125; } } } } else if(type == 3){//shock tube for(i=0;i<Nx;++i){ for(j=0;j<Ny;++j){ if(i < Ny/2){ U[i*Ny+j].mass = 1.; U[i*Ny+j].xvelocity = 0.; U[i*Ny+j].yvelocity = 0.; U[i*Ny+j].press = 1.; } else{ U[i*Ny+j].mass = .1; U[i*Ny+j].xvelocity = 0.; U[i*Ny+j].yvelocity = 0.; U[i*Ny+j].press = .125; } } } } for(i=0;i<Nx;++i){ for(j=0;j<Ny;++j){ U[i*Ny+j].energy = Et(U[i*Ny+j].mass, U[i*Ny+j].xvelocity, U[i*Ny+j].yvelocity, U[i*Ny+j].press); } } }