[TRNSYS-users] Problem with static object in user-dll

Henk Stijnen Stijnen at dwa.nl
Wed Aug 3 06:45:47 PDT 2011


Hi all,

I've created a user component and the corresponding C++ dll. At the start of the simulation an object TSA is to be created. This object is to be used in the simulation steps, and to be deleted at the end of the simulation.

Roughly I did it as follows:

//    ADD DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES HERE
     static TSA *tsa;    // TSA DECLARED STATIC

//    SET THE VERSION INFORMATION FOR TRNSYS
      if (info[6]== -2)
    {
               info[11]=16;
     // add additional initialisation code here, if any

           tsa = new TSA();         // TSA CREATED IN THE VERY BEGIN
           return 1;
    }

//    DO ALL THE VERY LAST CALL OF THE SIMULATION MANIPULATIONS HERE
      if (info[7]== -1)  {
         if(tsa) {
            delete tsa;       // TSA TO BE DELETED AT THE VERY LAST CALL
            tsa = NULL;
         }
         return 1;
      }

//    DO ALL OF THE INITIAL TIMESTEP MANIPULATIONS HERE - THERE ARE NO ITERATIONS AT THE INTIAL TIME
      if (time < (getSimulationStartTime() + getSimulationTimeStep()/2.0))
       {
       // AT THIS POINT TSA STILL EXISTS
          tsa->SetOntw(kValue, Area, DesFlowRatePrim, DesTinPrim, DesDeltaPPrim, DesFlowRateSec, DesTinSec, DesDeltaPSec);
          tsa->Bereken();
//          tempOutPrim
                        xout2 = tsa->TuPri;
                        xout[2]=xout2;
            etc....
         return 1;
      }

//-----------------------------------------------------------------------------------------------------------------------
//    *** ITS AN ITERATIVE CALL TO THIS COMPONENT ***
       // AT THIS POINT TSA IS LOST !?!?!?!
       tsa->SetOntw(kValue, Area, DesFlowRatePrim, DesTinPrim, DesDeltaPPrim, DesFlowRateSec, DesTinSec, DesDeltaPSec);
       tsa->Bereken();
//    SET THE OUTPUTS FROM THIS MODEL IN SEQUENTIAL ORDER AND GET OUT
//                      tempOutPrim
                        xout2 = tsa->TuPri;
                                   xout[2]=xout2;
                   etc......

//-----------------------------------------------------------------------------------------------------------------------
//    EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON
      return 1;
      }


As you see TSA is declared static and initialized. At the initial timestep TSA still exists, but in the first iterative call TSA appears to be lost!

Any idea what to do?

Thanks for help or suggestions.

Regards
Henk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.onebuilding.org/pipermail/trnsys-users-onebuilding.org/attachments/20110803/1ef75c67/attachment-0005.htm>
-------------- next part --------------

#include <vcl.h>
#pragma hdrstop
#include <cmath>
#include <fstream>
#include "TRNSYS.h" //TRNSYS acess functions (allow to acess TIME etc.)
#include "tsa.h"
//************************************************************************
// Object: TSA
// IISiBat Model: type201 TSA
// 
// Author: Henk Stijnen
// Editor: 
// Date:	 July 20, 2007 last modified: Dec 13, 2007
// 
// 
// *** 
// *** Model Parameters 
// *** Model Inputs 
// *** Model Outputs 
// *** Model Derivatives 
// ***

// (Comments and routine interface generated by TRNSYS Studio)
//************************************************************************

