Debian Codegraph @

Aircraft/c172p/Nasal/icing.nas

raw
1 #############################################################################
2 # This file is part of FlightGear, the free flight simulator
3 # http://www.flightgear.org/
4 #
5 # Copyright (C) 2009 Torsten Dreyer, Torsten (at) t3r _dot_ de
6 # modified and used for C182S by Heiko H. Schulz 2018
7 #
8 # This program is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU General Public License as
10 # published by the Free Software Foundation; either version 2 of the
11 # License, or (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
17 #############################################################################
18
19 #########################################################################################
20 # this are the helper functions to model structural icing on airplanes
21 # Maintainer: Torsten Dreyer (Torsten at t3r dot de)
22 #
23 # Simple model: we listen to temperature and dewpoint. If the difference (spread)
24 # is near zero and temperature is below zero, icing may occour.
25 #
26 # inputs
27 # /environment/dewpoint-degc
28 # /environment/temperature-degc
29 # /environment/effective-visibility-m
30 # /velocities/airspeed-kt
31 # /environment/icing/max-spread-degc default: 0.1
32 # /environment/rain-norm
33 # /environment/snow-norm
34 #
35 ##Clouds detection see c182s.nas
36 #inputs
37 #/environment/icing/InCloudIcing
38 #
39 # outputs
40 # /environment/icing/icing-severity numeric value of icing severity
41 # /environment/icing/icing-severity-name textual representation of icing severity one of
42 # none,trace,light,moderate,severe
43 # /environment/icing/icing-factor ammound of ice accumulation per NAM
44
45 #############################################################################
46 # Taken from c182 team, with thanks!
47 #############################################################################
48
49 #########################################################################
50 # implementation of the global icemachine
51 #########################################################################
52
53 var ICING_NONE = 0;
54 var ICING_TRACE = 1;
55 var ICING_LIGHT = 2;
56 var ICING_MODERATE = 3;
57 var ICING_SEVERE = 4;
58
59 # these are the names for the icing severities
60 var ICING_CATEGORY = [ "none", "trace", "light", "moderate", "severe" ];
61
62 # the ice accumulating factors. Inches per nautical air mile flown
63 var ICING_FACTOR = [
64 # none: sublimating 0.3" / 80NM
65 -0.3/80,
66
67 # traces: 0.5" / 80NM
68 0.5/80.0,
69
70 # light: 0.5" / 40NM
71 0.5/40.0,
72
73 # moderate: 0.5" / 20NM
74 0.5/20.0,
75
76 #severe: 0.5" / 10NM
77 0.5/10
78 ];
79
80 # since we don't know the LWC of our clouds, just define some severities
81 # depending on temperature and a random offset
82 # format: upper temperatur, lower temperatur, minimum severity, maximum severity
83 var ICING_TEMPERATURE = [
84 [ 999, 0, ICING_NONE, ICING_NONE ],
85 [ 0, -2, ICING_NONE, ICING_MODERATE ],
86 [ -2, -12, ICING_LIGHT, ICING_SEVERE ],
87 [ -12, -20, ICING_LIGHT, ICING_MODERATE ],
88 [ -20, -30, ICING_TRACE, ICING_LIGHT ],
89 [ -30, -99, ICING_TRACE, ICING_NONE ]
90 ];
91
92 var dewpointN = props.globals.getNode( "/environment/dewpoint-degc" );
93 var temperatureN = props.globals.getNode( "/environment/temperature-degc" );
94 var speedN = props.globals.getNode( "/velocities/airspeed-kt" );
95 var icingRootN = props.globals.getNode( "/environment/icing", 1 );
96 var visibilityN = props.globals.getNode( "/environment/effective-visibility-m" );
97 var rain = props.globals.getNode( "/environment/rain-norm" );
98 var snow = props.globals.getNode( "/environment/snow-norm" );
99 #var visibility_factor = 0;
100 #var InCloudIcing = props.globals.getNode( "/environment/icing/InCloudIcing" );
101
102 #var ici = func{
103 #if (InCloudIcing ==1){
104 #visibility_factor.setValue(0.01)
105 #}else{
106 #visibility_factor.setValue(1)
107 #}
108 #}
109
110 var severityN = icingRootN.initNode( "icing-severity", ICING_NONE, "INT" );
111 var severityNameN = icingRootN.initNode( "icing-severity-name", ICING_CATEGORY[severityN.getValue()] );
112 var icingFactorN = icingRootN.initNode( "icing-factor", 0.0 );
113 var maxSpreadN = icingRootN.initNode( "max-spread-degc", 0.1 );
114
115 var setSeverity = func {
116 var value = arg[0];
117 if( severityN.getValue() != value ) {
118 severityN.setValue( value );
119 severityNameN.setValue( ICING_CATEGORY[value] );
120 }
121 }
122
123 #########################################################################################
124 # These are objects that are subject to icing
125 # inputs under /sim/model/icing/iceable (multiple instances allowed)
126 # ./name # name of this object, more or less useless
127 # ./salvage-control # name of a boolean property that salvages from ice
128 # ./output-property # the name of the property where the ice amount is written to
129 # ./sensitivity # a multiplier for the ice accumulation
130 #
131 # outputs
132 # ./ice-inches # the amount of ice in inches OR
133 # [property named by output-property] # the amount of ice in inches
134 #########################################################################################
135 var IceSensitiveElement = {};
136
137 IceSensitiveElement.new = func {
138 var obj = {};
139 obj.parents = [IceSensitiveElement];
140 obj.node = arg[0];
141
142 obj.nameN = obj.node.initNode( "name", "noname" );
143 var n = obj.node.getNode( "salvage-control", 0 );
144 obj.controlN = nil;
145 if( n != nil ) {
146 n = n.getValue();
147 if( n != nil ) {
148 obj.controlN = props.globals.initNode( n, 0, "BOOL" );
149 }
150 }
151 obj.sensitivityN = obj.node.initNode( "sensitivity", 1.0 );
152
153 obj.iceAmountN = nil;
154 n = obj.node.getNode( "output-property", 0 );
155 if( n != nil ) {
156 n = n.getValue();
157 if( n != nil ) {
158 obj.iceAmountN = props.globals.initNode( n, 0.0 );
159 }
160 }
161 if( obj.iceAmountN == nil ) {
162 obj.iceAmountN = obj.node.initNode( "ice-inches", 0.0 );
163 }
164
165 return obj;
166 };
167
168 #####################################################################
169 # this gets called from the icemachine on each update cycle
170 # arg[0] is the time in seconds since last update
171 # arg[1] is the number of NAM traveled since last update
172 # arg[2] is the ice-accumulation-factor for the current severity
173 #####################################################################
174 IceSensitiveElement.update = func {
175 if( me.controlN != nil and me.controlN.getBoolValue() ) {
176 if( me.iceAmountN.getValue() != 0.0 ) {
177 me.iceAmountN.setDoubleValue( 0.0 );
178 }
179 return;
180 }
181
182 var deltat = arg[0];
183 var dist_nm = arg[1];
184 var factor = arg[2];
185
186 #hack for getting rain and snow speeding up the ice amount
187
188 var rainN = (getprop ("/environment/rain-norm") or 0);
189 var snowN = (getprop ("/environment/snow-norm") or 0);
190 var tempN = (getprop ("/environment/temperature-degc") or 0);
191
192 if (tempN <0 ){
193 rainF = rainN * 1;
194 snowF = snowN * 1;
195 }else{
196 rainF = rainN * 0;
197 snowF = snowN * 0;
198 }
199
200 var v = me.iceAmountN.getValue() + dist_nm * factor * me.sensitivityN.getValue() + (0.005*rainF) + (0.005*snowF) ;
201 if(( v < 0.0 ) ){
202 v = 0.0;
203 }
204 if( me.iceAmountN.getValue() != v ) {
205 me.iceAmountN.setValue( v );
206 }
207 };
208
209 #####################################################################
210 # read the ice sensitive elements from the config file
211 #####################################################################
212 var iceSensitiveElements = nil;
213
214 var icingConfigN = props.globals.getNode( "/sim/model/icing", 0 );
215 if( icingConfigN != nil ) {
216 iceSensitiveElements = [];
217 var iceableNodes = icingConfigN.getChildren( "iceable" );
218 foreach( var iceable; iceableNodes ) {
219 append( iceSensitiveElements, IceSensitiveElement.new( iceable ) );
220 }
221 };
222
223 #########################################################################
224 # implementation of the clouds altitude detection (HHS)
225 #########################################################################
226
227 var Cloudaltitude = func {
228
229 var mdewpointN = props.globals.getValue( "/environment/metar/dewpoint-degc" ) or 0;
230 var mtemperatureN = getprop( "/environment/metar/temperature-degc");
231 var statE = props.globals.getValue( "/environment/metar/station-elevation-ft" ) or 0;
232 var cle0 = props.globals.getValue( "/environment/metar/clouds/layer[0]/elevation-ft" )or 0 ;
233 var cle1 = props.globals.getValue( "/environment/metar/clouds/layer[1]/elevation-ft" )or 0 ;
234 var cle2 = props.globals.getValue( "/environment/metar/clouds/layer[2]/elevation-ft" )or 0 ;
235 var cle3 = props.globals.getValue( "/environment/metar/clouds/layer[3]/elevation-ft" )or 0 ;
236 var cle4 = props.globals.getValue( "/environment/metar/clouds/layer[4]/elevation-ft" )or 0 ;
237
238
239 #cloffset = getprop("/local-weather/tmp/tile-alt-offset-ft") or 0;
240 #var cloffset = 0;
241
242 if (getprop("/local-weather/tmp/tile-alt-offset-ft") =="METAR") {
243 cloffset = getprop("/local-weather/tmp/tile-alt-offset-ft") or 0
244 }else{
245 cloffset = 0;
246 }
247
248 cloud0 = props.globals.getNode("/environment/icing/clouds/cloud0", 1);
249 cloud1 = props.globals.getNode("/environment/icing/clouds/cloud1", 1);
250 cloud2 = props.globals.getNode("/environment/icing/clouds/cloud2", 1);
251 cloud3 = props.globals.getNode("/environment/icing/clouds/cloud3", 1);
252 cloud4 = props.globals.getNode("/environment/icing/clouds/cloud4", 1);
253
254 setprop("/environment/icing/clouds/cloud0", (((mtemperatureN - mdewpointN)*30) + statE + cle0 ));
255 setprop("/environment/icing/clouds/cloud1", (((mtemperatureN - mdewpointN)*30) + statE + cle1 ));
256 setprop("/environment/icing/clouds/cloud2", (((mtemperatureN - mdewpointN)*30) + statE + cle2 ));
257 setprop("/environment/icing/clouds/cloud3", (((mtemperatureN - mdewpointN)*30) + statE + cle3 ));
258 setprop("/environment/icing/clouds/cloud4", (((mtemperatureN - mdewpointN)*30) + statE + cle4 ));
259
260 settimer(Cloudaltitude, 0.0);
261 }
262 Cloudaltitude();
263
264 ###################
265 #
266 ###################
267
268 var d = func {
269 cloud0 = getprop("/environment/icing/clouds/cloud0") or 0;
270 cloud1 = getprop("/environment/icing/clouds/cloud1") or 0;
271 cloud2 = getprop("/environment/icing/clouds/cloud2") or 0;
272 cloud3 = getprop("/environment/icing/clouds/cloud3") or 0;
273 cloud4 = getprop("/environment/icing/clouds/cloud4")or 0;
274
275 coverage0 = getprop("/environment/metar/clouds/layer/coverage") or 0;
276 coverage1 = getprop("/environment/metar/clouds/layer[1]/coverage") or 0;
277 coverage2 = getprop("/environment/metar/clouds/layer[2]/coverage") or 0;
278 coverage3 = getprop("/environment/metar/clouds/layer[3]/coverage") or 0;
279 coverage4 = getprop("/environment/metar/clouds/layer[4]/coverage") or 0;
280
281 var position = getprop("/position/altitude-ft") or 0;
282
283 if (((coverage0 == "overcast") and (position > cloud0) and (position < (cloud0 + 2500)) or ((coverage0 == "broken") and (position > cloud0) and (position < (cloud0 + 2000))) or ((coverage0 == "scattered") and (position > cloud0) and (position < (cloud0 + 1500))))
284 or
285 ((coverage1 == "overcast") and (position > cloud1) and (position < (cloud1 + 2500)) or ((coverage0 == "broken") and (position > cloud1) and (position < (cloud1 + 2000))) or ((coverage0 == "scattered") and (position > cloud0) and (position < (cloud0 + 1500))))
286 or
287 ((coverage2 == "overcast") and (position > cloud2) and (position < (cloud2 + 2500)) or ((coverage2 == "broken") and (position > cloud2) and (position < (cloud2 + 2000))) or ((coverage0 == "scattered") and (position > cloud0) and (position < (cloud0 + 1500))))
288 or
289 ((coverage3 == "overcast") and (position > cloud30) and (position < (cloud3 + 2500)) or ((coverage3 == "broken") and (position > cloud3) and (position < (cloud3 + 2000))) or ((coverage0 == "scattered") and (position > cloud0) and (position < (cloud0 + 1500))))
290 or
291 ((coverage4 == "overcast") and (position > cloud4) and (position < (cloud4 + 2500)) or ((coverage4 == "broken") and (position > cloud4) and (position < (cloud4 + 2000))) or((coverage0 == "scattered") and (position > cloud0) and (position < (cloud0 + 1500)))) )
292 {
293 setprop("/environment/icing/InCloudIcing/detected", 1);
294 } else{
295 setprop("/environment/icing/InCloudIcing/detected", 0);
296 }
297 settimer(d, 0.0);
298 }
299
300 d();
301
302 #####################################################################
303 # the time triggered loop
304 #####################################################################
305 var elapsedTimeNode = props.globals.getNode( "/sim/time/elapsed-sec" );
306 var lastUpdate = 0.0;
307 var icing = func {
308
309 var temperature = temperatureN.getValue();
310
311 #var rain = rainN.getValue();
312 #var snow = snowN.getValue();
313 var severity = ICING_NONE;
314 icingFactorN.setDoubleValue( ICING_FACTOR[severity] );
315
316 var visibility = 0;
317 if( visibilityN != nil ) {
318
319 #var visibility_factor = 0;
320
321 var InCloudIcing = getprop("/environment/icing/InCloudIcing/detected" ) or 0;
322 if (InCloudIcing ==1){
323 setprop("/environment/icing/InCloudIcing/visibilityfactor", 0.01);
324 }else{
325 setprop("/environment/icing/InCloudIcing/visibilityfactor", 1);
326 }
327
328 visibilityE = visibilityN.getValue();
329 visibility = visibilityE * (getprop ("/environment/icing/InCloudIcing/visibilityfactor") or 0);
330 }
331
332 # check if we should create some ice
333 var spread = temperature - dewpointN.getValue();
334 if( spread < maxSpreadN.getValue() and visibility < 5000 ) {
335 for( var i = 0; i < size(ICING_TEMPERATURE); i = i + 1 ) {
336 if( ICING_TEMPERATURE[i][0] > temperature and ICING_TEMPERATURE[i][1] <= temperature ) {
337 var s1 = ICING_TEMPERATURE[i][2];
338 var s2 = ICING_TEMPERATURE[i][3];
339 var ds = s2 - s1 + 1;
340 severity = s1 + int(rand()*ds);
341 icingFactorN.setDoubleValue( ICING_FACTOR[severity] );
342 break;
343 }
344 }
345 } else {
346 # clear air
347 # melt ice if above freezing temperature
348 # the warmer, the faster. Lets guess that at 10degc
349 # 0.5 inch goes in 10miles
350 if( temperature > 0.0 ) {
351 icingFactorN.setDoubleValue( factor = -0.05 * temperature / 10.0 );
352 }
353 # if temperature below zero, sublimating factor is initialized
354 }
355
356 if (! getprop("/sim/model/c172p/enable-fog-frost") ) {
357 # insta de-ice if option is disabled
358 icingFactorN.setDoubleValue( -100000 );
359 }
360
361 setSeverity( severity );
362
363 # update all sensitive areas
364 var now = elapsedTimeNode.getValue();
365 var dt = now - lastUpdate;
366 foreach( var iceable; iceSensitiveElements ) {
367 iceable.update( dt, dt * speedN.getValue()/3600.0, icingFactorN.getValue() );
368 }
369
370 lastUpdate = now;
371 settimer( icing, 2 );
372 }
373
374 #####################################################################
375 # start our icemachine
376 # don't care if there is nothing to put ice on
377 #####################################################################
378 if( iceSensitiveElements != nil ) {
379 lastUpdate = elapsedTimeNode.getValue();
380 icing();
381 }
382 #####################################################################