aboutsummaryrefslogtreecommitdiffstats
path: root/Helpers/collision_helpers.py
blob: 80dcfb67a332a6b29bd9f709a262a22b313df03e (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
"""
Collision Helpers.
"""

import numpy as np
import dynamics_helpers as dh
import vector_helpers as vh
import constants as C


def assign_ell(row):
    """
    Compute Projected Interaction Distance (X-Section) for Collisions.
    Cf. Quintana 2015, Eqn. (4), Fig. 7
    http://adsabs.harvard.edu/abs/2015arXiv151103663Q

    To call this function, use dfc.apply(assign_ell, axis='columns').
    Here, dfc is a DataFrame of Particle Collisions.

    @param: row - Pandas DataFrame Row [Series]
    @return: ell - Projected Interaction Distance (X-Section) [Numpy Float]
    """
    
    B = np.sin(row.theta*C.d2r) * ( row.ri + row.rj )
    
    if (B + row.rj) <= row.ri:
        ell = 2.0 * row.rj
    else:
        ell = row.rj + row.ri - B
    
    return ell


def compute_specific_impact_energy(dfc):
    """
    Compute the Specific Impact Energy for Collisions.
    Cf. Quintana 2015, Eqns. (1,2,3,4,5,6,7)
    http://adsabs.harvard.edu/abs/2015arXiv151103663Q

    The input collision dataframe must have been processed to include
    collisions geometries. We require impact velocities and angle.

    @param: dfc - Genga Collision DataFrame [Pandas DataFrame]
    @return: dfc - Genga Collision DataFrame [Pandas DataFrame]
    """

    # Reduce Mass, Base Specific Impact Energy
    # Eqns. (1,2)
    mu = (dfc.mi * dfc.mj) / (dfc.mi + dfc.mj)           # Msun
    Q = mu * dfc.v_impact**2.0 / 2.0 / (dfc.mi + dfc.mj) # km2/s2
    Q *= 1000.0**2.0                                     # m2/s2
                                                         # = kg m2/s2 1/kg
                                                         # = J/kg

    # Write Uncorrected Q
    dfc['Q'] = Q

    # Compute Geometry Corrected Specific Impact Energy
    # Eqns. (4,5,6,7)
    ell = dfc.apply(assign_ell, axis='columns')
    alpha = ( 3.0 * dfc.rj * ell**2.0 - ell**3.0 ) / ( 4.0 * dfc.rj**3.0 )
    mu_alpha = ( alpha * dfc.mi * dfc.mj ) / ( alpha * dfc.mj + dfc.mi )
    QPrime = Q * mu_alpha / mu

    # Write QPrime
    dfc['QPrime'] = QPrime

    # Compute Shock Wave Corrected Specific Impact Energy
    # Eqn. (3)
    Qs = QPrime * ( 1.0 + dfc.mj/dfc.mi ) * ( 1.0 - np.sin(dfc.theta * C.d2r) )

    # Write to DataFrame
    dfc['Qs'] = Qs

    # Return DataFrame
    return dfc


def reconstruct_geometries(dfc):
    """
    Reconstruct collision geometries by integrating to the boundary.

    Goes through all collisions in a collision file (dataframe), etxracts
    the geometry, integrates to the boundary, and computes impact parameter 
    and impact angle. The impact parameter is normalized to the sum of the
    bodies' radii.

    For collisions dataframe, see io_helpers.py/read_collisions_and_stack().

    We expect certain units in the collision dataframe:
    - Distances are in km.
    - Velocities in km/s.
    - Masses in Earth Masses.

    @param: dfc - Collisions Dataframe [Pandas Dataframe]
    @return: theta_all    - Impact Angles (Degree) [Numpy Float Array]
    @return: b_over_r_all - Normalised Impact Parameter (-) [Numpy Float Array]
    """

    # Compute Relative Position
    dfc['dx'] = dfc.xj - dfc.xi
    dfc['dy'] = dfc.yj - dfc.yi
    dfc['dz'] = dfc.zj - dfc.zi
    dfc['distance'] = np.sqrt(dfc['dx']**2.0 + \
                              dfc['dy']**2.0 + \
                              dfc['dz']**2.0)/(dfc.ri+dfc.rj)

    # Compute Relative Velocity
    dfc['dvx'] = dfc.vxi - dfc.vxj
    dfc['dvy'] = dfc.vyi - dfc.vyj
    dfc['dvz'] = dfc.vzi - dfc.vzj

    # Fix Units
    dfc['dx'] *= C.au2km
    dfc['dy'] *= C.au2km
    dfc['dz'] *= C.au2km
    dfc['ri'] *= C.au2km
    dfc['rj'] *= C.au2km

    theta_all = np.ones(len(dfc)) * np.nan
    b_over_r_all = np.ones(len(dfc)) * np.nan
    v_impact_all = np.ones(len(dfc)) * np.nan   
    for ii, [index, dfc_loc] in enumerate(dfc.iterrows()):

        # Debug. Only do one row.
        # if ii > 3:
        #    break

        # Distance (km)
        x1 = 0.0; y1 = 0.0; z1 = 0.0
        x2 = dfc_loc.dx; y2 = dfc_loc.dy; z2 = dfc_loc.dz

        # Velocity (km/s)
        vx1 = 0.0; vy1 = 0.0; vz1 = 0.0
        vx2 = dfc_loc.dvx; vy2 = dfc_loc.dvy; vz2 = dfc_loc.dvz

        # If vectors are aligned within 180 degree, the dot product
        # is positive. Otherwise, it is negative. For example:
        # vh.dot(2,0,0,1,1,0) ==> 2
        # vh.dot(2,0,0,-1,-1,0) ==> -2
        # We use this to determine whether the impactor is moving towards 
        # or away from the target, i.e. whether we have to integrate forward 
        # or backward.
        if vh.dot(x2, y2, z2, vx2, vy2, vz2) > 0:
            vx2 *= -1.0
            vy2 *= -1.0
            vz2 *= -1.0

        # Mass (kg)
        m1 = dfc_loc.mi * C.mearth
        m2 = dfc_loc.mj * C.mearth

        # Radius (km)
        r1 = dfc_loc.ri
        r2 = dfc_loc.rj

        # Cutoff Distance (km)
        dlo = 0.95*(r1+r2)
        dhi = 1.05*(r1+r2)

        # Estimate Time to Impacts (s), Determine Steps (-)
        dd = np.sqrt(x2**2.0 + y2**2.0 + z2**2.0)
        vv = np.sqrt(vx2**2.0 + vy2**2.0 + vz2**2.0)
        tt = dd/vv
        nsteps = 2048
        dt = tt / float(nsteps)

        # Integrate
        x1_out, y1_out, z1_out, vx1_out, vy1_out, vz1_out, \
        x2_out, y2_out, z2_out, vx2_out, vy2_out, vz2_out, terminated = \
            dh.integrate_system_3d(x1, y1, z1, vx1, vy1, vz1, \
                                   x2, y2, z2, vx2, vy2, vz2, \
                                   m1, m2, dt, 2.5 * nsteps, \
                                   G=C.G, dlo=dlo, dhi=dhi)
            
        # Extract Geometry.
        # Only if the integration managed to put the body on the boundary.
        if terminated:

            # Shift Frame
            vx = np.asarray(vx2_out) - np.asarray(vx1_out)
            vy = np.asarray(vy2_out) - np.asarray(vy1_out)
            vz = np.asarray(vz2_out) - np.asarray(vz1_out)
            x = np.asarray(x2_out) - np.asarray(x1_out)
            y = np.asarray(y2_out) - np.asarray(y1_out)
            z = np.asarray(z2_out) - np.asarray(z1_out)

            # Impact Velocity (km/s)
            v_impact = np.sqrt(vx[-1]**2.0 + vy[-1]**2.0 + vz[-1]**2.0)

            # Extract Geometry
            theta, cos_theta, sin_theta = \
                vh.compute_angle(np.atleast_1d(x[-1]), \
                                 np.atleast_1d(y[-1]), \
                                 np.atleast_1d(z[-1]), \
                                 np.atleast_1d(vx[-1]), \
                                 np.atleast_1d(vy[-1]), \
                                 np.atleast_1d(vz[-1]), \
                                 vanilla=False)
            theta = theta[0]
            b = np.abs(np.sin(theta) * vh.norm(x[-1], y[-1], z[-1]))

            # Shift Angle
            if theta < -np.pi/2.0:
                theta += np.pi
            if theta > np.pi/2.0:
                theta -= np.pi
            theta = np.abs(theta)

            # Add to Master
            theta_all[ii] = theta * C.r2d
            b_over_r_all[ii] = b/(r1+r2)
            v_impact_all[ii] = v_impact

    # Return
    return theta_all, b_over_r_all, v_impact_all