Exemplo n.º 1
0
def tst_dmatrix():
  # expected values are the d_jmn at beta=1.0 and j=2, m=-2, -2<=n<=2
  expect = [0.593133, 0.64806, 0.433605, 0.193411, 0.0528305]
  eps = 1e-4
  d2 = dmatrix( 2, 1.0)  # dmatrix( max_L, beta )
  for n in range(-2, 3):
    assert abs(expect[n+2] - d2.djmn(2, -2, n) ) < eps

  d4 = dmatrix( 4, 1.0)
  for n in range(-2, 3):
    assert abs(expect[n+2] - d4.djmn(2, -2, n) ) < eps

  d7 = dmatrix( 2, 1.0)
  for n in range(-2, 3):
    assert abs(expect[n+2] - d7.djmn(2, -2, n) ) < eps

  # check agains d(2,2,1) = -(1+cos(beta))*sin(beta)/2.0
  for ii in range(10):
    expt = -(1.0+smath.cos(ii))*smath.sin(ii)/2.0
    assert abs( dmatrix(2,ii).djmn(2,2,1) - expt ) < eps
  # check beta= multiple of pi/2

  assert abs( dmatrix( 20, smath.pi ).djmn(2,2,0) )< eps
  assert abs( dmatrix( 2, smath.pi*2 ).djmn(2,2,0) )< eps
  assert abs( dmatrix( 20, smath.pi*0.5 ).djmn(2,2,0)-smath.sqrt(6.0)/4.0 )< eps
Exemplo n.º 2
0
def tst():
  M=10
  xy = []
  for ii in range(M):
    r=10.0
    phi = ii*(smath.pi*2/M)
    x = r*smath.cos(phi)
    y = r*smath.sin(phi)
    xy.append( flex.double([x,y]) )
  # build distance matrix
  dmat = []
  for ii in range(M):
    tmp = []
    for jj in range(M):

      x1=xy[ii][0]
      x2=xy[jj][0]

      y1=xy[ii][1]
      y2=xy[jj][1]

      dd = smath.sqrt( (x1-x2)**2.0 +(y1-y2)**2.0  )
      if jj != ii:
          tmp.append( (jj,dd) )
    dmat.append( tmp )
  spee=classic_spe_engine( dmat, l=1.0,max_cycle=5000 )
  x,s = spee.embed(2,M)
  assert s<1e-4
  print "OK"
Exemplo n.º 3
0
def tst():
    M = 10
    xy = []
    for ii in range(M):
        r = 10.0
        phi = ii * (smath.pi * 2 / M)
        x = r * smath.cos(phi)
        y = r * smath.sin(phi)
        xy.append(flex.double([x, y]))
    # build distance matrix
    dmat = []
    for ii in range(M):
        tmp = []
        for jj in range(M):

            x1 = xy[ii][0]
            x2 = xy[jj][0]

            y1 = xy[ii][1]
            y2 = xy[jj][1]

            dd = smath.sqrt((x1 - x2)**2.0 + (y1 - y2)**2.0)
            if jj != ii:
                tmp.append((jj, dd))
        dmat.append(tmp)
    spee = classic_spe_engine(dmat, l=1.0, max_cycle=5000)
    x, s = spee.embed(2, M)
    assert s < 1e-4
    print "OK"
Exemplo n.º 4
0
def tst_dmatrix():
    # expected values are the d_jmn at beta=1.0 and j=2, m=-2, -2<=n<=2
    expect = [0.593133, 0.64806, 0.433605, 0.193411, 0.0528305]
    eps = 1e-4
    d2 = dmatrix(2, 1.0)  # dmatrix( max_L, beta )
    for n in range(-2, 3):
        assert abs(expect[n + 2] - d2.djmn(2, -2, n)) < eps

    d4 = dmatrix(4, 1.0)
    for n in range(-2, 3):
        assert abs(expect[n + 2] - d4.djmn(2, -2, n)) < eps

    d7 = dmatrix(2, 1.0)
    for n in range(-2, 3):
        assert abs(expect[n + 2] - d7.djmn(2, -2, n)) < eps

    # check agains d(2,2,1) = -(1+cos(beta))*sin(beta)/2.0
    for ii in range(10):
        expt = -(1.0 + smath.cos(ii)) * smath.sin(ii) / 2.0
        assert abs(dmatrix(2, ii).djmn(2, 2, 1) - expt) < eps
    # check beta= multiple of pi/2

    assert abs(dmatrix(20, smath.pi).djmn(2, 2, 0)) < eps
    assert abs(dmatrix(2, smath.pi * 2).djmn(2, 2, 0)) < eps
    assert abs(
        dmatrix(20, smath.pi * 0.5).djmn(2, 2, 0) -
        smath.sqrt(6.0) / 4.0) < eps
Exemplo n.º 5
0
 def __init__(self, n):
   self.n = n
   self.step = 2*math.pi/self.n
   self.sin_table = flex.double()
   self.cos_table = flex.double()
   for i in xrange(self.n):
     self.sin_table.append(math.sin(i*self.step))
     self.cos_table.append(math.cos(i*self.step))
Exemplo n.º 6
0
 def __init__(self, n):
   self.n = n
   self.step = 2*math.pi/self.n
   self.sin_table = flex.double()
   self.cos_table = flex.double()
   for i in xrange(self.n):
     self.sin_table.append(math.sin(i*self.step))
     self.cos_table.append(math.cos(i*self.step))
