You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

bond.py 5.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. # Copyright 2021 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. '''Bond'''
  16. class Bond:
  17. '''Bond'''
  18. def __init__(self, controller, md_info):
  19. self.atom_numbers = md_info.atom_numbers
  20. if controller.amber_parm is not None:
  21. file_path = controller.amber_parm
  22. self.read_information_from_amberfile(file_path)
  23. def read_information_from_amberfile(self, file_path):
  24. '''read amber file'''
  25. file = open(file_path, 'r')
  26. context = file.readlines()
  27. file.close()
  28. for idx, val in enumerate(context):
  29. if idx < len(context) - 1:
  30. if "%FLAG POINTERS" in val + context[idx + 1] and "%FORMAT(10I8)" in val + context[idx + 1]:
  31. start_idx = idx + 2
  32. count = 0
  33. value = list(map(int, context[start_idx].strip().split()))
  34. self.bond_with_hydrogen = value[2]
  35. self.bond_numbers = value[3]
  36. self.bond_numbers += self.bond_with_hydrogen
  37. information = []
  38. information.extend(value)
  39. while count < 16:
  40. start_idx += 1
  41. value = list(map(int, context[start_idx].strip().split()))
  42. information.extend(value)
  43. count += len(value)
  44. self.bond_type_numbers = information[15]
  45. print("bond type numbers ", self.bond_type_numbers)
  46. break
  47. for idx, val in enumerate(context):
  48. if "%FLAG BOND_FORCE_CONSTANT" in val:
  49. count = 0
  50. start_idx = idx
  51. information = []
  52. while count < self.bond_type_numbers:
  53. start_idx += 1
  54. if "%FORMAT" in context[start_idx]:
  55. continue
  56. else:
  57. value = list(map(float, context[start_idx].strip().split()))
  58. information.extend(value)
  59. count += len(value)
  60. self.bond_type_k = information[:self.bond_type_numbers]
  61. break
  62. for idx, val in enumerate(context):
  63. if "%FLAG BOND_EQUIL_VALUE" in val:
  64. count = 0
  65. start_idx = idx
  66. information = []
  67. while count < self.bond_type_numbers:
  68. start_idx += 1
  69. if "%FORMAT" in context[start_idx]:
  70. continue
  71. else:
  72. value = list(map(float, context[start_idx].strip().split()))
  73. information.extend(value)
  74. count += len(value)
  75. self.bond_type_r = information[:self.bond_type_numbers]
  76. break
  77. self.processor(context)
  78. def processor(self, context):
  79. '''processor'''
  80. for idx, val in enumerate(context):
  81. if "%FLAG BONDS_INC_HYDROGEN" in val:
  82. self.h_atom_a = [0] * self.bond_numbers
  83. self.h_atom_b = [0] * self.bond_numbers
  84. self.h_k = [0] * self.bond_numbers
  85. self.h_r0 = [0] * self.bond_numbers
  86. count = 0
  87. start_idx = idx
  88. information = []
  89. while count < 3 * self.bond_with_hydrogen:
  90. start_idx += 1
  91. if "%FORMAT" in context[start_idx]:
  92. continue
  93. else:
  94. value = list(map(int, context[start_idx].strip().split()))
  95. information.extend(value)
  96. count += len(value)
  97. for i in range(self.bond_with_hydrogen):
  98. self.h_atom_a[i] = information[3 * i + 0] / 3
  99. self.h_atom_b[i] = information[3 * i + 1] / 3
  100. tmpi = information[3 * i + 2] - 1
  101. self.h_k[i] = self.bond_type_k[tmpi]
  102. self.h_r0[i] = self.bond_type_r[tmpi]
  103. break
  104. for idx, val in enumerate(context):
  105. if "%FLAG BONDS_WITHOUT_HYDROGEN" in val:
  106. count = 0
  107. start_idx = idx
  108. information = []
  109. while count < 3 * (self.bond_numbers - self.bond_with_hydrogen):
  110. start_idx += 1
  111. if "%FORMAT" in context[start_idx]:
  112. continue
  113. else:
  114. value = list(map(int, context[start_idx].strip().split()))
  115. information.extend(value)
  116. count += len(value)
  117. for i in range(self.bond_with_hydrogen, self.bond_numbers):
  118. self.h_atom_a[i] = information[3 * (i - self.bond_with_hydrogen) + 0] / 3
  119. self.h_atom_b[i] = information[3 * (i - self.bond_with_hydrogen) + 1] / 3
  120. tmpi = information[3 * (i - self.bond_with_hydrogen) + 2] - 1
  121. self.h_k[i] = self.bond_type_k[tmpi]
  122. self.h_r0[i] = self.bond_type_r[tmpi]
  123. break