source: issm/trunk-jpl/src/py3/classes/qmu.py@ 23670

Last change on this file since 23670 was 23670, checked in by bdef, 6 years ago

CHG: python scripts after 2to3 and indentation fix

File size: 7.1 KB
RevLine 
[23670]1import numpy as np
2from MatlabFuncs import *
3from IssmConfig import *
[19895]4from project3d import project3d
5from collections import OrderedDict
6from fielddisplay import fielddisplay
7from checkfield import checkfield
8from WriteData import WriteData
[23670]9from helpers import *
10from dakota_method import *
[19895]11
12class qmu(object):
13 """
14 QMU class definition
15
16 Usage:
17 qmu=qmu();
18 """
19
20 def __init__(self): # {{{
21 self.isdakota = 0
[23670]22 self.variables = OrderedStruct()
23 self.responses = OrderedStruct()
[19895]24 self.method = OrderedDict()
[23670]25 self.params = OrderedStruct()
[19895]26 self.results = OrderedDict()
27 self.partition = float('NaN')
28 self.numberofpartitions = 0
29 self.numberofresponses = 0
30 self.variabledescriptors = []
31 self.responsedescriptors = []
32 self.mass_flux_profile_directory = float('NaN')
33 self.mass_flux_profiles = float('NaN')
34 self.mass_flux_segments = []
35 self.adjacency = float('NaN')
36 self.vertex_weight = float('NaN')
37
38 #set defaults
39 self.setdefaultparameters()
40
41 #}}}
42 def __repr__(self): # {{{
43 s =' qmu parameters:\n'
44
45 s+="%s\n" % fielddisplay(self,'isdakota','is qmu analysis activated?')
[23670]46 maxlen = 0
47 s+=" variables: (arrays of each variable class)\n"
[19895]48
[23670]49 # OrderedStruct's iterator returns individual name/array-of-functions pairs
50 for variable in self.variables:
51 fname=variable[0]
52 maxlen=max(maxlen,len(fname))
53 size = np.shape(variable[1])
54 a = size[0]
55 b = 1 if len(size) < 2 else size[1]
56 s+=" %-*s: [%ix%i] '%s'\n" % (maxlen+1,fname,a,b,type(variable[1][0]))
[19895]57
[23670]58 s+=" responses: (arrays of each response class)\n"
59 for response in self.responses:
60 fname=response[0]
61 maxlen=max(maxlen,len(fname))
62 size = np.shape(response[1])
63 a = size[0]
64 b = 1 if len(size) < 2 else size[1]
65 s+=" %-*s: [%ix%i] '%s'\n" % (maxlen+1,fname,a,b,type(response[1][0]))
[19895]66
[23670]67 s+="%s\n" % fielddisplay(self,'numberofresponses','number of responses')
[19895]68
[23670]69 if type(self.method) != OrderedDict:
70 self.method = [self.method]
71 # self.method must be iterable
72 for method in self.method:
73 if isinstance(method,dakota_method):
74 s+=" method : '%s'\n" % (method.method)
[19895]75
[23670]76 # params could be have a number of forms (mainly 1 struct or many)
77 if type(self.params) == OrderedStruct:
78 params = [self.params]
79 else:
80 params = np.hstack(np.atleast_1d(np.array(self.params)))
81 for param in params:
82 print(type(param))
83 print(param)
84 s+=" params: (array of method-independent parameters)\n"
[19895]85 fnames=vars(param)
86 maxlen=0
87 for fname in fnames:
88 maxlen=max(maxlen,len(fname))
89
90 for fname in fnames:
[23670]91 s+=" %-*s: %s\n" % (maxlen+1,fname,str(getattr(param,fname)))
[19895]92
[23670]93 # results could be have a number of forms (mainly 1 struct or many)
94 results = np.hstack(np.atleast_1d(np.array(self.results)))
95 for result in results:
96 s+=" results: (information from dakota files)\n"
[19895]97 fnames=vars(result)
98 maxlen=0
99 for fname in fnames:
100 maxlen=max(maxlen,len(fname))
101
102 for fname in fnames:
[23670]103 size = np.shape(response[1])
104 a = size[0]
105 b = 0 if len(size) < 2 else size[1]
106 size = np.shape(getattr(result,fname))
107 s+=" %-*s: [%ix%i] '%s'\n" % (maxlen+1,fname,a,b,type(getattr(result,fname)))
[19895]108
109 s+="%s\n" % fielddisplay(self,'partition','user provided mesh partitioning, defaults to metis if not specified')
110 s+="%s\n" % fielddisplay(self,'numberofpartitions','number of partitions for semi-discrete qmu')
111 s+="%s\n" % fielddisplay(self,'variabledescriptors','')
112 s+="%s\n" % fielddisplay(self,'responsedescriptors','')
113 s+="%s\n" % fielddisplay(self,'method','array of dakota_method class')
114 s+="%s\n" % fielddisplay(self,'mass_flux_profile_directory','directory for mass flux profiles')
115 s+="%s\n" % fielddisplay(self,'mass_flux_profiles','list of mass_flux profiles')
116 s+="%s\n" % fielddisplay(self,'mass_flux_segments','')
117 s+="%s\n" % fielddisplay(self,'adjacency','')
118 s+="%s\n" % fielddisplay(self,'vertex_weight','weight applied to each mesh vertex')
119
120 return s
121 # }}}
122 def extrude(self,md): # {{{
[23670]123 self.partition=project3d(md,'vector',np.transpose(self.partition),'type','node')
[19895]124 return self
125 #}}}
126 def setdefaultparameters(self): # {{{
127 return self
128 #}}}
129 def checkconsistency(self,md,solution,analyses): # {{{
130
131 #Early return
132 if not md.qmu.isdakota:
133 return
134
[23670]135 version=IssmConfig('_DAKOTA_VERSION_')
136 version=float(version[0])
137
138 if version < 6:
139 if not md.qmu.params.evaluation_concurrency==1:
140 md.checkmessage("concurrency should be set to 1 when running dakota in library mode")
141 else:
142 if not strcmpi(self.params.evaluation_scheduling,'master'):
143 md.checkmessage('evaluation_scheduling in qmu.params should be set to "master"')
144
145 if md.cluster.np <= 1:
146 md.checkmessage('in parallel library mode, Dakota needs to run on at least 2 cpus, 1 cpu for the master, 1 cpu for the slave. Modify md.cluser.np accordingly.')
147
148 if self.params.processors_per_evaluation < 1:
149 md.checkmessage('in parallel library mode, Dakota needs to run at least one slave on one cpu (md.qmu.params.processors_per_evaluation >=1)!')
150
151 if np.mod(md.cluster.np-1,self.params.processors_per_evaluation):
152 md.checkmessage('in parallel library mode, the requirement is for md.cluster.np = md.qmu.params.processors_per_evaluation * number_of_slaves, where number_of_slaves will automatically be determined by Dakota. Modify md.cluster.np accordingly')
153
154 if np.size(md.qmu.partition) > 0:
155 if np.size(md.qmu.partition)!=md.mesh.numberofvertices and np.size(md.qmu.partition) != md.mesh.numberofelements:
156 md.checkmessage("user supplied partition for qmu analysis should have size (md.mesh.numberofvertices x 1) or (md.mesh.numberofelements x 1)")
157 if not min(md.qmu.partition.flatten())==0:
[19895]158 md.checkmessage("partition vector not indexed from 0 on")
[23670]159 if max(md.qmu.partition.flatten())>=md.qmu.numberofpartitions:
[19895]160 md.checkmessage("for qmu analysis, partitioning vector cannot go over npart, number of partition areas")
161
162 return md
163 # }}}
[23670]164 def marshall(self,prefix,md,fid): # {{{
165 WriteData(fid,prefix,'object',self,'fieldname','isdakota','format','Boolean')
[19895]166 if not self.isdakota:
[23670]167 WriteData(fid,prefix,'data',False,'name','md.qmu.mass_flux_segments_present','format','Boolean');
[19895]168 return
[23670]169 WriteData(fid,prefix,'object',self,'fieldname','partition','format','DoubleMat','mattype',2)
170 WriteData(fid,prefix,'object',self,'fieldname','numberofpartitions','format','Integer')
171 WriteData(fid,prefix,'object',self,'fieldname','numberofresponses','format','Integer')
172 WriteData(fid,prefix,'object',self,'fieldname','variabledescriptors','format','StringArray')
173 WriteData(fid,prefix,'object',self,'fieldname','responsedescriptors','format','StringArray')
174 if not isempty(self.mass_flux_segments):
175 WriteData(fid,prefix,'data',self.mass_flux_segments,'name','md.qmu.mass_flux_segments','format','MatArray');
[19895]176 flag=True;
177 else:
178 flag=False;
[23670]179 WriteData(fid,prefix,'data',flag,'name','md.qmu.mass_flux_segments_present','format','Boolean');
[19895]180 # }}}
Note: See TracBrowser for help on using the repository browser.