Exemplo n.º 7
0
def print_correlation( out_correlation, correlation, q_array, N_phi):
  print >>out_correlation, "# phi, q, correlation"
  two_pi = smath.pi*2.0
  ii = 0
  for qq in q_array:
    for pp in range(N_phi):
      phi = pp*two_pi/N_phi
      print >>out_correlation, qq*smath.cos(phi),qq*smath.sin(phi), correlation[pp][ii][ii]
    ii=ii+1
def rosca(m=9, hemisphere=True):
  """
  Regular grid on the unit sphere, Rosca (2010).
  """
  def truncate(x):
    if(abs(x)<1.e-6): return 0
    else:             return x
  def add(result, new):
    foud = False
    for s in result:
      d = math.sqrt((s[0]-new[0])**2+(s[1]-new[1])**2+(s[2]-new[2])**2)
      if(d<1.e-3): return
    result.append(new)
    return
  d_l = math.sqrt(2)/m
  result = flex.vec3_double()
  for m_ in xrange(m+1):
    if(m_==0):
      add(result, [0,0,1])
    else:
      l_m = m_ * d_l
      d_phi = math.pi/(4*m_)
      assert l_m>=0 and l_m<=math.sqrt(2)
      for k in xrange(m_+1):
        arg1 = k*d_phi
        arg2 = l_m*math.sqrt(1-l_m**2/4)
        x_km = truncate(math.cos(arg1)*arg2)
        y_km = truncate(math.sin(arg1)*arg2)
        z_m  = truncate(1.-l_m**2/2                                )
        add(result, [ x_km, y_km,z_m])
        add(result, [ y_km, x_km,z_m])
        add(result, [-y_km, x_km,z_m])
        add(result, [-x_km, y_km,z_m])
        add(result, [ x_km,-y_km,z_m])
        add(result, [ y_km,-x_km,z_m])
        add(result, [-x_km,-y_km,z_m])
        add(result, [-y_km,-x_km,z_m])
        if(not hemisphere):
          add(result, [ x_km, y_km,-z_m])
          add(result, [ y_km, x_km,-z_m])
          add(result, [-y_km, x_km,-z_m])
          add(result, [-x_km, y_km,-z_m])
          add(result, [ x_km,-y_km,-z_m])
          add(result, [ y_km,-x_km,-z_m])
          add(result, [-x_km,-y_km,-z_m])
          add(result, [-y_km,-x_km,-z_m])
  for r in result:
    assert abs(1.-math.sqrt(r[0]**2+r[1]**2+r[2]**2))<1.e-6
  # XXX for debugging
  if(0):
    f = "HETATM%5d  O   HOH A%4d    %8.3f%8.3f%8.3f  1.00 23.99           O "
    o = open("junk.pdb", "w")
    for i, r in enumerate(result):
      print >> o, f%(i, i, r[0],r[1],r[2])
    o.close()
  return result
Exemplo n.º 9
0
def euler_angles_as_matrix(angles, deg=False):
    if (deg):
        angles = [a * math.pi / 180 for a in angles]
    c = [math.cos(a) for a in angles]
    s = [math.sin(a) for a in angles]
    # Based on subroutine angro7 in CONVROT.
    # Urzhumtseva, L.M. & Urzhumtsev, A.G. (1997).
    return matrix.sqr(
        (c[0] * c[1] * c[2] - s[0] * s[2], -c[0] * c[1] * s[2] - s[0] * c[2],
         c[0] * s[1], s[0] * c[1] * c[2] + c[0] * s[2],
         -s[0] * c[1] * s[2] + c[0] * c[2], s[0] * s[1], -s[1] * c[2],
         s[1] * s[2], c[1]))
Exemplo n.º 10
0
 def rotate_image(self,data,angle_deg,sigma=1.0):
   # we take an image, rotate, interpolate it back onto a regular grid and return it
   #
   # There is however the problem that some pixels will never be seen. those pixels will be filled up by interpolation
   data = data*self.mask 
   cost = smath.cos( angle_deg*smath.pi/180.0 )
   sint = smath.sin( angle_deg*smath.pi/180.0 )
   new_image = flex.double(self.grid,0)
   visited   = flex.bool(self.grid,False)
   nx_array  = flex.double()
   ny_array  = flex.double()
   val_array = flex.double()
   for ix in range(self.np):
     for iy in range(self.np):
       x  = ix - self.ox
       y  = iy - self.oy
       nx = cost*x - sint*y
       ny = sint*x + cost*y
       jx = int(nx+self.ox+0.5)
       jy = int(ny+self.oy+0.5)
       if jx >= self.np : jx = self.np-1
       if jy >= self.np : jy = self.np-1
       if jx<0: jx=0
       if jy<0: jy=0
       new_image[ (jx,jy) ] = data[ (ix,iy) ]
       visited[ (jx,jy) ] = True
       nx_array.append( nx+self.ox )
       ny_array.append( ny+self.oy )
       val_array.append( data[ (ix,iy) ] )
   
   assert nx_array.size() == self.mask.size()
   not_visited = ~visited
   not_visited = not_visited.iselection()
   # now we need to loop over all non-visited pixel values
   for pixel in not_visited:
     #for ix in range(self.np):
     # for iy in range(self.np):
     nv = 0
     if self.mask[(ix,iy)]>-0.1:
       nv = -9
       index =  n_dim_index_from_one_dim(pixel, [self.np,self.np] )
       nvx  = index[1]
       nvy  = index[0]
       dx   = nx_array-nvx
       dy   = ny_array-nvy
       dd   = (dx*dx+dy*dy)
       ss   = flex.exp( -dd/(sigma*sigma) )*self.mask
       nv   = flex.sum(ss*val_array)/(1e-12+flex.sum(ss))
       new_image[ (ix,iy) ] = nv
   new_image = new_image*self.mask
   return new_image
