Browse Source

normnalized fmu prefixes, to improve readability

Cláudio Gomes 3 years ago
parent
commit
cd4de728a1

+ 1 - 0
.gitignore

@@ -48,3 +48,4 @@ xtend-gen/
 /SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_loop_sa/log.txt
 /SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_controller/log.txt
 /SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_controller_sa/log.txt
+/SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/Debug/*.libs

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_Environment/FMI_Environment.c

@@ -8,7 +8,7 @@ Template for a sc FMU
 
 #define MODEL_IDENTIFIER SEL
 #define MODEL_GUID "{41f87101-edf2-4eef-90f3-42db56d4565f}"
-#define FMI2_FUNCTION_PREFIX PW_ENV
+#define FMI2_FUNCTION_PREFIX PW_ENV_
 
 
 #include <stdio.h>

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_Environment/test.c

@@ -13,7 +13,7 @@ int main(void) {
 
 	puts("Loading Dlls\n");
 	/* loading */
-    loadDll("libFMI_Environment.dll", &fmu_env, "PW_ENV");
+    loadDll("libFMI_Environment.dll", &fmu_env, "PW_ENV_");
 
     puts("instantiating fmus\n");
     fmi2Component c_env;

+ 33 - 31
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_controller_sa/FMI_GM.c

@@ -349,37 +349,39 @@ fmi2Status fmi2DoStep(fmi2Component fc , fmi2Real currentCommPoint, fmi2Real com
 	printf("%s in fmiDoStep(), ct:%f, h:%f\n",fi->instanceName,currentCommPoint,commStepSize);
 	fi->aux_obj_detected = 0;
 	fi->step_size = commStepSize;
-	if( (
-			(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
-			&& fi->previous_arm_current < fi->CROSSING
-		)
-		&&
-		(
-			(!is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE))
-			&& fi->stored_arm_current > fi->CROSSING
-		)
-	   ){
-		double negative_value = fi->previous_arm_current - fi->CROSSING;
-		double positive_value = fi->stored_arm_current - fi->CROSSING;
-		fi->step_size = (commStepSize * (-negative_value)) / (positive_value - negative_value);
-		fi->currentTime = currentCommPoint + fi->step_size;
-		externalSimStatus = fmi2Discard;
-		printf("%s: crossed too far, rejecting step size %f and proposing %f... \n",fi->instanceName, commStepSize, fi->step_size);
-		printf("%s: fi->previous_arm_current = %f \n",fi->instanceName, fi->previous_arm_current);
-		printf("%s: fi->stored_arm_current = %f \n",fi->instanceName, fi->stored_arm_current);
-	}else{
-		if((
-				(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
-				&& fi->previous_arm_current < fi->CROSSING
-			)
-			&&
-			(
-				is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE)
-			)
-		  ){
-			fi->aux_obj_detected = 1;
-			printf("%s: crossed just right... \n",fi->instanceName);
-		}
+
+	if (fi->toleranceDefined){
+		printf("%s: Checking for threshold crossing...\n",fi->instanceName);
+		if( (
+					(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
+					&& fi->previous_arm_current < fi->CROSSING
+				)
+				&&
+				(
+					(!is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE))
+					&& fi->stored_arm_current > fi->CROSSING
+				)
+			   ){
+				double negative_value = fi->previous_arm_current - fi->CROSSING;
+				double positive_value = fi->stored_arm_current - fi->CROSSING;
+				fi->step_size = (commStepSize * (-negative_value)) / (positive_value - negative_value);
+				fi->currentTime = currentCommPoint + fi->step_size;
+				externalSimStatus = fmi2Discard;
+				printf("%s: crossed too far, rejecting step size %f and proposing %f... \n",fi->instanceName, commStepSize, fi->step_size);
+				printf("%s: fi->previous_arm_current = %f \n",fi->instanceName, fi->previous_arm_current);
+				printf("%s: fi->stored_arm_current = %f \n",fi->instanceName, fi->stored_arm_current);
+			} else if((
+						(!is_close(fi->previous_arm_current, fi->CROSSING , fi->REL_TOLERANCE, fi->ABS_TOLERANCE))
+						&& fi->previous_arm_current < fi->CROSSING
+					)
+					&&
+					(
+						is_close(fi->stored_arm_current,fi->CROSSING,fi->REL_TOLERANCE,fi->ABS_TOLERANCE)
+					)
+				  ){
+					fi->aux_obj_detected = 1;
+					printf("%s: crossed just right... \n",fi->instanceName);
+			}
 	}
 
 	if (externalSimStatus == fmi2OK){ // only do the internal step if the current step is OK

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_controller_sa/test.c

@@ -47,7 +47,7 @@ int main(void) {
 
     c_control_sa = fmu_control_sa.instantiate("control_sa", fmi2CoSimulation, "1", fmuResourceLocation_control_sa, &callbacks_control_sa, fmi2False, fmi2False);
 
-    fmi2Boolean toleranceDefined = fmi2False;  // true if model description define tolerance
+    fmi2Boolean toleranceDefined = fmi2True;  // Enables zero crossing location
     fmi2Real tolerance = 0;                    // used in setting up the experiment
 
     puts("FMU components instantiated, setting up experiments\n");

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_loop_sa/FMI_loop_sa.c

@@ -7,7 +7,7 @@ Template for a  FMU
 
 #define MODEL_IDENTIFIER GM
 #define MODEL_GUID "{41f87101-edf2-4eef-90f3-42db56d4565f}"
-#define FMI2_FUNCTION_PREFIX LOOP_SA
+#define FMI2_FUNCTION_PREFIX LOOP_SA_
 
 #include <stdio.h>
 #include "string.h"

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/fmus/FMI_loop_sa/test.c

@@ -13,7 +13,7 @@ int main(void) {
 
 	puts("Loading Dlls\n");
 	/* loading */
-    loadDll("libFMI_loop_sa.dll", &fmu_loop_sa, "LOOP_SA");
+    loadDll("libFMI_loop_sa.dll", &fmu_loop_sa, "LOOP_SA_");
 
     puts("instantiating fmus\n");
     fmi2Component c_loop_sa;

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/build.ps1

@@ -3,7 +3,7 @@ mingw32-make exe makefile
 
 pause
 
-$LibAbsolutionPath = (Get-Item -Path ".\lib" -Verbose).FullName
+$LibAbsolutionPath = (Get-Item -Path ".\Debug" -Verbose).FullName
 
 Echo "Building FMUs and getting their dlls..."
 

+ 0 - 3
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/lib/.gitignore

@@ -1,3 +0,0 @@
-FMI_Obstacle
-FMI_PowerSystem
-FMI_Window

+ 140 - 100
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/main.c

@@ -30,10 +30,14 @@ int main(void) {
 
 	puts("Loading Dlls\n");
 
-    loadDll("libFMI_Environment.dll", &fmu_env, "PW_ENV");
-    loadDll("libFMI_controller_sa.dll", &fmu_control_sa, "PW_CONTROLLER_SA");
-    loadDll("libFMI_power_sa.dll", &fmu_power_sa, "POWER_SA");
-    loadDll("libFMI_loop_sa.dll", &fmu_loop_sa, "LOOP_SA");
+    loadDll("libFMI_Environment.dll", &fmu_env, "PW_ENV_");
+    fflush(stdout);
+    loadDll("libFMI_controller_sa.dll", &fmu_control_sa, "PW_CONTROLLER_SA_");
+    fflush(stdout);
+    loadDll("libFMI_power_sa.dll", &fmu_power_sa, "POWER_SA_");
+    fflush(stdout);
+    loadDll("libFMI_loop_sa.dll", &fmu_loop_sa, "LOOP_SA_");
+    fflush(stdout);
 
     puts("instantiating fmus\n");
     fmi2Component c_env, c_control_sa, c_power_sa, c_loop_sa;
@@ -53,62 +57,87 @@ int main(void) {
     c_power_sa = fmu_power_sa.instantiate("power_sa", fmi2CoSimulation, "1", fmuResourceLocation_power_sa, &callbacks_power_sa, fmi2False, fmi2False);
     c_loop_sa = fmu_loop_sa.instantiate("loop_sa",fmi2CoSimulation, "1", fmuResourceLocation_window_sa, &callbacks_loop_sa, fmi2False, fmi2False );
 
-    fmi2Boolean toleranceDefined = fmi2False;  // true if model description define tolerance
+    fmi2Boolean toleranceDefined = fmi2False;  // Disables zero crossings
     fmi2Real tolerance = 0;                    // used in setting up the experiment
 
     puts("FMU components instantiated, setting up experiments\n");
-    fmi2Status fmi2Flag[4];
-    fmi2Flag[0] = fmu_env.setupExperiment(c_env, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
-    if (fmi2Flag[0] == fmi2Error){
-    	return -1;
+    fmi2Status auxFlag;
+    auxFlag = fmu_env.setupExperiment(c_env, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
+    if (auxFlag == fmi2Error){
+    	printf("Failed setupExperiment\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[1] = fmu_control_sa.setupExperiment(c_control_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
-    if (fmi2Flag[1] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_control_sa.setupExperiment(c_control_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
+    if (auxFlag == fmi2Error){
+    	printf("Failed setupExperiment\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[2] = fmu_power_sa.setupExperiment(c_power_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
-    if (fmi2Flag[2] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_power_sa.setupExperiment(c_power_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
+    if (auxFlag == fmi2Error){
+    	printf("Failed setupExperiment\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[3] = fmu_loop_sa.setupExperiment(c_loop_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
-    if (fmi2Flag[3] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_loop_sa.setupExperiment(c_loop_sa, toleranceDefined, tolerance, START_TIME, fmi2True, STOP_TIME);
+    if (auxFlag == fmi2Error){
+    	printf("Failed setupExperiment\n");
+		fflush(stdout);
+		return -1;
     }
 
     puts("Experiment setup, entering init mode\n");
-    fmi2Flag[0] = fmu_env.enterInitializationMode(c_env);
-    if (fmi2Flag[0] == fmi2Error){
-    	return -1;
+
+    auxFlag = fmu_env.enterInitializationMode(c_env);
+    if (auxFlag == fmi2Error){
+    	printf("Failed enterInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[1] = fmu_control_sa.enterInitializationMode(c_control_sa);
-    if (fmi2Flag[1] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_control_sa.enterInitializationMode(c_control_sa);
+    if (auxFlag == fmi2Error){
+    	printf("Failed enterInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[2] = fmu_power_sa.enterInitializationMode(c_power_sa);
-    if (fmi2Flag[2] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_power_sa.enterInitializationMode(c_power_sa);
+    if (auxFlag == fmi2Error){
+    	printf("Failed enterInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[3] = fmu_loop_sa.enterInitializationMode(c_loop_sa);
-    if (fmi2Flag[3] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_loop_sa.enterInitializationMode(c_loop_sa);
+    if (auxFlag == fmi2Error){
+    	printf("Failed enterInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
 
     puts("Experiment setup, exiting init mode\n");
-    fmi2Flag[0] = fmu_env.exitInitializationMode(c_env);
-    if (fmi2Flag[0] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_env.exitInitializationMode(c_env);
+    if (auxFlag == fmi2Error){
+    	printf("Failed exitInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[1] = fmu_control_sa.exitInitializationMode(c_control_sa);
-    if (fmi2Flag[1] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_control_sa.exitInitializationMode(c_control_sa);
+    if (auxFlag == fmi2Error){
+    	printf("Failed exitInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[2] = fmu_power_sa.exitInitializationMode(c_power_sa);
-    if (fmi2Flag[2] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_power_sa.exitInitializationMode(c_power_sa);
+    if (auxFlag == fmi2Error){
+    	printf("Failed exitInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
-    fmi2Flag[3] = fmu_loop_sa.exitInitializationMode(c_loop_sa);
-    if (fmi2Flag[3] == fmi2Error){
-    	return -1;
+    auxFlag = fmu_loop_sa.exitInitializationMode(c_loop_sa);
+    if (auxFlag == fmi2Error){
+    	printf("Failed exitInitializationMode\n");
+		fflush(stdout);
+		return -1;
     }
 
     fmi2ValueReference vr_out_env[9]={0,1,2,3,4,5,6,7,8};
@@ -132,30 +161,41 @@ int main(void) {
     while(currentTime <= STOP_TIME){
     	printf("\n----master new loop, ct:%f, h:%f\n",currentTime,STEP_SIZE);
     	double next_step_size = STEP_SIZE;
-    	fmi2Component ctemp_env, ctemp_control_sa;
-    	// Make backup
-    	fmu_env.getFMUstate(c_env,&ctemp_env);
-    	fmu_control_sa.getFMUstate(c_control_sa, &ctemp_control_sa);
-    	// do step
 
-    	fmi2Flag[2] = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_u_d,2, &r_in_power_from_control[0]);
-    	if(fmi2Flag[1] != fmi2OK){
-    		return 1;
+    	// do step
+    	auxFlag = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_u_d,2, &r_in_power_from_control[0]);
+    	if(auxFlag != fmi2OK){
+    		printf("Failed setReal\n");
+			fflush(stdout);
+			return 1;
     	}
-    	fmi2Flag[2] = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_tau,1, &r_in_power_from_loop[0]);
-    	if(fmi2Flag[1] != fmi2OK){
-    		return 1;
+    	auxFlag = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_tau,1, &r_in_power_from_loop[0]);
+    	if(auxFlag != fmi2OK){
+    		printf("Failed setReal\n");
+			fflush(stdout);
+			return 1;
     	}
 
-    	fmi2Flag[2] = fmu_power_sa.doStep(c_power_sa, currentTime, STEP_SIZE, fmi2True);
+    	auxFlag = fmu_power_sa.doStep(c_power_sa, currentTime, STEP_SIZE, fmi2True);
 
     	if (fmu_power_sa.getReal(c_power_sa, vr_out_power_sa, 3, &r_out_power[0]) != fmi2OK){
-    		return 1;
+    		printf("Failed getReal\n");
+			fflush(stdout);
+			return 1;
     	}
-    	fflush(stdout);
-    	fmi2Flag[0] = fmu_env.doStep(c_env, currentTime, STEP_SIZE, fmi2True);
+
+    	auxFlag = fmu_env.doStep(c_env, currentTime, STEP_SIZE, fmi2True);
+
+    	if (auxFlag != fmi2OK){
+			printf("Failed fmu_env.doStep\n");
+			fflush(stdout);
+			return 1;
+		}
+
     	if (fmu_env.getBoolean(c_env, vr_out_env, 9, &b_out_env[0]) != fmi2OK){
-    		return 1;
+    		printf("Failed fmu_env.getBoolean\n");
+			fflush(stdout);
+			return 1;
     	}
 
     	fprintf(fp_fmu_env,"%f,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
@@ -170,20 +210,33 @@ int main(void) {
 				b_out_env[7],
 				b_out_env[8]);
 
-    	fmi2Flag[1] = fmu_control_sa.setBoolean(c_control_sa,vr_in_control_sa_from_env,8,&b_out_env[0]);
-    	if(fmi2Flag[1] != fmi2OK){
-    		return 1;
+    	auxFlag = fmu_control_sa.setBoolean(c_control_sa,vr_in_control_sa_from_env,8,&b_out_env[0]);
+    	if(auxFlag != fmi2OK){
+    		printf("Failed fmu_control_sa.setBoolean\n");
+			fflush(stdout);
+			return 1;
     	}
-    	fmi2Flag[1] = fmu_control_sa.setReal(c_control_sa,vr_in_control_sa_from_window,1, &r_out_power[0]);
-    	if(fmi2Flag[1] != fmi2OK){
-    	    		return 1;
+    	auxFlag = fmu_control_sa.setReal(c_control_sa,vr_in_control_sa_from_window,1, &r_out_power[0]);
+    	if(auxFlag != fmi2OK){
+    		printf("Failed fmu_control_sa.setReal\n");
+			fflush(stdout);
+			return 1;
     	}
-    	fflush(stdout);
-    	fmi2Flag[1] = fmu_control_sa.doStep(c_control_sa, currentTime, STEP_SIZE, fmi2True);
+
+    	auxFlag = fmu_control_sa.doStep(c_control_sa, currentTime, STEP_SIZE, fmi2True);
+
+    	if(auxFlag != fmi2OK){
+			printf("Failed fmu_control_sa.doStep\n");
+			fflush(stdout);
+			return 1;
+		}
 
     	if (fmu_control_sa.getBoolean(c_control_sa, vr_out_control_sa, 2, &b_out_control_sa[0]) != fmi2OK){
-    	    	            return 1;
-    	    	}
+    		printf("Failed getBoolean\n");
+			fflush(stdout);
+			return 1;
+		}
+
     	fprintf(fp_fmu_control_sa,"%f,%d,%d\n",
     			currentTime,
 				b_out_control_sa[0],
@@ -191,27 +244,37 @@ int main(void) {
 
     	r_in_power_from_control[0] = (int)b_out_control_sa[0];
     	r_in_power_from_control[1] = (int)b_out_control_sa[1];
-    	fflush(stdout);
+
     	fprintf(fp_fmu_power_sa,"%f,%f,%f,%f\n",
     	    			currentTime,
     					r_out_power[0],
     					r_out_power[1],
 						r_out_power[2]);
 
-
     	fmi2ValueReference vr_toLoop[2] = {1,2};
     	fmi2Real toLoop[2];
     	toLoop[0] = r_out_power[1];
     	toLoop[1] = r_out_power[2];
 
     	if (fmu_loop_sa.setReal(c_loop_sa, vr_toLoop, 2, &toLoop[0]) != fmi2OK){
-    	    	    	            return 1;
-    	    	    	}
-    	fmi2Flag[3] = fmu_loop_sa.doStep(c_loop_sa, currentTime, STEP_SIZE, fmi2True);
-    	fflush(stdout);
+    		printf("Failed fmu_loop_sa.setReal\n");
+			fflush(stdout);
+			return 1;
+		}
+
+    	auxFlag = fmu_loop_sa.doStep(c_loop_sa, currentTime, STEP_SIZE, fmi2True);
+
+    	if(auxFlag != fmi2OK){
+			printf("Failed fmu_loop_sa.doStep\n");
+			fflush(stdout);
+			return 1;
+		}
+
     	if (fmu_loop_sa.getReal(c_loop_sa, vr_out_loop_sa, 1, &r_out_loop_sa[0]) != fmi2OK){
-    	    	    	            return 1;
-    	    	    	}
+    		printf("Failed getReal\n");
+			fflush(stdout);
+			return 1;
+		}
 
     	fprintf(fp_fmu_loop_sa,"%f,%f\n",
     	    	    			currentTime,
@@ -220,29 +283,7 @@ int main(void) {
 
     	r_in_power_from_loop[0] = r_out_loop_sa[0];
 
-    	//r_in_power_from_loop[0] = -7000;
-
-    	int redoStep  = 0;
-    	for(int i=0; i<2; i++){
-    		if (fmi2Flag[i] == fmi2Discard){
-				redoStep = 1;
-				fmi2Real newtime;
-
-				fmu_control_sa.getRealStatus(c_control_sa, lst, &newtime);
-				fmi2Real the_FMU_new_step = newtime - currentTime;
-				if(the_FMU_new_step < next_step_size){
-					next_step_size = the_FMU_new_step;
-				}
-			}
-    	}
-
-
-    	if(redoStep){ // should be a while loop!
-    		// Recover all FMUs and do step again
-    		printf("recover not yet implemented\n");
-    	}else{
-    		currentTime += STEP_SIZE;
-    	}
+    	currentTime += STEP_SIZE;
 
     	fflush(stdout);
 
@@ -251,6 +292,5 @@ int main(void) {
     fclose(fp_fmu_power_sa);
     fclose(fp_fmu_control_sa);
 
-
 	return EXIT_SUCCESS;
 }

+ 1 - 1
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/makefile

@@ -3,7 +3,7 @@ CFLAGS=-IC:fmi2 -O0 -g3 -Wall -c -fmessage-length=0
 DEPS = $(wildcard *.h)
 SOURCES = $(wildcard *.c)
 OBJECTS = $(SOURCES:.c=.o)
-EXE = PowerWindowCaseStudy.exe
+EXE = PowerWindowScenario.exe
 
 exe: $(OBJECTS)
 	$(CC) -o Debug\$(EXE) $(OBJECTS)