Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Examples

variable.h

Go to the documentation of this file.
00001 /////////////////////////////////////////////////////////////////////////////////////////
00002 /// \file   variable.h
00003 /// \brief  Declaration of the classes and types used to describe the problem variables.
00004 ///
00005 /// Class declaration :
00006 ///     \li     SPARK::TVariable 
00007 /// \li SPARK::TUnknown
00008 /// \li SPARK::Variable::TInterface
00009 ///
00010 /// Function declaration :
00011 /// \li SPARK::Variable::ComputeScale()
00012 /// \li SPARK::Variable::Write()
00013 ///
00014 /////////////////////////////////////////////////////////////////////////////////////////
00015 ///
00016 /// \author  Dimitri Curtil (LBNL/SRG)
00017 /// \date    May 15, 2002
00018 ///
00019 /////////////////////////////////////////////////////////////////////////////////////////
00020 /// \attention
00021 /// PORTIONS COPYRIGHT (C) 2003 AYRES SOWELL ASSOCIATES, INC.
00022 /// PORTIONS COPYRIGHT (C) 2003 THE REGENTS OF THE UNIVERSITY OF CALIFORNIA .
00023 ///   PENDING APPROVAL BY THE US DEPARTMENT OF ENERGY. ALL RIGHTS RESERVED.
00024 /////////////////////////////////////////////////////////////////////////////////////////
00025 
00026 
00027 #if !defined(__VARIABLE_H__)
00028 #define __VARIABLE_H__
00029 
00030 #include <cmath>    // for fabs
00031 #include <iosfwd>
00032 #include <bitset>
00033 #include <string>
00034 
00035 #include "types.h"
00036 #include "exceptions.h"
00037 #include "compat.h"  // for CLASS_DECLSPEC, API_DECLSPEC
00038 
00039 
00040 
00041 /////////////////////////////////////////////////////////////////////////////////////////////
00042 namespace SPARK { 
00043         
00044         /////////////////////////////////////////////////////////////////////////////////////////
00045         // Forward declaration
00046         /////////////////////////////////////////////////////////////////////////////////////////
00047         class TVariable;
00048         class TObject;
00049         class TTrajectory;
00050 
00051 
00052         /////////////////////////////////////////////////////////////////////////////////////////
00053         /// \namespace SPARK::Variable
00054         /// \brief Definition of classes and free functions used to implement the SPARK::TVariable class
00055         ///        and the various interfaces to a SPARK::TVariable object.
00056         namespace Variable {
00057 
00058                 /////////////////////////////////////////////////////////////////////////////////////
00059                 // struct TCurrentValue
00060                 // brief Defines the current values associated with a problem variable
00061                 struct TCurrentValue {
00062                         double*       Active;    ///< Pointer to the currently active inner value of this problem variable
00063                                                                          ///< Used by TVariable::operator double() const and TVariable::operator=(double )
00064                         const double* Argument;  ///< Pointer to readable current value of this problem variable 
00065                                                                          ///< Used by TArgument::operator double() const 
00066                         double*       Target;    ///< Pointer to writable current value of this problem variable
00067                                                                          ///< Used by TArgument::operator=(double ) (e.g., assignment operations in callbacks)
00068                         double        Temporary; ///< Temporary value used to compute residuals and partial derivatives
00069                 };
00070                 /////////////////////////////////////////////////////////////////////////////////////
00071 
00072                 
00073                 /////////////////////////////////////////////////////////////////////////////////////
00074                 // class TValueManager
00075                 // brief Manager class of current and past values for a SPARK::TVariable object.
00076                 //
00077                 // note It is only possible to modify the inner value that is currently active.
00078                 //
00079                 class CLASS_DECLSPEC TValueManager {
00080                 public:
00081                         /////////////////////////////////////////////////////////////////////////////////
00082                         // Type definitions
00083                         enum TType {
00084                                 NON_ITERATIVE   = 0,  // e.g., parameters, inputs, non-break unknowns
00085                                 ITERATIVE       = 1,  // e.g., break unknowns
00086                                 NOT_INITIALIZED
00087                         };
00088 
00089                         /////////////////////////////////////////////////////////////////////////////////
00090                         // Structors
00091                         TValueManager();
00092                         ~TValueManager() SPARK_NOT_THROWING;
00093                         /////////////////////////////////////////////////////////////////////////////////
00094 
00095 
00096                         /////////////////////////////////////////////////////////////////////////////////
00097                         // Value management methods
00098                         void SetType(bool isIterative);
00099                         void SetHandler(const SPARK::TTrajectory& handler); // called when loading topology in TProblem
00100                         
00101                         void ActivateCurrentValue();
00102 
00103                         void Perturb();
00104                         void RePerturb();
00105                         void UnPerturb();
00106 
00107                         // TValueManager can no longer be invoked until SetPastValues() and SetInnerValues() are invoked first
00108                         void Reset(); 
00109                         void SetPastValues(double* storage);
00110                         void SetInnerValues(double* storage);
00111                         /////////////////////////////////////////////////////////////////////////////////
00112 
00113                         
00114                         /////////////////////////////////////////////////////////////////////////////////
00115                         // Access methods
00116                         unsigned GetNumPastValues() const;
00117                         double GetPastValue(unsigned pastStep) const;
00118                         inline double GetInnerValue(unsigned innerStep) const { return InnerValues[innerStep]; }
00119 
00120                         inline double GetCurrentValue() const { return *CurrentValue.Active; }
00121                         inline void SetCurrentValue(double scalar) { *CurrentValue.Active = scalar; }
00122 
00123                         inline double GetArgument() const { return *CurrentValue.Argument; }
00124                         inline void SetTarget(double scalar) { *CurrentValue.Target = scalar; }
00125                         inline double GetTarget() const { return *CurrentValue.Target; }
00126                         /////////////////////////////////////////////////////////////////////////////////
00127 
00128 
00129                         /////////////////////////////////////////////////////////////////////////////////
00130                         /// \name Interpolate/Extrapolate methods 
00131                         ///      (using TTrajectory to access global time)
00132                         //@{
00133                         /// Returns the interpolated value at time using the past values
00134                         double Interpolate(double time) const;
00135                         /// Returns the predicted value at the current time
00136                         double Predict() const;
00137                         //@}
00138                         /////////////////////////////////////////////////////////////////////////////////
00139 
00140 
00141                 private:
00142                         /////////////////////////////////////////////////////////////////////////////////
00143                         // Not implemented private methods 
00144                         /////////////////////////////////////////////////////////////////////////////////
00145                         TValueManager(const TValueManager& ) ;
00146                         TValueManager& operator=(const TValueManager& ) ;
00147 
00148 
00149                         /////////////////////////////////////////////////////////////////////////////////
00150                         // Member data
00151                         /////////////////////////////////////////////////////////////////////////////////
00152                         TType              Type;        ///< Manager type 
00153                         const TTrajectory* Trajectory;  ///< Shared by all variables in same problem
00154 
00155 
00156                         /////////////////////////////////////////////////////////////////////////////////
00157                         /// \name Numerical values associated with this variable
00158                         ///
00159                         /// \note double PastValues[] and double InnerValues[] are value repositories.
00160                         ///       The other double* pointers defined in TCurrentValue are used to point to 
00161                         ///       the correct place holder within these repositories depending on the 
00162                         ///       calling context (i.e., SPARK::TVariable or SPARK::TArgument).
00163                         ///       They are set through calls to :
00164                         ///       - ActivateCurrentValue()
00165                         ///       - Perturb(), RePerturb() and UnPerturb()
00166                         //@{
00167                         TCurrentValue   CurrentValue; ///< Handler for the currently active inner value
00168                         double*         PastValues;   ///< Array of past values 
00169                         double*         InnerValues;  ///< Array of intermediate values used to compute various inner steps 
00170                         //@}
00171                         /////////////////////////////////////////////////////////////////////////////////
00172                         
00173                 }; // class TValueManager
00174                 /////////////////////////////////////////////////////////////////////////////////////
00175 
00176 
00177                 /////////////////////////////////////////////////////////////////////////////////////
00178                 /// \class TInterface
00179                 /// \brief Base class for all interfaces to a SPARK::TVariable object.
00180                 ///        Read and write methods for the current values are provided as protected 
00181                 ///        methods to can be used in the derived classes to implement the proper
00182                 ///        behavior for each type of interface. 
00183                 class CLASS_DECLSPEC TInterface {
00184                 public:
00185                         /////////////////////////////////////////////////////////////////////////////////
00186                         // Type definition
00187                         typedef TInterface  base_type;
00188 
00189 
00190                         /////////////////////////////////////////////////////////////////////////////////
00191                         /// \name Structors
00192                         //@{
00193                         /// Default constructor
00194                         TInterface();
00195                         /// Normal constructor using a pointer to a SPARK::TVariable object
00196                         explicit
00197                         TInterface(SPARK::TVariable* variable) ;
00198                         /// Copy constructor
00199                         TInterface(const TInterface& interf) ;
00200                         /// Destructor
00201                         virtual ~TInterface() SPARK_NOT_THROWING
00202                         {}
00203                         //@}
00204                         /////////////////////////////////////////////////////////////////////////////////
00205 
00206 
00207                         /////////////////////////////////////////////////////////////////////////////////
00208                         /// \name Restricted access methods for variable properties
00209                         //@{
00210                         /// Returns the unique handle as unsigned
00211                         unsigned GetHandle() const;
00212                         /// Returns the variable type as SPARK::VariableTypes
00213                         SPARK::VariableTypes GetType() const;
00214                         /// Returns internal flag 
00215                         unsigned GetFlag() const;
00216                         
00217                         /// Returns the past value from idx steps ago
00218                         double GetPastValue(unsigned idx) const SPARK_RETHROWING( (SPARK::XAssertion) );
00219                         /// Returns the past value from idx steps ago
00220                         double operator[](unsigned idx) const { return GetPastValue(idx); }
00221 
00222                         /// Returns the variable name of the underlying SPARK::TVariable object as const std::string&
00223                         const std::string& GetName() const;
00224                         /// Returns the variable unit string of the underlying SPARK::TVariable object as const std::string&
00225                         const std::string& GetUnit() const      ;
00226                         /// Sets the unit string for the underlying variable
00227                         void SetUnit(const char* unit) ;
00228                         
00229                         /// Returns the inital value of the underlying SPARK::TVariable object as double
00230                         double GetInit() const  ;
00231                         /// Sets the initial value to init
00232                         void SetInit(double init);
00233                         /// Returns the minimum value of the underlying SPARK::TVariable object as double
00234                         double GetMin() const   ;
00235                         /// Sets the minimum value to min
00236                         void SetMin(double min);
00237                         /// Returns the maximum value of the underlying SPARK::TVariable object as double
00238                         double GetMax() const   ;
00239                         /// Sets the maximum value to max
00240                         void SetMax(double max);
00241                         /// Returns the absolute tolerance of the underlying SPARK::TVariable object as double
00242                         double GetAbsTolerance() const ;
00243                         /// Sets the absolute tolerance to atol
00244                         void SetAbsTolerance(double atol) ;
00245 
00246                         /// Returns the current scale 
00247                         double GetScale() const;
00248 
00249                         /// Returns Read URL string
00250                         const char* GetReadUrlString() const ;
00251                         /// Returns Read URL handle
00252                         int GetReadUrlHandle() const ;
00253                         /// Returns Write URL string
00254                         const char* GetWriteUrlString() const ;
00255                         /// Returns Write URL handle
00256                         int GetWriteUrlHandle() const ;
00257                         //@}
00258                         /////////////////////////////////////////////////////////////////////////////////
00259 
00260 
00261                         /////////////////////////////////////////////////////////////////////////////////
00262                         /// \name Predicate methods
00263                         //@{    
00264                         bool IsResidualForm() const; 
00265                         //@}
00266                         /////////////////////////////////////////////////////////////////////////////////
00267 
00268 
00269                         /////////////////////////////////////////////////////////////////////////////////
00270                         /// \name IO methods
00271                         //@{
00272                         /// Writes the variable name, unit and current value to the output stream os
00273                         void Write(std::ostream& os) const;
00274                         //@}
00275                         /////////////////////////////////////////////////////////////////////////////////
00276                 
00277                 
00278                 protected:
00279                         /// Returns underlying pointer
00280                         SPARK::TVariable* GetVariable() { return Variable; }
00281                         const SPARK::TVariable* GetVariable() const { return Variable; }
00282 
00283                         /// Sets the current value to scalar
00284                         void SetValue(double scalar);
00285 
00286                         /// Allows implicit conversion to double by returning the current value
00287                         double GetValue() const;
00288 
00289                         /// Sets the current target value to scalar
00290                         void SetTarget(double scalar);
00291 
00292                         /// Returns value from evaluating the matched object (used by TEquationSystem and TJacobian internally)
00293                         double GetTarget() const;
00294 
00295                         /// Returns the current argument value as a double
00296                         double GetArgument() const;
00297 
00298                         /// Returns the inner value for innerStep
00299                         double GetInnerValue(unsigned innerStep) const ;
00300 
00301                         /// Used by derived classes if required (see TUnknown)
00302                         void Bind(SPARK::TVariable* variable);
00303                         TInterface& operator=(const TInterface& ) SPARK_THROWING( (SPARK::XInitialization) );
00304                  
00305 
00306                 private:
00307 
00308                         /////////////////////////////////////////////////////////////////////////////////
00309                         // Only member datum
00310                         /////////////////////////////////////////////////////////////////////////////////
00311 
00312                         SPARK::TVariable* Variable;  ///< Pointer to the underlying SPARK::TVariable object 
00313 
00314                         friend class interface_adaptor; 
00315                 
00316                 }; // class TInterface
00317                 /////////////////////////////////////////////////////////////////////////////////////
00318 
00319 
00320                 /// \brief Computes scale for a problem variable using the relative tolerance \c tol and 
00321                 ///        the absolute tolerance \c atol
00322                 ///
00323                 /// \return      scale as double
00324                 /// \param tol   relative tolerance where log(tol) = -(number of significant digits)
00325                 /// \param atol  absolute tolerance for this variable (essentially the value we consider to be zero)
00326                 /// \param value current value of this variable
00327                 API_DECLSPEC(double) ComputeScale(double tol, double atol, double value);
00328 
00329 
00330                 // Function name        : ComputeScales
00331                 // Description      : 
00332                 // Return type          : void 
00333                 // Argument         : double tol
00334                 // Argument         : const TypicalValuesType& typicalValues
00335                 // Argument         : VariablePtrsType& variablePtrs
00336                 template<typename TypicalValuesType, typename VariablePtrsType>
00337                 void ComputeScales(double tol, const TypicalValuesType& typicalValues, VariablePtrsType& variablePtrs)
00338                 {
00339                         typename TypicalValuesType::const_iterator  T( typicalValues.begin() );
00340                         typename VariablePtrsType::iterator         V( variablePtrs.begin() );
00341                         typename VariablePtrsType::iterator         VEnd( variablePtrs.end() );
00342 
00343                         const double TOLERANCE = (tol > 0.0 ? tol : 1.0);
00344 
00345                         for ( ; V!=VEnd; ++V, ++T) {
00346 
00347                                 (*V)->SetScale(
00348                                         SPARK::Variable::ComputeScale( 
00349                                                 TOLERANCE,            // always different from zero !
00350                                                 (*V)->GetAbsTolerance(), 
00351                                                 fabs( double( *T ) )  // TypicalValue
00352                                         )
00353                                 );
00354 
00355                         }
00356                 }
00357 
00358                 /// Writes out to the stream os the name and unit of the SPARK::TVariable object
00359                 API_DECLSPEC(void) Write(std::ostream& os, const SPARK::TVariable& variable);
00360 
00361         /// Write argument list of evaluate callback for object associated with variable if any
00362         API_DECLSPEC(void) WriteMatchedObject(std::ostream& os, const SPARK::TVariable& variable, const std::string& before);
00363 
00364         
00365         }; // namespace Variable
00366     /////////////////////////////////////////////////////////////////////////////////////////
00367 
00368 
00369 
00370         /////////////////////////////////////////////////////////////////////////////////////////
00371         /// \class TVariable
00372         /// \brief Class used to represent the properties and the numerical values of a 
00373         ///        problem variable.
00374         ///
00375         /// This class is the building block of the %SPARK solver framework. 
00376         /// It is used to represent all problem variables at the problem and component levels. \n
00377         ///
00378         /// The low-level numerical solution methods do not operate directly on the SPARK::TVariable 
00379         /// instances but on arrays of fundamental types (i.e., double, unsigned...). \n
00380         ///
00381         /// Eventually, the numerical values in the SPARK::TVariable objects are updated with the 
00382         /// solution of the nonlinear solver.
00383         ///
00384         class CLASS_DECLSPEC TVariable {
00385         public:
00386                 /////////////////////////////////////////////////////////////////////////////////////
00387                 // Type definition
00388                 /////////////////////////////////////////////////////////////////////////////////////
00389                 
00390                 /// \enum FlagTypes
00391                 /// \brief Describes internal properties of the variable as specified by setupcpp
00392                 enum FlagTypes {
00393                         FlagType_BREAK = 0,        ///< setupcpp flaged this LINK as a break variable
00394                         FlagType_RESIDUAL,         ///< matched EVALUATE callback returns RESIDUAL value
00395                         FlagType_DYNAMIC,          ///< connected to X port of an INTEGRATOR atomic class
00396                         FlagType_INPUT_FROM_LINK,  ///< LINK specified with INPUT_FROM_LINK
00397                         FlagType_PREDICT_FROM_LINK,///< LINK specified with PREDICT_FROM_LINK
00398                         FlagType_REPORT,           ///< LINK specified with REPORT or Write URL specified with REPORT tag
00399                         
00400                         FLAGTYPES_L                ///< Number of different flags
00401                 };
00402 
00403                 typedef std::bitset<FLAGTYPES_L>        TFlag;
00404                 typedef SPARK::Variable::TValueManager  TValueManager;
00405 
00406 
00407                 /////////////////////////////////////////////////////////////////////////////////////
00408                 // Structors
00409                 /// Default constructor used when loading the problem at runtime
00410                 TVariable();
00411                 /// Constructor used with compiled "problem.cpp" file
00412                 TVariable(
00413                         unsigned handle,
00414                         const char* name, 
00415                         const char* unit,
00416                         SPARK::VariableTypes type,
00417                         double initVal, 
00418                         double minVal, 
00419                         double maxVal,
00420                         double atol,
00421                         unsigned fromLinkId,
00422                         const char* rd_url, 
00423                         const char* wr_url
00424                 );
00425                 ~TVariable() SPARK_NOT_THROWING;
00426                 /////////////////////////////////////////////////////////////////////////////////////
00427 
00428 
00429                 /////////////////////////////////////////////////////////////////////////////////////
00430                 // Initialize operations
00431                 //
00432                 // Before using SPARK::TVariable, the following methods must have been invoked :
00433                 //  - Setup()
00434                 //  - SetHandler()
00435                 //  - SetPastValues()
00436                 //  - SetInnerValues()
00437                 //  - SetMatchedObject()
00438                 //
00439                 // Sets the value manager (current, intermediate, past values) depending on variable type + flag
00440                 //
00441                 // Invoked by the topology handler object that owns the problem variable 
00442                 void Setup() ;
00443 
00444                 // Sets pointers to history handler, past and current values 
00445                 //
00446                 // Note: History handler must be constructed prior to using this object.
00447                 // Note: The problem object that owns this variable is responsible for setting the history handler up.
00448                 void SetHandler(const SPARK::TTrajectory& handler);
00449                 
00450                 // Sets pointer to storage where past values are to be stored
00451                 void SetPastValues(double* storage);
00452                 
00453                 // Sets pointer to staorage where inner values are to be stored
00454                 void SetInnerValues(double* storage);
00455 
00456                 // Sets the variable type 
00457                 void SetType(SPARK::VariableTypes t) { Type = t; }
00458 
00459                 // Sets the variable flag
00460                 void SetFlag(FlagTypes flag) { Flag.set( flag ); }
00461 
00462                 // Set the object pointer that this variable is matched with (if any)
00463                 void SetMatchedObject(TObject* object) ;
00464                 /////////////////////////////////////////////////////////////////////////////////////
00465 
00466                 
00467                 /////////////////////////////////////////////////////////////////////////////////////
00468                 // Main operations (used to switch between calling contexts internally)
00469                 void Perturb();
00470                 void RePerturb();
00471                 void UnPerturb();
00472                 /////////////////////////////////////////////////////////////////////////////////////
00473 
00474                 
00475                 /////////////////////////////////////////////////////////////////////////////////////
00476                 /// \name Access methods for the current numerical value of the variable
00477                 //@{    
00478                 /// Returns the curent value as a double
00479                 inline double GetValue() const { return ValueManager.GetCurrentValue(); }
00480                 /// Enables implicit conversion to (double)  
00481                 inline operator double() const { return GetValue(); }
00482                 
00483                 /// Sets the current value to scalar
00484                 inline void SetValue(double scalar) { ValueManager.SetCurrentValue( scalar ); }
00485                 /// Sets the current value to scalar with operator=(double )
00486                 inline SPARK::TVariable& operator=(double scalar) { SetValue( scalar ); return *this; }
00487                 
00488                 /// Returns value for argument variable in an object 
00489                 inline double GetArgument() const { return ValueManager.GetArgument(); }
00490                 /// Sets the target value for the matched object
00491                 inline void SetTarget(double scalar) { ValueManager.SetTarget( scalar ); }
00492                 /// Returns value from evaluating the matched object (used by TEquationSystem and TJacobian internally)
00493                 inline double GetTarget() const { return ValueManager.GetTarget(); }
00494 
00495                 /// \brief Predicts value for current time based on past values only
00496                 ///
00497                 /// Returns an estimate of the solution at the current time using the explicit Euler scheme
00498                 /// to calculate the extrapolatation from past values 
00499                 ///
00500                 /// \note After a static step, it returns the value at the past step 
00501                 double Predict() const; 
00502                 //@}    
00503                 /////////////////////////////////////////////////////////////////////////////////////
00504                 
00505                 
00506                 /////////////////////////////////////////////////////////////////////////////////////
00507                 /// \name Access methods for past and inner values
00508                 //@{
00509                 /// Returns the number of past values that are being kept track of
00510                 unsigned GetNumPastValues() const;
00511 
00512                 /// Returns the past value from idx steps ago
00513                 inline double GetPastValue(unsigned pastStep) const { return ValueManager.GetPastValue(pastStep); }
00514                 /// Returns the past value from idx steps ago
00515                 inline double operator[](unsigned pastStep) const { return GetPastValue(pastStep); }
00516                 
00517                 /// Access the intermediate values over the currrent step
00518                 inline double GetInnerValue(unsigned innerStep) const { return ValueManager.GetInnerValue(innerStep); }
00519                 /// Access the intermediate values over the currrent step
00520                 inline double operator()(unsigned innerStep) const { return GetInnerValue(innerStep); }
00521                 //@}
00522                 /////////////////////////////////////////////////////////////////////////////////////
00523 
00524                 
00525                 /////////////////////////////////////////////////////////////////////////////////////
00526                 /// \name Updating methods 
00527                 //@{
00528                 /// Set INPUT_FROM_LINK=FromLink or PREDICT_FROM_LINK=FromLink
00529                 void SetFromLinkId(unsigned var_id) { FromLinkId = var_id; }
00530                 /// Get var_id of FromLink variables, SPARK::NotAssigned if not specified
00531                 unsigned GetFromLinkId() const { return FromLinkId; }
00532                 //@}
00533                 /////////////////////////////////////////////////////////////////////////////////////
00534 
00535 
00536                 /////////////////////////////////////////////////////////////////////////////////////
00537                 /// \name Access operations for the internal properties
00538                 //@{
00539                 /// Returns the pointer to the object this variable is matched with
00540                 ///
00541                 /// \warning If the variable is not an unknown, then there is no matched object and the 
00542                 ///          data member is equal to 0.
00543                 TObject* GetMatchedObject() { return MatchedObject; }
00544                 /// Returns the const pointer to the object this variable is matched with
00545                 ///
00546                 /// \warning If the variable is not an unknown, then there is no matched object and the 
00547                 ///          data member is equal to 0.
00548                 const TObject* GetMatchedObject() const { return MatchedObject; }
00549                 //@}
00550                 /////////////////////////////////////////////////////////////////////////////////////
00551 
00552 
00553                 /////////////////////////////////////////////////////////////////////////////////////
00554                 /// \name Access operations for the LINK properties
00555                 //@{
00556                 /// Returns the unique handle as unsigned int
00557                 unsigned GetHandle() const { return Handle; }
00558 
00559                 /// Returns the variable type as SPARK::VariableTypes
00560                 SPARK::VariableTypes GetType() const { return Type; }
00561 
00562                 /// Returns the variable flag as an unsigned int 
00563                 unsigned long GetFlag() const { return Flag.to_ulong(); }
00564 
00565                 /// Returns the variable name as a const char*
00566                 const std::string& GetName() const      { return Name; }
00567 
00568                 /// Returns the unit string as const char*
00569                 const std::string& GetUnit() const { return Unit; }
00570                 /// Sets the unit string to unit
00571                 void SetUnit(const char* unit) { Unit = unit; }
00572                 
00573                 /// Returns the initial value as double
00574                 double GetInit() const    { return InitValue; }
00575                 /// Sets the initial value to init
00576                 void SetInit(double init) { InitValue =  init; }
00577 
00578                 /// Returns the mimimum value as double
00579                 double GetMin() const     { return MinValue; }
00580                 /// Sets the minimum value to min
00581                 void SetMin(double min)  { MinValue = min; }
00582 
00583                 /// Returns the maximum value as double
00584                 double GetMax() const { return MaxValue; }
00585                 /// Sets the maximum value to max
00586                 void SetMax(double max) { MaxValue = max; }
00587 
00588                 /// Returns the absolute tolerance as double
00589                 double GetAbsTolerance() const { return AbsTolerance; }
00590                 /// Sets the absolute tolerance to atol
00591                 void SetAbsTolerance(double atol);
00592                 
00593                 /// Returns the current scale 
00594                 double GetScale() const { return Scale; }
00595                 /// Sets the current scale to scalar
00596                 void SetScale(double scalar) { Scale = scalar; }
00597                 //@}
00598                 /////////////////////////////////////////////////////////////////////////////////////
00599 
00600                 
00601                 /////////////////////////////////////////////////////////////////////////////////////
00602                 /// \name Predicate methods
00603                 //@{    
00604                 /// Returns if flag is set for this variable, false otherwise
00605                 bool IsFlag(FlagTypes flag)  const { return Flag[ flag ]; }
00606                 //@}
00607                 /////////////////////////////////////////////////////////////////////////////////////
00608 
00609 
00610                 /////////////////////////////////////////////////////////////////////////////////////
00611                 /// \name IO operations
00612                 //@{    
00613                 /// Pretty printing of the variable name, its numerical value and its unit string to 
00614                 /// the output stream os
00615                 void Write(std::ostream& os, unsigned width, unsigned precision) const;
00616                 //@}
00617                 /////////////////////////////////////////////////////////////////////////////////////
00618 
00619 
00620                 /////////////////////////////////////////////////////////////////////////////////////
00621                 /// \name R/W URL methods
00622                 /// \note Not yet documented
00623                 //@{
00624                 const char* GetReadUrlString() const { return ReadUrlString.c_str(); }
00625                 void SetReadUrlString(const char* url) { ReadUrlString = url; }
00626                 int GetReadUrlHandle() const { return ReadUrlHandle; }
00627                 void SetReadUrlHandle(int s) { ReadUrlHandle = s; }
00628                 
00629                 const char* GetWriteUrlString() const { return WriteUrlString.c_str(); }
00630                 void SetWriteUrlString(const char* url) { WriteUrlString = url; }
00631                 int GetWriteUrlHandle() const { return WriteUrlHandle; }
00632                 void SetWriteUrlHandle(int s) { WriteUrlHandle = s; }
00633                 //@}
00634                 /////////////////////////////////////////////////////////////////////////////////////
00635 
00636 
00637         private:
00638                 /////////////////////////////////////////////////////////////////////////////////////
00639                 // Private Methods (deactivated)
00640                 /////////////////////////////////////////////////////////////////////////////////////
00641                 TVariable(const TVariable& ); // Copy constructor
00642                 TVariable& operator=(const TVariable& ); // Assignment operator
00643 
00644                 /// Sets the variable name to name
00645                 void SetName(const char* name) { Name = name; }
00646 
00647                 /// \brief Interpolates trajectory at \c time 
00648                 ///
00649                 /// Returns the interpolated solution using past values as support points to build the 
00650                 /// interpolating polynomial
00651                 double Interpolate(double time) const;
00652 
00653 
00654                 /////////////////////////////////////////////////////////////////////////////////////
00655                 // Private data
00656                 /////////////////////////////////////////////////////////////////////////////////////
00657                 
00658                 /////////////////////////////////////////////////////////////////////////////////////
00659                 /// \name Main properties
00660                 //@{
00661                 SPARK::VariableTypes Type;   ///< Type of this problem variable
00662                 TFlag                Flag;   ///< Flag associated with unknown variables
00663                 unsigned             Handle; ///< Unique identifier for this problem variable 
00664                                      ///< It MUST BE the position in the problem-level array (0-based indexing) of SPARK::TVariable objects.
00665                 std::string          Name;   ///< Name of the problem variable 
00666                 std::string          Unit;   ///< Unit string of the problem variable
00667                 unsigned             FromLinkId;  ///< handle of the variable used to update INPUT_FROM_LINK, PREDICT_FROM_LINK (SPARK::NotAssigned if not used)
00668                 //@}
00669                 /////////////////////////////////////////////////////////////////////////////////////
00670 
00671                 /////////////////////////////////////////////////////////////////////////////////////
00672                 /// \name Numerical properties
00673                 //@{
00674                 double                InitValue;    ///< Initial value used for the initial time solution
00675                 double                MinValue;     ///< Minimum value allowed for this variable
00676                 double                MaxValue;     ///< Maximum value allowed for this variable
00677                 double                AbsTolerance; ///< Absolute tolerance for this variable (essentially what we consider to be zero)
00678                 double                Scale;        ///< Current scale. Used to compute the weighted norms. Updated by solver.
00679                 //@}
00680                 /////////////////////////////////////////////////////////////////////////////////////
00681 
00682                 /////////////////////////////////////////////////////////////////////////////////////
00683                 /// \name R/W url data 
00684                 /// \note Not yet documented
00685                 //@{
00686                 std::string          ReadUrlString;
00687                 int                  ReadUrlHandle;
00688                 
00689                 std::string          WriteUrlString;
00690                 int                  WriteUrlHandle;
00691                 //@}
00692                 /////////////////////////////////////////////////////////////////////////////////////
00693 
00694                 /////////////////////////////////////////////////////////////////////////////////////
00695                 /// \name Data set internally
00696                 //@{
00697                 TValueManager        ValueManager;   ///< manages values for this variable
00698         SPARK::TObject*      MatchedObject;  ///< pointer to object that computes this value (0 if not matched)
00699                 //@}
00700                 /////////////////////////////////////////////////////////////////////////////////////
00701 
00702                 friend class TTrajectory;    // to grant access to ValueManager
00703         };
00704         /////////////////////////////////////////////////////////////////////////////////////////
00705 
00706 
00707 
00708         /////////////////////////////////////////////////////////////////////////////////////////
00709         /// \class TUnknown
00710         /// \brief Class used to represent the properties and the numerical values of an 
00711         ///        unknown problem variable.
00712         ///
00713         ///  Interface for internal manipulation of a SPARK::TVariable instance when it is an unknown.
00714         ///  Dereference underlying SPARK::TVariable* and provides numeric interface for basic
00715         ///  arithmetic.
00716         ///
00717         class CLASS_DECLSPEC TUnknown : public SPARK::Variable::TInterface {
00718         public:
00719                 /////////////////////////////////////////////////////////////////////////////////////
00720                 /// \name Structors
00721                 ///
00722                 //@{
00723                 TUnknown();                             ///< Default constructor
00724                 explicit
00725                 TUnknown(SPARK::TVariable* variable);   ///< Explicit constructor
00726                 TUnknown(const TUnknown& unknown);      ///< Copy constructor
00727                 
00728                 /// Assignment operator 
00729                 /// \warning Use carefully. It works only if the interface has not been initialized yet.
00730                 /// \param u  Another TUnknown object used to initialize the underlying pointer of this TUnknown object
00731                 /// \exception SPARK::XInitialization  Thrown if this object has already been initialized before this call.
00732                 TUnknown& operator=(const TUnknown& u) ;
00733                 //@}
00734                 /////////////////////////////////////////////////////////////////////////////////////
00735 
00736                 
00737                 /////////////////////////////////////////////////////////////////////////////////////
00738                 /// \name Main operations (used to switch between calling contexts internally)
00739                 ///
00740                 //@{
00741                 void Perturb();    ///< Prepares the interface for a perturbation
00742                 void RePerturb();  ///< Prepares the interface for a re-perturbation
00743                 void UnPerturb();  ///< Unperturb the interface after various perturbations
00744                 //@}
00745                 /////////////////////////////////////////////////////////////////////////////////////
00746 
00747                  
00748                 /////////////////////////////////////////////////////////////////////////////////////
00749                 /// \name Access methods
00750                 ///
00751                 //@{
00752                 /// Dereferencing operator
00753                 inline SPARK::TVariable& operator*() { return *base_type::GetVariable(); }
00754                 /// Const flavor of dereferencing operator
00755                 inline const SPARK::TVariable& operator*() const { return *base_type::GetVariable(); }
00756 
00757                 /// Indirection operator
00758                 inline SPARK::TVariable* operator->() { return base_type::GetVariable(); }
00759                 /// Const flavor of indirection operator
00760                 inline const SPARK::TVariable* operator->() const { return base_type::GetVariable(); }
00761 
00762                 /// Returns value from evaluating the matched object (used by TEquationSystem and TJacobian internally)
00763                 double GetTarget() const;
00764 
00765                 /// Sets the current scale to scalar
00766                 void SetScale(double scalar);
00767                 //@}
00768                 /////////////////////////////////////////////////////////////////////////////////////
00769 
00770                 
00771                 /////////////////////////////////////////////////////////////////////////////////////
00772                 /// \name Numerical access and arithmetic unary operators
00773                 ///
00774                 /// The overloaded arithmetic operators operate on double only and are all unary 
00775                 /// operators.
00776                 //@{    
00777                 inline operator double() const { return base_type::GetValue(); }
00778 
00779                 inline TUnknown& operator=(double scalar)  { base_type::SetValue(scalar); return *this; }
00780                 inline TUnknown& operator+=(double scalar) { base_type::SetValue( base_type::GetValue()+scalar ); return *this; }
00781                 inline TUnknown& operator-=(double scalar) { base_type::SetValue( base_type::GetValue()-scalar ); return *this; }
00782                 inline TUnknown& operator*=(double scalar) { base_type::SetValue( base_type::GetValue()*scalar ); return *this; }
00783                 inline TUnknown& operator/=(double scalar) { base_type::SetValue( base_type::GetValue()/scalar ); return *this; }
00784                 //@}
00785                 /////////////////////////////////////////////////////////////////////////////////////
00786 
00787                 /////////////////////////////////////////////////////////////////////////////////////
00788                 /// \name Predicate methods
00789                 //@{    
00790                 bool IsResidualForm() const;
00791                 //@}
00792                 /////////////////////////////////////////////////////////////////////////////////////
00793 
00794         };
00795         /////////////////////////////////////////////////////////////////////////////////////////
00796 
00797 
00798         /// Output operator << overload for the SPARK::TUnknown class
00799         std::ostream& operator<<(std::ostream& os, const SPARK::TUnknown& unknown);
00800 
00801         /// Output operator << overload for the SPARK::TVariable class
00802         API_DECLSPEC(std::ostream&) operator<<(std::ostream& os, const SPARK::TVariable &V);
00803 
00804 
00805 }; // namespace SPARK
00806 /////////////////////////////////////////////////////////////////////////////////////////////
00807 
00808 
00809 
00810 #endif  //__VARIABLE_H__
00811 
00812 


Generated on 5 Nov 2003 for VisualSPARK 2.01