Пример #1
0
    def integracao(self, tempo):
        '''(Nbody, float) -> None
        Realiza integracao numerica da forca gravitacional em Nbody
        '''
        t = 0
        while t < tempo and self.n > 1:
            for i in range(self.n):
                body = self.bodies[i]

                # somatoria de forcas devido as demais particulas
                f_res = Vetor()
                for j in range(self.n):
                    if i != j:
                        f_res += body.gravity(self.bodies[j])

                # calculando impulso nesse corpo
                dp = f_res.multiply(dt)

                # calculando velocidade atual
                body.p = body.p + dp
                body.v = body.p.multiply(1 / body.m)

                # calculando posicao atual (na pos auxiliar)
                body.r_ = body.r + body.v.multiply(dt)

            # atualiza posicoes das particulas
            for body in self.bodies:
                body.r = body.r_

            # trata as colisoes
            #self.colisoes()

            t += dt
            # atualiza animacao
            self.atualiza_anim(t)
Пример #2
0
 def mass_center(self):
     '''(Nbody) -> 3-tuple, float
     Retorna a posicao do centro de massa e massa total
     '''
     s, m = Vetor(), 0
     for body in self.bodies:
         s += body.r.multiply(body.m)
         m += body.m
     c = s.multiply(1 / m)
     return c.to_list()
Пример #3
0
class Particula:
    """Classe Partícula"""
    def __init__(self, lbl, r=(0, 0, 0), v=(0, 0, 0), mass=1, color=None):
        '''(Particula, str, 3-tuple, 3-tuple, float) -> None
        O padrão é uma "partícula" na posição origem
        '''
        self.label = lbl
        # vetor posição
        self.r = Vetor(r)
        # vetor velocidade
        self.v = Vetor(v)
        # massa
        self.m = mass
        # vetor momentum
        self.p = self.v.multiply(self.m)
        # vetor posição auxiliar
        self.r_ = Vetor(r)
        # cor
        self.cor = color

    def __str__(self):
        '''(Particula) -> str'''
        txt = "%s, %s, %s, %.14f" % (self.label, self.r, self.v, self.m)
        return txt

    def __eq__(self, other):
        return self.label == other.label

    def __ne__(self, other):
        return not self == other

    def __gt__(self, other):
        return self.p > other.p

    def __lt__(self, other):
        return self.p < other.p

    def gravity(self, other):
        '''(Particula, Particula) -> Vetor
        Retorna o vetor força gravitacional em self causada por other
        g = G * m1 * m2 / r^2 * r (r é o vetor posicao)
        '''
        # vetor posição
        r = other.r - self.r
        # intensidade da força
        f = G * (self.m * other.m / r.modulo2())
        # retorna vetor força
        return r.unit().multiply(f)