Changeset 789
- Timestamp:
- 01/13/11 23:54:31 (2 years ago)
- Location:
- trunk
- Files:
-
- 7 modified
-
casadi/fx/fx_internal.cpp (modified) (1 diff)
-
casadi/fx/fx_internal.hpp (modified) (1 diff)
-
casadi/fx/integrator_internal.cpp (modified) (2 diffs)
-
interfaces/ipopt/ipopt_internal.cpp (modified) (1 diff)
-
interfaces/sundials/idas_internal.cpp (modified) (29 diffs)
-
interfaces/sundials/idas_internal.hpp (modified) (2 diffs)
-
modelica/fmi_parser_internal.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/casadi/fx/fx_internal.cpp
r781 r789 125 125 } 126 126 127 void FXInternal::log(const std::string& fcn, const std::string& msg) const{ 128 if(verbose()){ 129 cout << "CasADi log message: In \"" << fcn << "\" --- " << msg << endl; 130 } 131 } 132 127 133 bool FXInternal::verbose() const{ 128 134 return verbose_; -
trunk/casadi/fx/fx_internal.hpp
r781 r789 140 140 void log(const std::string& msg) const; 141 141 142 /** \brief Log the status of the solver, function given */ 143 void log(const std::string& fcn, const std::string& msg) const; 144 142 145 /// Set of module names which are extra monitored 143 146 std::set<std::string> monitors_; -
trunk/casadi/fx/integrator_internal.cpp
r782 r789 97 97 input_[INTEGRATOR_XP0].setSize(nx_,1); // initial state derivative value 98 98 input_[INTEGRATOR_P].setSize(np_,1); // parameter 99 input_[INTEGRATOR_Z0].setSize(nz_,1); // initial algebraic statee 99 100 100 101 // Allocate space for outputs … … 102 103 output_[INTEGRATOR_XF].setSize(nx_,1); 103 104 output_[INTEGRATOR_XPF].setSize(nx_,1); 105 output_[INTEGRATOR_ZF].setSize(nz_,1); 104 106 } 105 107 -
trunk/interfaces/ipopt/ipopt_internal.cpp
r781 r789 257 257 throw "Error during initialization!\n"; 258 258 } 259 J_.evaluate();260 261 259 } 262 260 -
trunk/interfaces/sundials/idas_internal.cpp
r782 r789 40 40 } 41 41 42 int IdasInternal::getNX(const FX& f, const FX& q){ 42 // IdasInternal::IdasInternal(const IdasInternal& integrator): IntegratorInternal(integrator){ 43 // f_ = integrator_.f_; 44 // q_ = integrator_.q_; 45 // 46 // } 47 48 IdasInternal::IdasInternal(const FX& f, const FX& q) : f_(f), q_(q){ 43 49 // Check dimensions 44 50 if(f.getNumInputs()!=DAE_NUM_IN) throw CasadiException("IdasInternal: f has wrong number of inputs"); … … 49 55 } 50 56 51 // Number of states52 int nx = f.input(DAE_Y).get().numel();53 54 // Add quadratures, if any_55 if(!q.isNull()) nx += q.output().get().numel();56 57 return nx;58 }59 60 int IdasInternal::getNP(const FX& f){61 return f.input(DAE_P).get().numel();62 }63 64 // IdasInternal::IdasInternal(const IdasInternal& integrator): IntegratorInternal(integrator){65 // f_ = integrator_.f_;66 // q_ = integrator_.q_;67 //68 // }69 70 IdasInternal::IdasInternal(const FX& f, const FX& q) : f_(f), q_(q){71 57 addOption("suppress_algebraic", OT_BOOLEAN, false); // supress algebraic variables in the error testing 72 58 addOption("calc_ic", OT_BOOLEAN, true); // use IDACalcIC to get consistent initial conditions … … 88 74 is_init = false; 89 75 90 // Set dimensions91 setDimensions(getNX(f,q),getNP(f),f.input(DAE_Z).get().numel());92 93 76 ny_ = f.input(DAE_Y).get().numel(); 94 77 nq_ = q.isNull() ? 0 : q.output().get().numel(); 95 78 int np = f.input(DAE_P).get().numel(); 79 int nz = f.input(DAE_Z).get().numel(); 80 setDimensions(ny_+nq_,np,nz); 81 nyz_ = ny_ + nz; 96 82 ncheck_ = 0; 97 83 } … … 121 107 // Call the base class init 122 108 IntegratorInternal::init(); 123 109 log("IdasInternal::init","begin"); 110 111 // Print 112 if(verbose()){ 113 cout << "Initializing IDAS with ny_ = " << ny_ << ", nq_ = " << nq_ << ", np_ = " << np_ << " and nz_ = " << nz_ << endl; 114 } 115 124 116 // Init ODE rhs function and quadrature functions, jacobian function 125 117 f_.init(); 126 118 if(!q_.isNull()) q_.init(); 119 120 log("IdasInternal::init","functions initialized"); 127 121 128 122 if(!jac_.isNull()){ … … 133 127 linsol_.setSparsity(rowind,col); 134 128 linsol_.init(); 135 } 136 129 130 log("IdasInternal::init","user defined linear solver initialized"); 131 } 132 137 133 // Get the number of forward and adjoint directions 138 134 nfdir_f_ = f_.getOption("number_of_fwd_dir").toInt(); … … 144 140 if(is_init){ 145 141 reset(ad_order_,ad_order_); 142 log("IdasInternal::init","end, Idas already initialized"); 146 143 return; 147 144 } … … 157 154 158 155 // Allocate n-vectors for ivp 159 yz_ = N_VNew_Serial(ny _+nz_);160 yP_ = N_VNew_Serial(ny _+nz_);161 id_ = N_VNew_Serial(ny _+nz_);156 yz_ = N_VNew_Serial(nyz_); 157 yP_ = N_VNew_Serial(nyz_); 158 id_ = N_VNew_Serial(nyz_); 162 159 163 160 // Initialize Idas … … 166 163 N_VConst(0.0, yP_); 167 164 IDAInit(mem_, res_wrapper, t0, yz_, yP_); 165 log("IdasInternal::init","IDA initialized"); 168 166 169 167 // Set error handler function … … 187 185 if(flag != IDA_SUCCESS) idas_error("IDASetMaxStep",flag); 188 186 189 190 187 if(hasSetOption("abstolv")){ 191 188 // Vector absolute tolerances … … 223 220 if(getOption("linear_solver")=="dense"){ 224 221 // Dense jacobian 225 flag = IDADense(mem_, ny _);222 flag = IDADense(mem_, nyz_); 226 223 if(flag != IDA_SUCCESS) idas_error("IDADense",flag); 227 224 if(exact_jacobian_){ … … 242 239 } else if(getOption("linear_solver")=="banded") { 243 240 // Banded jacobian 244 flag = IDABand(mem_, ny _, getOption("upper_bandwidth").toInt(), getOption("lower_bandwidth").toInt());241 flag = IDABand(mem_, nyz_, getOption("upper_bandwidth").toInt(), getOption("lower_bandwidth").toInt()); 245 242 if(flag != IDA_SUCCESS) idas_error("IDABand",flag); 246 243 … … 308 305 } 309 306 } 307 308 log("IdasInternal::init","attached linear solver"); 310 309 311 310 // Sensitivities … … 318 317 yPS_.resize(nfdir_); 319 318 for(int i=0; i<nfdir_; ++i){ 320 yzS_[i] = N_VNew_Serial(ny _+nz_);321 yPS_[i] = N_VNew_Serial(ny _+nz_);319 yzS_[i] = N_VNew_Serial(nyz_); 320 yPS_[i] = N_VNew_Serial(nyz_); 322 321 } 323 322 … … 405 404 if(flag != IDA_SUCCESS) idas_error("IDAQuadSensSStolerances",flag); 406 405 } 406 407 log("IdasInternal::init","initialized forward sensitivities"); 407 408 } // enable fsens 408 409 409 // Adjoint sensitivity problem410 whichB_.resize(nadir_);411 412 // Allocate n-vectors413 yzB_.resize(nadir_);414 yPB_.resize(nadir_);415 for(int i=0; i<nadir_; ++i){416 yzB_[i] = N_VNew_Serial(ny_+nz_);417 yPB_[i] = N_VNew_Serial(ny_+nz_);418 }419 420 // Allocate n-vectors for the adjoint sensitivities of the parameters421 if(np_>0){422 yBB_.resize(nadir_);423 for(int i=0; i<nadir_; ++i){424 yBB_[i] = N_VMake_Serial(np_,&input(INTEGRATOR_P).getAdj(i)[0]);425 }426 }427 428 410 if(nadir_>0){ 429 // Get the number of steos per checkpoint 411 // Adjoint sensitivity problem 412 whichB_.resize(nadir_); 413 414 // Allocate n-vectors 415 yzB_.resize(nadir_); 416 yPB_.resize(nadir_); 417 for(int i=0; i<nadir_; ++i){ 418 yzB_[i] = N_VNew_Serial(nyz_); 419 yPB_[i] = N_VNew_Serial(nyz_); 420 } 421 422 // Allocate n-vectors for the adjoint sensitivities of the parameters 423 if(np_>0){ 424 yBB_.resize(nadir_); 425 for(int i=0; i<nadir_; ++i){ 426 yBB_[i] = N_VMake_Serial(np_,&input(INTEGRATOR_P).getAdj(i)[0]); 427 } 428 } 429 430 // Get the number of steos per checkpoint 430 431 int Nd = getOption("steps_per_checkpoint").toInt(); 431 432 … … 442 443 if(flag != IDA_SUCCESS) idas_error("IDAAdjInit",flag); 443 444 } 445 log("IdasInternal::init","initialized adjoint sensitivities"); 444 446 } // ad_order>0 445 447 446 448 is_init = true; 447 449 isInitAdj_ = false; 450 log("IdasInternal::init","end"); 448 451 } 449 452 … … 481 484 if(getOption("asens_linear_solver")=="dense"){ 482 485 // Dense jacobian 483 flag = IDADenseB(mem_, whichB_[dir], ny _);486 flag = IDADenseB(mem_, whichB_[dir], nyz_); 484 487 if(flag != IDA_SUCCESS) idas_error("IDADenseB",flag); 485 488 } else if(getOption("asens_linear_solver")=="banded") { 486 489 // Banded jacobian 487 flag = IDABandB(mem_, whichB_[dir], ny _, getOption("asens_upper_bandwidth").toInt(), getOption("asens_lower_bandwidth").toInt());490 flag = IDABandB(mem_, whichB_[dir], nyz_, getOption("asens_upper_bandwidth").toInt(), getOption("asens_lower_bandwidth").toInt()); 488 491 if(flag != IDA_SUCCESS) idas_error("IDABand",flag); 489 492 } else if(getOption("asens_linear_solver")=="iterative") { … … 522 525 523 526 524 527 static int counter = 0; 525 528 void IdasInternal::res(double t, const double* yz, const double* yp, double* r){ 529 526 530 // Get time 527 531 time1 = clock(); 528 532 529 // Pass input530 f_.setInput(t,DAE_T);531 f_.setInput(yz,DAE_Y);532 f_.setInput(yp,DAE_YDOT);533 f_.setInput(yz+ny_,DAE_Z);534 f_.setInput(input(INTEGRATOR_P).get(),DAE_P);535 536 // Evaluate537 f_.evaluate();538 539 // Get results540 f_.getOutput(r);541 542 // Check the result for consistency543 for(int i=0; i<ny_; ++i){544 if(isnan(r[i]) || isinf(r[i])){545 if(verbose_)546 cerr << "Warning: The " << i << "-th component of the DAE residual is " << r[i] << " at time t=" << t << "." << endl;547 throw 1;548 }549 }550 551 time2 = clock();552 t_res += double(time2-time1)/CLOCKS_PER_SEC;533 // Pass input 534 f_.setInput(t,DAE_T); 535 f_.setInput(yz,DAE_Y); 536 f_.setInput(yp,DAE_YDOT); 537 f_.setInput(yz+ny_,DAE_Z); 538 f_.setInput(input(INTEGRATOR_P).get(),DAE_P); 539 540 // Evaluate 541 f_.evaluate(); 542 543 // Get results 544 f_.getOutput(r); 545 546 // Check the result for consistency 547 for(int i=0; i<ny_+nz_; ++i){ 548 if(isnan(r[i]) || isinf(r[i])){ 549 if(verbose_) 550 cerr << "Warning: The " << i << "-th component of the DAE residual is " << r[i] << " at time t=" << t << "." << endl; 551 throw 1; 552 } 553 } 554 555 time2 = clock(); 556 t_res += double(time2-time1)/CLOCKS_PER_SEC; 553 557 } 554 558 … … 670 674 671 675 void IdasInternal::reset(int fsens_order, int asens_order){ 676 log("IdasInternal::reset","begin"); 677 672 678 // Reset timers 673 679 t_res = t_fres = t_jac = t_lsolve = t_lsetup_jac = t_lsetup_fac = 0; … … 717 723 int calc_ic = getOption("calc_ic").toInt(); 718 724 if(calc_ic){ 725 log("IdasInternal::reset","trying to find initial values"); 726 if(monitored("IdasInternal::reset")){ 727 cout << "initial guess: " << endl; 728 cout << "x0 = " << argument(INTEGRATOR_X0) << endl; 729 cout << "xp0 = " << argument(INTEGRATOR_XP0) << endl; 730 cout << "z0 = " << argument(INTEGRATOR_Z0) << endl; 731 } 732 719 733 int icopt = IDA_YA_YDP_INIT; // calculate z and xdot given x 720 734 // int icopt = IDA_Y_INIT; // calculate z and x given zdot and xdot (e.g. start in stationary) … … 727 741 flag = IDAGetConsistentIC(mem_, yz_, yP_); 728 742 if(flag != IDA_SUCCESS) idas_error("IDAGetConsistentIC",flag); 729 } 743 744 // Save the algebraic state 745 const double *yzd = NV_DATA_S(yz_); 746 copy(yzd+ny_,yzd+ny_+nz_, argument(INTEGRATOR_Z0).begin()); 747 748 // Save the differential state derivatives 749 const double *yPd = NV_DATA_S(yP_); 750 copy(yPd,yPd+ny_, argument(INTEGRATOR_XP0).begin()); 751 752 // Print progress 753 log("IdasInternal::reset","found consistent initial values"); 754 if(monitored("IdasInternal::reset")){ 755 cout << "x0 = " << argument(INTEGRATOR_X0) << endl; 756 cout << "xp0 = " << argument(INTEGRATOR_XP0) << endl; 757 cout << "z0 = " << argument(INTEGRATOR_Z0) << endl; 758 } 759 } 760 log("IdasInternal::reset","end"); 730 761 } 731 762 732 763 void IdasInternal::integrate(double t_out){ 764 log("IdasInternal::integrate","begin"); 733 765 int flag; 734 766 … … 736 768 double ttol = 1e-9; 737 769 if(fabs(t_-t_out)<ttol){ 738 copy(input(INTEGRATOR_X0).get().begin(),input(INTEGRATOR_X0).get().end(),output(INTEGRATOR_XF).get().begin()); 739 copy(input(INTEGRATOR_XP0).get().begin(),input(INTEGRATOR_XP0).get().end(),output(INTEGRATOR_XPF).get().begin()); 770 // Save the final state 771 setFinalState(); 772 740 773 if(fsens_order_>0){ 741 for(int i=0; i<nfdir_; ++i){ 742 copy(input(INTEGRATOR_X0).getFwd(i).begin(),input(INTEGRATOR_X0).getFwd(i).end(),output(INTEGRATOR_XF).getFwd(i).begin()); 743 copy(input(INTEGRATOR_XP0).getFwd(i).begin(),input(INTEGRATOR_XP0).getFwd(i).end(),output(INTEGRATOR_XPF).getFwd(i).begin()); 744 } 745 } 774 // Set the obtained forward sensitivities 775 setForwardSensitivities(); 776 } 777 log("IdasInternal::integrate","end, already at the end of the horizon end"); 746 778 return; 747 779 } 748 780 749 781 if(asens_order_>0){ 782 log("IdasInternal::integrate","integration with taping"); 750 783 flag = IDASolveF(mem_, t_out, &t_, yz_, yP_, IDA_NORMAL, &ncheck_); 751 784 if(flag != IDA_SUCCESS && flag != IDA_TSTOP_RETURN) idas_error("IDASolveF",flag); 752 785 753 786 } else { 787 log("IdasInternal::integrate","integration without taping"); 754 788 flag = IDASolve(mem_, t_out, &t_, yz_, yP_, IDA_NORMAL); 755 789 if(flag != IDA_SUCCESS && flag != IDA_TSTOP_RETURN) idas_error("IDASolve",flag); 756 790 } 757 791 792 log("IdasInternal::integrate","integration complete"); 793 758 794 if(nq_>0){ 759 795 double tret; … … 779 815 setForwardSensitivities(); 780 816 } 817 log("IdasInternal::integrate","end"); 781 818 } 782 819 … … 1031 1068 const vector<double>& asens_z = q_.input(DAE_Z).getAdj(); 1032 1069 for(int i=0; i<nz_; ++i) 1033 resvalB[i ] += asens_z[i];1070 resvalB[i+ny_] += asens_z[i]; 1034 1071 } 1035 1072 } … … 1265 1302 1266 1303 // Number of rows of the linear solver 1267 int nrow = ny _/nrhs;1304 int nrow = nyz_/nrhs; 1268 1305 1269 1306 // Pass right hand side to the linear solver (transpose necessary) … … 1283 1320 1284 1321 void IdasInternal::psetup(double t, N_Vector yz, N_Vector yp, N_Vector rr, double cj, N_Vector tmp1, N_Vector tmp2, N_Vector tmp3){ 1322 log("IdasInternal::psetup","begin"); 1323 1285 1324 // Get time 1286 1325 time1 = clock(); … … 1310 1349 time1 = clock(); 1311 1350 t_lsetup_fac += double(time1-time2)/CLOCKS_PER_SEC; 1351 1352 log("IdasInternal::psetup","end"); 1312 1353 } 1313 1354 … … 1495 1536 if(hasSetOption("is_differential")){ 1496 1537 vector<int> is_diff = getOption("is_differential").toIntVector(); 1497 if(is_diff.size()!=ny _) throw CasadiException("is_differential has incorrect length");1498 vector<int> is_diff_aug(ny _*(1+ns));1538 if(is_diff.size()!=nyz_) throw CasadiException("is_differential has incorrect length"); 1539 vector<int> is_diff_aug(nyz_*(1+ns)); 1499 1540 for(int i=0; i<1+ns; ++i) 1500 for(int j=0; j<ny _; ++j)1501 is_diff_aug[j+i*ny _] = is_diff[j];1541 for(int j=0; j<nyz_; ++j) 1542 is_diff_aug[j+i*nyz_] = is_diff[j]; 1502 1543 integrator.setOption("is_differential",is_diff_aug); 1503 1544 } … … 1506 1547 vector<int> jacmap(nx_*(1+ns)); 1507 1548 for(int i=0; i<1+ns; ++i){ 1508 for(int j=0; j<ny _; ++j)1509 jacmap[j+nx_*i] = j+ny _*i;1549 for(int j=0; j<nyz_; ++j) 1550 jacmap[j+nx_*i] = j+nyz_*i; 1510 1551 for(int j=0; j<nq_; ++j) 1511 jacmap[ny _+j+nx_*i] = ny_*(1+ns) + j + nq_*i;1552 jacmap[nyz_+j+nx_*i] = nyz_*(1+ns) + j + nq_*i; 1512 1553 } 1513 1554 integrator.setOption("jacmap",jacmap); … … 1574 1615 1575 1616 copy(x0,x0+ny_,yz); 1617 copy(xp0,xp0+ny_,yp); 1576 1618 copy(z0,z0+nz_,yz+ny_); 1577 copy(xp0,xp0+ny_,yp);1578 1619 1579 1620 if(nq_>0){ -
trunk/interfaces/sundials/idas_internal.hpp
r782 r789 188 188 int ny_; // number of differential states 189 189 int nq_; // number of quadratures 190 int nyz_; // number of states seen by IDA (differential states and algebraic states) 190 191 191 192 bool is_init; … … 202 203 // Throw error 203 204 static void idas_error(const std::string& module, int flag); 204 205 // Auxiliary206 static int getNX(const FX& f, const FX& q); // count the total number of states207 static int getNP(const FX& f); // count the number of parameters208 205 209 206 // Set the user defined linear solver -
trunk/modelica/fmi_parser_internal.cpp
r773 r789 231 231 } 232 232 233 ocp_.variables->print(cout,0);234 235 cout << "VariableCategories:" << endl;233 // ocp_.variables->print(cout,0); 234 235 /* cout << "VariableCategories:" << endl; 236 236 for(map<string,int>::const_iterator it=catCounter.begin(); it!=catCounter.end(); ++it) 237 cout << " " << it->first << ": " << it->second << endl; 237 cout << " " << it->first << ": " << it->second << endl;*/ 238 238 } 239 239