-
Notifications
You must be signed in to change notification settings - Fork 4
/
cdsolver.py
1030 lines (819 loc) · 32 KB
/
cdsolver.py
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
Class solving the problem with a Conjugate Direction method.
Algorithm is from Tarantolla, 2005, Inverse Problem Theory, SIAM, Chap.6, p. 218.
Written by R. Jolivet 2017
License:
MPITS: Multi-Pixel InSAR Time Series
Copyright (C) 2018 <Romain Jolivet>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
import numpy as np
import itertools
import time
import sys, gc
import scipy.optimize as sciopt
# Myself
from . import utils
class cdsolver(object):
def __init__(self, massive, dataCovariance, modelCovariance,
orbitVariance=None, iteration=1, mprior=None, preconditioner=None,
rtol = None, atol = None, initialModel=None,
debug=False):
'''
Initializes the solver.
Args:
* massive : Instance of massivets
* dataCovariance : array/list of tuples of (Lambda; Sigma) for the data covariance
* modelCovariance : array/list of tuples of (Lambda; Sigma) for the model covariance
* orbitVariance : array/list of variances for the orbit parameters
* iteration : number of iterations
* mprior : PETSc vector of the a priori model (if None, set to 0.)
* rtol : Stop the code when the change in least square norm is less than rtol between
2 successive iterations (%)
* atol : Stop the code when the change in least square norm is less than atol (%)
* initialModel : Dictionary of the initial model {0: np.array(Ny, Nx), 2: np.array(Ny, Nx),....}
* preconditioner : PETSc Matrix for a preconditioner of the steepest Ascent vector (if None, identity is used).
The solver is going to iterate over the conjugate direction for least squares expression.
The covariances are exponential covariances so the matrix multiplication can be done in the Fourier
domain.
'''
self.debug = debug
# Get PETSc and the Communicator
self.PETSc = massive.PETSc
self.MPI = massive.MPI
self.Com = massive.MPI.COMM_WORLD
self.PETSc.Sys.Print('-------------------------------------------------------')
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' Initialize the Conjugate Direction for least squares solver')
# Pass some things
self.massive = massive
self.rank = self.massive.rank
# Talk to me
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' Prepare Covariances')
# Data Covariance
if type(dataCovariance) in (tuple, float):
self.Cd = [dataCovariance for i in range(self.massive.Ndata)]
else:
assert len(dataCovariance)==self.massive.Ndata,\
'Need to provide as many tuples of (Lambda, Sigma) as there is interferograms'
self.Cd = dataCovariance
self.dataConvolution = []
for cd in self.Cd:
if type(cd) is float:
self.dataConvolution.append(utils._diagonalConvolution)
elif type(cd) is tuple:
assert len(cd)==2, 'Tuple for exponential covariance needs 2 elements...'
self.dataConvolution.append(utils._expConvolution)
else:
assert False, '{}: Unkown covariance type for data...'.format(cd)
# Model Covariance
if type(modelCovariance) in (tuple, float):
self.Cm = [modelCovariance for i in range(self.massive.nParams)]
else:
assert len(modelCovariance)==self.massive.nParams, \
'Need to provide {} tuples of (Lambda, Sigma)'.format(self.massive.nParams)
self.Cm = modelCovariance
self.modelConvolution = []
for cm in self.Cm:
if type(cm) is float:
self.modelConvolution.append(utils._diagonalConvolution)
elif type(cm) is tuple:
assert len(cm)==2, 'Tuple for exponential covariance needs 2 elements...'
self.modelConvolution.append(utils._expConvolution)
else:
assert False, '{}: Unkown covariance type for model...'.format(cm)
# Orbit Variance
if self.massive.orbit:
assert orbitVariance is not None, 'You need to provide a variance for orbital parameters'
nOrb = self.massive.nOrb*self.massive.Nsar+self.massive.Nifg
if type(orbitVariance) is float:
self.oCm = [orbitVariance for i in range(nOrb)]
elif type(orbitVariance) is type(None):
self.oCm = None
else:
assert len(orbitVariance) == nOrb, 'Need to provide as many \
variance values as there is \
orbital parameters ({})'.format(nOrb)
self.oCm = orbitVariance
# iteration
assert type(iteration) is int and iteration>0, 'Iteration number has to be an integer > 0'
self.iteration = 0
self.iterations = iteration
self.rtol = rtol
self.atol = atol
self.Norms = []
self.uNorms = []
# Talk to me
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' Get some informations')
# Get things
self.m = self.massive.m
self.d = self.massive.d
self.G = self.massive.G
# Talk to me
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' Initialize tables')
self.PETSc.Sys.Print(' ')
# Some initialization things
self.massive.mIndex2ParamsPixels(onmyown=True)
# Initialize mprior
if mprior is None:
self.mprior = self.PETSc.Vec().createMPI(self.massive.Nc, comm=self.Com)
self.mprior.zeroEntries()
else:
self.mprior = mprior
self.mprior.assemble()
# Initialize Pre-Conditioner
self.F0 = None
if preconditioner is not None:
self.F0 = preconditioner
# X and Y arrays
x = range(self.massive.Nx)
y = range(self.massive.Ny)
self.x, self.y = np.meshgrid(x,y)
# Initialize first step
self.refineMu = False
self.muResidual = False
self.muLS = True
self.initialize(initialModel=initialModel)
self.printState(iteration=0)
# All done
return
def initialize(self, initialModel=None):
'''
Initialize the things needed.
'''
# Speak to me
self.PETSc.Sys.Print(' Initialize vectors for conjugate directions')
self.PETSc.Sys.Print(' ')
# Initialize the lists
self.steepestAscents = []
self.Lambdans = []
self.Phins = []
self.Alphans = []
self.Mus = []
self.ms = []
# Compute the first steepest ascent
self.resConv = False
self.computeSteepestAscent()
# Compute the first Lambdan
self.computeLambdan()
# The first Phin is Lambdan
self.phin = self.lambdan.copy()
self.phin.assemble()
self.Phins.append(self.phin)
# The first Alphans is 0.
self.alphan = 0.0
self.Alphans.append(self.alphan)
# Initialize mu
self.mu = None
self.Mus.append(self.mu)
# Initial Model
if initialModel is not None:
self.massive.setmodel(initialModel, vector=self.m)
# Save the first model
mcopy = self.m.copy()
mcopy.assemble()
self.ms.append(mcopy)
# Starting Norm
self.Norms.append(self.computeNorm())
self.uNorms.append(self.computeResidualNorm())
# All done
return
def Solve(self, view=False, zeroOutInitial=True, writeintermediatesolution=False,
plotthings=False):
'''
Solve the problem.
Args:
* view : If True, prints out a bunch of stuff from Solver.view()
* zeroOutInitial : False, uses self.m as an initial guess
* writeintermediatesolution : if True, writes every step in a new h5 file
'''
self.PETSc.Sys.Print('-------------------------------------------------------')
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' ')
self.PETSc.Sys.Print(' Solving...')
self.PETSc.Sys.Print(' ')
# Initialize check
self.runAgain = True
# Set initial Guess
if zeroOutInitial:
self.PETSc.Sys.Print(' Initial Model has been set to zero')
self.PETSc.Sys.Print(' ')
self.m.zeroEntries()
else:
self.PETSc.Sys.Print(' Initial Model taken from what was in self.m')
self.PETSc.Sys.Print(' ')
# Check if write
if writeintermediatesolution:
self.writeState2File(iteration=self.iteration)
# Iterations
self.runAgain = True
while self.runAgain:
# Make an additional iteration
self.oneIteration()
atol, rtol = self.getARTol(self.iteration)
# Check if one has to stop
if self.iteration>=self.iterations:
self.runAgain = False
if rtol is not None:
if rtol<self.rtol:
self.runAgain = False
if atol is not None:
if atol<self.atol:
self.runAgain = False
# Print state
self.printState(iteration=self.iteration)
if writeintermediatesolution:
self.writeState2File(iteration=self.iteration)
# Plot
if plotthings:
# plot something to screen
orbits = []
for m in self.ms:
orbs, ind, wor = self.massive.getOrbits(vector=m,
target=0)
orbits.append(orbs)
import matplotlib.pyplot as plt
if self.Com.Get_rank()==0:
plt.figure()
plt.subplot(511)
for steepest in self.steepestAscents:
sa = steepest.getValues(range(100))
plt.semilogy(np.abs(sa), '.-')
plt.subplot(512)
for m in self.ms:
mo = m.getValues(range(100))
plt.plot(mo, '.-')
plt.plot(self.mprior.getValues(range(100)), '+-k')
plt.subplot(513)
for m in self.ms:
mo = m.getValues(range(100))
plt.plot(np.abs(mo - self.mprior.getValues(range(100))), '+-')
plt.subplot(514)
plt.plot(self.residuals.getValues(range(130)))
plt.subplot(515)
for orb in orbits:
plt.plot(orb, '.-')
plt.show()
# Solved
self.massive.solved = True
# All done
return
def oneIteration(self):
'''
Do one iteration of the solver.
'''
# Calculate the residuals
self.computeSteepestAscent()
# Calculate Lambda_n
self.computeLambdan()
# Calculate Alpha_n
self.computeAlphan()
# Calculate Phi_n
self.computePhin()
# Update mu
self.mu = self.computeMuLinearised()
#assert self.mu>0., 'Why is mu<0?'
if self.refineMu:
if self.mu<0.: self.mu *= -1.
self.searchMu(initialGuess=self.mu)
self.Mus.append(self.mu)
# Update m
self.updatem()
# Update Norms and iteration
self.Norms.append(self.computeNorm())
self.uNorms.append(self.computeResidualNorm())
self.iteration += 1
# Activate Garbage Collector to clean up memory
gc.collect()
# All done
return
def computeNorm(self, model='m'):
'''
Compute the norm (d-Gm).Cd-1.(d-Gm) + (m-mprior)Cm-1(m-mprior).
'''
# Get m
if type(model) is str:
m = getattr(self, model)
else:
m = model
# Compute residuals
self.computeResiduals(model=m)
# If residuals have been convolved
self.cr = self.residuals.copy()
self.cr.assemble()
self.convolveDataSpace(vector=self.residuals, inverse=True)
gc.collect()
# Create a temporary vector
self.mdiff = m.copy()
self.mdiff.axpy(-1.0, self.mprior)
self.mdiff.assemble()
self.mdcopy = self.mdiff.copy()
self.mdcopy.assemble()
# Convolution
self.convolveModelSpace(self.mdiff, inverse=True)
gc.collect()
# Norm
mNorm = self.mdcopy.dot(self.mdiff)
dNorm = self.cr.dot(self.residuals)
norm = mNorm+dNorm
# Destroy temp vector
self.mdiff.destroy(); del self.mdiff
self.cr.destroy(); del self.cr
self.mdcopy.destroy(); del self.mdcopy
# All done
return norm, mNorm, dNorm
def computeResidualNorm(self, model='m'):
'''
Compute the l2 norm of the residuals (unweighted).
'''
# Compute the residuals
self.computeResiduals(factor=1., model=model)
# Get norm
norm = self.residuals.norm()
# all done
return norm
def computeResiduals(self, factor=1., model='m'):
'''
Computes the residuals (d-Gm)
'''
# Check if vector exists
if hasattr(self, 'residuals'):
self.residuals.destroy()
del self.residuals
# Create a residual vector if needed
self.residuals = self.PETSc.Vec().createMPI(self.massive.Nl, comm=self.Com)
self.residuals.assemble()
# Get m
if type(model) is str:
m = getattr(self, model)
else:
m = model
# Compute the prediction
self.G.mult(m, self.residuals)
# Compute the residuals
self.residuals.aypx(-1., self.d)
# Multiply
self.residuals *= factor
# Final
self.residuals.assemble()
# Not convolved
self.resConv = False
# All done
return
def computeSteepestAscent(self):
'''
Compute the steepest Ascent vector.
'''
# Create a vector if it does not exist
if hasattr(self, 'steepestAscent'):
self.steepestAscent.destroy()
del self.steepestAscent
self.steepestAscent = self.PETSc.Vec().createMPI(self.massive.Nc, comm=self.Com)
self.steepestAscent.assemble()
# Convolution in data space
if not self.resConv:
self.computeResiduals(factor=-1.)
self.convolveDataSpace(vector=self.residuals, inverse=True)
gc.collect()
# Multiplication
self.G.multTranspose(self.residuals, self.steepestAscent)
self.steepestAscent.assemble()
# Convolution in model space
self.convolveModelSpace(self.steepestAscent, inverse=False)
gc.collect()
self.steepestAscent.assemble()
# Add m
self.steepestAscent.axpy(1., self.m)
self.steepestAscent.assemble()
# Remove mprior
self.steepestAscent.axpy(-1., self.mprior)
self.steepestAscent.assemble()
# Keep it, copy and save
sacopy = self.steepestAscent.copy()
sacopy.assemble()
self.steepestAscents.append(sacopy)
# All done
return
def computeLambdan(self):
'''
Compute and save Lambdan.
'''
# Copy to create a new one of the same size
self.lambdan = self.steepestAscents[-1].copy()
self.lambdan.assemble()
# Precondition
if self.F0 is not None:
self.lambdan.zeroEntries()
self.F0.mult(self.steepestAscents[-1], self.lambdan)
# Copy and save
lacopy = self.lambdan.copy()
lacopy.assemble()
self.Lambdans.append(lacopy)
# All done
return
def computeAlphan(self):
'''
Compute and save Alphan
'''
# Copy Lambdans
lambdan = self.Lambdans[-1].copy()
lambdanminus1 = self.Lambdans[-2].copy()
lambdan.assemble()
lambdanminus1.assemble()
# Convolve these guys
self.convolveModelSpace(lambdan, inverse=True)
self.convolveModelSpace(lambdanminus1, inverse=True)
# Compute values
wn1 = self.steepestAscents[-1].dot(lambdan)
wn2 = self.steepestAscents[-2].dot(lambdanminus1)
wn3 = self.steepestAscents[-2].dot(lambdan)
self.alphan = (wn1 - wn3)/wn2
# Save it
self.Alphans.append(self.alphan)
# Clean up
lambdan.destroy(); del lambdan
lambdanminus1.destroy(); del lambdanminus1
gc.collect()
# All done
return
def computePhin(self):
'''
Compute and save Phin.
'''
# Create a new vector of the right size
self.phin = self.Phins[-1].copy()
self.phin.assemble()
# Do the multiplication
self.phin.aypx(self.Alphans[-1], self.Lambdans[-1])
self.phin.assemble()
# Copy and save
phcopy = self.phin.copy()
phcopy.assemble()
self.Phins.append(phcopy)
# All done
return
def searchMu(self, initialGuess=1e-10):
'''
Search for the function that will minimize the
Least Square norm.
'''
# Function
def createFunction(normtype, initialNorm, ownx):
def getLSNorm(snes, x, f):
# Get m
m = self.m.copy()
m.assemble()
# Get mu
if ownx:
mu = np.exp(x[0])
else:
mu = None
self.mu = self.Com.bcast(mu, root=0)
# Update m
m.axpy(-1.0*self.mu, self.Phins[-1])
m.assemble()
# Compute norm
if normtype is 'ls':
a = self.computeNorm(model=m)[0]
elif normtype is 'residual':
a = self.computeResidualNorm(model=m)
# Put it in f
if ownx:
f[0] = a/initialNorm
#self.PETSc.Sys.Print('Mu: {} -- {} -- {} -- {}'.\
#format(self.mu, a, initialNorm, a/initialNorm))
# All done
f.assemble()
# Save it
self.muFunction = getLSNorm
# All done
return
# Print stuff
self.PETSc.Sys.Print('--------------------------------------------')
self.PETSc.Sys.Print('--------------------------------------------')
self.PETSc.Sys.Print('Searchin for the best Step Size...')
self.PETSc.Sys.Print('Initial Guess: {}'.format(initialGuess))
# Create a snes solver to search for mu
snes = self.PETSc.SNES().create()
snes.setType(self.PETSc.SNES.Type.NCG)
# Create the vectors we need
f = self.PETSc.Vec().createMPI(1, comm=self.Com)
b = self.PETSc.Vec().createMPI(1, comm=self.Com)
# Who owns it
ownx = False
if b.getOwnershipRange()[0]<=self.Com.Get_rank()<b.getOwnershipRange()[1]:
ownx = True
# Initialize them
b.set(0.0)
# Assemble
b.assemble()
# Search for mu that will minimize the residual norm
if self.muResidual and not self.muLS:
self.PETSc.Sys.Print('Searching first guess based on the Residual norm')
initialNorm = self.computeResidualNorm()
createFunction('residual', initialNorm, ownx)
# Minimize then the LS norm
elif self.muLS and not self.muResidual:
self.PETSc.Sys.Print('Searching Step Size by optimizing the LS norm')
initialNorm = self.computeNorm()[0]
createFunction('ls', initialNorm, ownx)
else:
assert False, 'I cannot understand your choice to optimize step size'
# Line search
opts = self.PETSc.Options()
opts['snes_linesearch_type'] = 'l2'
snes.setFromOptions()
# Tolerances
snes.setTolerances(1e-50, 1e-50, 100, 10000)
# Convergence History
snes.setConvergenceHistory()
# Set function
snes.setFunction(self.muFunction, f)
# Initial Guess
x = self.PETSc.Vec().createMPI(1, comm=self.Com)
x.set(np.log(initialGuess))
x.assemble()
# Solve
snes.solve(b, x)
## View
#snes.view()
# Save it
if ownx:
if initialNorm<f[0]:
mu = initialGuess
else:
mu = np.exp(x[0])
else:
mu = None
self.mu = self.Com.bcast(mu, root=0)
# Print stuff
self.PETSc.Sys.Print('Best Step Size found: {}'.format(self.mu))
# All done
return
def computeMuLinearised(self):
'''
Estimate Mu by linearisation of g(m) (Tarantolla, 2005, p. 232)
'''
# Convolve self.phin
self.convolveModelSpace(self.phin, inverse=True)
gc.collect()
# Get values
up = self.steepestAscents[-1].dot(self.phin)
down1 = self.Phins[-1].dot(self.phin)
# temporary vector
self.bn = self.d.copy()
self.bn.assemble()
self.G.mult(self.Phins[-1], self.bn)
# Second temporary vector
self.bn2 = self.bn.copy()
self.bn2.assemble()
# Convolve temporary vector
self.convolveDataSpace(vector=self.bn, inverse=True)
gc.collect()
# Get values
down2 = self.bn2.dot(self.bn)
# Delete self.bn
self.bn.destroy()
self.bn2.destroy()
del self.bn
del self.bn2
# All done
return up/(down1+down2)
def updatem(self):
'''
Compute the in-place update of m
'''
# Update current m
self.m.axpy(-1.0*self.mu, self.Phins[-1])
self.m.assemble()
# Copy and save
mcopy = self.m.copy()
mcopy.assemble()
self.ms.append(mcopy)
# all done
return
def convolveDataSpace(self, vector, inverse=False):
'''
Do the convolution of the residuals.
'''
# Get the residual interferograms
tstart = time.ctime()
dataSpace = self.massive.getDataSpace(vector=vector)
tend = time.ctime()
if self.debug:
self.PETSc.Sys.Print('-------------------------------------')
self.PETSc.Sys.Print('-------------------------------------')
self.PETSc.Sys.Print('Data Convolution.....')
self.PETSc.Sys.Print('-------------------------------------')
self.PETSc.Sys.Print('-------------------------------------')
self.Com.Barrier()
print('Worker {} gets the data between {} and {}: {}'.format(self.Com.Get_rank(), tstart, tend, [data[4] for data in dataSpace]))
# Wait
self.Com.Barrier()
# Do the convolutions
for data in dataSpace:
tstart = time.ctime()
# Get Lambda and Sigma
covariance = self.Cd[data[4]]
# get the convolution function
dataConv = self.dataConvolution[data[4]]
# Do the convolution
conv = dataConv(data[3], # Image
data[0], # X coord
data[1], # Y coord
self.massive.dx,
self.massive.dy,
covariance,
inverse=inverse)
# Save the results
data[3] = conv
# Set the results
tend = time.ctime()
if self.debug:
print('Data Convolution Worker {}: Ifg {} start: {}, end: {}'.format(self.Com.Get_rank(), data[4], tstart, tend))
# Wait
self.Com.Barrier()
# Send back the data
tstart = time.ctime()
self.massive.setbackDataSpace(dataSpace, vector)
tend = time.ctime()
if self.debug:
print('Worker {} puts back the data between {} and {}'.format(self.Com.Get_rank(), tstart, tend))
# Clean up
del dataSpace
if 'conv' in locals():
del conv
# check
if vector==self.residuals:
self.resConv = True
# All done
return
def convolveModelSpace(self, vector, inverse=False):
'''
Do the convolution in the Model space.
'''
cm = self.massive.m.copy()
cm.assemble()
# Models
tstart = time.ctime()
Models = self.massive.getModelSpace(vector=vector, target=None)
tend = time.ctime()
if self.debug:
self.PETSc.Sys.Print('-------------------------------------')
self.PETSc.Sys.Print('-------------------------------------')
self.PETSc.Sys.Print('Model Convolution.....')
self.PETSc.Sys.Print('-------------------------------------')
self.PETSc.Sys.Print('-------------------------------------')
self.Com.Barrier()
print('Worker {} gets the model between {} and {}: {}'.format(self.Com.Get_rank(), tstart, tend, [model[4] for model in Models]))
# Wait
self.Com.Barrier()
# Convolve models
for model in Models:
tstart = time.ctime()
# Get Lambda and Sigma
covariance = self.Cm[model[4]]
# Get the function
modelConv = self.modelConvolution[model[4]]
# Do the convolution
conv = modelConv(model[3],
model[0],
model[1],
self.massive.dx,
self.massive.dy,
covariance,
inverse=inverse)
# Save the results
model[3] = conv
# Set the results
tend = time.ctime()
if self.debug:
print('Model Convolution Worker {}: Model {} start: {}, end: {}'.format(self.Com.Get_rank(), model[4], tstart, tend))
# Wait
self.Com.Barrier()
# Put back the model vector
tstart = time.ctime()
self.massive.setbackModelSpace(Models, vector=vector)
tend = time.ctime()
if self.debug:
print('Worker {} puts back the model between {} and {}'.format(self.Com.Get_rank(), tstart, tend))
# Clean up
del Models
if 'conv' in locals():
del conv
# Multiply orbits by variance
if self.oCm is not None:
Orbits, Indexes, Workers = self.massive.getOrbits(vector=vector, target=0)
convOrbits = []
for orbit,variance in zip(Orbits,self.oCm):
if inverse:
convOrbits.append(orbit / variance)
else:
convOrbits.append(orbit * variance)
self.massive.setbackOrbits([convOrbits, Indexes, Workers], vector=vector)
# All done
return
def writeState2File(self, iteration):
'''
Writes the solution to a in the h5file.
'''
# Write the model to file
name = 'parms {:d}'.format(iteration)
self.massive.writeModel2File(name=name)
# Write the phase evolution
name = 'recons {:d}'.format(iteration)
self.massive.writePhaseEvolution2File(name=name, Jmat=False)
return
def getARTol(self, iteration):
'''
Returns the relative change in the Least-Sqaure norm between iteration and
iteration-1 (in percent).
'''
# Get it
rtol = 1.-(self.Norms[iteration][0]/self.Norms[iteration-1][0])
atol = (self.Norms[iteration][0]/self.Norms[0][0])
# All done
return atol, rtol
def cleanup(self):
'''
Cleans up memory
'''
del self.Alphans
for lambdan in self.Lambdans:
lambdan.destroy()
del lambdan
del self.Lambdans
for phin in self.Phins:
phin.destroy()
del phin
del self.Phins
for sa in self.steepestAscents:
sa.destroy()
del sa
del self.steepestAscents
# Call the garbage collcetor
gc.collect()
# All done
return
def printState(self, iteration=0, nel=3, minmax=True):
'''
Show me what the current state is...
'''
self.PETSc.Sys.Print('--------------------------------------------')
self.PETSc.Sys.Print('--------------------------------------------')
self.PETSc.Sys.Print('Iteration {} / {}: '.format(iteration, self.iterations))
self.PETSc.Sys.Print('Iter. {}: Step Size: {}'.format(iteration, self.Mus[-1]))
# Print norm
try:
self.PETSc.Sys.Print('Iter. {}: Least Squares Norm: {} ({}%% reduction)'\
.format(iteration,
self.Norms[-1][0],
100.*(1.-(self.Norms[-1][0]/self.Norms[-2][0]))))
except:
self.PETSc.Sys.Print('Iter. {}: Least Squares Norm: {}'.format(iteration, self.Norms[-1][0]))
self.PETSc.Sys.Print('Iter. {}: Model Contrib. {}'.format(iteration, self.Norms[-1][1]))
self.PETSc.Sys.Print('Iter. {}: Data Contrib. {}'.format(iteration, self.Norms[-1][2]))
try:
self.PETSc.Sys.Print('Iter. {}: Unweighted Residual Norm: {} ({}%% reduction)'.\
format(iteration,
self.uNorms[-1],
100.*(1. - self.uNorms[-1]/self.uNorms[-2])))
except:
self.PETSc.Sys.Print('Iter. {}: Unweighted Residual Norm: {}'.\
format(iteration, self.uNorms[-1]))