Exemplo n.º 11
0
def r3_rotation_matrix_logarithm(r):
  """
  Chirikjian, G. Stochastic models, information theory, and Lie groups.
  Volume 2, pp 39-40.
  Applied and Numerical Harmonic Analysis, Birkhauser
  """

  cos_theta = r3_rotation_cos_rotation_angle_from_matrix( r )

  if cos_theta < 1.0:
      theta = math.acos( cos_theta )
      a1 = theta / math.sin( theta )

  else:
      a1 = 1.0

  return 0.5 * a1 * ( r - r.transpose() )
Exemplo n.º 12
0
def r3_rotation_matrix_logarithm(r):
  """
  Chirikjian, G. Stochastic models, information theory, and Lie groups.
  Volume 2, pp 39-40.
  Applied and Numerical Harmonic Analysis, Birkhauser
  """

  cos_theta = r3_rotation_cos_rotation_angle_from_matrix( r )

  if cos_theta < 1.0:
      theta = math.acos( cos_theta )
      a1 = theta / math.sin( theta )

  else:
      a1 = 1.0

  return 0.5 * a1 * ( r - r.transpose() )
Exemplo n.º 13
0
def euler_angles_as_matrix(angles, deg=False):
  if (deg):
    angles = [a*math.pi/180 for a in angles]
  c = [math.cos(a) for a in angles]
  s = [math.sin(a) for a in angles]
  # Based on subroutine angro7 in CONVROT.
  # Urzhumtseva, L.M. & Urzhumtsev, A.G. (1997).
  return matrix.sqr((
     c[0]*c[1]*c[2]-s[0]*s[2],
    -c[0]*c[1]*s[2]-s[0]*c[2],
     c[0]*s[1],
     s[0]*c[1]*c[2]+c[0]*s[2],
    -s[0]*c[1]*s[2]+c[0]*c[2],
     s[0]*s[1],
    -s[1]*c[2],
     s[1]*s[2],
     c[1]))
Exemplo n.º 14
0
def j1(x):
    result = smath.sin(x) / (x * x) - smath.cos(x) / x
    return result
Exemplo n.º 15
0
def j2(x):
  result = (3.0/(x*x) -1.0)*(smath.sin(x)/x) - 3.0*smath.cos(x)/(x*x)
  return result
Exemplo n.º 16
0
def j1(x):
  result =  smath.sin(x)/(x*x) - smath.cos(x)/x
  return result
Exemplo n.º 17
0
def j0(x):
  result = smath.sin(x)/x
  return result
Exemplo n.º 18
0
def get_coefficients(ih, a0, a1, a2, ih2, use_ideal_bonds_angles, sites_cart,
                     typeh):
    rh = matrix.col(sites_cart[ih])
    r0 = matrix.col(sites_cart[a0.iseq])
    r1 = matrix.col(sites_cart[a1.iseq])
    r2 = matrix.col(sites_cart[a2.iseq])
    uh0 = (rh - r0).normalize()
    u10 = (r1 - r0).normalize()
    u20 = (r2 - r0).normalize()
    if use_ideal_bonds_angles:
        alpha0 = math.radians(a0.angle_ideal[0])
        alpha1 = math.radians(a1.angle_ideal)
        alpha2 = math.radians(a2.angle_ideal)
        c0, c1, c2 = math.cos(alpha0), math.cos(alpha1), math.cos(alpha2)
    else:
        alpha0 = (u10).angle(u20)
        alpha0 = math.acos(u10.dot(u20))
        alpha1 = (u10).angle(uh0)
        alpha2 = (uh0).angle(u20)
        c0 = (u10).dot(u20)
        c1 = (u10).dot(uh0)
        c2 = (uh0).dot(u20)
    sumang = alpha0 + alpha1 + alpha2
    denom = (1.0 - c0**2)
    if (denom == 0):
        raise RuntimeError(
            "Denominator zero: (1-c0*c0) in get_h_parameterization.")
    a = (c1 - c0 * c2) / (1 - c0 * c0)
    b = (c2 - c0 * c1) / (1 - c0 * c0)
    root = None
    # check if H, A0, A1, A2 are in a plane
    if (sumang < (2 * math.pi + 0.05) and (sumang > 2 * math.pi - 0.05)):
        h = None
    elif (sumang > (2 * math.pi + 0.05)
          and 1 - c1 * c1 - c2 * c2 - c0 * c0 + 2 * c0 * c1 * c2 < 0):
        root = 1 - c1 * c1 - c2 * c2 - c0 * c0 + 2 * c0 * c1 * c2
        h = None
        return sumang, a, b, h, root
    else:
        # two tetragonal geometry: e.g. CH2 group
        if (ih2 is not None):
            rh2 = matrix.col(sites_cart[ih2.iseq])
            uh02 = (rh2 - r0).normalize()
            if use_ideal_bonds_angles:
                h = math.radians(ih2.angle_ideal) * 0.5
            else:
                h = (uh0).angle(uh02) * 0.5
            #test if vector v points to same 'side' as uh0
            if ((u10.cross(u20)).dot(uh0) < 0):
                h = -h
        else:
            # if H is out of plane, but not in tetrahedral geometry
            root = 1 - c1 * c1 - c2 * c2 - c0 * c0 + 2 * c0 * c1 * c2
            if (root < 0):
                raise RuntimeError(
                    "Expression in square root < 0 in get_h_parameterization.")
            denom = math.sin(alpha0)
            if (denom == 0):
                raise RuntimeError(
                    "Denominator zero: sin(alpha0)in get_h_parameterization.")
            cz = (math.sqrt(1 - c1 * c1 - c2 * c2 - c0 * c0 +
                            2 * c0 * c1 * c2)) / math.sin(alpha0)
            h = cz
            #test if vector v points to same 'side' as uh0
            if ((u10.cross(u20)).dot(uh0) < 0):
                h = -h
    return sumang, a, b, h, root
