source: palm/trunk/EXAMPLES/agents/corners/OUTPUT/ani_agt.py @ 3550

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

Added multi agent system

File size: 2.5 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  = './corners_agt.059.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'][:,:]
26group           = 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
54input_file_name  = './topo_nav.txt'
55f     = open(input_file_name, mode='r')
56t_x = []
57t_y = []
58
59l_count = 0
60for line in f:
61  l_count = l_count + 1
62  line = line.strip()
63  columns_t = line.split()
64  t_x.append(columns_t[2])
65  t_y.append(columns_t[3])
66
67Nt = np.shape(time)[0]
68print(Nt)
69Nt = Nt -1
70Nags = np.shape(agx[1,:])
71Nx = 20
72Ny = 20
73
74x = np.linspace(0.0, 20.0, Nx)
75y = np.linspace(0.0, 20.0, Ny)
76X, Y = np.meshgrid(x, y)
77
78fig,ax = plt.subplots()
79fig.set_size_inches(7., 7.)
80xrange = [0, 20]
81yrange = [0, 20]
82colors = ['yellow','red','blue']
83
84
85def animate(i):
86    ax.clear()
87    ax.add_collection(p)
88    x = agx[i,:]
89    y = agy[i,:]
90    ax.set_xlim(*xrange)
91    ax.set_ylim(*yrange)
92    ax.scatter(x,y,s=3,c=group[i,:])
93    ax.scatter(t_x,t_y,s=1,c='red')
94    plt.title(r'simulated time: %i s' % time[i])
95    k = i/Nt*100.
96    print("Done: %6.2f percent\r" % k, file=sys.stdout, end=" ")
97    return ax,
98
99anim = animation.FuncAnimation(fig, animate, frames=Nt, interval=60, blit=False,save_count=0)
100anim.save('ags.mp4', dpi=200, writer='ffmpeg')
101#anim.save('basic_animation.gif', fps=1, extra_args=['-vcodec', 'libx264'])
102
103#plt.show()
Note: See TracBrowser for help on using the repository browser.