int main() { int n = 2000; double T = 2000_ns; double delta_t = T/n; vector<double> f1_list = { -30_MHz,-25_MHz,-20_MHz,-14.5_MHz,-10_MHz,-5_MHz,0_MHz,5_MHz,10_MHz,14.5_MHz,20_MHz,25_MHz,30_MHz }; for(auto k=f1_list.begin();k!=f1_list.end();k++) { /* Hamiltonian */ double f1 = *k; Operator H = 2*pi*(fe*Sz(0)+f1*Sx(0)); for(int i=0;i<N;i++) H += 2*pi*(fi*Sz(i+1)+Sz(0)*(Azx[i]*Sx(i+1)+Azy[i]*Sy(i+1)+Azz[i]*Sz(i+1))); auto U = H.U(); /* initial state */ double alpha_e = 7.6216e-4; double alpha_i = -1.1579e-6; //Operator rho0 = Op<2>(0,0.5*(1-alpha_e),0,0,0.5*(1+alpha_e)); Operator rho0 = Op<2>(0,0.5,0.5,0.5,0.5); for(int i=1;i<=N;i++) rho0 *= Op<2>(i,0.5*(1-alpha_i),0,0,0.5*(1+alpha_i)); /* output stream */ stringstream fnstream; fnstream << "malonic-DNP_" << f1/1_MHz << "MHz.txt"; string fn = fnstream.str(); ofstream out(fn); cout << fn << ":" << endl; /* calculate and output */ for(int i=0;i<=n;i++) { double t = delta_t*i; cout << fn << ":\t" << t/T*100 << "%" << endl; out << t/1_ns << '\t'; Operator rho = U(t)*rho0*U(-t); Operator rhoe = rho; for(int j=1;j<=N;j++) rhoe = rhoe.tr(j); out << real(tr(rhoe*Sx(0))) << '\t'; out << real(tr(rhoe*Sy(0))) << '\t'; out << real(tr(rhoe*Sz(0))) << '\t'; for(int j=1;j<=N;j++) { Operator rhoj = rho; for(int k=0;k<=N;k++) { if(k==j) continue; rhoj = rhoj.tr(k); } out << real(tr(rhoj*Sz(j))) << '\t'; } out << endl; } out.close(); } }
int main() { int n = 2000; double T = 2000_ns; double delta_t = T/n; vector<double> f1_list = { 5_MHz,10_MHz,14.5_MHz,20_MHz,25_MHz }; for(auto k=f1_list.begin();k!=f1_list.end();k++) { /* Hamiltonian */ double f1 = *k; Operator HI; for(int i=0;i<N;i++) HI += 2*pi*(fi*Sz(i+1)+Sz(0)*(Azx[i]*Sx(i+1)+Azy[i]*Sy(i+1)+Azz[i]*Sz(i+1))); auto Ht = [&HI,f1](double t) { return 2*pi*(fe*Sz(0)+2*f1*cos(2*pi*fe*t)*Sx(0))+HI; }; /* initial state */ double alpha_e = 7.6216e-4; double alpha_i = -1.1579e-6; //Operator rho0 = Op<2>(0,0.5*(1-alpha_e),0,0,0.5*(1+alpha_e)); Operator rho0 = Op<2>(0,0.5,0.5,0.5,0.5); for(int i=1;i<=N;i++) rho0 *= Op<2>(i,0.5*(1-alpha_i),0,0,0.5*(1+alpha_i)); /* output stream */ stringstream fnstream; fnstream << "malonic-lab-coord-DNP_" << f1/1_MHz << "MHz.txt"; string fn = fnstream.str(); ofstream out(fn); cout << fn << ":" << endl; /* calculate and output */ vector<double> time(n+1); for(int i=0;i<=n;i++) { time[i] = i*delta_t; } vector<Operator> rhot = Operator::SolveLiouvilleEq(Ht,rho0,0.01_ns,time,[fn](double t){ cout << fn << ":" << t/1_ns << endl; }); for(auto &rho: rhot) { Operator rhoe = rho; for(int j=1;j<=N;j++) rhoe = rhoe.tr(j); out << real(tr(rhoe*Sz(0))) << '\t'; for(int j=1;j<=N;j++) { Operator rhoj = rho; for(int k=0;k<=N;k++) { if(k==j) continue; rhoj = rhoj.tr(k); } out << real(tr(rhoj*Sz(j))) << '\t'; } out << endl; } out.close(); } }
RtToken RI_PIXARNOTICE = "\tThe RenderMan (R) Interface Procedures and Protocol are:\n" \ "\t\tCopyright 1988, 1989, Pixar\n" \ "\t\t\tAll Rights Reserved\n\n" \ "\tRenderman (R) is a registered trademark of Pixar\n"; RtBasis RiBezierBasis = { { -1.0, 3.0, -3.0, 1.0 }, { 3.0, -6.0, 3.0, 0.0 }, { -3.0, 3.0, 0.0, 0.0 }, { 1.0, 0.0, 0.0, 0.0 } }; /* Define One Sixth of */ #define Sx(a) ((a)/6) RtBasis RiBSplineBasis = { { Sx(-1.0), Sx( 3.0), Sx(-3.0), Sx( 1.0) }, { Sx( 3.0), Sx(-6.0), Sx( 3.0), Sx( 0.0) }, { Sx(-3.0), Sx( 0.0), Sx( 3.0), Sx( 0.0) }, { Sx( 1.0), Sx( 4.0), Sx( 1.0), Sx( 0.0) } }; /* Define One Half of */ #define Hf(a) ((a)/2) RtBasis RiCatmullRomBasis = { { Hf(-1.0), Hf( 3.0), Hf(-3.0), Hf( 1.0) }, { Hf( 2.0), Hf(-5.0), Hf( 4.0), Hf(-1.0) }, { Hf(-1.0), Hf( 0.0), Hf( 1.0), Hf( 0.0) }, { Hf( 0.0), Hf( 2.0), Hf( 0.0), Hf( 0.0) } }; RtBasis RiHermiteBasis = { { 2.0, 1.0, -2.0, 1.0 }, { -3.0, -2.0, 3.0, -1.0 },
/* >>> start tutorial code >>> */ int main( ) { DifferentialState xT; // the trolley position DifferentialState vT; // the trolley velocity IntermediateState aT; // the trolley acceleration DifferentialState xL; // the cable length DifferentialState vL; // the cable velocity IntermediateState aL; // the cable acceleration DifferentialState phi; // the excitation angle DifferentialState omega; // the angular velocity DifferentialState uT; // trolley velocity control DifferentialState uL; // cable velocity control Control duT; Control duL; // // DEFINE THE PARAMETERS: // const double tau1 = 0.012790605943772; const double a1 = 0.047418203070092; const double tau2 = 0.024695192379264; const double a2 = 0.034087337273386; const double g = 9.81; const double c = 0.2; const double m = 1318.0; // // DEFINE THE MODEL EQUATIONS: // DifferentialEquation f, f2, test_expr; ExportAcadoFunction fun, fun2; aT = -1.0 / tau1 * vT + a1 / tau1 * uT; aL = -1.0 / tau2 * vL + a2 / tau2 * uL; Expression states; states << xT; states << vT; states << xL; states << vL; states << phi; states << omega; states << uT; states << uL; Expression controls; controls << duT; controls << duL; Expression arg; arg << states; arg << controls; int NX = states.getDim(); int NU = controls.getDim(); IntermediateState expr(2); expr(0) = - 1.0/xL*(-g*sin(phi)-aT*cos(phi)-2*vL*omega-c*omega/(m*xL)) + log(duT/duL)*pow(xL,2); expr(1) = - 1.0/xL*(-g*tan(phi)-aT*acos(phi)-2*atan(vL)*omega-c*asin(omega)/exp(xL)) + duT/pow(omega,3); //~ expr(0) = - 1.0/xL*(-g*sin(phi)); //~ expr(1) = duT/pow(omega,3); DifferentialState lambda("", expr.getDim(),1); DifferentialState Sx("", states.getDim(),states.getDim()); DifferentialState Su("", states.getDim(),controls.getDim()); Expression S = Sx; S.appendCols(Su); // SYMMETRIC DERIVATIVES Expression S_tmp = S; S_tmp.appendRows(zeros<double>(NU,NX).appendCols(eye<double>(NU))); IntermediateState dfS,dl; Expression f_tmp = symmetricDerivative( expr, arg, S_tmp, lambda, &dfS, &dl ); f << returnLowerTriangular( f_tmp ); fun.init(f, "symmetricDerivative", NX*(1+NX+NU)+expr.getDim(), 0, 2, 0, 0, 0); // ALTERNATIVE DERIVATIVES IntermediateState tmp = backwardDerivative( expr, states, lambda ); IntermediateState tmp2 = forwardDerivative( tmp, states ); Expression tmp3 = backwardDerivative( expr, controls, lambda ); Expression tmp4 = multipleForwardDerivative( tmp3, states, Su ); Expression tmp5 = tmp4 + tmp4.transpose() + forwardDerivative( tmp3, controls ); Expression f2_tmp1; f2_tmp1 = symmetricDoubleProduct( tmp2, Sx ); f2_tmp1.appendCols( zeros<double>(NX,NU) ); Expression f2_tmp2; f2_tmp2 = Su.transpose()*tmp2*Sx + multipleForwardDerivative( tmp3, states, Sx ); f2_tmp2.appendCols( symmetricDoubleProduct( tmp2, Su ) + tmp5 ); f2_tmp1.appendRows( f2_tmp2 ); f2 << returnLowerTriangular( f2_tmp1 ); fun2.init(f2, "alternativeSymmetric", NX*(1+NX+NU)+expr.getDim(), 0, 2, 0, 0, 0); Function f1; f1 << dfS; f1 << dl; std::ofstream stream2( "ADtest/ADsymbolic_output2.c" ); stream2 << f1; stream2.close(); std::ofstream stream( "ADtest/ADsymbolic_output.c" ); fun.exportCode( stream, "double" ); fun2.exportCode( stream, "double" ); test_expr << expr; stream << test_expr; stream.close(); return 0; }