Exemplo n.º 19
0
def compute_H_position(ih, sites_cart, hp):
    r0 = matrix.col(sites_cart[hp.a0])
    r1 = matrix.col(sites_cart[hp.a1])
    dh = hp.dist_h
    a, b, h = hp.a, hp.b, hp.h
    # alg2a
    if (hp.htype == 'flat_2neigbs'):
        a, b = hp.a, hp.b
        r2 = matrix.col(sites_cart[hp.a2])
        u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
        length = math.sqrt(a * a + b * b + 2 * a * b * (u10).dot(u20))
        if (length == 0):
            raise RuntimeError(
                "Denominator zero: length in generate_H_positions")
        uh0 = (a * u10 + b * u20) / length
        rh_calc = r0 + dh * uh0
        h_distance = (rh_calc - matrix.col(sites_cart[ih])).length()
    # 2 neigbs
    elif (hp.htype == '2neigbs'):
        a, b, h = hp.a, hp.b, hp.h
        r2 = matrix.col(sites_cart[hp.a2])
        u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
        v0 = (u10.cross(u20)).normalize()
        rh0 = (a * u10 + b * u20 + h * v0)
        length = math.sqrt(rh0.dot(rh0))
        if (length == 0):
            raise RuntimeError(
                "Denominator zero: length in generate_H_positions")
        uh0 = rh0 / length
        rh_calc = r0 + dh * uh0
        h_distance = (rh_calc - matrix.col(sites_cart[ih])).length()
    # 2tetrahedral
    elif (hp.htype == '2tetra'):
        a, b, delta = hp.a, hp.b, hp.alpha
        r2 = matrix.col(sites_cart[hp.a2])
        u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
        v0 = (u10.cross(u20)).normalize()
        d0 = (a * u10 + b * u20).normalize()
        rh_calc = r0 + dh * (math.cos(delta) * d0 + math.sin(delta) * v0)
        h_distance = (rh_calc - matrix.col(sites_cart[ih])).length()
    # tetragonal alg3
    elif (hp.htype == '3neigbs'):
        a, b, h = hp.a, hp.b, hp.h
        r2 = matrix.col(sites_cart[hp.a2])
        r3 = matrix.col(sites_cart[hp.a3])
        u10 = (r1 - r0).normalize()
        u20 = (r2 - r0).normalize()
        u30 = (r3 - r0).normalize()
        rh0 = (a * u10 + b * u20 + h * u30)
        length = math.sqrt(rh0.dot(rh0))
        if (length == 0):
            raise RuntimeError(
                "Denominator zero: length in generate_H_positions")
        uh0 = rh0 / length
        rh_calc = r0 + dh * uh0
        h_distance = (rh_calc - matrix.col(sites_cart[ih])).length()


# alg1b or alg1a or propeller group
    elif (hp.htype in ['alg1b', 'alg1a', 'prop']):
        rb1 = matrix.col(sites_cart[hp.a2])
        n = hp.n
        phi = hp.phi + n * 2 * math.pi / 3
        alpha = hp.alpha
        salpha = math.sin(alpha)
        calpha = math.cos(alpha)
        sphi = math.sin(phi)
        cphi = math.cos(phi)
        u1 = (r0 - r1).normalize()
        rb10 = rb1 - r1
        u2 = (rb10 - ((rb10).dot(u1)) * u1).normalize()
        u3 = u1.cross(u2)
        rh_calc = r0 + dh * (salpha * (cphi * u2 + sphi * u3) - calpha * u1)
        h_distance = (rh_calc - matrix.col(sites_cart[ih])).length()
    else:
        rh_calc = sites_cart[ih]
    return rh_calc
