source: issm/trunk/src/py3/classes/toolkits.py@ 20500

Last change on this file since 20500 was 20500, checked in by Mathieu Morlighem, 9 years ago

merged trunk-jpl and trunk for revision 20497

File size: 3.3 KB
Line 
1from IssmConfig import IssmConfig
2from mumpsoptions import mumpsoptions
3from iluasmoptions import iluasmoptions
4from EnumToString import EnumToString
5from fielddisplay import fielddisplay
6from EnumDefinitions import *
7from checkfield import checkfield
8
9class toolkits(object):
10 """
11 TOOLKITS class definition
12
13 Usage:
14 self=toolkits();
15 """
16
17 def __init__(self): # {{{
18 #default toolkits
19 if IssmConfig('_HAVE_PETSC_')[0]:
20 #MUMPS is the default toolkits
21 if IssmConfig('_HAVE_MUMPS_')[0]:
22 self.DefaultAnalysis = mumpsoptions()
23 else:
24 self.DefaultAnalysis = iluasmoptions()
25 else:
26 if IssmConfig('_HAVE_MUMPS_')[0]:
27 self.DefaultAnalysis = issmmumpssolver()
28 elif IssmConfig('_HAVE_GSL_')[0]:
29 self.DefaultAnalysis = issmgslsolver()
30 else:
31 raise IOError("ToolkitsFile error: need at least Mumps or Gsl to define issm solver type")
32 #The other properties are dynamic
33 # }}}
34 def __repr__(self): # {{{
35 print('enter repr')
36 s ="List of toolkits options per analysis:\n\n"
37 for analysis in list(vars(self).keys()):
38 s+="%s\n" % fielddisplay(self,analysis,'')
39
40 return s
41 # }}}
42 def addoptions(self,analysis,*args): # {{{
43 print('enter addoption')
44 # Usage example:
45 # md.toolkits=addoptions(md.toolkits,StressbalanceAnalysisEnum(),FSoptions());
46 # md.toolkits=addoptions(md.toolkits,StressbalanceAnalysisEnum());
47
48 #Convert analysis from enum to string
49 [analysis]=EnumToString(analysis)
50
51 #Create dynamic property if property does not exist yet
52 if not hasattr(self,analysis):
53# exec("self.%s = None" % analysis)
54 setattr(self,analysis,None)
55
56 #Add toolkits options to analysis
57 if len(args)==1:
58 setattr(self,analysis,args[0])
59
60 return self
61 # }}}
62 def checkconsistency(self,md,solution,analyses): # {{{
63 print('enter check')
64 for analysis in list(vars(self).keys()):
65 if not getattr(self,analysis):
66 md.checkmessage("md.toolkits.%s is empty" % analysis)
67
68 return md
69 # }}}
70 def ToolkitsFile(self,filename): # {{{
71 """
72 TOOLKITSFILE- build toolkits file
73
74 Build a Petsc compatible options file, from the toolkits model field + return options string
75 This file will also be used when the toolkit used is 'issm' instead of 'petsc'
76
77
78 Usage: ToolkitsFile(toolkits,filename);
79 """
80
81 #open file for writing
82 try:
83 fid=open(filename,'w')
84 except IOError as e:
85 raise IOError("ToolkitsFile error: could not open '%s' for writing." % filename)
86
87 #write header
88 fid.write("%s%s%s\n" % ('%Petsc options file: ',filename,' written from Matlab toolkits array'))
89
90 #start writing options
91 for analysis in list(vars(self).keys()):
92 options=getattr(self,analysis)
93
94 #first write analysis:
95 fid.write("\n+%s\n" % analysis) #append a + to recognize it's an analysis enum
96
97 #now, write options
98 for optionname,optionvalue in list(options.items()):
99
100 if not optionvalue:
101 #this option has only one argument
102 fid.write("-%s\n" % optionname)
103 else:
104 #option with value. value can be string or scalar
105 if isinstance(optionvalue,(bool,int,float)):
106 fid.write("-%s %g\n" % (optionname,optionvalue))
107 elif isinstance(optionvalue,str):
108 fid.write("-%s %s\n" % (optionname,optionvalue))
109 else:
110 raise TypeError("ToolkitsFile error: option '%s' is not well formatted." % optionname)
111
112 fid.close()
113 # }}}
Note: See TracBrowser for help on using the repository browser.