//
extern "C" __declspec(dllexport) __stdcall
int TYPE201           (
             double &time,  // the simulation time
             double xin[],  // the array containing the component InpUTS
             double xout[], // the array which the component fills with its appropriate OUTPUTS
             double &t,     // the array containing the dependent variables for which the derivatives are evaluated
             double &dtdt,  // the array containing the derivatives of T which are evaluated
             double par[],  // the array containing the PARAMETERS of the component
             int info[],    // the information array described in Section 3.3.3 of the manual
             int icntrl     // the control array described in Section 3.3.4 of the manual
            )
{

//-----------------------------------------------------------------------------------------------------------------------
//    ADD DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES HERE
     static TSA *tsa;    // TSA DECLARED STATIC
//-----------------------------------------------------------------------------------------------------------------------

//    PARAMETERS
//    INPUTS
//-----------------------------------------------------------------------------------------------------------------------
//       READ IN THE VALUES OF THE PARAMETERS IN SEQUENTIAL ORDER
//    RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER
//-----------------------------------------------------------------------------------------------------------------------
//    SET THE VERSION INFORMATION FOR TRNSYS
      if (info[6]== -2)
    {
	   info[11]=16;
     // add additional initialisation code here, if any

           tsa = new TSA();         // TSA CREATED IN THE VERY BEGIN
           return 1;
    }
//-----------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------
//    DO ALL THE VERY LAST CALL OF THE SIMULATION MANIPULATIONS HERE
      if (info[7]== -1)  {
         if(tsa) {
            delete tsa;       // TSA TO BE DELETED AT THE VERY LAST CALL  
            tsa = NULL;
         }
         return 1;
      }
//-----------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------
//    DO ALL THE VERY FIRST CALL OF THE SIMULATION MANIPULATIONS HERE
      if (info[6]== -1) // first call of this component in the simulation
      {
         return 1;
      }
//-----------------------------------------------------------------------------------------------------------------------
//    DO ALL OF THE INITIAL TIMESTEP MANIPULATIONS HERE - THERE ARE NO ITERATIONS AT THE INTIAL TIME
      if (time < (getSimulationStartTime() +
       getSimulationTimeStep()/2.0)) 
       {
//       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL VALUES OF THE OUTPUTS HERE

       // AT THIS POINT TSA STILL EXISTS
          tsa->SetOntw(kValue, Area, DesFlowRatePrim, DesTinPrim, DesDeltaPPrim, DesFlowRateSec, DesTinSec, DesDeltaPSec);
          tsa->SetMediumPri(mediumRhoPrim, mediumCPrim);
          tsa->SetMediumSec(mediumRhoSec, mediumCSec);
          tsa->Qpri = DesFlowRatePrim;
          tsa->Qsec = DesFlowRateSec;
          tsa->TiPri = DesTinPrim;
          tsa->TiSec = DesTinSec;
          tsa->Bereken();
//		 tempOutPrim
                        xout2 = tsa->TuPri;
			xout[2]=xout2;
            etc....

//       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL STORAGE VARIABLES HERE
//         nitems=0;
//	   stored[0]=...

//       PUT THE STORED ARRAY IN THE GLOBAL STORED ARRAY
//         setStorageVars(stored,nitems,info)

//       RETURN TO THE CALLING PROGRAM
         return 1;

      }
//-----------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------
//    *** ITS AN ITERATIVE CALL TO THIS COMPONENT ***
//-----------------------------------------------------------------------------------------------------------------------


//-----------------------------------------------------------------------------------------------------------------------
//    RETRIEVE THE VALUES IN THE STORAGE ARRAY FOR THIS ITERATION
//      nitems=
//	    getStorageVars(stored,nitems,info)
//      stored[0]=
//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------
//    CHECK THE INPUTS FOR PROBLEMS
//      if(...) TYPECK(-3,INFO,'BAD INPUT #',0,0)
//	if(IERROR.GT.0) RETURN 1
//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------
//    *** PERFORM ALL THE CALCULATION HERE FOR THIS MODEL. ***
//-----------------------------------------------------------------------------------------------------------------------

//		ADD YOUR COMPONENT EQUATIONS HERE; BASICALLY THE EQUATIONS THAT WILL
//		CALCULATE THE OUTPUTS BASED ON THE PARAMETERS AND THE INPUTS.	REFER TO
//		CHAPTER 3 OF THE TRNSYS VOLUME 1 MANUAL FOR DETAILED INFORMATION ON
//		WRITING TRNSYS COMPONENTS.

       // AT THIS POINT TSA IS LOST !?!?!?!

       tsa->SetOntw(kValue, Area, DesFlowRatePrim, DesTinPrim, DesDeltaPPrim, DesFlowRateSec, DesTinSec, DesDeltaPSec);
       tsa->SetMediumPri(mediumRhoPrim, mediumCPrim);
       tsa->SetMediumSec(mediumRhoSec, mediumCSec);
       tsa->Qpri = FlowInPrim;
       tsa->Qsec = FlowInSec;
       tsa->TiPri = tempInPrim;
       tsa->TiSec = tempInSec;
       tsa->Bereken();
//-----------------------------------------------------------------------------------------------------------------------
//    SET THE STORAGE ARRAY AT THE END OF THIS ITERATION IF NECESSARY
//      nitmes=
//      stored(1)=
//	    setStorageVars(STORED,NITEMS,INFO)
//-----------------------------------------------------------------------------------------------------------------------
//    SET THE OUTPUTS FROM THIS MODEL IN SEQUENTIAL ORDER AND GET OUT
//		 tempOutPrim
                        xout2 = tsa->TuPri;
			xout[2]=xout2;
                   etc......

//-----------------------------------------------------------------------------------------------------------------------
//    EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON
      return 1;
      }
//-----------------------------------------------------------------------------------------------------------------------


More information about the TRNSYS-users mailing list