Exemplo n.º 20
0
 def get_coefficients(self, ih):
     neighbors = self.h_connectivity[ih]
     if (neighbors.number_h_neighbors == 1):
         i_h1 = neighbors.h1['iseq']
     else:
         i_h1 = None
     i_a0 = neighbors.a0['iseq']
     i_a1 = neighbors.a1['iseq']
     i_a2 = neighbors.a2['iseq']
     rh = matrix.col(self.sites_cart[ih])
     r0 = matrix.col(self.sites_cart[i_a0])
     r1 = matrix.col(self.sites_cart[i_a1])
     r2 = matrix.col(self.sites_cart[i_a2])
     uh0 = (rh - r0).normalize()
     u10 = (r1 - r0).normalize()
     u20 = (r2 - r0).normalize()
     if self.use_ideal_bonds_angles:
         alpha0 = math.radians(neighbors.a0['angle_a1a0a2'])
         alpha1 = math.radians(neighbors.a1['angle_ideal'])
         alpha2 = math.radians(neighbors.a2['angle_ideal'])
         c0, c1, c2 = math.cos(alpha0), math.cos(alpha1), math.cos(alpha2)
     else:
         alpha0 = (u10).angle(u20)
         alpha0 = math.acos(u10.dot(u20))
         alpha1 = (u10).angle(uh0)
         alpha2 = (uh0).angle(u20)
         c0 = (u10).dot(u20)
         c1 = (u10).dot(uh0)
         c2 = (uh0).dot(u20)
     sumang = alpha0 + alpha1 + alpha2
     denom = (1.0 - c0**2)
     if (denom == 0):
         raise RuntimeError(
             "Denominator zero: (1-c0*c0) in get_h_parameterization.")
     a = (c1 - c0 * c2) / (1 - c0 * c0)
     b = (c2 - c0 * c1) / (1 - c0 * c0)
     root = None
     #  # check if H, A0, A1, A2 are in a plane
     if (sumang < (2 * math.pi + 0.05) and (sumang > 2 * math.pi - 0.05)):
         h = None
     elif (sumang > (2 * math.pi + 0.05)
           and 1 - c1 * c1 - c2 * c2 - c0 * c0 + 2 * c0 * c1 * c2 < 0):
         root = 1 - c1 * c1 - c2 * c2 - c0 * c0 + 2 * c0 * c1 * c2
         h = None
         return sumang, a, b, h, root
     else:
         # two tetragonal geometry: e.g. CH2 group
         if (i_h1 is not None):
             rh2 = matrix.col(self.sites_cart[neighbors.h1['iseq']])
             uh02 = (rh2 - r0).normalize()
             if self.use_ideal_bonds_angles:
                 h = math.radians(neighbors.h1['angle_ideal']) * 0.5
             else:
                 h = (uh0).angle(uh02) * 0.5
             #test if vector v points to same 'side' as uh0
             if ((u10.cross(u20)).dot(uh0) < 0):
                 h = -h
         else:
             # if H is out of plane, but not in tetrahedral geometry
             root = 1 - c1 * c1 - c2 * c2 - c0 * c0 + 2 * c0 * c1 * c2
             if (root < 0):
                 raise RuntimeError(
                     "Expression in square root < 0 in get_h_parameterization."
                 )
             denom = math.sin(alpha0)
             if (denom == 0):
                 raise RuntimeError(
                     "Denominator zero: sin(alpha0)in get_h_parameterization."
                 )
             cz = (math.sqrt(1 - c1 * c1 - c2 * c2 - c0 * c0 +
                             2 * c0 * c1 * c2)) / math.sin(alpha0)
             h = cz
             #test if vector v points to same 'side' as uh0
             if ((u10.cross(u20)).dot(uh0) < 0):
                 h = -h
     return sumang, a, b, h, root
