예제 #1
0
def exercise_fast_minimum_reduction():
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)))
    assert mr.iteration_limit() == 100
    assert mr.multiplier_significant_change_test() == 16
    assert mr.min_n_no_significant_change() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
                                      90)
    assert mr.iteration_limit() == 90
    assert mr.multiplier_significant_change_test() == 16
    assert mr.min_n_no_significant_change() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
                                      90, 8)
    assert mr.iteration_limit() == 90
    assert mr.multiplier_significant_change_test() == 8
    assert mr.min_n_no_significant_change() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
                                      90, 8, 4)
    assert mr.iteration_limit() == 90
    assert mr.multiplier_significant_change_test() == 8
    assert mr.min_n_no_significant_change() == 4
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((2, 3, 5, 80, 90, 100)))
    assert approx_equal(mr.as_gruber_matrix(),
                        (4, 9, 25, -5.209445, 0, -2.083778))
    assert approx_equal(mr.as_niggli_matrix(),
                        (4, 9, 25, -5.209445 / 2, 0, -2.083778 / 2))
    assert approx_equal(mr.as_sym_mat3(),
                        (4, 9, 25, -2.083778 / 2, 0, -5.209445 / 2))
    assert mr.as_unit_cell().is_similar_to(
        uctbx.unit_cell((2, 3, 5, 100, 90, 100)))
    assert approx_equal(mr.r_inv(), (-1, 0, 0, 0, -1, 0, 0, 0, 1))
    assert mr.n_iterations() == 1
    assert not mr.termination_due_to_significant_change_test()
    assert mr.type() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((5, 3, 2, 50, 120, 130)),
                                      8)
    assert mr.n_iterations() == 8
    assert not mr.termination_due_to_significant_change_test()
    try:
        uctbx.fast_minimum_reduction(uctbx.unit_cell((5, 3, 2, 50, 120, 130)),
                                     2, 7)
    except RuntimeError as e:
        assert str(e) == "cctbx Error: Iteration limit exceeded."
    else:
        raise AssertionError('exception expected')
    try:
        u = uctbx.unit_cell((2, 3, 5, 70, 120, 50))
    except Exception:
        pass
    else:
        try:
            uctbx.fast_minimum_reduction(u)
        except RuntimeError as e:
            if ("--Verbose" in sys.argv[1:]):
                print("Expected:", e)
예제 #2
0
def exercise_fast_minimum_reduction():
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)))
  assert mr.iteration_limit() == 100
  assert mr.multiplier_significant_change_test() == 16
  assert mr.min_n_no_significant_change() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90)
  assert mr.iteration_limit() == 90
  assert mr.multiplier_significant_change_test() == 16
  assert mr.min_n_no_significant_change() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90,8)
  assert mr.iteration_limit() == 90
  assert mr.multiplier_significant_change_test() == 8
  assert mr.min_n_no_significant_change() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1,1,1,90,90,90)), 90,8,4)
  assert mr.iteration_limit() == 90
  assert mr.multiplier_significant_change_test() == 8
  assert mr.min_n_no_significant_change() == 4
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((2,3,5,80,90,100)))
  assert approx_equal(mr.as_gruber_matrix(),(4,9,25,-5.209445,0,-2.083778))
  assert approx_equal(mr.as_niggli_matrix(),(4,9,25,-5.209445/2,0,-2.083778/2))
  assert approx_equal(mr.as_sym_mat3(),(4,9,25,-2.083778/2,0,-5.209445/2))
  assert mr.as_unit_cell().is_similar_to(uctbx.unit_cell((2,3,5,100,90,100)))
  assert approx_equal(mr.r_inv(), (-1,0,0,0,-1,0,0,0,1))
  assert mr.n_iterations() == 1
  assert not mr.termination_due_to_significant_change_test()
  assert mr.type() == 2
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 8)
  assert mr.n_iterations() == 8
  assert not mr.termination_due_to_significant_change_test()
  try:
    uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 2, 7)
  except RuntimeError, e:
    assert str(e) == "cctbx Error: Iteration limit exceeded."
