[TRNSYS-users] using storage c++ type

Wouter Lumen wouter.lumen at ugent.be
Sat Apr 19 10:50:13 PDT 2008


Dear trnsys users,

I'm having some troubles with using storage in my new c++ component.
Every time i run the component i get a "invalid floating point error".

I've have read about the possible causes of this error and i have narrowed
it down to a problem with storage.

I have attached a very simple sourcecode:
It reads in 2 numbers (getal1 and getal2) and puts them back out after adding
a constant to them (in this case, double a and double b).

When i run this component and use hard coded values, such as 1 and 2  
for a and b, the
component works flawless.

But when i use storage (as in the attached file), i get the error.
The storage is used in the following way:
Every timestep "1" is added to the stored variables.
So basically, now I add a larger, with timestep increasing, value to getal1
and getal2.

I have tried severall configurations when using the storage such as:
not using the info[12]>0 manipulations,
not using the setstorage at the end of the calculations,
using them both, etc etc...
Maybe the problem is in this, may be not...

Another possible cause (which i think is more likely), is that it has  
something
to do with the pointerconstruction for "nitems":
int nitems=2;
int* pitems=&nitems;
double stored[2]={0,0};
so that pitems points to nitems.
This is done because the storage functions take an int* as argument  
and not an int.

If anybody has any suggestions on what the problem could be, or could modify
the sourcefile in a way that it should work. Please let me know.

Thank you very much.

Kind regards,

Wouter Lumen.
-------------- next part --------------
#include <cmath>
#include <fstream>
#include "TRNSYS.h" //TRNSYS acess functions (allow to acess TIME etc.) 
//************************************************************************
// Object: type154
// IISiBat Model: type154
// 
// Author: 
// Editor: 
// Date:	 April 19, 2008 last modified: April 19, 2008
// 
// 
// *** 
// *** Model Parameters 
// *** 
//			kaf	- [-Inf;+Inf]

// *** 
// *** Model Inputs 
// *** 
//			getal1	- [-Inf;+Inf]
//			getal2	- [-Inf;+Inf]

// *** 
// *** Model Outputs 
// *** 
//			uit1	- [-Inf;+Inf]
//			uit2	- [-Inf;+Inf]

// *** 
// *** Model Derivatives 
// *** 

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

