Debian Codegraph @

Nasal/local_weather/weather_dynamics.nas

raw
1 ########################################################
2 # routines to simulate cloud wind drift and evolution
3 # Thorsten Renk, October 2010
4 ########################################################
5
6 # function purpose
7 #
8 # get_windfield to get the current wind in the tile
9 # timing_loop to provide accurate timing information for wind drift calculations
10 # quadtree_loop to manage drift of clouds in the field of view
11 # weather_dynamics_loop to manage drift of weather effects, tile centers and interpolation points
12 # convective_loop to regularly recreate convective clouds
13 # generate_quadtree_structure to generate a quadtree data structure used for managing the visual field
14 # sort_into_quadtree to sort objects into a quadtree structure
15 # sorting_recursion to recursively sort into a quadree (helper)
16 # quadtree_recursion to search the quadtree for objects in the visual field
17 # check_visibility to check if a quadrant is currently visible
18 # move_tile to move tile coordinates in the wind
19 # get_cartesian to get local Cartesian coordinates out of coordinates
20
21
22 ####################################################
23 # get the windfield for a given location and altitude
24 # (currently constant, but supposed to be local later)
25 ####################################################
26
27
28 var get_windfield = func (tile_index) {
29
30
31 if (hardcoded_clouds_flag == 1)
32 {
33 var wind_direction = local_weather.wind.current[0];
34 var windspeed = local_weather.wind.current[1] * kt_to_ms;
35
36 var windfield_x = -windspeed * math.sin(wind_direction * math.pi/180.0);
37 var windfield_y = -windspeed * math.cos(wind_direction * math.pi/180.0);
38
39 return [windfield_x,windfield_y];
40 }
41
42
43
44
45
46 if ((local_weather.wind_model_flag == 1) or (local_weather.wind_model_flag == 3))
47 {
48 var windspeed = tile_wind_speed[0] * kt_to_ms;
49 var wind_direction = tile_wind_direction[0];
50 }
51 else if ((local_weather.wind_model_flag ==2) or (local_weather.wind_model_flag == 4) or (local_weather.wind_model_flag == 5))
52 {
53 var windspeed = tile_wind_speed[tile_index-1] * kt_to_ms;
54 var wind_direction = tile_wind_direction[tile_index-1];
55 }
56
57
58
59 var windfield_x = -windspeed * math.sin(wind_direction * math.pi/180.0);
60 var windfield_y = -windspeed * math.cos(wind_direction * math.pi/180.0);
61
62 return [windfield_x,windfield_y];
63 }
64
65
66 var get_wind_direction = func (tile_index) {
67
68 if (hardcoded_clouds_flag == 1)
69 {
70 return local_weather.wind.current[0];
71 }
72
73 if ((local_weather.wind_model_flag == 1) or (local_weather.wind_model_flag == 3))
74 {
75 return tile_wind_direction[0];
76 }
77 else if ((local_weather.wind_model_flag ==2) or (local_weather.wind_model_flag == 4) or (local_weather.wind_model_flag == 5))
78 {
79 return tile_wind_direction[tile_index-1];
80 }
81
82 }
83
84 var get_wind_speed = func (tile_index) {
85
86 if (hardcoded_clouds_flag == 1)
87 {
88 return local_weather.wind.current[1];
89 }
90
91 if ((local_weather.wind_model_flag == 1) or (local_weather.wind_model_flag == 3))
92 {
93 return tile_wind_speed[0];
94 }
95 else if ((local_weather.wind_model_flag ==2) or (local_weather.wind_model_flag == 4) or (local_weather.wind_model_flag == 5))
96 {
97 return tile_wind_speed[tile_index-1];
98 }
99
100 }
101
102 ########################################################
103 # timing loop
104 # this gets the accurate time since the start of weather dynamics
105 # and hence the timestamps for cloud evolution since
106 # the available elapsed-time-sec is not accurate enough
107 ########################################################
108
109 var timing_loop = func {
110
111 if (local_weather.local_weather_running_flag == 0) {return;}
112
113 dt_lw = getprop("/sim/time/delta-sec");
114 time_lw = time_lw + dt_lw;
115
116 # this is a really ugly hack to get the sun angle information to the shaders
117 # directly referencing /sim/time/sun-angle-rad as uniform doesn't
118 # work since that is a tied property
119
120 #var sun_angle = 1.57079632675 - getprop("/sim/time/sun-angle-rad");
121
122 #var terminator_offset = sun_angle / 0.017451 * 110000.0;# + 250000.0;
123 #setprop("/environment/terminator-relative-position-m",terminator_offset);
124
125 var viewpos = geo.viewer_position();
126
127 # setprop("/environment/alt-in-haze-m", getprop("/environment/ground-haze-thickness-m")-viewpos.alt());
128
129 #setprop("/sim/rendering/eye-altitude-m", viewpos.alt());
130
131
132
133 if (local_weather.presampling_flag == 1)
134 {
135 var mean_terrain_elevation_m = ft_to_m * local_weather.current_mean_alt ; }
136 else
137 {var mean_terrain_elevation_m = 0.0;}
138
139 setprop("/environment/mean-terrain-elevation-m", mean_terrain_elevation_m);
140
141 if (getprop(lw~"timing-loop-flag") ==1) {settimer(timing_loop, 0);}
142
143 }
144
145
146 ###########################################################
147 # quadtree loop
148 # the quadtree loop is a fast loop updating the position
149 # of visible objects in the field of view only
150 ###########################################################
151
152 var quadtree_loop = func {
153
154 if (local_weather.local_weather_running_flag == 0) {return;}
155
156 var vangle = 0.55 * getprop("/sim/current-view/field-of-view");
157 var viewdir = getprop("/sim/current-view/goal-heading-offset-deg");
158 var lat = getprop("position/latitude-deg");
159 var lon = getprop("position/longitude-deg");
160 var course = getprop("orientation/heading-deg");
161
162 cloud_counter = 0;
163
164 # pre-calculate trigonometry
165
166 tan_vangle = math.tan(vangle * math.pi/180.0);
167
168
169
170 # use the quadtree to move clouds inside the field of view
171
172 var tiles = props.globals.getNode(lw~"tiles").getChildren("tile");
173
174
175 foreach (var t; tiles)
176 {
177 var generated_flag = t.getNode("generated-flag").getValue();
178
179 if ((generated_flag == 1) or (generated_flag ==2))
180 {
181 var index = t.getNode("tile-index").getValue();
182 current_tile_index_wd = index;
183
184 var blat = t.getNode("latitude-deg").getValue();
185 var blon = t.getNode("longitude-deg").getValue();
186 var alpha = t.getNode("orientation-deg").getValue();
187 var xy_vec = get_cartesian(blat, blon, alpha, lat, lon);
188
189 var beta = course - alpha - viewdir ;
190 cos_beta = math.cos(beta * math.pi/180.0);
191 sin_beta = math.sin(beta * math.pi/180.0);
192 plane_x = xy_vec[0]; plane_y = xy_vec[1];
193
194 windfield = get_windfield(index);
195
196 quadtree_recursion(cloudQuadtrees[index-1],0,1,0.0,0.0);
197 }
198
199 }
200
201
202 # dynamically adjust the range of the processed view field
203 # if there are plenty of moving clouds nearby, no one pays attention to the small motion of distant clouds
204 # price to pay is that some clouds appear to jump once they get into range
205
206 if (cloud_counter < 0.5 * max_clouds_in_loop) {view_distance = view_distance * 1.1;}
207 else if (cloud_counter > max_clouds_in_loop) {view_distance = view_distance * 0.9;}
208 if (view_distance > weather_tile_management.cloud_view_distance) {view_distance = weather_tile_management.cloud_view_distance;}
209
210 #print(cloud_counter, " ", view_distance/1000.0);
211
212 # shift the tile centers with the windfield
213
214 var tiles = props.globals.getNode("local-weather/tiles", 1).getChildren("tile");
215 foreach (var t; tiles) {move_tile(t);}
216
217
218
219
220 # loop over
221
222 if (getprop(lw~"dynamics-loop-flag") ==1) {settimer(quadtree_loop, 0);}
223 }
224
225
226 ###########################################################
227 # weather_dynamics_loop
228 # the weather dynamics loop is a slow loop updating
229 # position and state of invisible objects, currently
230 # effect volumes and weather stations
231 ###########################################################
232
233
234
235 var weather_dynamics_loop = func (index, cindex) {
236
237 if (local_weather.local_weather_running_flag == 0) {return;}
238
239 var n = 20;
240 var nc = 1;
241
242 var csize = weather_tile_management.n_cloudSceneryArray;
243
244 var i_max = index + n;
245 if (i_max > local_weather.n_effectVolumeArray) {i_max = local_weather.n_effectVolumeArray;}
246
247 var ecount = 0;
248
249 for (var i = index; i < i_max; i = i+1)
250 {
251 var ev = local_weather.effectVolumeArray[i];
252 if (ev.index !=0)
253 {ev.move();}
254 if ((ev.lift_flag == 2) and (rand() < 0.05) and (local_weather.presampling_flag == 1))
255 {
256 if (local_weather.dynamical_convection_flag ==1)
257 {
258 ev.correct_altitude_and_age();
259
260 if (ev.flt > 1.2) # beyond 1.0, sink is still active
261 {
262 local_weather.effectVolumeArray = weather_tile_management.delete_from_vector(local_weather.effectVolumeArray,i);
263 local_weather.n_effectVolumeArray = local_weather.n_effectVolumeArray - 1;
264 i = i-1; i_max = i_max -1; ecount = ecount + 1;
265 }
266
267 }
268 else
269 {ev.correct_altitude();}
270 }
271 }
272 setprop(lw~"effect-volumes/number",getprop(lw~"effect-volumes/number")- ecount);
273
274 index = index + n;
275 if (i >= local_weather.n_effectVolumeArray) {index = 0;}
276
277
278 var ccount = 0;
279
280 if (csize > 0)
281 {
282
283 var j_max = cindex + nc;
284 if (j_max > csize -1) {j_max = csize-1;}
285
286
287 for (var j = cindex; j < j_max; j = j+1)
288 {
289 var cs = weather_tile_management.cloudSceneryArray[j];
290 #cs.move();
291 if (cs.type !=0)
292 {
293 if ((rand() < 0.1) and (local_weather.presampling_flag == 1))
294 {
295 if (local_weather.dynamical_convection_flag ==1)
296 {
297 cs.correct_altitude_and_age();
298 if (cs.flt > 1.0) # the cloud has reached its maximum age and decays
299 {
300 cs.removeNodes();
301 weather_tile_management.cloudSceneryArray = weather_tile_management.delete_from_vector(weather_tile_management.cloudSceneryArray,j);
302 ccount = ccount + 1;
303 }
304 }
305 else
306 {
307 cs.correct_altitude();
308 }
309 }
310 }
311 }
312
313 cindex = cindex + nc;
314 if (j >= csize) {cindex = 0;}
315 }
316
317
318
319 foreach (var s; local_weather.weatherStationArray)
320 {
321 s.move();
322 }
323
324 foreach (var a; local_weather.atmosphereIpointArray)
325 {
326 a.move();
327 }
328
329 if (getprop(lw~"dynamics-loop-flag") ==1) {settimer( func {weather_dynamics_loop(index, cindex); },0);}
330
331 }
332
333
334 ###########################################################
335 # convective evolution loop
336 ###########################################################
337
338 var convective_loop = func {
339
340 if (local_weather.local_weather_running_flag == 0) {return;}
341
342 # a 30 second loop needs a different strategy to end, otherwise there is trouble if it is restarted while still running
343
344 if (convective_loop_kill_flag == 1)
345 {convective_loop_kill_flag = 0; return;}
346
347 var cloud_respawning_interval_s = 30.0;
348
349
350 if (getprop(lw~"tmp/thread-status") == "placing")
351 {if (getprop(lw~"convective-loop-flag") ==1) {settimer( func {convective_loop()}, 5.0);} return;}
352
353 # open the system for write status
354 setprop(lw~"tmp/buffer-status","placing");
355
356 if (local_weather.debug_output_flag == 1)
357 {print("Respawning convective clouds...");}
358
359 for(var i = 0; i < 9; i = i + 1)
360 {
361 var index = getprop(lw~"tiles/tile["~i~"]/tile-index");
362 if ((index == -1) or (index == 0)) {continue;}
363 if (getprop(lw~"tiles/tile["~i~"]/generated-flag") != 2)
364 {continue;}
365
366 var strength = tile_convective_strength[index-1];
367 var alt = tile_convective_altitude[index-1];
368 var n = weather_tiles.get_n(strength);
369 if (local_weather.detailed_clouds_flag == 1)
370 {n = int(0.7 * n);}
371
372 n = n/cloud_convective_lifetime_s * cloud_respawning_interval_s * math.sqrt(0.35);
373
374 var n_res = n - int(n);
375 n = int(n);
376 if (rand() < n_res) {n=n+1;}
377
378 if (local_weather.debug_output_flag == 1)
379 {print("Tile: ", index, " n: ", n);}
380
381 var lat = getprop(lw~"tiles/tile["~i~"]/latitude-deg");
382 var lon = getprop(lw~"tiles/tile["~i~"]/longitude-deg");
383 var alpha = getprop(lw~"tiles/tile["~i~"]/orientation-deg");
384
385 compat_layer.buffered_tile_latitude = lat;
386 compat_layer.buffered_tile_longitude = lon;
387 compat_layer.buffered_tile_alpha = alpha;
388 compat_layer.buffered_tile_index = index;
389
390 setprop(lw~"tmp/buffer-tile-index", index);
391
392 if (local_weather.presampling_flag == 1)
393 {var alt_offset = local_weather.alt_20_array[index -1];}
394 else
395 {var alt_offset = getprop(lw~"tmp/tile-alt-offset-ft");}
396
397 local_weather.recreate_cumulus(lat,lon, alt + alt_offset, alpha, n, 20000.0, index);
398
399 }
400
401 # close the write process
402 setprop(lw~"tmp/buffer-status","idle");
403
404
405
406 if (getprop(lw~"convective-loop-flag") ==1) {settimer(convective_loop, cloud_respawning_interval_s);}
407
408 }
409
410 ###########################################################
411 # generate quadtree structure
412 ###########################################################
413
414 var generate_quadtree_structure = func (depth, tree_base_vec) {
415
416 var c_vec = [];
417
418 for (var i=0; i<4; i=i+1)
419 {
420 if (depth == quadtree_depth)
421 {var c = [];}
422 else
423 {var c = generate_quadtree_structure(depth+1, tree_base_vec);}
424
425 if (depth==0)
426 {append(tree_base_vec,c); }
427 else
428 {append(c_vec,c); }
429 }
430
431 if (depth ==0) {return tree_base_vec;} else {return c_vec;}
432
433 }
434
435
436 ###########################################################
437 # sort into quadtree
438 ###########################################################
439
440 var sort_into_quadtree = func (blat, blon, alpha, lat, lon, tree, object) {
441
442 var xy_vec = get_cartesian (blat, blon, alpha, lat, lon);
443
444 sorting_recursion (xy_vec[0], xy_vec[1], tree, object, 0);
445
446 }
447
448
449 var sorting_recursion = func (x, y, tree, object, depth) {
450
451 if (depth == quadtree_depth+1) {append(tree,object); return;}
452
453 var length_scale = 20000.0 / math.pow(2,depth);
454
455 # print("depth: ", depth, "x: ", x, "y: ",y);
456
457 if (y > 0.0)
458 {
459 if (x < 0.0)
460 {var v = tree[0]; x = x + 0.5 * length_scale; y = y - 0.5 * length_scale;}
461 else
462 {var v = tree[1]; x = x - 0.5 * length_scale; y = y - 0.5 * length_scale;}
463 }
464 else
465 {
466 if (x < 0.0)
467 {var v = tree[2]; x = x + 0.5 * length_scale; y = y + 0.5 * length_scale;}
468 else
469 {var v = tree[3]; x = x - 0.5 * length_scale; y = y + 0.5 * length_scale;}
470 }
471
472 sorting_recursion(x, y, v, object, depth+1);
473
474 }
475
476
477 ####################################################
478 # quadtree recursive search
479 ####################################################
480
481 var quadtree_recursion = func (tree, depth, flag, qx, qy) {
482
483 # flag = 0: quadrant invisible, stop search
484 # flag = 1: quadrant partially visible, continue search with visibility tests
485 # flag = 2: quadrant fully visible, no further visibility test needed
486
487
488 if (depth == quadtree_depth +1)
489 {
490 foreach (var c; tree)
491 {
492 c.move();
493 c.to_target_alt();
494 cloud_counter = cloud_counter + 1;
495 }
496 return;
497 }
498
499
500
501 for (var i =0; i<4; i=i+1)
502 {
503 if (flag==2) {quadtree_recursion(tree[i], depth+1, flag, qx, qy);}
504 else if (flag==1)
505 {
506 # compute the subquadrant coordinates
507 var length_scale = 20000.0 / math.pow(2,depth);
508 if (i==0) {var qxnew = qx - 0.5 * length_scale; var qynew = qy + 0.5 * length_scale;}
509 else if (i==1) {var qxnew = qx + 0.5 * length_scale; var qynew = qy + 0.5 * length_scale;}
510 else if (i==2) {var qxnew = qx - 0.5 * length_scale; var qynew = qy - 0.5 * length_scale;}
511 else if (i==3) {var qxnew = qx + 0.5 * length_scale; var qynew = qy - 0.5 * length_scale;}
512
513
514 var newflag = check_visibility(qxnew,qynew, length_scale);
515
516 if (newflag!=0) {quadtree_recursion(tree[i], depth+1, newflag, qxnew, qynew);}
517 }
518 }
519
520 }
521
522 ####################################################
523 # quadrant visibility test
524 ####################################################
525
526 var check_visibility = func (qx,qy, length_scale) {
527
528 # (qx,qy) are the quadrant coordinates in tile local Cartesian
529 # beta is the plane course in the tile local Cartesian
530
531 # the function returns a flag: 0: invisible 1: partially visible, track further 2: fully visible
532
533
534
535 # first translate/rotate (qx,qy) into the plane system
536
537 qx = qx - plane_x; qy = qy - plane_y;
538
539 var x = qx * cos_beta - qy * sin_beta;
540 var y = qy * cos_beta + qx * sin_beta;
541
542 # now get the maximum and minimum quadrant extensions
543
544 var ang_factor = abs(cos_beta) + abs(sin_beta); # a square seen from an angle extends larger
545
546 var xmax = x + 0.5 * length_scale * ang_factor;
547 var xmin = x - 0.5 * length_scale * ang_factor;
548
549 var ymax = y + 0.5 * length_scale * ang_factor;
550 var ymin = y - 0.5 * length_scale * ang_factor;
551
552 # now do visibility checks
553
554 if ((ymax < 0.0) and (ymin < 0.0)) # quadrant is behind us, we can never see it
555 {return 0;}
556
557 if (ymin > view_distance) # the quadrant is beyond visible range
558 {return 0;}
559
560 var xcomp_min = ymin * tan_vangle;
561 var xcomp_max = ymax * tan_vangle;
562
563 if ((ymax > 0.0) and (ymin < 0.0)) # object is at most partially visible, check if in visual cone at ymax
564 {
565 if ((xmax < -xcomp_max) and (xmin < -xcomp_max)) {return 0;}
566 if ((xmax > xcomp_max) and (xmin > xcomp_max)) {return 0;}
567 return 1;
568 }
569
570 # now we know the quadrant must be in front
571
572 # check if invisible
573
574 if ((xmax < -xcomp_max) and (xmin < -xcomp_max)) {return 0;}
575 if ((xmax > xcomp_max) and (xmin > xcomp_max)) {return 0;}
576
577 # check if completely visible
578
579 if ((xmax > -xcomp_min) and (xmin > -xcomp_min) and (xmax < xcomp_min) and (xmin < xcomp_min))
580 {return 2;}
581
582 # at this point, it must be partially visible
583
584 return 1;
585 }
586
587 ####################################################
588 # move a tile
589 ####################################################
590
591 var move_tile = func (t) {
592
593 # get the old spacetime position of the tile
594
595 var lat_old = t.getNode("latitude-deg").getValue();
596 var lon_old = t.getNode("longitude-deg").getValue();
597 var timestamp = t.getNode("timestamp-sec").getValue();
598
599 var tile_index = t.getNode("tile-index").getValue();
600
601 # if the tile is not yet generated, we use the windfield of the tile we're in
602
603 if (tile_index == -1)
604 {
605 tile_index = props.globals.getNode(lw~"tiles").getChild("tile",4).getNode("tile-index").getValue();
606 }
607
608 # get windfield and time since last update
609
610 var windfield = get_windfield(tile_index);
611 var dt = time_lw - timestamp;
612
613
614 # update the spacetime position of the tile
615
616 t.getNode("latitude-deg",1).setValue(lat_old + windfield[1] * dt * local_weather.m_to_lat);
617 t.getNode("longitude-deg",1).setValue(lon_old + windfield[0] * dt * local_weather.m_to_lon);
618 t.getNode("timestamp-sec",1).setValue(weather_dynamics.time_lw);
619
620 }
621
622
623
624 ###########################################################
625 # get local Cartesian coordinates
626 ###########################################################
627
628 var get_cartesian = func (blat, blon, alpha, lat, lon) {
629
630 var xy_vec = [];
631
632 var phi = alpha * math.pi/180.0;
633
634 var delta_lat = lat - blat;
635 var delta_lon = lon - blon;
636
637 var x1 = delta_lon * lon_to_m;
638 var y1 = delta_lat * lat_to_m;
639
640 var x = x1 * math.cos(phi) - y1 * math.sin(phi);
641 var y = y1 * math.cos(phi) + x1 * math.sin(phi);
642
643 append(xy_vec,x);
644 append(xy_vec,y);
645
646 return xy_vec;
647
648 }
649
650
651 ################################
652 # globals, constants, properties
653 ################################
654
655
656
657 var lat_to_m = 110952.0; # latitude degrees to meters
658 var m_to_lat = 9.01290648208234e-06; # meters to latitude degrees
659 var ft_to_m = 0.30480;
660 var m_to_ft = 1.0/ft_to_m;
661 var inhg_to_hp = 33.76389;
662 var hp_to_inhg = 1.0/inhg_to_hp;
663
664 var kt_to_ms = 0.514;
665 var ms_to_kt = 1./kt_to_ms;
666
667 var lon_to_m = 0.0; # needs to be calculated dynamically
668 var m_to_lon = 0.0; # we do this on startup
669
670 # abbreviations
671
672 var lw = "/local-weather/";
673
674
675 # globals
676
677 var time_lw = 0.0;
678 var dt_lw = 0.0;
679 var max_clouds_in_loop = 250;
680 var cloud_max_vertical_speed_fts = 30.0;
681 var cloud_convective_lifetime_s = 1800.0; # max. lifetime of convective clouds
682
683 var convective_loop_kill_flag = 0;
684
685 # the quadtree structure
686
687 var cloudQuadtrees = [];
688 var quadtree_depth = 3;
689
690 # the wind info for the individual weather tiles
691 # (used for 'constant in tile' wind model)
692
693 var tile_wind_direction = [];
694 var tile_wind_speed = [];
695 var tile_convective_altitude = [];
696 var tile_convective_strength = [];
697
698 # define these as global, as we need to evaluate them only once per frame
699 # but use them over and over
700
701 var tan_vangle = 0;
702 var cos_beta = 0;
703 var sin_beta = 0;
704 var plane_x = 0;
705 var plane_y = 0;
706 var windfield = [];
707
708 var current_tile_index_wd = 0;
709
710 var cloud_counter = 0;
711 var view_distance = 30000.0;
712
713 # create the loop flags
714
715 setprop(lw~"timing-loop-flag",0);
716 setprop(lw~"dynamics-loop-flag",0);