~reducedmodelling/fluidity/ROM_Non-intrusive-ann

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
#!/usr/bin/env python

# James Maddison

import getopt
import sys

try:
  import psyco
  psyco.full()
except:
  pass

import vtktools

def EPrint(message):
  """
  Send an error message to standard error
  """

  sys.stderr.write(message + "\n")
  sys.stderr.flush()

  return

def Help():
  """
  Prints program usage information
  """

  print "Usage: vtudiff [OPTIONS] ... INPUT1 INPUT2 OUTPUT [FIRST] [LAST]\n" + \
        "\n" + \
        "Generates vtus with fields equal to the difference between the corresponding\n" + \
        "fields in two input vtus (INPUT1 - INPUT2). The fields of INPUT2 are projected\n" + \
        "onto the cell points of INPUT1.\n" + \
        "\n" + \
        "If FIRST is supplied, treats INPUT1 and INPUT2 as project names, and generates\n" + \
        "a different vtu for the specified range of output files.\n" + \
        "\n" + \
        "Options:\n" + \
        "\n" + \
        "-s  If supplied together with FIRST and LAST, only INPUT1 is treated as a\n" + \
        "    project name. Allows a range of vtus to be diffed against a single vtu."

  return

def Error(message, displayHelp = True):
  """
  Print an error message, usage information and quit
  """

  if displayHelp:
    Help()

  EPrint(message)
  sys.exit(1)

try:
  opts, args = getopt.getopt(sys.argv[1:], "ms")
except:
  Help()
  sys.exit(1)

if len(args) > 5:
  Error("Invalid argument \"" + args[5] + "\" supplied")

diffAgainstSingle = ("-s", "") in opts

try:
  inputFilename1 = args[0]
  inputFilename2 = args[1]
  outputFilename = args[2]
except:
  Help()
  sys.exit(1)

if len(args) > 3:
  try:
    firstId = int(args[3])
    if len(args) > 4:
      try:
        lastId = int(args[4])
      except:
        Error("Invalid last ID entered")
    else:
      lastId = firstId
  except:
    Error("Invalid first ID entered")
else:
  firstId = None

if firstId is None:
  inputFilenames1 = [inputFilename1]
  inputFilenames2 = [inputFilename2]
  outputFilenames = [outputFilename]
else:
  inputFilenames1 = [inputFilename1 + "_" + str(i) + ".vtu" for i in range(firstId, lastId + 1)]
  if diffAgainstSingle:
    inputFilenames2 = [inputFilename2 for i in range(firstId, lastId + 1)]
  else:
    inputFilenames2 = [inputFilename2 + "_" + str(i) + ".vtu" for i in range(firstId, lastId + 1)]

  outputFilenames = [outputFilename + "_" + str(i) + ".vtu" for i in range(firstId, lastId + 1)]

for i in range(len(inputFilenames1)):
  try:
    vtu1 = vtktools.vtu(inputFilenames1[i])
  except:
    Error("Unable to read input vtu \"" + inputFilenames1[i] + "\"", False)
  try:
    vtu2 = vtktools.vtu(inputFilenames2[i])
  except:
    Error("Unable to read input vtu \"" + inputFilenames2[i] + "\"", False)

  diffVtu = vtktools.VtuDiff(vtu1, vtu2, outputFilenames[i])

  try:
    diffVtu.Write()
  except:
    Help()
    Error("Unable to write output file \"" + outputFilenames[i] + "\"", False)

  print "Generated vtu diff file \"" + outputFilenames[i] + "\""