예제 #3
0
 def reduced_cell(self):
   #convenience function to transform the orientation matrix to the reduced cell
   from cctbx.uctbx import fast_minimum_reduction
   from scitbx import matrix
   uc = self.unit_cell()
   R = fast_minimum_reduction(uc)
   rinverse = matrix.sqr( R.r_inv() )
   return self.change_basis(rinverse.transpose().inverse().elems)
예제 #4
0
 def reduced_cell(self):
     #convenience function to transform the orientation matrix to the reduced cell
     from cctbx.uctbx import fast_minimum_reduction
     from scitbx import matrix
     uc = self.unit_cell()
     R = fast_minimum_reduction(uc)
     rinverse = matrix.sqr(R.r_inv())
     return self.change_basis(rinverse.transpose().inverse().elems)
예제 #5
0
def rwgk_niggli(UC,epsilon=None,cutoff=100.):

  '''Reference:
  R.W. Grosse-Kunstleve, N.K. Sauter and P.D. Adams.
  Numerically stable algorithms for the computation of reduced unit cells.
  Acta Cryst. A60, 1-6 (2004)
  '''

  if isinstance(UC,unit_cell):
    #return UC.niggli_cell(epsilon)
    return fast_minimum_reduction(UC).as_unit_cell()
  elif isinstance(UC,crystal_orientation.crystal_orientation):
    uc = UC.unit_cell()
    unit_cell_too_small(uc,cutoff=cutoff)
    #R = uc.niggli_reduction(epsilon)
    R = fast_minimum_reduction(uc)
    #minimum reduction gets r_inv as a tuple instead of scitbx.matrix
    rinverse = matrix.sqr( R.r_inv() )
    #NIG = R.as_unit_cell().parameters()
    #MIN = fast_minimum_reduction(uc).as_unit_cell().parameters()
    return UC.change_basis(rinverse.transpose().inverse().elems)
예제 #6
0
 def __init__(self, n_iterations=None):
   if (n_iterations is not None):
     self.n_iterations = n_iterations
   else:
     if ("--hardest" in sys.argv[1:]):
       self.n_iterations = 1000000
     elif ("--harder" in sys.argv[1:]):
       self.n_iterations = 100000
     elif ("--hard" in sys.argv[1:]):
       self.n_iterations = 10000
     else:
       self.n_iterations = 100
   self.n_stable = [0,0]
   self.n_unstable = 0
   i_iteration = 0
   rnd = random.random
   while 1:
     lengths = [rnd(), rnd(), rnd()]
     for alpha in xrange(10,180,10):
       for beta in xrange(10,180,10):
         for gamma in xrange(10,180,10):
           try:
             u = uctbx.unit_cell((2,3,5,alpha,beta,gamma))
           except Exception:
             pass
           else:
             is_degenerate = u.is_degenerate(1.e-10, 1.e-5)
             try:
               uctbx.fast_minimum_reduction(u)
               self.n_stable[int(is_degenerate)] += 1
             except RuntimeError, e:
               assert is_degenerate
               self.n_unstable += 1
             i_iteration += 1
             if (i_iteration == self.n_iterations):
               return
예제 #7
0
 def __init__(self, n_iterations=None):
     if (n_iterations is not None):
         self.n_iterations = n_iterations
     else:
         if ("--hardest" in sys.argv[1:]):
             self.n_iterations = 1000000
         elif ("--harder" in sys.argv[1:]):
             self.n_iterations = 100000
         elif ("--hard" in sys.argv[1:]):
             self.n_iterations = 10000
         else:
             self.n_iterations = 100
     self.n_stable = [0, 0]
     self.n_unstable = 0
     i_iteration = 0
     rnd = random.random
     while 1:
         lengths = [rnd(), rnd(), rnd()]
         for alpha in range(10, 180, 10):
             for beta in range(10, 180, 10):
                 for gamma in range(10, 180, 10):
                     try:
                         u = uctbx.unit_cell((2, 3, 5, alpha, beta, gamma))
                     except Exception:
                         pass
                     else:
                         is_degenerate = u.is_degenerate(1.e-10, 1.e-5)
                         try:
                             uctbx.fast_minimum_reduction(u)
                             self.n_stable[int(is_degenerate)] += 1
                         except RuntimeError as e:
                             assert is_degenerate
                             self.n_unstable += 1
                         i_iteration += 1
                         if (i_iteration == self.n_iterations):
                             return