def get_h_parameterization(
  connectivity, sites_cart, idealize):
  #connectivity, xray_structure, names, atoms_list, idealize):
  #sites_cart = xray_structure.sites_cart()
  h_parameterization = {}
  for ih in connectivity.keys():
    a0 = connectivity[ih][0]
    count_H, reduced_neighbs = a0.count_H, a0.reduced_neighbs
    n_red_neigbs = len(reduced_neighbs)
    rh = matrix.col(sites_cart[ih])
    r0 = matrix.col(sites_cart[a0.iseq])
    if idealize:
      dist_h = a0.dist_ideal
    else:
      dist_h = (r0 - rh).length()
  # case 2a and 2b, case 3
    if(n_red_neigbs == 2 or
      (n_red_neigbs == 3 and count_H == 0)):
      a1, a2  = reduced_neighbs[0], reduced_neighbs[1]
      r1 = matrix.col(sites_cart[a1.iseq])
      r2 = matrix.col(sites_cart[a2.iseq])
      uh0 = (rh - r0).normalize()
      u10 = (r1 - r0).normalize()
      u20 = (r2 - r0).normalize()
      if idealize:
        alpha0 = math.radians(a0.angle_ideal)
        alpha1 = math.radians(a1.angle_ideal)
        alpha2 = math.radians(a2.angle_ideal)
        c0, c1, c2 = math.cos(alpha0), math.cos(alpha1), math.cos(alpha2)
      else:
        alpha0 = (u10).angle(u20)
        alpha1 = (u10).angle(uh0)
        alpha2 = (uh0).angle(u20)
        c0 = (u10).dot(u20)
        c1 = (u10).dot(uh0)
        c2 = (uh0).dot(u20)
      #sumang = math.degrees(alpha0) + math.degrees(alpha1) + math.degrees(alpha2)
      #c0, c1, c2 = math.cos(alpha0), math.cos(alpha1), math.cos(alpha2)
      sumang = alpha0 + alpha1 + alpha2
      denom = (1.0-c0**2)
      if(denom==0):
        raise RuntimeError(
          "Denominator zero: (1-c0*c0) in get_h_parameterization.")
      a, b = (c1-c0*c2)/(1-c0*c0), (c2-c0*c1)/(1-c0*c0)
      h_parameterization[ih] = parameterization_info(
        a0     = a0.iseq,
        a1     = a1.iseq,
        a2     = a2.iseq,
        a      = a,
        b      = b,
        dist_h = dist_h)
      #if ((sumang < 361 and sumang > 359) and idealize == True ):
      #if (0):
      #if (sumang < 361 and sumang > 359):
      if (sumang < (2*math.pi + 0.01) and (sumang > 2*math.pi - 0.01)):
        h_parameterization[ih].htype = 'flat_2neigbs'
      else:
        root = 1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2
        if(root < 0):
          raise RuntimeError(
            "Expression in square root < 0 in get_h_parameterization.")
        denom = math.sin(alpha0)
        if(denom==0):
          raise RuntimeError(
            "Denominator zero: sin(alpha0)in get_h_parameterization.")
        cz = (math.sqrt(1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2))/math.sin(alpha0)
        h = cz/math.sin(alpha0)
        #test if vector v points to same 'side' as uh0
        if((u10.cross(u20)).dot(uh0) < 0):
          h = -h
        h_parameterization[ih].h = h
        if (n_red_neigbs == 2): # case 2b
          h_parameterization[ih].htype = '2neigbs'
        elif (n_red_neigbs == 3): # case 3
          h_parameterization[ih].htype = '3neigbs'
    # case 1a
    elif(n_red_neigbs == 1 and count_H == 1):
      neigbs_14 = connectivity[ih][2]
      a1 = reduced_neighbs[0]
      b1, b2 = neigbs_14[0], neigbs_14[1]
      r1 = matrix.col(sites_cart[a1.iseq])
      rb1 = matrix.col(sites_cart[b1.iseq])
      rb2 = matrix.col(sites_cart[b2.iseq])
      # chose 1-4 neighbor which is closer - important!
      if((rh-rb2).length() < (rh-rb1).length()):
        neigbs_14[0], neigbs_14[1] = neigbs_14[1], neigbs_14[0]
      rb1 = matrix.col(sites_cart[neigbs_14[0].iseq])
      r2 = r0 + (r1 - rb1).normalize()
      uh0 = (rh - r0).normalize()
      u10 = (r1 - r0).normalize()
      u20 = (r2 - r0).normalize()
      alpha0 = (u10).angle(u20)
      alpha1 = (u10).angle(uh0)
      alpha2 = (uh0).angle(u20)
      c0, c1, c2 = math.cos(alpha0), math.cos(alpha1), math.cos(alpha2)
      denom = (1-c0*c0)
      if(denom==0):
        raise RuntimeError(
          "Denominator zero: (1-c0*c0) in get_h_parameterization.")
      a, b = (c1-c0*c2)/(1-c0*c0), (c2-c0*c1)/(1-c0*c0)
      root = 1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2
      if(root < 0):
        raise RuntimeError(
          "Expression in square root < 0 in get_h_parameterization.")
      denom = math.sin(alpha0)
      if(denom==0):
        raise RuntimeError(
          "Denominator zero: sin(alpha0)in get_h_parameterization.")
      cz = (math.sqrt(1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2))/math.sin(alpha0)
      h = cz/math.sin(alpha0)
      # test if vector v points to same 'side' as uh0
      if((u10.cross(u20)).dot(uh0) < 0):
        h = -h
      h_parameterization[ih] = parameterization_info(
        htype  = 'alg1a',
        a0     = a0.iseq,
        a1     = a1.iseq,
        a2     = neigbs_14[0].iseq,
        a      = a,
        b      = b,
        h      = h,
        dist_h = dist_h)
    # case 1b
    elif(n_red_neigbs == 1 and (count_H == 0 or count_H ==2)):
      a1 = reduced_neighbs[0]
      a, b = 1, 1
      sec_neigbs = connectivity[ih][2]
      b1 = sec_neigbs[0]
      r1 = matrix.col(sites_cart[a1.iseq])
      rb1 = matrix.col(sites_cart[b1.iseq])
      #dh = a0.dist_ideal
      uh0 = (rh - r0).normalize()
      u10 = (r1 - r0).normalize()
      if idealize:
        alpha = math.radians(a1.angle_ideal)
      else:
        alpha = (u10).angle(uh0)
      rb10 = rb1 - r1
      v10 = (rb10 - ((rb10).dot(u10)) * u10).normalize()
      if(v10.dot(uh0) < 0):
        v10 = -v10
        a = -1
      #if(abs((u10.cross(v10)).dot(uh0)) < 0.001):
      #  h_parameterization[ih] = parameterization_info(
      #    htype  = 'alg1b_flat',
      #    a0     = a0.iseq,
      #    a1     = a1.iseq,
      #    a2     = b1.iseq,
      #    a      = a,
      #    alpha  = alpha,
      #    dist_h = dist_h)
      #else:
      #  w10 = (u10.cross(v10)).normalize()
      #  if(w10.dot(uh0) < 0):
      #    w10 = -w10
      #    b   = -1
      #  chi = math.asin((uh0).dot(w10))
      #  c_chi, s_chi = math.cos(abs(chi)), math.sin(abs(chi))
      #  rp = rh - dh * s_chi * w10
      #  rp0 = rp - r0
      #  eps = (rp0).angle(u10)
      #  h_parameterization[ih] = parameterization_info(
      #    htype  = 'alg1b',
      #    a0     = a0.iseq,
      #    a1     = a1.iseq,
      #    a2     = b1.iseq,
      #    a      = a,
      #    b      = b,
      #    chi    = chi,
      #    eps    = eps,
      #    alpha  = alpha,
      #    dist_h = dist_h)
      w10 = (u10.cross(v10)).normalize()
      if(w10.dot(uh0) < 0):
        w10 = -w10
        b   = -1
      chi = math.asin((uh0).dot(w10))
      c_chi, s_chi = math.cos(abs(chi)), math.sin(abs(chi))
      rp = rh - dist_h * s_chi * w10
      rp0 = rp - r0
      eps = (rp0).angle(u10)
      h_parameterization[ih] = parameterization_info(
        htype  = 'alg1b',
        a0     = a0.iseq,
        a1     = a1.iseq,
        a2     = b1.iseq,
        a      = a,
        b      = b,
        chi    = chi,
        eps    = eps,
        alpha  = alpha,
        dist_h = dist_h)
    else:
      h_parameterization[ih] = parameterization_info(
        htype  = 'unk',
        a0     = a0.iseq,
        a1     = a1.iseq)
  return h_parameterization
