/
galaxia.py
122 lines (92 loc) · 4.25 KB
/
galaxia.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#! /usr/bin/python2
# -*- coding: utf-8 -*-
# Using AC for the construction of galaxies
import numpy as np
import scipy.stats.distributions as dis
from mayavi import mlab
from itertools import repeat, izip, ifilter
#import pylab as pl
class Star:
"Clase para definir una Estrella"""
def __init__(self, r, angle, state=0):
"""Constructor de estrellas """
self.r = r
self.angle = angle
self.state = state
class Galaxy:
"""Clase galaxia"""
def __init__(self, radio, neighborhood):
"""Initialize the galaxy with some radius
and the chosen neighborhood """
self.distributionFuncStars = dis.rv_discrete(name='custm', values=[(0, 1),
(0.84, 0.16)])
self.distributionGas = dis.rv_discrete(name='custm_gas', values=[(0,1),(0.3,0.7)])
stars = []
stars.append(Star(0, neighborhood[0], self.distributionFuncStars.rvs()))
try:
for r in np.arange(1, radio + 1,0.5): # para regular el tamaño de la celda
map(lambda a: stars.append(Star(a[1], a[0] / float(a[1]),
self.distributionFuncStars.rvs())), izip(neighborhood,
repeat(r, len(neighborhood))))
self.stars = stars
except e:
print e
def birthFunction(self, star):
"""Initialize one star"""
star[0].state = self.distributionFuncStars.rvs()
#print "nacio otra estrella"
def growthFunction(self, star):
"""Increases growth of each star"""
if star.state > 8:
star.state = 0
star.state += 1
try:
#set angular velocity
star.angle = star.angle + 1 / float(star.r)
except ZeroDivisionError:
#just for save the day :)
star.angle = star.angle + 1
def scanning(self):
"""See state of stars and change formations"""
for s in self.stars:
localNeighborhood = ifilter(lambda star: star[1] == star[0].r + 1,
izip(self.stars, repeat(s.r, len(self.stars))))
map(self.birthFunction, localNeighborhood)
#aqui envejecemos la galaxia
map(self.growthFunction, self.stars)
def plottingStars(self, colors):
"""For plotting the stars"""
x = []
y = []
activeStars = filter(lambda s: s.state != 0, self.stars)
map(lambda star: x.append(float(star.r * np.cos(star.angle))), activeStars)
map(lambda star: y.append(float(star.r * np.sin(star.angle))), activeStars)
return mlab.points3d(x, y, x, resolution=20, scale_factor=.5, scale_mode='none')
def countOfActiveStars(self):
"""Show counter of stars"""
activeStars = ifilter(lambda s: s.state != 0, self.stars)
noActiveStars = ifilter(lambda s: s.state == 0, self.stars)
return len(list(activeStars)), len(list(noActiveStars))
def plottingInterstellarGas(self):
"""Insert randomness in the production of the galaxy """
x = []; y = []
activeGas = filter( lambda s: self.distributionGas.rvs() == 1, self.stars)
map(lambda star: x.append(float(star.r * np.cos(star.angle))), activeGas)
map(lambda star: y.append(float(star.r * np.sin(star.angle))), activeGas)
return mlab.points3d(x, y, x, color = (0.8,0.5,0), resolution=20, scale_factor=.5, scale_mode='none')
if __name__ == "__main__":
neighborhood = [0, 60, 120, 180, 240, 330]
colors = {1:(1,1,0.9), 2:(1,1,0.6), 3:(1,1,0.4), 4:(1,1,0.2),
5:(1,1,0), 6:(1,0.8,0), 7:(1,0.5,0), 8:(1,0.2,0), 9:(1,0,0)}
myGalaxy = Galaxy(50, neighborhood)
print "al comienzo tenemos ", myGalaxy.countOfActiveStars()
for t in range(100):
myGalaxy.scanning()
points = myGalaxy.plottingStars(colors)
otherpoints = myGalaxy.plottingInterstellarGas()
print "al final tenemos ", myGalaxy.countOfActiveStars()
print points
mlab.pipeline.volume(mlab.pipeline.gaussian_splatter(points))
mlab.pipeline.volume(mlab.pipeline.gaussian_splatter(otherpoints))
mlab.view(49, 31.5, 52.8, (4.2, 37.3, 20.6))
mlab.show()