예제 #8
0
def exercise_fast_minimum_reduction():
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)))
    assert mr.iteration_limit() == 100
    assert mr.multiplier_significant_change_test() == 16
    assert mr.min_n_no_significant_change() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
                                      90)
    assert mr.iteration_limit() == 90
    assert mr.multiplier_significant_change_test() == 16
    assert mr.min_n_no_significant_change() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
                                      90, 8)
    assert mr.iteration_limit() == 90
    assert mr.multiplier_significant_change_test() == 8
    assert mr.min_n_no_significant_change() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((1, 1, 1, 90, 90, 90)),
                                      90, 8, 4)
    assert mr.iteration_limit() == 90
    assert mr.multiplier_significant_change_test() == 8
    assert mr.min_n_no_significant_change() == 4
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((2, 3, 5, 80, 90, 100)))
    assert approx_equal(mr.as_gruber_matrix(),
                        (4, 9, 25, -5.209445, 0, -2.083778))
    assert approx_equal(mr.as_niggli_matrix(),
                        (4, 9, 25, -5.209445 / 2, 0, -2.083778 / 2))
    assert approx_equal(mr.as_sym_mat3(),
                        (4, 9, 25, -2.083778 / 2, 0, -5.209445 / 2))
    assert mr.as_unit_cell().is_similar_to(
        uctbx.unit_cell((2, 3, 5, 100, 90, 100)))
    assert approx_equal(mr.r_inv(), (-1, 0, 0, 0, -1, 0, 0, 0, 1))
    assert mr.n_iterations() == 1
    assert not mr.termination_due_to_significant_change_test()
    assert mr.type() == 2
    mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((5, 3, 2, 50, 120, 130)),
                                      8)
    assert mr.n_iterations() == 8
    assert not mr.termination_due_to_significant_change_test()
    try:
        uctbx.fast_minimum_reduction(uctbx.unit_cell((5, 3, 2, 50, 120, 130)),
                                     2, 7)
    except RuntimeError, e:
        assert str(e) == "cctbx Error: Iteration limit exceeded."
예제 #9
0
  mr = uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 8)
  assert mr.n_iterations() == 8
  assert not mr.termination_due_to_significant_change_test()
  try:
    uctbx.fast_minimum_reduction(uctbx.unit_cell((5,3,2,50,120,130)), 2, 7)
  except RuntimeError, e:
    assert str(e) == "cctbx Error: Iteration limit exceeded."
  else:
    raise AssertionError, 'exception expected'
  try:
    u = uctbx.unit_cell((2,3,5,70,120,50))
  except Exception:
    pass
  else:
    try:
      uctbx.fast_minimum_reduction(u)
    except RuntimeError, e:
      if ("--Verbose" in sys.argv[1:]):
        print "Expected:", e

class exercise_is_degenerate(object):

  def __init__(self, n_iterations=None):
    if (n_iterations is not None):
      self.n_iterations = n_iterations
    else:
      if ("--hardest" in sys.argv[1:]):
        self.n_iterations = 1000000
      elif ("--harder" in sys.argv[1:]):
        self.n_iterations = 100000
      elif ("--hard" in sys.argv[1:]):
