Hello Everyone, I tried to write a new component simulating adaptive windows. I first used FORTRAN but didn’t find a compiler that worked. So I wrote it all again in C++, got it compiled, but it still won’t work. When I try to run the simulation I get the following error: “Access violation at address 6F6910BC in module ‘222.dll’. Write of address 0000001E” This component is the first thing I ever programmed, so I can’t figure out the mistake in the code. The source code is attached If someone maybe could look it over and tell me what I’m doing wrong I would be very greatful. Greetings, Pascal |
// 222.cpp : Definiert die exportierten Funktionen für die DLL-Anwendung. // #include "stdafx.h" #include <cmath> #include <fstream> #include "TRNSYS.h" #pragma comment( lib, "TRNDll" ) //************************************************************************* //*** 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 //************************************************************************* extern "C" __declspec(dllexport) int TYPE222 ( 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 ) { std::ofstream logFile("type222.log"); int npar=3; int nin=4; int nout=2; int nder=0; int iunit; // UNIT number int itype; // TYPE number // read context information from TRNSYS // (uncomment lines as required) iunit=info[0]; itype=info[1]; //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] = 0; // 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) //----------------------------------------------------------------------------------------------------------------------- logFile << "Type222 was called"; logFile.close(); //----------------------------------------------------------------------------------------------------------------------- // ADD DECLARATIONS AND DEFINITIONS FOR THE USER-VARIABLES HERE //----------------------------------------------------------------------------------------------------------------------- // PARAMETERS int ns; const int ns_max=99; double shadstep; double twant; //----------------------------------------------------------------------------------------------------------------------- // INPUTS double tin; double tradin[ns_max]; double bradin[ns_max]; double ai[ns_max]; // OTHER double shadperc(0); // OUTPUTS double tradout[ns_max]; double bradout[ns_max]; // READ IN THE VALUES OF THE PARAMETERS IN SEQUENTIAL ORDER ns = (int) (par[0]); twant = (double) (par[1]); shadstep = (double) (par[2]); //----------------------------------------------------------------------------------------------------------------------- // RETRIEVE THE CURRENT VALUES OF THE INPUTS TO THIS MODEL FROM THE XIN ARRAY IN SEQUENTIAL ORDER nin = info[2]; nout= info[5]; tin = xin[0]; for (int i = 0; i < ns; i++) { tradin[i]=xin[1+i]; } for (int i = 0; i < ns; i++) { bradin[i]=xin[1+ns+i]; } for (int i = 0; i < ns; i++) { ai[i]=xin[1+2*ns+i]; } //----------------------------------------------------------------------------------------------------------------------- // SET THE VERSION INFORMATION FOR TRNSYS if (info[6]== -2) { info[11]=17; // 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) { //nitems=1; //stored[1]=shadperc; // setStorageVars(stored,nitems,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 nin=(1+3*ns); nout=(2*ns); info[2]=nin; info[5]=nout; info[8]=1; info[9]=0; // 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.... npar=3; 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 //int* nitems(1); //setStorageSize(nitems,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 < (getSimulationTimeStep()+getSimulationStartTime()/2.)) { // 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 // OUT1 for (int i = 0; i < nout; i++) { xout[i]=xin[1+i]; } // PERFORM ANY REQUIRED CALCULATIONS TO SET THE INITIAL STORAGE VARIABLES HERE // nitems=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. if (time > (getnTimeSteps()*getSimulationTimeStep()/2.)) { if (tin > twant) {if (shadperc < 1.) shadperc=(shadperc+shadstep);} else if (tin < twant) {if (shadperc > 0.) shadperc=(shadperc-shadstep);} for (int i = 0; i < ns; i++) { tradout[i]=(0.12-(8.4*shadperc)*tradin[i]); bradout[i]=(0.12-(8.4*shadperc)*bradin[i]); }; } //----------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------- // SET THE STORAGE ARRAY AT THE END OF THIS ITERATION IF NECESSARY // nitmes= // stored(1)= // setStorageVars(STORED,NITEMS,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) //----------------------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------------------- // SET THE OUTPUTS FROM THIS MODEL IN SEQUENTIAL ORDER AND GET OUT // OUT1 for (int i = 0; i < ns; i++) { xout[i]=tradout[i]; } for (int i = 0; i < ns; i++) { xout[ns+i]=bradout[i]; } //----------------------------------------------------------------------------------------------------------------------- // Get the openfoam results and send them back to TRNSYS // get the pressure of certain area // define the area & Sampling post-processing // create a sampleDict in project system folder // get the values of this area // //----------------------------------------------------------------------------------------------------------------------- // EVERYTHING IS DONE - RETURN FROM THIS SUBROUTINE AND MOVE ON return 1; } //-----------------------------------------------------------------------------------------------------------------------