1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Changeset 789

Show
Ignore:
Timestamp:
01/13/11 23:54:31 (2 years ago)
Author:
janderss
Message:

idas debug

Location:
trunk
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • trunk/casadi/fx/fx_internal.cpp

    r781 r789  
    125125} 
    126126 
     127void 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 
    127133bool FXInternal::verbose() const{ 
    128134  return verbose_; 
  • trunk/casadi/fx/fx_internal.hpp

    r781 r789  
    140140  void log(const std::string& msg) const; 
    141141 
     142  /** \brief  Log the status of the solver, function given */ 
     143  void log(const std::string& fcn, const std::string& msg) const; 
     144 
    142145  /// Set of module names which are extra monitored 
    143146  std::set<std::string> monitors_; 
  • trunk/casadi/fx/integrator_internal.cpp

    r782 r789  
    9797  input_[INTEGRATOR_XP0].setSize(nx_,1); // initial state derivative value 
    9898  input_[INTEGRATOR_P].setSize(np_,1); // parameter 
     99  input_[INTEGRATOR_Z0].setSize(nz_,1); // initial algebraic statee 
    99100   
    100101  // Allocate space for outputs 
     
    102103  output_[INTEGRATOR_XF].setSize(nx_,1); 
    103104  output_[INTEGRATOR_XPF].setSize(nx_,1); 
     105  output_[INTEGRATOR_ZF].setSize(nz_,1); 
    104106} 
    105107 
  • trunk/interfaces/ipopt/ipopt_internal.cpp

    r781 r789  
    257257    throw "Error during initialization!\n"; 
    258258  } 
    259         J_.evaluate(); 
    260  
    261259} 
    262260 
  • trunk/interfaces/sundials/idas_internal.cpp

    r782 r789  
    4040} 
    4141 
    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 
     48IdasInternal::IdasInternal(const FX& f, const FX& q) : f_(f), q_(q){ 
    4349  // Check dimensions 
    4450  if(f.getNumInputs()!=DAE_NUM_IN) throw CasadiException("IdasInternal: f has wrong number of inputs"); 
     
    4955  } 
    5056 
    51   // Number of states 
    52   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){ 
    7157  addOption("suppress_algebraic",          OT_BOOLEAN, false); // supress algebraic variables in the error testing 
    7258  addOption("calc_ic",                     OT_BOOLEAN, true);  // use IDACalcIC to get consistent initial conditions 
     
    8874  is_init = false; 
    8975 
    90   // Set dimensions 
    91   setDimensions(getNX(f,q),getNP(f),f.input(DAE_Z).get().numel()); 
    92  
    9376  ny_ = f.input(DAE_Y).get().numel(); 
    9477  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; 
    9682  ncheck_ = 0; 
    9783} 
     
    121107  // Call the base class init 
    122108  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   
    124116  // Init ODE rhs function and quadrature functions, jacobian function 
    125117  f_.init(); 
    126118  if(!q_.isNull()) q_.init(); 
     119   
     120  log("IdasInternal::init","functions initialized"); 
    127121   
    128122  if(!jac_.isNull()){ 
     
    133127      linsol_.setSparsity(rowind,col); 
    134128      linsol_.init(); 
    135   } 
    136    
     129     
     130    log("IdasInternal::init","user defined linear solver initialized"); 
     131  } 
     132 
    137133  // Get the number of forward and adjoint directions 
    138134  nfdir_f_ = f_.getOption("number_of_fwd_dir").toInt(); 
     
    144140  if(is_init){ 
    145141    reset(ad_order_,ad_order_); 
     142    log("IdasInternal::init","end, Idas already initialized"); 
    146143    return; 
    147144  } 
     
    157154 
    158155  // 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_); 
    162159 
    163160  // Initialize Idas 
     
    166163  N_VConst(0.0, yP_); 
    167164  IDAInit(mem_, res_wrapper, t0, yz_, yP_); 
     165  log("IdasInternal::init","IDA initialized"); 
    168166 
    169167  // Set error handler function 
     
    187185  if(flag != IDA_SUCCESS) idas_error("IDASetMaxStep",flag); 
    188186   
    189  
    190187  if(hasSetOption("abstolv")){ 
    191188    // Vector absolute tolerances 
     
    223220  if(getOption("linear_solver")=="dense"){ 
    224221    // Dense jacobian 
    225     flag = IDADense(mem_, ny_); 
     222    flag = IDADense(mem_, nyz_); 
    226223    if(flag != IDA_SUCCESS) idas_error("IDADense",flag); 
    227224    if(exact_jacobian_){ 
     
    242239  } else if(getOption("linear_solver")=="banded") { 
    243240    // 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()); 
    245242    if(flag != IDA_SUCCESS) idas_error("IDABand",flag); 
    246243     
     
    308305    } 
    309306  } 
     307   
     308  log("IdasInternal::init","attached linear solver"); 
    310309     
    311310 // Sensitivities 
     
    318317     yPS_.resize(nfdir_); 
    319318     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_); 
    322321      } 
    323322 
     
    405404      if(flag != IDA_SUCCESS) idas_error("IDAQuadSensSStolerances",flag); 
    406405    } 
     406     
     407    log("IdasInternal::init","initialized forward sensitivities"); 
    407408  } // enable fsens 
    408409 
    409   // Adjoint sensitivity problem 
    410   whichB_.resize(nadir_); 
    411  
    412   // Allocate n-vectors 
    413   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 parameters 
    421   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  
    428410  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 
    430431      int Nd = getOption("steps_per_checkpoint").toInt(); 
    431432 
     
    442443      if(flag != IDA_SUCCESS) idas_error("IDAAdjInit",flag); 
    443444  } 
     445  log("IdasInternal::init","initialized adjoint sensitivities"); 
    444446 } // ad_order>0 
    445447  
    446448 is_init = true; 
    447449 isInitAdj_ = false; 
     450 log("IdasInternal::init","end"); 
    448451} 
    449452 
     
    481484    if(getOption("asens_linear_solver")=="dense"){ 
    482485      // Dense jacobian 
    483       flag = IDADenseB(mem_, whichB_[dir], ny_); 
     486      flag = IDADenseB(mem_, whichB_[dir], nyz_); 
    484487      if(flag != IDA_SUCCESS) idas_error("IDADenseB",flag); 
    485488    } else if(getOption("asens_linear_solver")=="banded") { 
    486489      // 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()); 
    488491      if(flag != IDA_SUCCESS) idas_error("IDABand",flag); 
    489492    } else if(getOption("asens_linear_solver")=="iterative") { 
     
    522525 
    523526 
    524  
     527static int counter = 0; 
    525528void IdasInternal::res(double t, const double* yz, const double* yp, double* r){ 
     529 
    526530  // Get time 
    527531  time1 = clock(); 
    528532   
    529    // Pass input 
    530    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     // Evaluate 
    537    f_.evaluate(); 
    538      
    539     // Get results 
    540    f_.getOutput(r); 
    541     
    542    // Check the result for consistency 
    543    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; 
    553557} 
    554558 
     
    670674 
    671675void IdasInternal::reset(int fsens_order, int asens_order){ 
     676  log("IdasInternal::reset","begin"); 
     677 
    672678  // Reset timers 
    673679  t_res = t_fres = t_jac = t_lsolve = t_lsetup_jac = t_lsetup_fac = 0; 
     
    717723  int calc_ic = getOption("calc_ic").toInt(); 
    718724  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 
    719733    int icopt = IDA_YA_YDP_INIT; // calculate z and xdot given x 
    720734    // int icopt = IDA_Y_INIT; // calculate z and x given zdot and xdot (e.g. start in stationary) 
     
    727741    flag = IDAGetConsistentIC(mem_, yz_, yP_); 
    728742    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"); 
    730761} 
    731762   
    732763void IdasInternal::integrate(double t_out){ 
     764  log("IdasInternal::integrate","begin"); 
    733765  int flag; 
    734766   
     
    736768  double ttol = 1e-9; 
    737769  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     
    740773    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"); 
    746778    return; 
    747779  } 
    748780 
    749781  if(asens_order_>0){ 
     782    log("IdasInternal::integrate","integration with taping"); 
    750783    flag = IDASolveF(mem_, t_out, &t_, yz_, yP_, IDA_NORMAL, &ncheck_); 
    751784    if(flag != IDA_SUCCESS && flag != IDA_TSTOP_RETURN) idas_error("IDASolveF",flag); 
    752785     
    753786  } else { 
     787    log("IdasInternal::integrate","integration without taping"); 
    754788    flag = IDASolve(mem_, t_out, &t_, yz_, yP_, IDA_NORMAL); 
    755789    if(flag != IDA_SUCCESS && flag != IDA_TSTOP_RETURN) idas_error("IDASolve",flag); 
    756790  } 
    757    
     791 
     792  log("IdasInternal::integrate","integration complete"); 
     793 
    758794  if(nq_>0){ 
    759795    double tret; 
     
    779815    setForwardSensitivities(); 
    780816  } 
     817  log("IdasInternal::integrate","end"); 
    781818} 
    782819 
     
    10311068    const vector<double>& asens_z = q_.input(DAE_Z).getAdj(); 
    10321069    for(int i=0; i<nz_; ++i) 
    1033       resvalB[i] += asens_z[i]; 
     1070      resvalB[i+ny_] += asens_z[i]; 
    10341071  } 
    10351072} 
     
    12651302   
    12661303  // Number of rows of the linear solver 
    1267   int nrow = ny_/nrhs; 
     1304  int nrow = nyz_/nrhs; 
    12681305   
    12691306  // Pass right hand side to the linear solver (transpose necessary) 
     
    12831320 
    12841321void 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 
    12851324  // Get time 
    12861325  time1 = clock(); 
     
    13101349  time1 = clock(); 
    13111350  t_lsetup_fac += double(time1-time2)/CLOCKS_PER_SEC; 
     1351 
     1352  log("IdasInternal::psetup","end"); 
    13121353} 
    13131354 
     
    14951536  if(hasSetOption("is_differential")){ 
    14961537    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)); 
    14991540    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]; 
    15021543    integrator.setOption("is_differential",is_diff_aug); 
    15031544  } 
     
    15061547  vector<int> jacmap(nx_*(1+ns)); 
    15071548  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; 
    15101551    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; 
    15121553  } 
    15131554  integrator.setOption("jacmap",jacmap); 
     
    15741615   
    15751616  copy(x0,x0+ny_,yz); 
     1617  copy(xp0,xp0+ny_,yp); 
    15761618  copy(z0,z0+nz_,yz+ny_); 
    1577   copy(xp0,xp0+ny_,yp); 
    15781619 
    15791620  if(nq_>0){ 
  • trunk/interfaces/sundials/idas_internal.hpp

    r782 r789  
    188188  int ny_; // number of differential states 
    189189  int nq_; // number of quadratures 
     190  int nyz_; // number of states seen by IDA (differential states and algebraic states) 
    190191   
    191192  bool is_init; 
     
    202203  // Throw error 
    203204  static void idas_error(const std::string& module, int flag); 
    204    
    205   // Auxiliary 
    206   static int getNX(const FX& f, const FX& q); // count the total number of states 
    207   static int getNP(const FX& f); // count the number of parameters 
    208205   
    209206  // Set the user defined linear solver 
  • trunk/modelica/fmi_parser_internal.cpp

    r773 r789  
    231231  } 
    232232   
    233   ocp_.variables->print(cout,0); 
    234    
    235   cout << "VariableCategories:" << endl; 
     233//  ocp_.variables->print(cout,0); 
     234   
     235/*  cout << "VariableCategories:" << endl; 
    236236  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;*/ 
    238238} 
    239239