Diana  0.8.3
PARDASPKSolver.hpp
00001 /* -----------------------------------------------------------------------------
00002  *  Diana process modelling, simulation and analysis software
00003  *  Copyright (c) 2005, Michael Krasnyk
00004  *  All rights reserved.
00005 
00006  *  This file is a part of Diana process modelling, simulation and analysis software
00007  *
00008  *  Diana is free software; you can redistribute it and/or modify it
00009  *  under the terms of the GNU General Public License as published
00010  *  by the Free Software Foundation (see accompanying file LICENSE)
00011  * -----------------------------------------------------------------------------
00012  *  $Id: PARDASPKSolver.hpp 6577 2007-04-20 09:58:00Z dosta $
00013  * -----------------------------------------------------------------------------
00014  *  Description:
00015  */
00016 
00017 #ifndef PAR_DASPK_SOLVER_H
00018 #define PAR_DASPK_SOLVER_H
00019 #include "PARDASPK_lib.h"
00020 
00021 namespace Diana 
00022 {
00025   class PARDASPKSolver : public Diana::IDianaNumericDAESolver,
00026                          public Diana::DianaNumericSolver    
00027  {
00028   public:
00029   
00030    PARDASPKSolver (Numeric::Solvers::Model::ICapeNumericModel* model)
00031       throw (Common::Error::ECapeUnknown);
00032    ~PARDASPKSolver();
00033     
00034    const Common::Types::CapeString& GetComponentName() const
00035       throw (Common::Error::ECapeUnknown);
00036       
00037    void SetComponentName(const Common::Types::CapeString& _name)
00038       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00039       
00040    const Common::Types::CapeString& GetComponentDescription() const
00041       throw (Common::Error::ECapeUnknown);
00042       
00043    void SetComponentDescription(const Common::Types::CapeString& _description)
00044       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00045       
00046     
00047    Common::Collection::ICapeCollection* GetParameters()
00048       throw (Common::Error::ECapeUnknown, Common::Error::ECapeFailedInitialisation,
00049              Common::Error::ECapeNoImpl);
00050      
00051           
00052    void SetSimulationContext(Common::Identification::ICapeIdentification* simContextManager)
00053       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00054              Common::Error::ECapeFailedInitialisation, Common::Error::ECapeNoImpl);
00055 
00056 
00057    void Initialize()
00058       throw (Common::Error::ECapeUnknown, Common::Error::ECapeFailedInitialisation,
00059              Common::Error::ECapeOutOfResources, Common::Error::ECapeLicenceError,
00060              Common::Error::ECapeBadInvOrder);
00061 
00062      
00063    void Terminate()
00064       throw (Common::Error::ECapeUnknown, Common::Error::ECapeOutOfResources,
00065              Common::Error::ECapeBadInvOrder);
00066 
00067      
00068    Numeric::Solvers::Solver::SolveReturn Solve()
00069       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder,
00070              Common::Error::ECapeSolvingError, Common::Error::ECapeOutOfResources);          
00071      
00072    
00073    Common::Types::CapeArrayDouble GetSolution()
00074       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00075      
00076           
00077    void Destroy()
00078       throw (Common::Error::ECapeUnknown);
00079 
00080    void SetRelTolerance(const Common::Types::CapeArrayDouble& relTolValue)
00081       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00082              Common::Error::ECapeOutOfBounds);
00083 
00084    Common::Types::CapeArrayDouble GetRelTolerance()
00085       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00086 
00087    void SetAbsTolerance(const Common::Types::CapeArrayDouble& absTolValues)
00088       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00089              Common::Error::ECapeOutOfBounds);
00090 
00091    Common::Types::CapeArrayDouble GetAbsTolerance()
00092       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00093       
00094       
00095       
00096    Numeric::Solvers::Model::CapeArrayNumericEventInfo*
00097    AdvanceToNextEvent(Numeric::Solvers::Model::CapeArrayNumericEventInfo endConditions,
00098                        Common::Types::CapeDouble& timeBefore,
00099                        Common::Types::CapeDouble& timeAfter)
00100       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00101              Common::Error::ECapeOutOfBounds, Common::Error::ECapeBadInvOrder,
00102              Common::Error::ECapeSolvingError, Common::Error::ECapeOutOfResources,
00103              Common::Error::ECapeNoImpl);
00104          
00105    void setValuesFromESO();
00106     
00107    void RecalculateIC();
00108     
00109    void SolveWR()
00110       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder,
00111              Common::Error::ECapeSolvingError, Common::Error::ECapeOutOfResources); 
00112                     
00113 protected:  
00115    static void calculateResiduals( double* T, double Y[], double YPrime[],
00116       double Delta[], int IPar[] );
00117 
00119    static void calculateJacobian( int* NEQ, double* T, double Y[], 
00120       double YPRIME[], double* CJ, int IPAR[], int* RECALC );
00121 
00123    static void LinearSolver( int* NEQ, double B[], int IPar[] );        
00124    
00125    static void SaveInternalStates( double* X, int* NEQ,
00126       int* KOLD, double PHI[], double PSI[], int IPar[] );
00127 protected:
00128    Common::Types::CapeString                    strName;
00129    Common::Types::CapeString                    strDescription;
00130     
00131    Numeric::Solvers::Model::ICapeNumericModel*  pModel; 
00132    Numeric::Solvers::Eso::ICapeNumericDAESO*    pESO;   
00133     
00134                                                                                                                                                 
00135    Diana::DianaCollection                       collParameters; 
00136    
00138    Common::Types::CapeBoolean                   m_bStartSimulation; 
00139    Common::Types::CapeBoolean                   m_bNeedToInitialize;
00140    CapeBoolean                                  m_bNeedToRestoreState;
00141    CapeBoolean                                  m_bNeedToSaveState;
00142     
00143    Common::Types::CapeLong                  m_lVarsQuantity; 
00144    Common::Types::CapeLong                  m_lEquationsQuantity; 
00145    Common::Types::CapeDouble                    dblT; 
00146    Common::Types::CapeDouble                    dblT0; 
00147    Common::Types::CapeDouble                    dblTend; 
00148    Common::Types::CapeLong                      nVerboseLevel; 
00149     
00150     
00151    int                                          nDASKPStatus, nDASKPStatus_old; 
00152    CapeDouble                                   dblRTol, dblAbsTol;
00153    
00155    double                                       *vecRelTol;
00156    
00158    double                                       *vecAbsTol;
00159    
00160    int*                                         IPar; 
00161                                                                                                                                                 
00163    CapeDouble                                   m_dLastInternalSolverStep;
00164    
00165    /* Structure for data exchange between fortran DASPK and
00166      C++ Jac, Res ans Solve functions */    
00167    struct sExchangeStruct
00168    {
00170       Numeric::Solvers::Eso::ICapeNumericDAESO* pESO; 
00171       
00174       CapeArrayDouble                    JacHistoryTimes;
00175       CapeArrayDouble                    CJHistory;
00176       std::vector<DianaSparseArray*>     JacHistory;
00177       std::vector<DianaSparseSolver*>    SolversHistory;
00178       CapeLong                           lCurrentIndex;      
00179       CapeLong                           lOffset;      
00180       CapeDouble                         m_dIterationNumber;
00181       CapeBoolean                        bNeedToSaveHistory;
00182             
00184       CapeDouble                         dTempJacCalls;
00185       
00187       CapeDouble                         dTempSolCalls;
00188       
00190       CapeDouble                         dTempResCalls;
00191 
00194       void*                              UpdateReceivedValues;
00195       void*                              pMemoryBanks;
00196       void*                              SaveStates;
00197 
00198       //this flag indicate to use backup version of jacobian
00199       CapeBoolean                        bUseBackupJacobian; 
00200       DianaSparseArray*                  BackupJacobian;
00201       DianaSparseSolver*                 BackupSolver;
00202        
00203       
00204       CapeBoolean   bTempTemp;
00205    } Adresses;
00206  
00207    int  nLRW, nLIW; 
00208    
00210    double                                *RWORK, *RWORK_old; 
00211    int                                   *IWORK, *IWORK_old; 
00212    double                                *Y, *Y_old;         
00213    double                                *YP, *YP_old;       
00214    double                                 T0_old;
00215    int                                   m_nInfo[ 30 ], m_nInfo_old[ 30 ]; 
00216 
00218    CapeDouble                            m_dMaxStep;
00219    CapeDouble                            m_bNeedToPrint;
00220    CapeDouble   m_dInitialStepSize;
00221 };
00222 };
00223 
00224 #endif // DASPK_SOLVER_H