Browse Source

beta plots being done, and case study finished

Cláudio Gomes 3 years ago
parent
commit
f0734ead4e

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

@@ -307,7 +307,7 @@ fmi2Status fmi2DoStep(fmi2Component fc , fmi2Real currentCommPoint, fmi2Real com
     	fi->b[_out_driver_up] = 0;
     	fi->b[_out_driver_up_stop] = 0;
 		fi->b[_out_driver_down] = 0;
-		fi->b[_out_driver_down_stop] = 0;
+		fi->b[_out_driver_down_stop] = 1;
 		fi->b[_out_passenger_up] = 0;
 		fi->b[_out_passenger_up_stop] = 0;
 		fi->b[_out_passenger_down] = 0;

+ 22 - 16
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/main.c

@@ -13,8 +13,8 @@
 #include "sim_support.h"
 
 #define START_TIME 0.0
-#define STOP_TIME 9.0
-#define STEP_SIZE 0.001
+#define STOP_TIME 4.5
+#define STEP_SIZE 0.00001
 
 FMU fmu_env, fmu_control_sa, fmu_power_sa, fmu_loop_sa;
 
@@ -28,6 +28,11 @@ int main(void) {
 	FILE *fp_fmu_loop_sa;
 	fp_fmu_loop_sa = fopen("result_loop_sa.csv", "w");
 
+	fprintf(fp_fmu_env,"t,dup,dup_stop,ddown,ddown_stop,pup,pup_stop,pdown,pdown_stop,obj\n");
+	fprintf(fp_fmu_control_sa,"t,u,d\n");
+	fprintf(fp_fmu_power_sa,"t,i,omega,theta\n");
+	fprintf(fp_fmu_loop_sa,"t,tau,height\n");
+
 	puts("Loading Dlls\n");
 
     loadDll("libFMI_Environment.dll", &fmu_env, "PW_ENV_");
@@ -45,7 +50,7 @@ int main(void) {
     char *fmuResourceLocation_env = "libFMI_Environment.dll";
     char *fmuResourceLocation_control_sa = "libFMI_controller_sa.dll";
     char *fmuResourceLocation_power_sa = "libFMI_power_sa.dll";
-    char *fmuResourceLocation_window_sa = "libFMI_loop_sa.dll";
+    char *fmuResourceLocation_loop_sa = "libFMI_loop_sa.dll";
 
     fmi2CallbackFunctions callbacks_env= {fmuLogger, calloc, free, NULL, &fmu_env};
     fmi2CallbackFunctions callbacks_control_sa = {fmuLogger, calloc, free, NULL, &fmu_control_sa};
@@ -55,9 +60,9 @@ int main(void) {
     c_env = fmu_env.instantiate("env", fmi2CoSimulation, "1", fmuResourceLocation_env, &callbacks_env, fmi2False, fmi2False);
     c_control_sa = fmu_control_sa.instantiate("control_sa", fmi2CoSimulation, "1", fmuResourceLocation_control_sa, &callbacks_control_sa, fmi2False, fmi2False);
     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 );
+    c_loop_sa = fmu_loop_sa.instantiate("loop_sa",fmi2CoSimulation, "1", fmuResourceLocation_loop_sa, &callbacks_loop_sa, fmi2False, fmi2False );
 
-    fmi2Boolean toleranceDefined = fmi2False;  // Disables zero crossings
+    fmi2Boolean toleranceDefined = fmi2False;  // Disables accurate zero crossing
     fmi2Real tolerance = 0;                    // used in setting up the experiment
 
     puts("FMU components instantiated, setting up experiments\n");
@@ -143,24 +148,24 @@ int main(void) {
     fmi2ValueReference vr_out_env[9]={0,1,2,3,4,5,6,7,8};
     fmi2Boolean b_out_env[9];
     fmi2ValueReference vr_in_control_sa_from_env[8]={0,1,2,3,4,5,6,7};
-    fmi2ValueReference vr_in_control_sa_from_window[1] = {0};
+    fmi2ValueReference vr_in_control_sa_from_power[1] = {0};
     fmi2ValueReference vr_out_control_sa[2]={9,10};
-    fmi2ValueReference vr_in_power_sa_u_d[2] = {3,5};
+    fmi2ValueReference vr_in_power_sa_u_d[2] = {5,3};
     fmi2ValueReference vr_in_power_sa_tau[1] = {4};
-    fmi2ValueReference vr_out_loop_sa[1] = {0};
+    fmi2ValueReference vr_out_loop_sa[2] = {0,3}; // tau, height
+    fmi2ValueReference vr_out_power_sa[3] = {0,1,2};
     fmi2Real r_in_power_from_control[2] = {0,0};
     fmi2Real r_in_power_from_loop[1]={0};
-    fmi2ValueReference vr_out_power_sa[3] = {0,1,2};
     fmi2Real r_out_power[3];
-    fmi2Real r_out_loop_sa[1];
+    fmi2Real r_out_loop_sa[2];
 
     fmi2Boolean b_out_control_sa[2];
     double currentTime = START_TIME;
-    const fmi2StatusKind lst = fmi2LastSuccessfulTime;
+    //const fmi2StatusKind lst = fmi2LastSuccessfulTime;
 
     while(currentTime <= STOP_TIME){
     	printf("\n----master new loop, ct:%f, h:%f\n",currentTime,STEP_SIZE);
-    	double next_step_size = STEP_SIZE;
+    	//double next_step_size = STEP_SIZE;
 
     	// do step
     	auxFlag = fmu_power_sa.setReal(c_power_sa,vr_in_power_sa_u_d,2, &r_in_power_from_control[0]);
@@ -216,7 +221,7 @@ int main(void) {
 			fflush(stdout);
 			return 1;
     	}
-    	auxFlag = fmu_control_sa.setReal(c_control_sa,vr_in_control_sa_from_window,1, &r_out_power[0]);
+    	auxFlag = fmu_control_sa.setReal(c_control_sa,vr_in_control_sa_from_power,1, &r_out_power[0]);
     	if(auxFlag != fmi2OK){
     		printf("Failed fmu_control_sa.setReal\n");
 			fflush(stdout);
@@ -270,15 +275,16 @@ int main(void) {
 			return 1;
 		}
 
-    	if (fmu_loop_sa.getReal(c_loop_sa, vr_out_loop_sa, 1, &r_out_loop_sa[0]) != fmi2OK){
+    	if (fmu_loop_sa.getReal(c_loop_sa, vr_out_loop_sa, 2, &r_out_loop_sa[0]) != fmi2OK){
     		printf("Failed getReal\n");
 			fflush(stdout);
 			return 1;
 		}
 
-    	fprintf(fp_fmu_loop_sa,"%f,%f\n",
+    	fprintf(fp_fmu_loop_sa,"%f,%f,%f\n",
     	    	    			currentTime,
-    	    					r_out_loop_sa[0]
+    	    					r_out_loop_sa[0],
+    	    					r_out_loop_sa[1]
     	    					);
 
     	r_in_power_from_loop[0] = r_out_loop_sa[0];

+ 0 - 55
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/plot_csv.py

@@ -1,55 +0,0 @@
-import csv
-from bokeh.plotting import figure, output_file, show
-
-color_pallete = [
-                "#e41a1c",
-                "#377eb8",
-                "#4daf4a",
-                "#984ea3",
-                "#ff7f00",
-                "#ffff33",
-                "#a65628",
-                "#f781bf",
-                "#CDD460",
-                "#265C70",
-                "#622876",
-                "#B37737"
-                 ]
-
-def plot_csv(filename):
-    with open(filename, 'rb') as csvfile:
-        rows = csv.reader(csvfile, delimiter=',')
-        
-        # read all rows at once
-        matrixRows = []
-        for row in rows:
-            matrixRows.append(row)
-        
-        firstRow = matrixRows[0]
-        numVars = len(firstRow) -1 # discount time
-        times = []
-        vars = []
-        for i in range(numVars):
-            vars.append([])
-        
-        for rowIdx in range(len(matrixRows)):
-            row = matrixRows[rowIdx]
-            times.append(float(row[0]))
-            for varIdx in range(1, numVars):
-                col = row[varIdx]
-                vars[varIdx].append(float(col))
-        
-        plot_file = filename.replace(".csv", ".html")
-        output_file(plot_file, title="Results")
-        
-        p = figure(title="Plot", x_axis_label='time', y_axis_label='', width=1300, height=900)
-        
-        varName = "v{0}"
-        for i in range(numVars):
-            p.line(x=times, y=vars[i], legend=varName.format(i), color=color_pallete[i])
-    
-        show(p)
-        
-plot_csv('result_power_sa.csv')
-plot_csv('result_ENV.csv')
-plot_csv('result_Control_sa.csv')

+ 92 - 0
SemanticAdaptationForFMI/Experiments/power_window_case_study/top_level_scenario/plot_results.r

@@ -0,0 +1,92 @@
+library(ggplot2)
+library(cowplot)
+library(grid)
+library(dplyr)
+library(plyr)
+library(reshape)
+library(plotly)
+library(stringr)
+
+sizeAxisLabel <- 18
+
+# Plot environment
+  env <- read.csv("result_ENV.csv")
+  
+  # Drop columns that will not be plotted
+  env$ddown <- NULL
+  env$ddown_stop <- NULL
+  env$pup <- NULL
+  env$pup_stop <- NULL
+  env$pdown <- NULL
+  env$pdown_stop <- NULL
+  env$obj <- NULL
+  
+  env_molten <- melt(env, id=c("t"))
+  
+  pdf("results_Env.pdf", width=10, height=5)
+  
+  pallete <- c("#89cbaf",
+               "#be5440",
+               "#9699c3",
+               "#cca956",
+               "#4f314e")
+  penv <- ggplot(data=env_molten) +
+            geom_line(aes(x=t, y=value, color=variable), size=1)
+  
+  dev.off()
+
+# Plot Controller 
+  control <- read.csv("result_Control_sa.csv")
+  control_molten <- melt(control, id=c("t"))
+  
+  pdf("results_Control_sa.pdf", width=10, height=5)
+  
+  pallete <- c("#89cbaf",
+               "#be5440",
+               "#9699c3",
+               "#cca956",
+               "#4f314e")
+  pcontrol <- ggplot(data=control_molten) +
+                geom_line(aes(x=t, y=value, color=variable), size=1)
+  
+  dev.off()
+  
+# Plot power 
+  power <- read.csv("result_power_sa.csv")
+  power$omega <- NULL
+  power$theta <- NULL
+  power_molten <- melt(power, id=c("t"))
+  
+  pdf("results_power_sa.pdf", width=10, height=5)
+  
+  pallete <- c("#89cbaf",
+               "#be5440",
+               "#9699c3",
+               "#cca956",
+               "#4f314e")
+  ppower <- ggplot(data=power_molten) +
+            geom_line(aes(x=t, y=value, color=variable), size=1)
+  
+  dev.off()
+  
+# Plot Loop_sa
+  loop <- read.csv("result_loop_sa.csv")
+  loop$tau <- NULL
+  loop_molten <- melt(loop, id=c("t"))
+  
+  pdf("results_loop_sa.pdf", width=10, height=5)
+  
+  pallete <- c("#89cbaf",
+               "#be5440",
+               "#9699c3",
+               "#cca956",
+               "#4f314e")
+  ploop <- ggplot(data=loop_molten) +
+            geom_line(aes(x=t, y=value, color=variable), size=1)
+  
+  dev.off()
+  
+# Make multi-grid plot
+  row_plot <- plot_grid(penv, pcontrol, ppower, ploop, nrow = 4, ncol = 1, align = "v")
+  save_plot("vertical_plot.pdf", row_plot, nrow = 4, ncol = 1, base_aspect_ratio = 1.0)
+