Beispiel #1
0
/**
	Evaluation function for 'sunpos'
	@return 0 on success
*/
static int sunpos_nrel_calc(struct BBoxInterp *bbox,
		int ninputs, int noutputs,
		double *inputs, double *outputs,
		double *jacobian
){
	CALCPREPARE(4,2);

	double t, p, T, t_offset;

	t = inputs[0]; /* convert from JD seconds to JD days */
	p = inputs[1] / 100. /* convert Pa to mbar */;
	T = inputs[2] - 273.15 /* convert °C to K */;
	t_offset = inputs[3];

	spa_data S = *sunpos1;
	S.pressure = p;
	S.temperature = T;
	S.jd = (t + t_offset) / 3600 / 24; /* convert to days */

	int res = spa_calculate(&S);
	//CONSOLE_DEBUG("Sun position: t = %f JD, p  %f mbar, T = %f C: res = %d, az = %f, zen = %f",S.jd, p, T, res, S.azimuth, S.zenith);

	/* returned values are in degrees, need to convert back to base SI: radians */
	outputs[0] = S.zenith * PI/180.;
	outputs[1] = S.azimuth180 * PI/180.;

	switch(res){
	case 0: break;
	case 16: CONSOLE_DEBUG("Calculated julian day (t + offset) = %f is out of permitted range",S.jd); break;
	default: CONSOLE_DEBUG("Error code %d returned from spa_calculate",res);
	}

	/* 0 on success, non-zero is error code from spa_calculate (would prob be input parameters out-of-range) */
	return res;
}
Beispiel #2
0
void
SolverHooksManager::setHooks(SolverHooks *H){
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("Using hooks at %p",H);
#endif
	if(hooks && own_hooks){
#if SOLVERHOOKS_DEBUG
		CONSOLE_DEBUG("Deleting previous owned hooks");
#endif
		delete(hooks);
	}
	this->hooks = H;
	this->own_hooks = 0;
}
Beispiel #3
0
SolverHooksManager::SolverHooksManager(){
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("Creating SolverHooksManager with NULL hooks");
#endif
	this->hooks = NULL;
	this->own_hooks = 0;
}
Beispiel #4
0
SolverReporter *
SolverHooks::getSolverReporter(){
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("SolverReporter is at %p", R);
#endif
	return R;
}
Beispiel #5
0
/**
	@return 0 on success
*/
int datareader_tmy3_header(DataReader *d){
	Tmy3Location loc;
	d->data = ASC_NEW(Tmy3Data);
	DATA(d)->p = parseCreateFile(d->f);
	parse *p = DATA(d)->p;
	char rubbish[2049];

	if(!(
		parseLocation(p,&loc)
		&& parseStrExcept(p,"\r\n",rubbish,2048)
		&& parseEOL(p)
	)){
		ERROR_REPORTER_HERE(ASC_PROG_ERROR,"Parser error in header part of file");
	}

	CONSOLE_DEBUG("TMY3 file for '%s' at (%.2fN,%.2fE)",loc.stationname,loc.latitude,loc.longitude);

    // set the value of some of the Data Reader parameters
    d->i = 0;
    d->ninputs = 1;
    d->ndata = 8760; // FIXME -- is a variable length file possible?
    d->nmaxoutputs = 7; // FIXME

    DATA(d)->rows = ASC_NEW_ARRAY(Tmy3Point,d->ndata);

	/* set the number of inputs and outputs */
	d->ninputs = 1;
	d->noutputs = 7;
	return 0;
}
Beispiel #6
0
void
SolverHooks::assign(Simulation *S){
	S->setSolverHooks(this);
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("Assigning SolverHooks to Simulation...");
#endif
	slvreq_assign_hooks(S->getInternalType(),&ascxx_slvreq_set_solver, &ascxx_slvreq_set_option, &ascxx_slvreq_do_solve, (void *)S);
}
Beispiel #7
0
int ascxx_slvreq_set_solver(const char *solvername, void *user_data){
	Simulation *S = (Simulation *)user_data;
	if(NULL==S->getSolverHooks())return SLVREQ_SOLVER_HOOK_NOT_SET;
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("Got solver hooks at %p from Simulation at %p",S->getSolverHooks(),S);
#endif
	return S->getSolverHooks()->setSolver(solvername, S);
}
Beispiel #8
0
SolverHooksManager::~SolverHooksManager(){
	if(own_hooks){
#if SOLVERHOOKS_DEBUG
		CONSOLE_DEBUG("Delete owned hooks");
#endif
		delete hooks;
	}
}
Beispiel #9
0
/*
	Testing for Franc Ivankovic's CNF conversion code, ongoing, Jan 2010 -- JP.
*/
static void test_boolrel(void){
	struct module_t *m;
	int status;

	Asc_CompilerInit(0); /* no simplification of expressions for this test */
	Asc_PutEnv(ASC_ENV_LIBRARY "=models");

	/* load the file */
#define TESTFILE "boolrel"
	m = Asc_OpenModule("test/compiler/" TESTFILE ".a4c",&status);
	CU_ASSERT(status == 0);

	/* parse it */
	CU_ASSERT(0 == zz_parse());

	/* find the model */
	CU_ASSERT(FindType(AddSymbol(TESTFILE))!=NULL);

	/* instantiate it */
	struct Instance *sim = SimsCreateInstance(AddSymbol(TESTFILE), AddSymbol("sim1"), e_normal, NULL);
	CU_ASSERT_FATAL(sim!=NULL);

	/* call the on_load method */
	struct Name *name = CreateIdName(AddSymbol("on_load"));
	CU_ASSERT(Proc_all_ok == Initialize(GetSimulationRoot(sim),name,"sim1", ASCERR, WP_STOPONERR, NULL, NULL));

	/* Check that x := 2 was NOT executed (after error statement) */
	struct Instance *inst;
	struct Instance *root = GetSimulationRoot(sim);
	CU_ASSERT(NULL != root);
	CU_ASSERT(NULL != (inst = ChildByChar(root,AddSymbol("rel1"))));
	CONSOLE_DEBUG("Instance kind = %d",InstanceKind(inst));
	CU_ASSERT(InstanceKind(inst)==LREL_INST);

	char *out = WriteLogRelToString(inst,root);
	CONSOLE_DEBUG("Relation: %s",out);


	ASC_FREE(out);

	/* clean up */
	sim_destroy(sim);
	Asc_CompilerDestroy();
#undef TESTFILE
}
Beispiel #10
0
SolverHooks *
SolverHooksManager::getHooks(){
	if(this->hooks == NULL){
#if SOLVERHOOKS_DEBUG
		CONSOLE_DEBUG("Creating new default SolverHooks...");
#endif
		this->hooks = new SolverHooks();
		this->own_hooks = 1;
	}
	return this->hooks;
}
Beispiel #11
0
/**
	This function is called when the black-box relation is being instantiated.

	This just gets the data member and checks that it's valid, and stores
	it in the blackbox data field.
*/
static int sunpos_nrel_prepare(struct BBoxInterp *bbox,
	   struct Instance *data,
	   struct gl_list_t *arglist
){
	struct Instance *inst;
	double latitude, longitude, elevation;

	/* get the latitude */
	GET_CHILD_VAL(latitude);
	CONSOLE_DEBUG("Latitude: %0.3f",latitude);
	if(latitude > PI/2 || latitude < -PI/2){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"'latitude' is out of allowable range -PI/2 to PI/2.");
		return 1;
	}

	/* get the longitude */
	GET_CHILD_VAL(longitude);
	CONSOLE_DEBUG("Longitude: %0.3f",longitude);
	if(longitude > PI || longitude < -PI){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"'latitude' is out of allowable range -PI to PI.");
		return 1;
	}

	/* get the elevation */
	GET_CHILD_VAL(elevation);
	CONSOLE_DEBUG("Elevation: %0.3f m",elevation);
	if(elevation < -6500000){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"'elevation' is out of allowable range (must be > -6,500 km)");
		return 1;
	}

	spa_data *S = ASC_NEW(spa_data);
	S->latitude = latitude * 180/PI;
	S->longitude = longitude * 180/PI;
	S->elevation = elevation;
	S->function = SPA_ZA_JD;

	ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Prepared position for sun position.\n");
	bbox->user_data = (void *)S;
	return 0;
}
Beispiel #12
0
int
SolverHooks::doSolve(Instance *i, Simulation *S){
	CONSOLE_DEBUG("Solving model...");
	
	try{
		/* FIXME do solving of a particular instance? */
		if(!getSolverReporter()){
			CONSOLE_DEBUG("Creating default SolverReporter");
			SolverReporter R;
			S->solve(S->getSolver(), R);
		}else{
			CONSOLE_DEBUG("Using SolverReporter at %p",getSolverReporter());
			S->solve(S->getSolver(), *getSolverReporter());
		}
	}catch(std::runtime_error E){
		return SLVREQ_SOLVE_FAIL;
	}

	/* solver succeeded */
	return 0;
}
Beispiel #13
0
int
SolverHooks::setSolver(const char *solvername, Simulation *S){
	/* note desired return codes from slvreq.h */
	try{
		Solver solver(solvername);
		S->build();
		S->setSolver(solver);
	}catch(std::runtime_error *E){
		return SLVREQ_UNKNOWN_SOLVER;
	}
	CONSOLE_DEBUG("Solver set to '%s'",solvername);
	return 0;
}
Beispiel #14
0
int datareader_tmy3_vals(DataReader *d, double *v){
#if TMY3_DEBUG
	CONSOLE_DEBUG("At t=%f d, T = %lf, DNI = %f Wh/m2"
		,(ROW.t / 3600. / 24.),ROW.T, ROW.DNI
	);
#endif
	v[0]=ROW.T;
	v[1]=ROW.p;
	v[2]=ROW.rh;
	v[3]=ROW.DNI;
	v[4]=ROW.GHI;
	v[5]=ROW.v_wind;
	v[6]=ROW.d_wind;
	return 0;
}
FREESTEAM_EXPORT int freesteam_register(){
		int result = 0;

#ifdef BBOX_DEBUG
		CONSOLE_DEBUG("Initialising freesteam...");
#endif
		result += CreateUserFunctionBlackBox("freesteam_Tvsx_ph"
			, NULL /* alloc */
			, Tvsx_ph_calc /* value */
			, Tvsx_ph_calc /* deriv */
			, NULL /* deriv2 */
			, NULL /* free */
			, 2,4 /* inputs, outputs */
			, "[T,v,s,x] = freesteam_Tvsx_ph(p,h) (see http://freesteam.sf.net)"
			, 0.0
		);

		result += CreateUserFunctionBlackBox("freesteam_mukrhocp_pT"
			, NULL /* alloc */
			, mukrhocp_pT_calc /* value */
			, NULL /* deriv */
			, NULL /* deriv2 */
			, NULL /* free */
			, 2,4 /* inputs, outputs */
			, "[mu,k,rho,cp] = freesteam_mukrhocp_pT(p,T) (see http://freesteam.sf.net)"
			, 0.0
		);

		result += CreateUserFunctionBlackBox("freesteam_mu_Tv"
			, NULL /* alloc */
			, mu_Tv_calc /* value */
			, NULL /* deriv */
			, NULL /* deriv2 */
			, NULL /* free */
			, 2,1 /* inputs, outputs */
			, "[mu] = freesteam_mu_Tv(T,v) (see http://freesteam.sf.net)"
			, 0.0
		);

#define X
FREESTEAM_SISO_FUNCS(FREESTEAM_SISO_DECL,X)
#undef X

		return result;
}
LRESULT CPropertyView::OnItemEdit(int idCtrl, LPNMHDR pnmh, BOOL& /*bHandled*/)
{
	CONSOLE_DEBUG("Property about to change...\n");
	int idx = m_ctrlComboBox.GetFirstSel();
	int items = 0;
	while(idx != -1) {
		IPropertyEnabled *pProperty = (IPropertyEnabled *)m_ctrlComboBox.GetItemDataPtr(idx);
		if((int)pProperty == -1) return 0;

		pProperty->Commit();
		items++;
	
		idx = m_ctrlComboBox.GetNextSel();
	}
	m_PropertyList.Touch(); // cleans all changed flags in the property list.

	return 0;
}
Beispiel #17
0
static void test_create(void){

	CU_ASSERT(0 == Asc_CompilerInit(0));

#define DECLVAR(NAME) struct Expr *NAME = CreateVarExpr(CreateIdName(AddSymbol(#NAME)));
	DECLVAR(A);
	DECLVAR(B);
	DECLVAR(C);

	struct Expr *AandB = JoinExprLists(B,JoinExprLists(A,CreateOpExpr(e_and)));
	struct Expr *C_or_AandB = JoinExprLists(AandB,JoinExprLists(C,CreateOpExpr(e_or)));

	CONSOLE_DEBUG("write expr, in postfix form: ");
	WriteExpr(ASCERR,C_or_AandB);
	FPRINTF(ASCERR,"\n\n");

	Asc_CompilerDestroy();
}
Beispiel #18
0
int datareader_tmy3_eof(DataReader *d){
	parse *p = DATA(d)->p;
	if(parseEnd(p)){
		CONSOLE_DEBUG("REACHED END OF FILE");
		if(d->i < d->ndata){
			ERROR_REPORTER_HERE(ASC_PROG_WARNING,"Incomplete data set found (%d rows < %d expected",d->i, d->ndata);
		}
		d->ndata=d->i;
		int i;double tmin = +0.5*DBL_MAX,tmax = -0.5*DBL_MAX;
		for(i=0; i<d->ndata; ++i){
			double t = DATA(d)->rows[i].t;
			if(t < tmin)tmin = t;
			if(t > tmax)tmax = t;
		}
		ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Read %d rows, t in range [%f,%f] d",d->ndata,tmin/3600./24.,tmax/3600./24.);
		return 1; /* yes, end of file */
	}
	return 0; /* no, more data still */
}
/**
   'heatex_prepare' just gets the stream details and the number of slices for
	internal calculation, and checks that the stream names are valid in FPROPS.

	TODO FIXME allow general support for specification of components, incl type,source.
*/
int heatex_prepare(struct BBoxInterp *bbox,
	   struct Instance *data,
	   struct gl_list_t *arglist
){
	HeatExData *hxd = ASC_NEW(HeatExData);
	if(!hxd)goto fail;

	struct Instance *compinst[2], *ninst;
	const char *comp[2];

	N_SYM = AddSymbol("n");
	/* we look through these 0,1 below */
	heatex_symbols[0] = AddSymbol("component");
	heatex_symbols[1] = AddSymbol("component_hot");

	ninst = ChildByChar(data,N_SYM);
	if(!ninst){
		ERROR_REPORTER_HERE(ASC_USER_ERROR
			,"Couldn't locate '%s' in DATA, please check usage.",SCP(N_SYM)
		);
		goto fail;
	}
	if(InstanceKind(ninst)!=INTEGER_CONSTANT_INST){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member '%s' must be a symbol_constant",SCP(N_SYM));
		goto fail;
	}
	hxd->n = IC_INST(ninst)->value;

	int i;
	for(i=0;i<2;++i){
		/* get the component names for cold and hot sides */
		compinst[i] = ChildByChar(data,heatex_symbols[i]);
		if(!compinst[i]){
			ERROR_REPORTER_HERE(ASC_USER_ERROR
				,"Couldn't locate '%s' in DATA, please check usage."
				,SCP(heatex_symbols[i])
			);
			goto fail;
		}
		if(InstanceKind(compinst[i])!=SYMBOL_CONSTANT_INST){
			ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member '%s' must be a symbol_constant",SCP(heatex_symbols[i]));
			goto fail;
		}
		comp[i] = SCP(SYMC_INST(compinst[i])->value);
		CONSOLE_DEBUG("%s: %s",SCP(heatex_symbols[i]),comp[i]);
		if(comp[i]==NULL || strlen(comp[i])==0){
			ERROR_REPORTER_HERE(ASC_USER_ERROR,"'%s' is NULL or empty",heatex_symbols[i]);
			goto fail;
		}

		hxd->comp[i] = fprops_fluid(comp[i], NULL,NULL);
		if(hxd->comp[i] == NULL){
			ERROR_REPORTER_HERE(ASC_USER_ERROR,"Heat exchanger %s name '%s' not recognised. Check list of supported species.",SCP(heatex_symbols[i]),comp[i]);
			goto fail;
		}
	}

	bbox->user_data = (void *)hxd;
	ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Heat exchanger data structure OK.\n",comp);
	return 0;

fail:
	if(hxd){
		/* TODO FIXME implement FPROPS freeing, will be important with fprops2 */
		//if(hxd->comp[0])ASC_FREE(hxd->comp[0]);
		//if(hxd->comp[1])ASC_FREE(hxd->comp[1]);
		ASC_FREE(hxd);
	}
	return 1;
}
Beispiel #20
0
/**
   'fprops_prepare' just gets the data member and checks that it's
	valid, and stores it in the blackbox data field.
*/
int asc_fprops_prepare(struct BBoxInterp *bbox,
	   struct Instance *data,
	   struct gl_list_t *arglist
){
	struct Instance *compinst, *typeinst, *srcinst;
	const char *comp, *type = NULL, *src = NULL;

	fprops_symbols[0] = AddSymbol("component");
	fprops_symbols[1] = AddSymbol("type");
	fprops_symbols[2] = AddSymbol("source");

	/* get the component name */
	compinst = ChildByChar(data,COMPONENT_SYM);
	if(!compinst){
		ERROR_REPORTER_HERE(ASC_USER_ERROR
			,"Couldn't locate 'component' in DATA, please check usage of FPROPS."
		);
		return 1;
	}
	if(InstanceKind(compinst)!=SYMBOL_CONSTANT_INST){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member 'component' must be a symbol_constant");
		return 1;
	}
	comp = SCP(SYMC_INST(compinst)->value);
	if(comp==NULL || strlen(comp)==0){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"'component' is NULL or empty");
		return 1;
	}

	/* get the component correlation type (FPROPS doesn't mind if none given) */
	typeinst = ChildByChar(data,TYPE_SYM);
	if(typeinst){
		if(InstanceKind(typeinst)!=SYMBOL_CONSTANT_INST){
			ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member 'type' must be a symbol_constant");
			return 1;
		}
		type = SCP(SYMC_INST(typeinst)->value);
		//CONSOLE_DEBUG("TYPE: %s",type?type:"(null)");
		if(type && strlen(type)==0)type = NULL;
	}

	/* get the source data string (FPROPS doesn't mind if none given) */
	srcinst = ChildByChar(data,SOURCE_SYM);
	if(srcinst){
		if(InstanceKind(srcinst)!=SYMBOL_CONSTANT_INST){
			ERROR_REPORTER_HERE(ASC_USER_ERROR,"DATA member 'source' must be a symbol_constant");
			return 1;
		}
		src = SCP(SYMC_INST(srcinst)->value);
		CONSOLE_DEBUG("SOURCE: %s",src?src:"(null)");
		if(src && strlen(src)==0)src = NULL;
	}

	bbox->user_data = (void *)fprops_fluid(comp,type,src);
	if(bbox->user_data == NULL){
		ERROR_REPORTER_HERE(ASC_USER_ERROR,"Component name/type was not recognised. Check the source-code for for the supported species.");
		return 1;
	}

