Diana  0.8.3
DianaDAESO.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: DianaDAESO.hpp 8160 2009-03-16 18:36:30Z miha $
00013  * -----------------------------------------------------------------------------
00014  *  Description:
00015  */
00016 
00017 #ifndef DIANA_ESO_HPP
00018 #define DIANA_ESO_HPP
00019 
00020 
00021 #include <CapeOpen.hpp>
00022 #include <Diana/IDianaSolver.hpp>
00023 #include <Diana/IDianaDAESO.hpp>
00024 #include <Basic/DianaBasic.hpp>
00025 #include <Diana/DianaParameter.hpp>
00026 #include <Diana/DianaPetriNet.hpp>
00027 
00028 #ifdef _MSC_VER
00029 #define finite _finite
00030 #include "float.h"
00031 #endif
00032 
00033 namespace Diana{
00034 
00035   enum DianaVariableType {
00036     dvNone          = 0x00,
00037     dvState         = 0x01,
00038     dvDerivative    = 0x02,
00039     dvResidual      = 0x04,
00040     dvRealParam     = 0x08,
00041     dvIntegerParam  = 0x10,
00042     dvHelpVariables = 0x11,
00043     dvUnknown       = 0x12, 
00044   };
00045 
00046   class PetriNet;
00047 
00051   class DianaDAESO : virtual public IDianaDAESO{
00053     friend class DianaParameter;
00054   public:
00056     DianaDAESO(const Common::Types::CapeString& _name,
00057                const Common::Types::CapeString& _description,
00058                Common::Types::CapeLong          _nVars,
00059                Common::Types::CapeLong          _nEqns,
00060                Common::Types::CapeLong          _nRealParams,
00061                Common::Types::CapeLong          _nStructureParams,
00062                Common::Types::CapeLong          _nIntegerParams,
00063                Common::Types::CapeLong          _nHelpVariables,
00064                Common::Types::CapeLong          _nPhi = 0,
00065                Common::Types::CapeLong          _nPlaces = 0);
00067     DianaDAESO(const DianaDAESO& org);
00068       
00069 
00071     virtual ~DianaDAESO();
00072 
00075     virtual DIANA_DAESO_CLONERET* clone() throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00076 
00078     const Common::Types::CapeString& GetComponentName() const
00079       throw (Common::Error::ECapeUnknown);
00080 
00083     void SetComponentName(const Common::Types::CapeString& _name)
00084       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00085 
00087     const Common::Types::CapeString& GetComponentDescription() const
00088       throw (Common::Error::ECapeUnknown);
00089 
00092     void SetComponentDescription(const Common::Types::CapeString& _description)
00093       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00094 
00104     Common::Collection::ICapeCollection* GetParameters()
00105       throw (Common::Error::ECapeUnknown, Common::Error::ECapeFailedInitialisation);
00106 
00110     Common::Collection::ICapeCollection* GetStateVariables()
00111       throw (Common::Error::ECapeUnknown);
00112 
00126     void SetSimulationContext(Common::Identification::ICapeIdentification* simContextManager)
00127       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00128              Common::Error::ECapeFailedInitialisation, Common::Error::ECapeNoImpl);
00129 
00142     void Initialize()
00143       throw (Common::Error::ECapeUnknown, Common::Error::ECapeFailedInitialisation,
00144              Common::Error::ECapeOutOfResources, Common::Error::ECapeLicenceError,
00145              Common::Error::ECapeBadInvOrder);
00150       void Reset()
00151           throw(Common::Error::ECapeUnknown);
00152 
00161     void Terminate()
00162       throw (Common::Error::ECapeUnknown, Common::Error::ECapeOutOfResources,
00163              Common::Error::ECapeBadInvOrder);
00164 
00166     Common::Types::CapeLong GetNumVars()
00167       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00168 
00170     Common::Types::CapeLong GetNumEqns()
00171       throw (Common::Error::ECapeUnknown);
00172 
00174     Common::Types::CapeLong GetNumRealParams()
00175       throw (Common::Error::ECapeUnknown);
00176 
00181     void SetFixedVariables(const Common::Types::CapeArrayLong& varIndices,
00182                            const Common::Types::CapeArrayDouble& varValues)
00183       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00184              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds,
00185              Common::Error::ECapeNoImpl);
00186 
00189     void SetAllVariables(const Common::Types::CapeArrayDouble& varValues)
00190       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00191              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00192 
00196     void SetVariables(const Common::Types::CapeArrayLong& varIndices,
00197                       const Common::Types::CapeArrayDouble& varValues)
00198       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00199              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00200 
00202     const Common::Types::CapeArrayDouble& GetAllVariables()
00203       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00204 
00207     Common::Types::CapeArrayDouble GetVariables(const Common::Types::CapeArrayLong& varIndices)
00208       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00209              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00210 
00214     Common::Collection::ICapeCollection* GetRealParameters()
00215             throw (Common::Error::ECapeUnknown);
00216 
00219     void SetAllRealParameters(const Common::Types::CapeArrayDouble& parValues)
00220       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00221              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00222 
00226     void SetRealParameters(const Common::Types::CapeArrayLong& parIndices,
00227                                    const Common::Types::CapeArrayDouble& parValues)
00228       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00229              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00230 
00232     const Common::Types::CapeArrayDouble& GetAllRealParameters()
00233       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00234 
00237     virtual Common::Types::CapeArrayDouble GetRealParameters(const Common::Types::CapeArrayLong& parIndices)
00238       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00239              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00240 
00242     const Common::Types::CapeArrayDouble& GetAllResiduals()
00243       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder,
00244              Common::Error::ECapeFailedInitialisation);
00245 
00248     Common::Types::CapeArrayDouble GetResiduals(const Common::Types::CapeArrayLong& eqnIndices)
00249       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00250              Common::Error::ECapeOutOfBounds, Common::Error::ECapeBadInvOrder,
00251              Common::Error::ECapeFailedInitialisation);
00252 
00259     const Diana::DianaSparseArray& GetJacobianStruct()
00260       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00261 
00268     const Diana::DianaSparseArray& GetAllJacobianValues()
00269       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00270 
00276     Common::Types::CapeArrayDouble GetJacobianValues(const Common::Types::CapeArrayLong& indices)
00277       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00278              Common::Error::ECapeNoImpl, Common::Error::ECapeOutOfBounds);
00279 
00281     void SetVariablesIndex(const Common::Types::CapeArrayLong& varIndexes)
00282       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00283              Common::Error::ECapeOutOfBounds, Common::Error::ECapeNoImpl);
00284 
00286     const Common::Types::CapeArrayDouble& GetLowerBounds()
00287       throw (Common::Error::ECapeUnknown);
00288 
00290     const Common::Types::CapeArrayDouble& GetUpperBounds()
00291       throw (Common::Error::ECapeUnknown);
00292 
00298     void SetAllDerivatives(const Common::Types::CapeArrayDouble& derValues)
00299       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00300              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00301 
00307     const Common::Types::CapeArrayDouble& GetAllDerivatives()
00308       throw (Common::Error::ECapeUnknown, Common::Error::ECapeBadInvOrder);
00309 
00313     Common::Types::CapeArrayDouble GetDerivatives(const Common::Types::CapeArrayLong& derIndices)
00314       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00315              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00316 
00321     void SetDerivatives(const Common::Types::CapeArrayLong& derIndices,
00322                         const Common::Types::CapeArrayDouble& derValues)
00323       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00324              Common::Error::ECapeBadInvOrder, Common::Error::ECapeOutOfBounds);
00325 
00334     const Diana::DianaSparseArray& GetDiffJacobianStruct()
00335       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00336 
00344     const Diana::DianaSparseArray& GetAllDiffJacobianValues()
00345       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00346 
00351     Common::Types::CapeArrayDouble GetDiffJacobianValues(const Common::Types::CapeArrayLong& indices)
00352       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument,
00353              Common::Error::ECapeNoImpl, Common::Error::ECapeOutOfBounds);
00354 
00357     void SetIndependentVar(Common::Types::CapeDouble indVar)
00358       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00359 
00361     Common::Types::CapeDouble GetIndependentVar()
00362       throw (Common::Error::ECapeUnknown);
00363 
00364 
00365     void SaveState(const Common::Types::CapeString& filename)
00366       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00367     
00368     void LoadState(const Common::Types::CapeString& filename)
00369       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00370 
00371     void LoadStateDiva(const Common::Types::CapeString& states,
00372                        const Common::Types::CapeString& params,
00373                        const Common::Types::CapeString& mapfile=Common::Types::CapeString())
00374       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00375 
00376     const Diana::DianaSparseArray& GetHighOrderJacobian(Common::Types::CapeLong nXOrder,
00377                                                         Common::Types::CapeLong nDOrder,
00378                                                         Common::Types::CapeLong nPOrder)
00379       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00380     
00381     DianaParameter* GetVariable(const Common::Types::CapeString& name) const
00382       throw(Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00383 
00384     CapeArrayDouble GetAbsoluteError() const
00385       throw(Common::Error::ECapeUnknown);
00386 
00387 
00388 #ifndef SWIG
00389 
00391       virtual const CapeArrayDouble& GetExplicitEventTimes();
00392 
00395     virtual void calculatePhiFunctions();
00396 
00399     void SaveState() throw (Common::Error::ECapeUnknown);
00400 
00403     void RestoreState() throw (Common::Error::ECapeUnknown);
00404 
00406     void BeforeStep(Diana::IDianaNumericDAESolver *solver) throw (Common::Error::ECapeUnknown);
00407 
00409     void AfterStep(Diana::IDianaNumericDAESolver *solver) throw (Common::Error::ECapeUnknown);
00410 #endif
00411 
00412       const Common::Types::CapeArrayDouble& GetAllPhiValues() 
00413           throw (Common::Error::ECapeUnknown);
00414 
00416       Common::Types::CapeDouble GetPhiValue(CapeLong index) 
00417           throw (Common::Error::ECapeUnknown);
00418 
00420       IDianaPetriNet* GetPetriNet() 
00421           throw (Common::Error::ECapeUnknown);
00422 
00423     virtual const DianaSparseArray& GetAllParJacobianValues(const Common::Types::CapeArrayLong& parIndices)
00424       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00425 #ifndef SWIG
00426     virtual const DianaSparseArray& GetAllParJacobianValues(const Common::Types::CapeArrayLong* const parIndices=NULL)
00427       throw (Common::Error::ECapeUnknown, Common::Error::ECapeNoImpl);
00428 #endif
00429 
00430 #ifndef SWIG
00431 
00435     Common::Types::CapeDouble FindEventTime(int phi_index);
00436 
00438       //Common::Types::CapeDouble GetOldIndependentVar() throw (Common::Error::ECapeUnknown);
00439 
00441     //Common::Types::CapeDouble GetOldPhi(int index) throw (Common::Error::ECapeUnknown);
00442 #endif
00443 
00444   protected:
00445     void setChangedFlags(DianaVariableType bChanged);
00446 
00447   public:
00453     Common::Types::CapeDouble FindEventTime(Common::Types::CapeDouble t1,  Common::Types::CapeDouble t2, int phi_index);
00454 
00458     virtual void MoveToTime(Common::Types::CapeDouble t);
00459 
00460   protected:
00466     virtual DianaDAESO* createClone(const DianaDAESO& original){return NULL;};
00467 
00468     Common::Types::CapeString makeVarName(const Common::Types::CapeString name, 
00469                                           const Common::Types::CapeArrayLong& indices)
00470       throw (Common::Error::ECapeUnknown);
00471 
00472     Common::Types::CapeString makeVarName(const Common::Types::CapeString name, const int idxAmount, ...)
00473       throw (Common::Error::ECapeUnknown);
00474 
00475     int getVarPosition(void* var, DianaVariableType type) const
00476       throw (Common::Error::ECapeUnknown, Common::Error::ECapeInvalidArgument);
00477 
00478     Common::Types::CapeBoolean isAscendantIndices(const Common::Types::CapeArrayLong& idx,
00479                                                   const std::vector<DianaVariableType>& type);
00480 
00484       virtual void calculateHelpVariables(){};
00488       virtual void calculateConstantHelpVariables(){};
00489 
00494     virtual void calculateResiduals() = 0;
00495 
00496     Common::Types::CapeBoolean makeMatrixPartitioning(const Diana::DianaSparseArray& dsarray,
00497                                 std::vector<Common::Types::CapeArrayLong>& grouping);
00498 
00499     enum enumJacobianType{
00500       jtStateVariables,
00501       jtDerivatives,
00502     };
00503 
00504     void calculateFDJacobiMatrix(enumJacobianType type);
00505     Common::Types::CapeBoolean calculateFDJacobiMatrixPartition(enumJacobianType type, CapeDouble dblFDDir=1.0);
00506     Common::Types::CapeBoolean calculateFDJacobiMatrixSimple(enumJacobianType type, CapeDouble dblFDDir=1.0);
00510     Common::Types::CapeBoolean calculateFDParJacobiMatrix(const Common::Types::CapeArrayLong& parIndices);
00511 
00516     void checkResidualFiniteness()
00517       throw (Common::Error::ECapeUnknown);
00518 
00519     void checkMatrixFiniteness(const Diana::DianaSparseArray& mat)
00520       throw (Common::Error::ECapeUnknown);
00521 
00522 
00523   protected:
00524     Common::Types::CapeBoolean                           bInitialized;
00525     Common::Types::CapeString      name;                  
00526     Common::Types::CapeString      description;           
00527 
00528     Diana::DianaCollection         collParameters;        
00529     Diana::DianaCollection         collStateVariables;    
00530     Diana::DianaCollection         collRealParameters;    
00531 
00532     Common::Types::CapeDouble      dblIndependentVar;     
00533 
00534     // possible due to:
00535     // INTERNATIONAL STANDARD "Programming languages C++" ISO/IEC 14882:2003(E)
00536     // 23.2.4/1:
00537     // The elements of a vector are stored contiguously, meaning that if v is 
00538     // a vector<T, Allocator> where T is some type other than bool, 
00539     // then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().
00540     Common::Types::CapeArrayDouble arrStates;             
00541     Common::Types::CapeArrayDouble arrUpperBounds;        
00542     Common::Types::CapeArrayDouble arrLowerBounds;        
00543     Common::Types::CapeArrayDouble arrDerivatives;        
00544     Common::Types::CapeArrayDouble arrResiduals;          
00545     Common::Types::CapeArrayDouble arrRealParams;         
00546     Common::Types::CapeArrayLong   arrIntegerParams;      
00547     Common::Types::CapeArrayLong   arrStructureParams;      
00548     Common::Types::CapeArrayDouble  arrHelpVariables;   
00549     Common::Types::CapeArrayBoolean  arrPlaces;          
00550       std::vector<std::vector<CapeLong*> > arrPlaceIndexPointers; 
00551       std::vector<std::vector<CapeLong> > arrPlaceIndexValues; 
00552       std::vector<CharFunction> arrCharFunctions;   
00553       std::vector<CharFunction> arrForkFunctions;   
00554 
00555 
00557     Diana::DianaSparseArray mtrValuesJacobi;
00559     Diana::DianaSparseArray mtrStructJacobi;
00561     Diana::DianaSparseArray mtrValuesDiffJacobi;
00563     Diana::DianaSparseArray mtrStructDiffJacobi;
00565     Diana::DianaSparseArray mtrValuesParJacobi;
00566 
00567     //
00568     std::vector<DianaVariableType> bESOChanged;
00569 
00570     // FD members
00571     Common::Types::CapeBoolean     bFDPartition;
00572     Common::Types::CapeLong        nFDOrder;
00573     Common::Types::CapeDouble      dblFDEpsilon;
00574     Common::Types::CapeArrayDouble arrFDResidual;        
00575     Common::Types::CapeArrayDouble arrFDVariables;       
00576     Common::Types::CapeArrayDouble arrFDEpsilons;        
00577     std::vector<Common::Types::CapeArrayLong>   arrJacobianGrouping;
00578     std::vector<Common::Types::CapeArrayLong>   arrDiffJacobianGrouping;
00579 
00580     // Petri Nets
00581     PetriNet*                      petriNet;              
00582       //Common::Types::CapeDouble*     pOldStates;            ///< old state variables array (previous step)
00583       //Common::Types::CapeDouble*     pOldDerivatives;       ///< old derivatives array (previous step)
00584       //Common::Types::CapeDouble      dblOldIndependentVar;  ///< olf independent variable
00585       //Common::Types::CapeLong        nPhi;                  ///< amount of phi-functions in model
00586     Common::Types::CapeArrayDouble     arrPhi;                  
00587     Common::Types::CapeArrayDouble       arrExplicitEventTimes;  
00588 
00589       //Common::Types::CapeDouble*     pOldPhi;               ///< old phi values array
00590       //IDianaNumericDAESolver*        solver;                ///< currently used solver
00591       //Common::Types::CapeLong        nPlaces;               ///< amount of places
00592       //Common::Types::CapeBoolean*    pPlaces;               ///< array of marks in places
00593   };
00594 
00600     CapeDouble div(CapeDouble e, CapeDouble o);
00601 
00607     CapeDouble factorial(CapeLong f);
00608 
00613     CapeDouble factorial(CapeDouble f);
00614 
00615 
00616 
00617 };
00618 
00619 
00620 
00621 #endif // DIANA_ESO_HPP