//
extern "C" __declspec(dllexport) 
int TYPE154           (
             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
            )
{
  //*************************************************************************
  //*** TYPE implementation
  //*** This function will be called by TRNSYS 
  //*** - once at the beginning of the simulation for initialization
  //*** - once at the beginning of every timestep for initialization
  //*** - once for each iteration of the TRNSYS solver
  //*** - once at the end of each timestep for cleanup
  //*** - once at the end of the simulation for cleanup
  //*************************************************************************
  // 
  //***
  //*** WARNING: explanations in the TRNSYS manual use FORTRAN conventions for 
  //***          array indices. Subtract 1 to obtain 0-based C or C++ conventions. 
  //*** Example: 
  //***          TRNSYS manual: info(6) = number of OUTPUTS 
  //***          -> write no = info[5] to obtain number of outputs in C or C++
  //***
  //*** We also spell variables in lower case according to C tradition, while they
  //*** are spelled in uppercase in the TRNSYS manual (according to FORTRAN tradition)
  //*** 


//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------

  // *** STANDARD TRNSYS DECLARATIONS
  int npar= 1;   // number of parameters we expect
  int nin= 2;   // number of inputs
  int nout=2; // number of outputs
  int nder=0;   // number of derivatives
  int iunit; // UNIT number ('serial number' of the component, from the input file (the 'deck')
  int itype; // TYPE number (component number) 
  // read context information from TRNSYS
  // (uncomment lines as required)
	info[5] = nout;  // number of outputs 

	iunit = info[0]; // UNIT number
	itype = info[1]; // TYPE number

	//info[2]	; // number of INPUTS specified by the user of the component 
	//info[3]	; // number of PARAMETERS specified by the user of the component
	//info[4]	; // number of DERIVATIVES specified by the user of the component
	//info[5]	; // number of OUTPUTS specified by the user of the component

  //info[6]	; // number of iterative calls to the UNIT in the current timestep
              // -2 = initialization
              // -1	= initial call in simulation for this UNIT
	            //  0 = first call in timestep for this UNIT.
              //  1	= second call in timestep for this UNIT, etc.

	//info(7)	; // total number of calls to the UNIT in the simulation
  // *** inform TRNSYS about properties of this type
  info[8] = 1; // indicates whether TYPE depends on the passage of time: 0=no
	info[9]	= 0; //	use to allocate storage (see Section 3.5 of the TRNSYS manual): 0 = none
	// info[10]; // indicates number of discrete control variables (see Section 3.3.4 of the TRNSYS manual)
//-----------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------
//    ADD DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES HERE
      int nitems=2;
	  int* pitems=&nitems;
	  double stored[2]={0,0};

//-----------------------------------------------------------------------------------------------------------------------

//    PARAMETERS
      double kaf;

//    INPUTS
      double getal1;
      double getal2;

//-----------------------------------------------------------------------------------------------------------------------
//       READ IN THE VALUES OF THE PARAMETERS IN SEQUENTIAL ORDER
      kaf=par[0];

//-----------------------------------------------------------------------------------------------------------------------
//    RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER

      getal1=xin[0];
      getal2=xin[1];
	 iunit=info[0];
	 itype=info[1];

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

//-----------------------------------------------------------------------------------------------------------------------
//    DO ALL THE VERY LAST CALL OF THE SIMULATION MANIPULATIONS HERE
      if (info[7]== -1) 
	   return 1;
//-----------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------
//    PERFORM ANY 'AFTER-ITERATION' MANIPULATIONS THAT ARE REQUIRED HERE
//    e.g. save variables to storage array for the next timestep
      if (info[12]>0) 
      {
        getStorageVars(stored,pitems,info);
        stored[0]=+1;
        stored[1]=+1;
        setStorageVars(stored,pitems,info);
	     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
      {
//       SET SOME INFO ARRAY VARIABLES TO TELL THE TRNSYS ENGINE HOW THIS TYPE IS TO WORK
         info[5]=nout;				
         info[8]=1;				
	   info[9]=1;	// STORAGE FOR VERSION 16 HAS BEEN CHANGED				

//       SET THE REQUIRED NUMBER OF INPUTS, PARAMETERS AND DERIVATIVES THAT THE USER SHOULD SUPPLY IN THE INPUT FILE
//       IN SOME CASES, THE NUMBER OF VARIABLES MAY DEPEND ON THE VALUE OF PARAMETERS TO THIS MODEL....
         nin=2;
	       npar=1;
	       nder=0;
	       
//       CALL THE TYPE CHECK SUBROUTINE TO COMPARE WHAT THIS COMPONENT REQUIRES TO WHAT IS SUPPLIED IN 
//       THE TRNSYS INPUT FILE
    int dummy=1;
		TYPECK(&dummy,info,&nin,&npar,&nder);

//       SET THE NUMBER OF STORAGE SPOTS NEEDED FOR THIS COMPONENT

     setStorageSize(pitems,info);

//       RETURN TO THE CALLING PROGRAM
         return 1;
      }


//-----------------------------------------------------------------------------------------------------------------------
//    DO ALL OF THE INITIAL TIMESTEP MANIPULATIONS HERE - THERE ARE NO ITERATIONS AT THE INTIAL TIME
      if (time < (getSimulationStartTime() +
       getSimulationTimeStep()/2.0)) 
       {
//       SET THE UNIT NUMBER FOR FUTURE CALLS
         iunit=info[0];
         itype=info[1];

//       CHECK THE PARAMETERS FOR PROBLEMS AND RETURN FROM THE SUBROUTINE IF AN ERROR IS FOUND
//         if(...) TYPECK(-4,INFO,0,"BAD PARAMETER #",0)

//       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL VALUES OF THE OUTPUTS HERE
//		 uit1
			xout[0]=1;
//		 uit2
			xout[1]=2;

//       PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL STORAGE VARIABLES HERE

	   stored[0]=1; stored[1]=4;

//       PUT THE STORED ARRAY IN THE GLOBAL STORED ARRAY
         setStorageVars(stored,pitems,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

	    getStorageVars(stored,pitems,info);

//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------
//    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.

    double a = stored[0];
    double b = stored[1];
    
    for(int i=0;i<5;i++)
     {
             getal1= getal1 + stored[0];
             getal2= getal2 + stored[1];
     }
     
     


//-----------------------------------------------------------------------------------------------------------------------
//    SET THE OUTPUTS FROM THIS MODEL IN SEQUENTIAL ORDER AND GET OUT

//		 uit1
			xout[0]=getal1;
//		 uit2
			xout[1]=getal2;

//-----------------------------------------------------------------------------------------------------------------------







//-----------------------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------
//    SET THE STORAGE ARRAY AT THE END OF THIS ITERATION IF NECESSARY
//      stored[0]=+1;
//      stored[1]=+1;
    setStorageVars(stored,pitems,info);
//-----------------------------------------------------------------------------------------------------------------------




//-----------------------------------------------------------------------------------------------------------------------
//    REPORT ANY PROBLEMS THAT HAVE BEEN FOUND USING CALLS LIKE THIS:
//      MESSAGES(-1,'put your message here','MESSAGE',IUNIT,ITYPE)
//      MESSAGES(-1,'put your message here','WARNING',IUNIT,ITYPE)
//      MESSAGES(-1,'put your message here','SEVERE',IUNIT,ITYPE)
//      MESSAGES(-1,'put your message here','FATAL',IUNIT,ITYPE)
//-----------------------------------------------------------------------------------------------------------------------
//    EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON
      return 1;
      }
//-----------------------------------------------------------------------------------------------------------------------


More information about the TRNSYS-users mailing list