#ifdef ASC_FPROPS_DEBUG
	ERROR_REPORTER_HERE(ASC_PROG_NOTE,"Prepared component '%s'%s%s%s OK.\n"
		,comp
		,type?" type '":""
		,type?type:""
		,type?"'":""
	);
#endif
	return 0;
}
LRESULT CPropertyView::ItemUpdate(HPROPERTY hProperty, BOOL bCommit)
{
	LPCSTR Name = hProperty->GetName();
	HPROPERTY hCategory = hProperty->GetCategory();
	SProperty *pP = m_PropertyList.FindProperty(Name, hCategory?hCategory->GetName():"");
	ASSERT(pP);
	if(!pP) return 0;

	VARIANT value;
	memset(&value,0,sizeof(VARIANT));
	value.vt = VT_I4;
	if( pP->eType == SProperty::ptString ||
		pP->eType == SProperty::ptUCString ||
		pP->eType == SProperty::ptLCString ) value.vt = VT_BSTR;
	if(!hProperty->GetValue(&value)) return 0;

	switch(pP->eType) {
		case SProperty::ptUCString: 
		case SProperty::ptLCString: 
		case SProperty::ptString: 
			strncpy(pP->szString, COLE2CT(value.bstrVal), pP->uMDL-1);
			pP->bChanged = true;
			break;
		case SProperty::ptRGBColor: 
		case SProperty::ptARGBColor: 
			pP->rgbColor = ARGBCOLOR(*(RGBQUAD*)&(value.intVal));
			pP->bChanged = true;
			break;
		case SProperty::ptRangeValue: 
		case SProperty::ptValue: 
			pP->nValue = value.intVal;
			pP->bChanged = true;
			break;
		case SProperty::ptBoolean: 
			pP->bBoolean = !(value.boolVal==FALSE);
			pP->bChanged = true;
			break;
		case SProperty::ptList: 
			pP->nIndex = value.intVal;
			pP->bChanged = true;
			break;
	}

	CONSOLE_DEBUG("Property changed, updating...\n");
	int idx = m_ctrlComboBox.GetFirstSel();
	int items = 0;
	while(idx != -1) {
		IPropertyEnabled *pProperty = (IPropertyEnabled *)m_ctrlComboBox.GetItemDataPtr(idx);
		if((int)pProperty == -1) return 0;

		if(pProperty->SetProperties(m_PropertyList)) {
			if(bCommit==TRUE) {
				pProperty->Commit();
			}
			items++;
		}
		
		idx = m_ctrlComboBox.GetNextSel();
	}
	m_PropertyList.Touch(); // cleans all changed flags in the property list.

	CONSOLE_DEBUG("%d items changed%s.\n", items, (bCommit==TRUE)?" but all commited":"");

	// Update the list:
	OnUpdate();

	// Update the linked window
	if(::IsWindow(m_hWndLinked)) {
		::InvalidateRect(m_hWndLinked,  NULL, TRUE);
	}

	return 0;
}
Beispiel #22
0
SolverHooks::SolverHooks(SolverHooks &old) : R(old.R){
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("Creating new SolverHooks at %p (copy of old at %p",this,&old);
#endif
}
Beispiel #23
0
SolverHooks::SolverHooks(SolverReporter *R) : R(R){
#if SOLVERHOOKS_DEBUG
	CONSOLE_DEBUG("Creating SolverHooks at %p",this);
#endif
	// nothing else to do
}