def generate_H_positions(sites_cart, ih, para_info):
  #sites_cart = xray_structure.sites_cart()
  r0 = matrix.col(sites_cart[para_info.a0])
  r1 = matrix.col(sites_cart[para_info.a1])
  dh = para_info.dist_h
  # case 2a
  if (para_info.htype == 'flat_2neigbs'):
    a, b = para_info.a, para_info.b
    r2 = matrix.col(sites_cart[para_info.a2])
    u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
    #uh0 = (a * u10 + b * u20).normalize()
    uh0 = (a * u10 + b * u20)
    rH_gen = r0 + dh * uh0
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  # case 2b and 3
  elif (para_info.htype == '2neigbs' or para_info.htype == '3neigbs'):
    a, b, h= para_info.a, para_info.b, para_info.h
    r2 = matrix.col(sites_cart[para_info.a2])
    u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
    v = u10.cross(u20)
    uh0 = (a * u10 + b * u20 + h * v).normalize()
    rH_gen = r0 + dh * uh0
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  # case 1a
  elif (para_info.htype == 'alg1a'):
    a, b, h = para_info.a, para_info.b, para_info.h
    rb1 = matrix.col(sites_cart[para_info.a2])
    r2 = r0 + (r1 - rb1).normalize()
    u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
    v = u10.cross(u20)
    uh0 = (a * u10 + b * u20 + h * v).normalize()
    rH_gen = r0 + dh * uh0
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  elif (para_info.htype == 'alg1b_flat'):
    rb1 = matrix.col(sites_cart[para_info.a2])
    a = para_info.a
    alpha = para_info.alpha
    u10 = (r1 - r0).normalize()
    rb10 = rb1 - r1
    v10 = a * (rb10 - ((rb10).dot(u10)) * u10).normalize()
    calpha, salpha = math.cos(alpha), math.sin(alpha)
    rH_gen = r0 + dh * (calpha * u10 + salpha * v10)
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  # case one H atom free rotation
  elif (para_info.htype == 'alg1b'):
    rb1 = matrix.col(sites_cart[para_info.a2])
    a, b = para_info.a, para_info.b
    chi, eps = para_info.chi, para_info.eps
    alpha = para_info.alpha
    u10 = (r1 - r0).normalize()
    rb10 = rb1 - r1
    v10 = a * (rb10 - ((rb10).dot(u10)) * u10).normalize()
    w10 = b * (u10.cross(v10)).normalize()
    c_chi, s_chi = math.cos(abs(chi)), math.sin(abs(chi))
    c1, s1  = math.cos(eps), math.sin(eps)
    calpha = math.cos(alpha)
    rH_gen = r0 + dh * ((calpha * u10) + (calpha * s1/c1 * v10)  + (s_chi * w10))
    #rH_gen = r0 + dh * ((c1 * c_chi * u10) + (s1 * c_chi * v10) + (s_chi * w10))
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  else:
    deltaH, rH_gen = None, None
  return group_args(
    distance = deltaH,
    rH_gen   = rH_gen)
Exemplo n.º 23
0
def j2(x):
    result = (3.0 / (x * x) - 1.0) * (smath.sin(x) /
                                      x) - 3.0 * smath.cos(x) / (x * x)
    return result
Exemplo n.º 24
0
def j0(x):
    result = smath.sin(x) / x
    return result
