source: palm/trunk/EXAMPLES/agents/canyon/OUTPUT/ani_agt.py @ 3159

Last change on this file since 3159 was 3159, checked in by sward, 3 years ago

Added multi agent system

File size: 2.1 KB
Line 
1#!/usr/bin/env python3
2
3import matplotlib
4import sys
5from matplotlib import colors, ticker, cm
6from matplotlib.backends.backend_pdf import PdfPages
7from netCDF4 import Dataset
8import matplotlib.pyplot as plt
9import numpy as np
10import math
11from matplotlib import animation
12import matplotlib.gridspec as gridspec
13from matplotlib.lines import Line2D
14from matplotlib.collections import PatchCollection
15from matplotlib.patches import Polygon
16matplotlib.rcParams['text.usetex'] = True
17
18
19input_file_name  = './canyon_agt.001.nc'
20
21file_content     = Dataset(input_file_name, mode='r') # load entire netCDF file
22print(file_content)
23time          = file_content.variables['time'][:] # extract variable
24agx           = file_content.variables['ag_x'][:,:]
25agy           = file_content.variables['ag_y'][:,:]
26grp           = file_content.variables['ag_group'][:,:]
27file_content.close() # close netCDF file
28
29input_file_name  = './topo.txt'
30f     = open(input_file_name, mode='r')
31data = []
32building_id = []
33vertex_id = []
34polygon = []
35gons = []
36p_last = 1
37for line in f:
38  line = line.strip()
39  columns = line.split()
40  if (int(columns[0]) > p_last):
41    gons.append(polygon)
42    polygon = []
43  polygon.append([float(columns[2]),float(columns[3])])
44  p_last = int(columns[0])
45gons.append(polygon)
46nop = int(columns[0])
47patches = []
48
49for i in range(nop):
50  poly = Polygon(gons[i-1], True)
51  patches.append(poly)
52p = PatchCollection(patches,facecolors='grey')
53
54Nt = np.shape(time)[0]
55print(Nt)
56Nt = Nt -1
57Nags = np.shape(agx[1,:])
58Nx = 40
59Ny = 40
60
61x = np.linspace(0.0, 40.0, Nx)
62y = np.linspace(0.0, 40.0, Ny)
63X, Y = np.meshgrid(x, y)
64
65fig,ax = plt.subplots()
66fig.set_size_inches(4., 8.)
67xrange = [10, 30]
68yrange = [0, 40]
69
70
71def animate(i):
72    ax.clear()
73    ax.add_collection(p)
74    x = agx[i,:]
75    y = agy[i,:]
76    ax.set_xlim(*xrange)
77    ax.set_ylim(*yrange)
78    ax.scatter(x,y,s=15,c=grp[i,:])
79    plt.title(r'simulated time: %i s' % time[i])
80    k = i/Nt*100.
81    print("Done: %6.2f percent\r" % k, file=sys.stdout, end=" ")
82    return ax,
83
84anim = animation.FuncAnimation(fig, animate, frames=Nt, interval=60, blit=False,save_count=0)
85anim.save('ags.mp4', dpi=200, writer='ffmpeg')
Note: See TracBrowser for help on using the repository browser.