예제 #10
0
def do_reduce(inp):
    assert not inp.is_degenerate()
    time_krivy_gruber_1976.start()
    red = krivy_gruber_1976.reduction(inp, relative_epsilon=relative_epsilon)
    time_krivy_gruber_1976.stop()
    assert red.is_niggli_cell()
    red_cell = red.as_unit_cell()
    assert inp.change_basis(red.r_inv().elems).is_similar_to(red_cell)
    if (relative_epsilon is None):
        assert check_is_niggli_cell(red_cell).itva_is_niggli_cell()
    time_gruber_1973.start()
    gruber_reduction = gruber_1973.reduction(inp,
                                             relative_epsilon=relative_epsilon)
    time_gruber_1973.stop()
    assert gruber_reduction.is_buerger_cell()
    buerger_cell = gruber_reduction.as_unit_cell()
    assert inp.change_basis(
        gruber_reduction.r_inv().elems).is_similar_to(buerger_cell)
    time_krivy_gruber_1976_minimum.start()
    min_reduction = krivy_gruber_1976.minimum_reduction(inp)
    time_krivy_gruber_1976_minimum.stop()
    assert min_reduction.type() in (1, 2)
    min_cell = min_reduction.as_unit_cell()
    assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
    assert inp.change_basis(
        min_reduction.r_inv().elems).is_similar_to(min_cell)
    time_gruber_1973_minimum.start()
    min_reduction = gruber_1973.minimum_reduction(inp)
    time_gruber_1973_minimum.stop()
    assert min_reduction.type() in (1, 2)
    min_cell = min_reduction.as_unit_cell()
    assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
    assert inp.change_basis(
        min_reduction.r_inv().elems).is_similar_to(min_cell)
    time_gruber_1973_fast_minimum.start()
    min_reduction = gruber_1973.fast_minimum_reduction(inp)
    time_gruber_1973_fast_minimum.stop()
    assert min_reduction.type() in (1, 2)
    min_cell = min_reduction.as_unit_cell()
    assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
    assert inp.change_basis(
        min_reduction.r_inv().elems).is_similar_to(min_cell)
    time_uctbx_fast_minimum.start()
    min_reduction = uctbx.fast_minimum_reduction(inp)
    time_uctbx_fast_minimum.stop()
    assert min_reduction.type() in (1, 2)
    min_cell = min_reduction.as_unit_cell()
    assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
    assert inp.change_basis(min_reduction.r_inv()).is_similar_to(min_cell)
    global fast_minimum_reduction_max_n_iterations
    fast_minimum_reduction_max_n_iterations = max(
        fast_minimum_reduction_max_n_iterations, min_reduction.n_iterations())
    if (track_infinite):
        if (eq_always_false):
            red0 = reduction_with_tracking_and_eq_always_false(inp)
        else:
            red0 = reduction_with_tracking(inp)
        if (red0.iteration_limit_exceeded):
            n = 20
            print(inp)
            print("red0.action_log:", red0.action_log[-n:])
            print("red0.type_log:", red0.type_log[-n:])
            print("red0.meets_primary_conditions_log:", \
                   red0.meets_primary_conditions_log[-n:])
            print("red0.meets_main_conditions_log:", \
                   red0.meets_main_conditions_log[-n:])
            print("red0.is_niggli_cell_log:", red0.is_niggli_cell_log[-n:])
            if (1):
                for cell in red0.cell_log[-n:]:
                    print(cell)
                print()
            sys.stdout.flush()
    return red