def generate_H_positions(sites_cart, ih, para_info):
  r0 = matrix.col(sites_cart[para_info.a0])
  r1 = matrix.col(sites_cart[para_info.a1])
  dh = para_info.dist_h
  a, b, h = para_info.a, para_info.b, para_info.h
  # alg2a
  if (para_info.htype == 'flat_2neigbs'):
    a, b = para_info.a, para_info.b
    r2 = matrix.col(sites_cart[para_info.a2])
    u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
    length = math.sqrt(a*a + b*b + 2*a*b*(u10).dot(u20))
    if(length==0):
      raise RuntimeError("Denominator zero: length in generate_H_positions")
    uh0 = (a * u10 + b * u20)/length
    rH_gen = r0 + dh * uh0
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  # 2 neigbs
  elif (para_info.htype == '2neigbs'):
    a, b, h = para_info.a, para_info.b, para_info.h
    r2 = matrix.col(sites_cart[para_info.a2])
    u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
    v0 = (u10.cross(u20)).normalize()
    rh0 = (a * u10 + b * u20 + h * v0)
    length = math.sqrt(rh0.dot(rh0))
    if(length==0):
      raise RuntimeError("Denominator zero: length in generate_H_positions")
    uh0 = rh0/length
    rH_gen = r0 + dh * uh0
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  # 2tetrahedral
  elif (para_info.htype == '2tetra'):
    a, b, delta = para_info.a, para_info.b, para_info.alpha
    r2 = matrix.col(sites_cart[para_info.a2])
    u10, u20 = (r1 - r0).normalize(), (r2 - r0).normalize()
    v0 = (u10.cross(u20)).normalize()
    d0 = (a * u10 + b * u20).normalize()
    rH_gen = r0 + dh * (math.cos(delta) * d0 + math.sin(delta) * v0)
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  # tetragonal alg3
  elif (para_info.htype == '3neigbs'):
    a, b, h = para_info.a, para_info.b, para_info.h
    r2 = matrix.col(sites_cart[para_info.a2])
    r3 = matrix.col(sites_cart[para_info.a3])
    u10 = (r1 - r0).normalize()
    u20 = (r2 - r0).normalize()
    u30 = (r3 - r0).normalize()
    rh0 = (a*u10 + b*u20 + h*u30)
    length = math.sqrt(rh0.dot(rh0))
    if(length==0):
      raise RuntimeError("Denominator zero: length in generate_H_positions")
    uh0 = rh0/length
    rH_gen = r0 + dh * uh0
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
# alg1b or alg1a or propeller group
  elif (para_info.htype in ['alg1b', 'alg1a', 'prop']):
    rb1 = matrix.col(sites_cart[para_info.a2])
    n = para_info.n
    phi = para_info.phi + n*2*math.pi/3
    #phi = para_info.phi
    alpha = para_info.alpha
    salpha = math.sin(alpha)
    calpha = math.cos(alpha)
    sphi = math.sin(phi)
    cphi = math.cos(phi)
    u1 = (r0 - r1).normalize()
    rb10 = rb1 - r1
    u2 = (rb10 - ((rb10).dot(u1)) * u1).normalize()
    u3 = u1.cross(u2)
    rH_gen = r0 + dh * (salpha*(cphi*u2 + sphi*u3) - calpha*u1)
    deltaH = (rH_gen - matrix.col(sites_cart[ih])).length()
  else:
    deltaH, rH_gen = None, None
  return group_args(
    distance = deltaH,
    rH_gen   = rH_gen)
def get_coefficients(ih, a0, a1, a2, ih2, idealize, sites_cart, typeh):
  rh = matrix.col(sites_cart[ih])
  r0 = matrix.col(sites_cart[a0.iseq])
  r1 = matrix.col(sites_cart[a1.iseq])
  r2 = matrix.col(sites_cart[a2.iseq])
  uh0 = (rh - r0).normalize()
  u10 = (r1 - r0).normalize()
  u20 = (r2 - r0).normalize()
  if idealize:
    alpha0 = math.radians(a0.angle_ideal)
    alpha1 = math.radians(a1.angle_ideal)
    alpha2 = math.radians(a2.angle_ideal)
    c0, c1, c2 = math.cos(alpha0), math.cos(alpha1), math.cos(alpha2)
  else:
    alpha0 = (u10).angle(u20)
    alpha1 = (u10).angle(uh0)
    alpha2 = (uh0).angle(u20)
    c0 = (u10).dot(u20)
    c1 = (u10).dot(uh0)
    c2 = (uh0).dot(u20)
  sumang = alpha0 + alpha1 + alpha2
  denom = (1.0-c0**2)
  if(denom==0):
    raise RuntimeError(
      "Denominator zero: (1-c0*c0) in get_h_parameterization.")
  a = (c1-c0*c2)/(1-c0*c0)
  b = (c2-c0*c1)/(1-c0*c0)
  root = None
  # check if H, A0, A1, A2 are in a plane
  if (sumang < (2*math.pi + 0.05) and (sumang > 2*math.pi - 0.05)):
    h = None
  elif (sumang > (2*math.pi + 0.05) and 1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2 < 0):
    root = 1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2
    h = None
    return sumang, a, b, h, root
  else:
    # two tetragonal geometry: e.g. CH2 group
    if (ih2 is not None):
      rh2 = matrix.col(sites_cart[ih2.iseq])
      uh02 = (rh2 - r0).normalize()
      if idealize:
        h = math.radians(ih2.angle_ideal) * 0.5
      else:
        h = (uh0).angle(uh02) * 0.5
      #test if vector v points to same 'side' as uh0
      if((u10.cross(u20)).dot(uh0) < 0):
        h =  -h
    else:
    # if H is out of plane, but not in tetrahedral geometry
      root = 1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2
      if(root < 0):
        raise RuntimeError(
          "Expression in square root < 0 in get_h_parameterization.")
      denom = math.sin(alpha0)
      if(denom==0):
        raise RuntimeError(
          "Denominator zero: sin(alpha0)in get_h_parameterization.")
      cz = (math.sqrt(1-c1*c1-c2*c2-c0*c0+2*c0*c1*c2))/math.sin(alpha0)
      h = cz
      #test if vector v points to same 'side' as uh0
      if((u10.cross(u20)).dot(uh0) < 0):
        h = -h
  return sumang, a, b, h, root