1 #-*-coding:iso-8859-1-*-
8 from daCore.AssimilationStudy import AssimilationStudy
12 def __init__(self, optim_algo):
13 self.optim_algo = optim_algo
16 self.sample_counter = 0
17 self.counter_lock = threading.Lock()
19 def Direct(self, X, sync = true):
20 print "Call Direct OptimizerHooks"
22 # 1: Get a unique sample number
23 self.counter_lock.acquire()
24 self.sample_counter += 1
25 local_counter = self.sample_counter
27 # 2: Put sample in the job pool
28 matrix_to_pool = pickle.dumps(X)
29 self.optim_algo.pool.pushInSample(local_counter, matrix_to_pool)
33 self.optim_algo.signalMasterAndWait()
34 if self.optim_algo.isTerminationRequested():
35 self.optim_algo.pool.destroyAll()
39 sample_id = self.pool.getCurrentId()
40 if sample_id == local_counter:
42 matrix_from_pool = self.optim_algo.pool.getOutSample(local_counter).getStringValue()
45 # Have to be done before but need a new implementation
46 # of the optimizer loop
47 self.counter_lock.release()
50 Y = pickle.loads(matrix_from_pool)
53 print "sync false is not yet implemented"
54 raise ValueError("sync == false not yet implemented")
56 def Tangent(self, X, sync = true):
57 print "Call Tangent OptimizerHooks"
59 # 1: Get a unique sample number
60 self.counter_lock.acquire()
61 self.sample_counter += 1
62 local_counter = self.sample_counter
64 # 2: Put sample in the job pool
65 matrix_to_pool = pickle.dumps(X)
66 self.optim_algo.pool.pushInSample(local_counter, matrix_to_pool)
70 self.optim_algo.signalMasterAndWait()
71 if self.optim_algo.isTerminationRequested():
72 self.optim_algo.pool.destroyAll()
76 sample_id = self.pool.getCurrentId()
77 if sample_id == local_counter:
79 matrix_from_pool = self.optim_algo.pool.getOutSample(local_counter).getStringValue()
82 # Have to be done before but need a new implementation
83 # of the optimizer loop
84 self.counter_lock.release()
87 Y = pickle.loads(matrix_from_pool)
90 print "sync false is not yet implemented"
91 raise ValueError("sync == false not yet implemented")
93 def Adjoint(self, (X, Y), sync = true):
94 print "Call Adjoint OptimizerHooks"
96 # 1: Get a unique sample number
97 self.counter_lock.acquire()
98 self.sample_counter += 1
99 local_counter = self.sample_counter
101 # 2: Put sample in the job pool
102 matrix_to_pool = pickle.dumps(Y)
103 self.optim_algo.pool.pushInSample(local_counter, matrix_to_pool)
107 self.optim_algo.signalMasterAndWait()
108 if self.optim_algo.isTerminationRequested():
109 self.optim_algo.pool.destroyAll()
113 sample_id = self.pool.getCurrentId()
114 if sample_id == local_counter:
116 matrix_from_pool = self.optim_algo.pool.getOutSample(local_counter).getStringValue()
119 # Have to be done before but need a new implementation
120 # of the optimizer loop
121 self.counter_lock.release()
124 Z = pickle.loads(matrix_from_pool)
127 print "sync false is not yet implemented"
128 raise ValueError("sync == false not yet implemented")
130 class AssimilationAlgorithm_asynch_3DVAR(SALOMERuntime.OptimizerAlgASync):
133 SALOMERuntime.RuntimeSALOME_setRuntime()
134 SALOMERuntime.OptimizerAlgASync.__init__(self, None)
135 self.runtime = SALOMERuntime.getSALOMERuntime()
137 # Definission des types d'entres et de sorties pour le code de calcul
138 self.tin = self.runtime.getTypeCode("pyobj")
139 self.tout = self.runtime.getTypeCode("pyobj")
141 self.optim_hooks = OptimizerHooks(self)
143 # input vient du port algoinit, input est un Any YACS !
144 def initialize(self,input):
145 print "Algorithme initialize"
147 def startToTakeDecision(self):
148 print "Algorithme startToTakeDecision"
155 xt = numpy.matrix(numpy.arange(dimension)).T
156 Eo = numpy.matrix(numpy.zeros((dimension,))).T
157 Eb = numpy.matrix(numpy.zeros((dimension,))).T
158 H = numpy.matrix(numpy.core.identity(dimension))
160 yo = FunctionH( xt ) + Eo
163 R = numpy.matrix(numpy.core.identity(dimension)).T
164 B = numpy.matrix(numpy.core.identity(dimension)).T
166 ADD = AssimilationStudy()
167 ADD.setBackground (asVector = xb )
168 ADD.setBackgroundError (asCovariance = B )
169 ADD.setObservation (asVector = yo )
170 ADD.setObservationError (asCovariance = R )
171 ADD.setObservationOperator(asFunction = {"Tangent":FunctionH,
172 "Adjoint":AdjointH} )
174 ADD.setAlgorithm(choice="3DVAR")
177 xa = numpy.array(ADD.get("Analysis").valueserie(0))
178 d = numpy.array(ADD.get("Innovation").valueserie(0))
179 if max(abs(xa - xb)) > precision:
180 raise ValueError("Résultat du test erroné (1)")
181 elif max(abs(d)) > precision:
182 raise ValueError("Résultat du test erroné (2)")
184 print " Test correct, erreur maximale inférieure à %s"%precision
187 self.pool.destroyAll()
191 print "Algorithme finish"
192 def parseFileToInit(self,fileName):
193 print "Algorithme parseFileToInit"
195 # Fonctions qui ne changent pas
196 def setPool(self,pool):
198 def getTCForIn(self):
200 def getTCForOut(self):