예제 #11
0
def do_reduce(inp):
  assert not inp.is_degenerate()
  time_krivy_gruber_1976.start()
  red = krivy_gruber_1976.reduction(
    inp, relative_epsilon=relative_epsilon)
  time_krivy_gruber_1976.stop()
  assert red.is_niggli_cell()
  red_cell = red.as_unit_cell()
  assert inp.change_basis(red.r_inv().elems).is_similar_to(red_cell)
  if (relative_epsilon is None):
    assert check_is_niggli_cell(red_cell).itva_is_niggli_cell()
  time_gruber_1973.start()
  gruber_reduction = gruber_1973.reduction(
    inp, relative_epsilon=relative_epsilon)
  time_gruber_1973.stop()
  assert gruber_reduction.is_buerger_cell()
  buerger_cell = gruber_reduction.as_unit_cell()
  assert inp.change_basis(gruber_reduction.r_inv().elems).is_similar_to(
    buerger_cell)
  time_krivy_gruber_1976_minimum.start()
  min_reduction = krivy_gruber_1976.minimum_reduction(inp)
  time_krivy_gruber_1976_minimum.stop()
  assert min_reduction.type() in (1,2)
  min_cell = min_reduction.as_unit_cell()
  assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
  assert inp.change_basis(min_reduction.r_inv().elems).is_similar_to(min_cell)
  time_gruber_1973_minimum.start()
  min_reduction = gruber_1973.minimum_reduction(inp)
  time_gruber_1973_minimum.stop()
  assert min_reduction.type() in (1,2)
  min_cell = min_reduction.as_unit_cell()
  assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
  assert inp.change_basis(min_reduction.r_inv().elems).is_similar_to(min_cell)
  time_gruber_1973_fast_minimum.start()
  min_reduction = gruber_1973.fast_minimum_reduction(inp)
  time_gruber_1973_fast_minimum.stop()
  assert min_reduction.type() in (1,2)
  min_cell = min_reduction.as_unit_cell()
  assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
  assert inp.change_basis(min_reduction.r_inv().elems).is_similar_to(min_cell)
  time_uctbx_fast_minimum.start()
  min_reduction = uctbx.fast_minimum_reduction(inp)
  time_uctbx_fast_minimum.stop()
  assert min_reduction.type() in (1,2)
  min_cell = min_reduction.as_unit_cell()
  assert approx_equal(min_cell.parameters()[:3], red_cell.parameters()[:3])
  assert inp.change_basis(min_reduction.r_inv()).is_similar_to(min_cell)
  global fast_minimum_reduction_max_n_iterations
  fast_minimum_reduction_max_n_iterations = max(
    fast_minimum_reduction_max_n_iterations, min_reduction.n_iterations())
  if (track_infinite):
    if (eq_always_false):
      red0 = reduction_with_tracking_and_eq_always_false(inp)
    else:
      red0 = reduction_with_tracking(inp)
    if (red0.iteration_limit_exceeded):
      n = 20
      print inp
      print "red0.action_log:", red0.action_log[-n:]
      print "red0.type_log:", red0.type_log[-n:]
      print "red0.meets_primary_conditions_log:", \
             red0.meets_primary_conditions_log[-n:]
      print "red0.meets_main_conditions_log:", \
             red0.meets_main_conditions_log[-n:]
      print "red0.is_niggli_cell_log:", red0.is_niggli_cell_log[-n:]
      if (1):
        for cell in red0.cell_log[-n:]:
          print cell
        print
      sys.stdout.flush()
  return red
예제 #12
0
    assert mr.n_iterations() == 8
    assert not mr.termination_due_to_significant_change_test()
    try:
        uctbx.fast_minimum_reduction(uctbx.unit_cell((5, 3, 2, 50, 120, 130)),
                                     2, 7)
    except RuntimeError, e:
        assert str(e) == "cctbx Error: Iteration limit exceeded."
    else:
        raise AssertionError, 'exception expected'
    try:
        u = uctbx.unit_cell((2, 3, 5, 70, 120, 50))
    except Exception:
        pass
    else:
        try:
            uctbx.fast_minimum_reduction(u)
        except RuntimeError, e:
            if ("--Verbose" in sys.argv[1:]):
                print "Expected:", e


class exercise_is_degenerate(object):
    def __init__(self, n_iterations=None):
        if (n_iterations is not None):
            self.n_iterations = n_iterations
        else:
            if ("--hardest" in sys.argv[1:]):
                self.n_iterations = 1000000
            elif ("--harder" in sys.argv[1:]):
                self.n_iterations = 100000
            elif ("--hard" in sys.argv[1:]):