File: check_test.py

package info (click to toggle)
openmolcas 25.02-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 170,204 kB
  • sloc: f90: 498,088; fortran: 139,779; python: 13,587; ansic: 5,745; sh: 745; javascript: 660; pascal: 460; perl: 325; makefile: 17
file content (177 lines) | stat: -rw-r--r-- 6,466 bytes parent folder | download | duplicates (3)
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
# -*- coding: utf-8 -*-

#***********************************************************************
# This file is part of OpenMolcas.                                     *
#                                                                      *
# OpenMolcas is free software; you can redistribute it and/or modify   *
# it under the terms of the GNU Lesser General Public License, v. 2.1. *
# OpenMolcas is distributed in the hope that it will be useful, but it *
# is provided "as is" and without any express or implied warranties.   *
# For more details see the full text of the license in the file        *
# LICENSE or in <http://www.gnu.org/licenses/>.                        *
#                                                                      *
# Copyright (C) 2017-2019, Ignacio Fdez. Galván                        *
#***********************************************************************

from __future__ import (unicode_literals, division, absolute_import, print_function)

from os.path import basename
from re import compile as re_compile
from molcas_aux import *

def check_test(infofile, checkfile, count):
  block = re_compile(r'#>>\s*(\d+)')
  reference = re_compile(r'#>\s*(.*)="(.*)"\/(.*)')
  rc = '_RC_ALL_IS_WELL_'
  last = True

  # Read actual values from the calculation
  vals = []
  try:
    with utf8_open(infofile) as i_file:
      for line in i_file:
        if (line.lstrip()[0] == '*'):
          continue
        match = reference.match(line)
        if match:
          try:
            vals.append({'lab': match.group(1),
                         'val': float(match.group(2)),
                         'tol': int(match.group(3)),
                         'raw': line.strip()})
          except:
            print('*** Error in {0}: {1}'.format(basename(infofile), line.rstrip()))
            return '_RC_CHECK_ERROR_'
  except:
    pass

  # If generating check file, write the values
  if (get_utf8('MOLCAS_TEST', default='').upper().startswith('GENE')):
    try:
      maxcount = int(get_utf8('MOLCAS_MAX_CHECKS', default=''))
    except:
      maxcount = 500
    if (count > maxcount):
      print('*** Too many program calls in test file')
      return '_RC_CHECK_ERROR_'
    print('\nGenerating check file')
    if (count == 1):
      with utf8_open(checkfile, 'w') as c_file:
        c_file.write('* This file is autogenerated:\n')
        for line in get_utf8('MOLCAS_INFO', default='').split('!'):
          prefix = '*'
          if (len(line) > 0):
            prefix += ' '
          c_file.write('{0}{1}\n'.format(prefix, line))
    with utf8_open(checkfile, 'a') as c_file:
      c_file.write('#>> {0:3}\n'.format(count))
      for item in vals:
        c_file.write('{0}\n'.format(item['raw']))

  # If checking, read the reference values
  elif (get_utf8('MOLCAS_TEST', default='').upper().startswith('CHECK')):
    start = False
    refs = []
    try:
      with utf8_open(checkfile) as c_file:
        for line in c_file:
          if (line.lstrip()[0] == '*'):
            continue
          match = block.match(line)
          if (match):
            if (start):
              last = False
              break
            if (int(match.group(1)) == count):
              start = True
            continue
          if (start):
            try:
              match = reference.match(line)
              refs.append({'lab': match.group(1),
                           'val': float(match.group(2)),
                           'tol': int(match.group(3))})
            except:
              print('*** Error in {0}: {1}'.format(basename(checkfile), line.rstrip()))
              return '_RC_CHECK_ERROR_'
    except:
      pass

    # Compare actual and reference values, and print summary
    start = False
    print('\nChecking results:')
    factor = int(get_utf8('MOLCAS_THR', default='0'))
    passcheck = int(get_utf8('MOLCAS_PASSCHECK', default='0'))
    failrc = '_RC_ALL_IS_WELL_' if (passcheck) else '_RC_CHECK_ERROR_'
    fuzzy = get_utf8('MOLCAS_CHECK_FUZZY', default='').upper()
    fuzzy = ((fuzzy == 'YES') or (fuzzy == 'ON'))
    if (factor > 0):
      print('\n*** Tolerance increased by a factor of {0}'.format(10**factor))
    else:
      factor = 0
    if (passcheck):
      print('\n*** This check is informative only, nothing fails')
    fmt_head = '\n{0:^30} {1:^20} {2:^20} {3:^9} {4:^9}'.format('Label','Value','Reference','Error','Tolerance')
    # maximum exponent is 3 digits long, this makes at most 7 extra characters (exponent, decimal point, "e" and two signs)
    # but error and tolerance are absolute values
    fmt_num = '{0:<30} {1:20.13g} {2:20.13g} {3:9.3e} {4:9.3e} {5}'
    fmt_rule = '-'*92
    j = -1
    failtol = False
    for i in range(len(refs)):
      # Find the corresponding item in the actual values
      j += 1
      extra = []
      while ((j < len(vals)) and (refs[i]['lab'] != vals[j]['lab'])):
        extra.append(vals[j]['lab'])
        j += 1
        if (not fuzzy):
          rc = failrc
      if (j >= len(vals)):
        print('*** Label not found: {0}'.format(refs[i]['lab']))
        rc = failrc
        break
      else:
        for item in extra:
          print('*** Extra label: {0}'.format(item))
      # Check the difference against the tolerance (with some dirty tricks)
      if (vals[j]['tol'] != refs[i]['tol']):
        failtol = True
      dif = abs(vals[j]['val'] - refs[i]['val'])
      tol = -refs[i]['tol']
      if (tol < 0):
        tol = 10**(tol+factor)
      else:
        tol = tol*10**(factor)
      if (dif > 0.0):
        if (not start):
          print(fmt_head)
          print(fmt_rule)
          start = True
        if (dif > tol):
          if ((refs[i]['lab'] == 'POTNUC') and (dif < tol*10)):
            tag = 'Skipped'
          elif (refs[i]['lab'].endswith('ITER') and (dif < tol+5)):
            tag = 'Skipped'
          else:
            tag = 'Failed'
            rc = failrc
        else:
          tag = ''
        print(fmt_num.format(refs[i]['lab'], vals[j]['val'], refs[i]['val'], dif, tol, tag))
    # Additional values at the end
    j += 1
    while (j < len(vals)):
      print('*** Extra label: {0}'.format(vals[j]['lab']))
      j += 1
      if (not fuzzy):
        rc = failrc
    if (failtol):
      print('*** Tolerances do not match')
      rc = failrc
    if (start):
      print(fmt_rule)
    elif (rc == '_RC_ALL_IS_WELL_'):
      print('All values match the reference')

  return rc, last