thermferm/simulator.c

changeset 262
d0014ccec615
parent 259
b7c967359771
child 263
93c1f91adaa7
--- a/thermferm/simulator.c	Sat Aug 23 12:48:55 2014 +0200
+++ b/thermferm/simulator.c	Sat Aug 23 23:38:18 2014 +0200
@@ -27,6 +27,7 @@
 
 extern int		my_shutdown;
 extern int		debug;
+extern sys_config	Config;
 
 
 
@@ -36,17 +37,73 @@
 void *my_simulator_loop(void *threadid)
 #endif
 {
+    simulator_list	*simulator;
+    time_t		now, last = (time_t)0;
+    int			seconds = 0;
+    double		k_room_air, sqm_room_air, thick_room_air, heat_transfer;
+    double		t_change, vhc_air = 0.00121, vhc_water = 4.1796;
 
     syslog(LOG_NOTICE, "Thread my_simulator_loop started");
     if (debug)
 	fprintf(stdout, "Thread my_simulator_loop started\n");
 
     for (;;) {
+	for (simulator = Config.simulators; simulator; simulator = simulator->next) {
+	    if (my_shutdown)
+	    	break;
 
-	if (my_shutdown)
-	    break;
+	    now = time(NULL);
+	    if (now != last) {
+		last = now;
+		/*
+		 * Each second
+		 */
+	    	seconds++;
+
+		/*
+	    	 * First, calculate temperature difference between the room and the air in the
+	    	 * fridge. We use the volume air to roughly calculate the total area between
+	    	 * the in and outside. Calculate the effect and shift the air temperature towards
+	    	 * the room temperature.
+	    	 */
+		sqm_room_air = (cbrtl(simulator->volume_air) * cbrtl(simulator->volume_air) * 6) / 100;	/* square meters all fridge sides */
+		thick_room_air = 0.02;	/* 4 cm walls	*/
+		k_room_air = 0.03;	/* Polystrene	*/
+		heat_transfer=(k_room_air * sqm_room_air * (simulator->room_temperature - simulator->air_temperature)) / thick_room_air;
+		if (heat_transfer != 0)
+		    t_change = vhc_air / (heat_transfer * (simulator->volume_air * 1000));
+		else
+		    t_change = 0.0;
+		simulator->air_temperature = simulator->air_temperature + t_change;
+		fprintf(stdout, "room_air=%f room=%f air=%.15f heat_transfer=%f t_change=%.15f\n", 
+				sqm_room_air, simulator->room_temperature, simulator->air_temperature, heat_transfer, t_change);
 
-	usleep(100000);
+	    	/*
+	    	 * If heating, calculate temperature of the heating plate. If heating is off but
+	    	 * the plate is warmer then the air, calculate the cooling down temperature.
+	    	 * Finally, calculate the new air and plate temperature.
+	    	 */
+
+	    	/* 
+	    	 * If cooling, calculate temperature of the cooling plate. If cooling is off but
+	    	 * the plate is colder then the air, calculate the warming up temperature.
+	    	 * Finsally, calculate the new air and plate temperature.
+	    	 */
+
+	    	/*
+	    	 * Calculate the extra beer temperatur rise to simulate the heat produced by the
+	    	 * fermentation process. Peak about one day after start and slowly decrease after
+	    	 * that.
+	    	 */
+
+	    	/*
+	    	 * Calculate final temperature of the beer and the air.
+	    	 */
+
+	    }
+	    usleep(100000);
+	}
+	usleep(50000);
     }
 
     syslog(LOG_NOTICE, "Thread my_simulator_loop stopped");

mercurial