1 | #include <iostream>
|
---|
2 | #include <cmath>
|
---|
3 | #include <fstream>
|
---|
4 | #include <string>
|
---|
5 | #include <cstdio>
|
---|
6 | #include <cstdlib>
|
---|
7 | #include <time.h>
|
---|
8 | #include <cassert>
|
---|
9 | #include <gsl/gsl_multifit.h>
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | class Matrix{/*{{{*/
|
---|
14 | private:
|
---|
15 | int M; /*Number of lines */
|
---|
16 | int N; /*Number if Columns */
|
---|
17 | double *values;
|
---|
18 | public:
|
---|
19 | Matrix(int m_in,int n_in){/*{{{*/
|
---|
20 | this->M = m_in;
|
---|
21 | this->N = n_in;
|
---|
22 | this->values = new double[M*N];
|
---|
23 | }/*}}}*/
|
---|
24 | ~Matrix(){/*{{{*/
|
---|
25 | delete [] this->values;
|
---|
26 | }/*}}}*/
|
---|
27 | void Echo(void){/*{{{*/
|
---|
28 | for(int i=0;i<M;i++){
|
---|
29 | for(int j=0;j<N;j++){
|
---|
30 | cout << " " << this->values[i*N+j];
|
---|
31 | }
|
---|
32 | cout << endl;
|
---|
33 | }
|
---|
34 | }/*}}}*/
|
---|
35 | void SetValue(int i,int j,double value){/*{{{*/
|
---|
36 | this->values[i*N+j] = value;
|
---|
37 | }/*}}}*/
|
---|
38 | double GetValue(int i,int j){/*{{{*/
|
---|
39 | return this->values[i*N+j];
|
---|
40 | }/*}}}*/
|
---|
41 | void GetSize(int* pM,int* pN){/*{{{*/
|
---|
42 | *pM = this->M;
|
---|
43 | *pN = this->N;
|
---|
44 | }/*}}}*/
|
---|
45 | double* GetPointer(void){/*{{{*/
|
---|
46 | return this->values;
|
---|
47 | }/*}}}*/
|
---|
48 | void MatrixSum(Matrix* A,Matrix* B){/*{{{*/
|
---|
49 | /*Check that sizes are compatible*/
|
---|
50 | int M_B,N_B,M_A,N_A;
|
---|
51 | B->GetSize(&M_B,&N_B);
|
---|
52 | A->GetSize(&M_A,&N_A);
|
---|
53 | assert(this->M==M_B && this->N==N_B);
|
---|
54 | assert(this->M==M_A && this->N==N_A);
|
---|
55 | for(int i=0;i<this->M;i++){
|
---|
56 | for(int j=0;j<this->N;j++){
|
---|
57 | this->SetValue(i,j,A->GetValue(i,j) + B->GetValue(i,j));
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }/*}}}*/
|
---|
61 | void MatrixDiff(Matrix* A,Matrix* B){/*{{{*/
|
---|
62 | /*Check that sizes are compatible*/
|
---|
63 | int M_B,N_B,M_A,N_A;
|
---|
64 | B->GetSize(&M_B,&N_B);
|
---|
65 | A->GetSize(&M_A,&N_A);
|
---|
66 | assert(this->M==M_B && this->N==N_B);
|
---|
67 | assert(this->M==M_A && this->N==N_A);
|
---|
68 | for(int i=0;i<this->M;i++){
|
---|
69 | for(int j=0;j<this->N;j++){
|
---|
70 | this->SetValue(i,j,A->GetValue(i,j) - B->GetValue(i,j));
|
---|
71 | }
|
---|
72 | }
|
---|
73 | }/*}}}*/
|
---|
74 | void MatrixAbs(Matrix* A){/*{{{*/
|
---|
75 | for(int i=0;i<this->M;i++){
|
---|
76 | for(int j=0;j<this->N;j++){
|
---|
77 | this->SetValue(i,j,fabs(A->GetValue(i,j)));
|
---|
78 | }
|
---|
79 | }
|
---|
80 | }/*}}}*/
|
---|
81 | void MatrixEqual(Matrix* A){/*{{{*/
|
---|
82 | for(int i=0;i<this->M;i++){
|
---|
83 | for(int j=0;j<this->N;j++){
|
---|
84 | this->SetValue(i,j,A->GetValue(i,j));
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }/*}}}*/
|
---|
88 | double MatrixInternSum(){/*{{{*/
|
---|
89 | double sum=0;
|
---|
90 | for(int i=0;i<this->M;i++){
|
---|
91 | for(int j=0;j<this->N;j++){
|
---|
92 | sum+=this->GetValue(i,j);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | return sum;
|
---|
96 | }/*}}}*/
|
---|
97 | void ExtractLine(Matrix* A,int i){/*{{{*/
|
---|
98 | /* Check that the size of A is compatible */
|
---|
99 | int M_A,N_A;
|
---|
100 | A->GetSize(&M_A,&N_A);
|
---|
101 | assert(M_A==1 && this->N==N_A);
|
---|
102 | for(int j=0;j<this->N;j++){
|
---|
103 | A->SetValue(0,j,this->GetValue(i,j));
|
---|
104 | }
|
---|
105 | }/*}}}*/
|
---|
106 | void ExtractColumn(Matrix* A,int j){/*{{{*/
|
---|
107 | /* Check that the size of A is compatible */
|
---|
108 | int M_A,N_A;
|
---|
109 | A->GetSize(&M_A,&N_A);
|
---|
110 | assert(N_A==1 && this->M==M_A);
|
---|
111 | for(int i=0;i<this->M;i++){
|
---|
112 | A->SetValue(i,0,this->GetValue(i,j));
|
---|
113 | }
|
---|
114 | }/*}}}*/
|
---|
115 | void AddNumber(double a){/*{{{*/
|
---|
116 | for(int i=0;i<this->M;i++){
|
---|
117 | for(int j=0;j<this->N;j++){
|
---|
118 | this->SetValue(i,j,this->GetValue(i,j) + a);
|
---|
119 | }
|
---|
120 | }
|
---|
121 | }/*}}}*/
|
---|
122 | };/*}}}*/
|
---|
123 |
|
---|
124 | /*Local prototypes{{{*/
|
---|
125 | void makep(Matrix *Pobs,int nx,int ny, int dx, int dy);
|
---|
126 | void vec2grid(Matrix *V,Matrix *V1,Matrix *V2,int nx, int ny);
|
---|
127 | void msplit( Matrix *m, Matrix *m1,Matrix *m2,double dlevel);
|
---|
128 | void plouff(Matrix *g,Matrix *Pobs,Matrix *Pp,Matrix * mesh,Matrix *rho,int dx,int dy, int dn,int m,int n,int l);
|
---|
129 | void vec2gridsimple(Matrix *V,Matrix *V1,int nx, int ny);
|
---|
130 | void reshape(Matrix* V,Matrix* V1,int nx,int ny);
|
---|
131 | double misfit(Matrix* m0,Matrix* evalid,Matrix* gobs,int dlevel,Matrix* Pobs,Matrix* xobs,Matrix* yobs,Matrix* Pp,Matrix* rho1, Matrix* rho2,int dx,int dy,int dn,int nx,int ny, int mx,int my);
|
---|
132 | void GSLsquarefit(Matrix** pX,Matrix* A,Matrix* B);
|
---|
133 | double signe(double a);
|
---|
134 | void filtergrav(Matrix* A,Matrix* Ain,double ctr,double sd,int mx,int my);
|
---|
135 | void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd);
|
---|
136 | /*}}}*/
|
---|
137 |
|
---|
138 | int main(){/*{{{*/
|
---|
139 |
|
---|
140 | /* Seed the random number generator {{{*/
|
---|
141 | srand (time(NULL)); /*}}}*/
|
---|
142 | /* Define the variables {{{*/
|
---|
143 |
|
---|
144 | int dx = 500; /* prism dimension in x-direction */
|
---|
145 | int dy = 500; /* prism dimension in y-direction */
|
---|
146 | int mx = 39; /* number of prisms in x-direction */
|
---|
147 | int my = 60; /* number of prisms in y-direction */
|
---|
148 | int nx = 39; /* number of data points in x-direction */
|
---|
149 | int ny = 60; /* number of data points in y-direction */
|
---|
150 | int dn = 15000; /* distance for neighbouting prisms for gravity calculation */
|
---|
151 | double ptval = 100.; /* max. amount to perturb model */
|
---|
152 | double ptval2 = 100.;
|
---|
153 |
|
---|
154 | Matrix *Pobs=new Matrix(nx*ny,2); /* data positions */
|
---|
155 | makep(Pobs,nx,ny,dx,dy);
|
---|
156 | // Pobs->Echo();
|
---|
157 |
|
---|
158 |
|
---|
159 | Matrix *Pp=new Matrix(mx*my,2); /* data positions */
|
---|
160 | makep(Pp,mx,my,dx,dy);
|
---|
161 | // Pp->Echo();
|
---|
162 |
|
---|
163 | double rhoi = 890; /* ice density */
|
---|
164 | double rhow = 1030; /* water density */
|
---|
165 | // double rhos = 2013; /* sediment density */
|
---|
166 | double rhoc = 2670; /* bedrock density */
|
---|
167 |
|
---|
168 | Matrix *Rho = new Matrix(1,2);
|
---|
169 | Rho->SetValue(0,0,rhoi);
|
---|
170 | Rho->SetValue(0,1,rhow);
|
---|
171 | Matrix *rho1 = new Matrix(1,3);
|
---|
172 | rho1->SetValue(0,0,rhoi);
|
---|
173 | rho1->SetValue(0,1,rhow);
|
---|
174 | rho1->SetValue(0,2,rhoc);
|
---|
175 | Matrix *rho2 = new Matrix(1,2);
|
---|
176 | rho2->SetValue(0,0,rhoi-rhoc);
|
---|
177 | rho2->SetValue(0,1,rhow-rhoc);
|
---|
178 |
|
---|
179 | double ctr=1; /* parameter for filtering */
|
---|
180 | double sd=0.1;
|
---|
181 |
|
---|
182 | Matrix *xobs= new Matrix(ny,nx);
|
---|
183 | Matrix *yobs= new Matrix(ny,nx);
|
---|
184 |
|
---|
185 | vec2grid(Pobs,xobs,yobs,nx,ny);
|
---|
186 | // xobs->Echo();
|
---|
187 | // yobs->Echo();
|
---|
188 |
|
---|
189 | double dlevel= 860; /* mean level of data acquisition */
|
---|
190 |
|
---|
191 | double mmax = 1000; /* max value for layer interfaces */
|
---|
192 | double mmax2 = 1000;
|
---|
193 | double mmax3 = 1000;
|
---|
194 |
|
---|
195 | /* control parameter for temperature schedule */
|
---|
196 |
|
---|
197 | double ca=0.9; /* for acceptance */
|
---|
198 | double cm=0.5; /* for model perturbation */
|
---|
199 | /*}}}*/
|
---|
200 | /* load the data {{{*/
|
---|
201 |
|
---|
202 | /* Observed gravity anomaly */
|
---|
203 |
|
---|
204 | ifstream file1("store_fa500_36s.txt");
|
---|
205 | Matrix * gobs= new Matrix(nx*ny,1);
|
---|
206 | double inputnumber;
|
---|
207 | for(int i=0;i<ny*nx; i++){
|
---|
208 | file1 >> inputnumber;
|
---|
209 | gobs->SetValue(i,0, inputnumber*1e-5);
|
---|
210 | }
|
---|
211 | file1.close();
|
---|
212 | // gobs->Echo();
|
---|
213 |
|
---|
214 | /* load data about the ice thickness */
|
---|
215 |
|
---|
216 | ifstream file2("store_flag_icethick500.txt");
|
---|
217 | Matrix * icethick= new Matrix(mx*my,1);
|
---|
218 | for(int s=0;s<mx*my; s++){
|
---|
219 | file2 >> inputnumber;
|
---|
220 | icethick->SetValue(s,0,inputnumber);
|
---|
221 | }
|
---|
222 | file2.close();
|
---|
223 | // icethick->Echo();
|
---|
224 |
|
---|
225 | /* load the batimethry data */
|
---|
226 |
|
---|
227 | ifstream file3("store_flag_bathy500.txt");
|
---|
228 | Matrix * bathy= new Matrix(mx*my,1);
|
---|
229 | for(int s=0;s<mx*my; s++){
|
---|
230 | file3 >> inputnumber;
|
---|
231 | bathy->SetValue(s,0,inputnumber);
|
---|
232 | }
|
---|
233 | file3.close();
|
---|
234 | // bathy->Echo();
|
---|
235 |
|
---|
236 | /* id of grid to evaluate misfit */
|
---|
237 |
|
---|
238 |
|
---|
239 | ifstream file4("store_flag_eval500.txt");
|
---|
240 | Matrix * evalid= new Matrix(mx*my,1);
|
---|
241 | for(int s=0;s<mx*my; s++){
|
---|
242 | file4 >> inputnumber;
|
---|
243 | evalid->SetValue(s,0,inputnumber);
|
---|
244 | }
|
---|
245 | file4.close();
|
---|
246 | // evalid->Echo();
|
---|
247 |
|
---|
248 | /* initial guess of the model */
|
---|
249 |
|
---|
250 | ifstream file5("m0_140114b.txt");
|
---|
251 | Matrix * mesh_ini= new Matrix(mx*my,3);
|
---|
252 | for(int s=0;s<mx*my; s++){
|
---|
253 | for(int j=0;j<3;j++){
|
---|
254 | file5 >> inputnumber;
|
---|
255 | mesh_ini->SetValue(s,j,inputnumber);
|
---|
256 | }
|
---|
257 | }
|
---|
258 | file5.close();
|
---|
259 | // mesh_ini->Echo();
|
---|
260 | /*}}}*/
|
---|
261 | double q=0;
|
---|
262 | /* q=misfit(mesh_ini,evalid,gobs,dlevel,Pobs,xobs,yobs,Pp,rho1,rho2,dx,dy,dn,nx,ny,mx,my);
|
---|
263 | cout<< q;
|
---|
264 | Matrix* G=new Matrix(4,3);
|
---|
265 | Matrix* df=new Matrix(4,1);
|
---|
266 | Matrix* o=new Matrix(4,1);
|
---|
267 | for(int i= 0;i<4;i++){
|
---|
268 | for(int j=0;j<3;j++){
|
---|
269 | if(j==2){G->SetValue(i,j,1);}
|
---|
270 | else{G->SetValue(i,j,i+j);}
|
---|
271 | }
|
---|
272 | df->SetValue(i,0,i*1e-3);
|
---|
273 | }
|
---|
274 | G->SetValue(0,0,1);
|
---|
275 | G->SetValue(0,1,2);
|
---|
276 |
|
---|
277 | Matrix* M = NULL;
|
---|
278 | GSLsquarefit(&M,G,df);
|
---|
279 | for(int i= 0;i<4;i++){
|
---|
280 | o->SetValue(i,0,G->GetValue(i,0)*M->GetValue(0,0)+G->GetValue(i,1)*M->GetValue(1,0)+G->GetValue(i,2)*M->GetValue(2,0));}
|
---|
281 |
|
---|
282 | G->Echo();
|
---|
283 | df->Echo();
|
---|
284 | M->Echo();
|
---|
285 | o->Echo();*/
|
---|
286 | Matrix* m1=new Matrix(mx*my,3);
|
---|
287 | double T=100;
|
---|
288 | newmodelgen(mesh_ini,m1,bathy,icethick,mx,my,T,ptval,mmax,mmax2,ctr,sd);
|
---|
289 | return 0;
|
---|
290 | }/*}}}*/
|
---|
291 |
|
---|
292 | void GSLsquarefit(Matrix** pX,Matrix* A,Matrix* B){/*{{{*/
|
---|
293 |
|
---|
294 | /*GSL Matrices and vectors: */
|
---|
295 | int M,N;
|
---|
296 | double chisq;
|
---|
297 |
|
---|
298 | /*Get system size*/
|
---|
299 | A->GetSize(&M,&N);
|
---|
300 |
|
---|
301 | /*Initialize gsl matrices and vectors: */
|
---|
302 | gsl_matrix* a = gsl_matrix_alloc(M,N);
|
---|
303 | for(int i=0;i<M;i++){
|
---|
304 | for(int j=0;j<N;j++){
|
---|
305 | gsl_matrix_set (a,i,j,A->GetValue(i,j));
|
---|
306 | }
|
---|
307 | }
|
---|
308 | gsl_vector* b = gsl_vector_alloc(M);
|
---|
309 | for(int i=0;i<M;i++){
|
---|
310 | gsl_vector_set(b,i,B->GetValue(i,0));
|
---|
311 | }
|
---|
312 |
|
---|
313 | gsl_vector* x = gsl_vector_alloc(N);
|
---|
314 | gsl_matrix* cov = gsl_matrix_alloc(N,N);
|
---|
315 |
|
---|
316 | /*Least square fit: */
|
---|
317 | gsl_multifit_linear_workspace* work = gsl_multifit_linear_alloc(M,N);
|
---|
318 | gsl_multifit_linear (a, b, x, cov, &chisq, work);
|
---|
319 | gsl_multifit_linear_free (work);
|
---|
320 |
|
---|
321 | /*Clean up and assign output pointer*/
|
---|
322 | Matrix* X = new Matrix(N,1);
|
---|
323 | for(int j=0;j<N;j++){
|
---|
324 | X->SetValue(j,0,gsl_vector_get(x,j));
|
---|
325 | }
|
---|
326 | *pX = X;
|
---|
327 |
|
---|
328 | gsl_matrix_free(a);
|
---|
329 | gsl_vector_free(b);
|
---|
330 | gsl_matrix_free(cov);
|
---|
331 |
|
---|
332 | }/*}}}*/
|
---|
333 | void makep(Matrix *Pobs,int nx,int ny, int dx, int dy){/*{{{*/
|
---|
334 | for(int i=0;i<ny;i++){
|
---|
335 | for(int j=0;j<nx;j++){
|
---|
336 | Pobs->SetValue(j+nx*i,0,j*dx);
|
---|
337 | Pobs->SetValue(j+nx*i,1,i*dy);
|
---|
338 | }
|
---|
339 | }
|
---|
340 | }/*}}}*/
|
---|
341 | void vec2grid(Matrix *V,Matrix *V1,Matrix *V2,int nx, int ny){/*{{{*/
|
---|
342 | for(int i=0;i<ny;i++){
|
---|
343 | for (int j=0;j<nx;j++){
|
---|
344 | V1->SetValue(i,j, V->GetValue(j+nx*i,0));
|
---|
345 | V2->SetValue(i,j, V->GetValue(j+nx*i,1));
|
---|
346 | }
|
---|
347 | }
|
---|
348 | }/*}}}*/
|
---|
349 | void msplit( Matrix *m, Matrix *m1,Matrix *m2,double dlevel){/*{{{*/
|
---|
350 | int sizem1,sizem2;
|
---|
351 | m->GetSize(&sizem1,&sizem2);
|
---|
352 | for(int i=0;i<sizem1;i++){
|
---|
353 | for(int j=0;j<sizem2+1;j++){
|
---|
354 | if(j<sizem2){
|
---|
355 | m1->SetValue(i,j,1e-10*(sizem2+1-j));
|
---|
356 | m2->SetValue(i,j,m->GetValue(i,j));
|
---|
357 | if(m->GetValue(i,j)<0){
|
---|
358 | m1->SetValue(i,j,m->GetValue(i,j));
|
---|
359 | m2->SetValue(i,j,i*1e-10);
|
---|
360 | }
|
---|
361 | }
|
---|
362 | else{
|
---|
363 | m1->SetValue(i,j,1e-10);
|
---|
364 | }
|
---|
365 | }
|
---|
366 | }
|
---|
367 | m1->AddNumber(dlevel);
|
---|
368 | m2->AddNumber(dlevel);
|
---|
369 | }/*}}}*/
|
---|
370 | void plouff(Matrix *g,Matrix *Pobs,Matrix *Pp,Matrix * mesh,Matrix *rho,int dx,int dy, int dn,int m,int n,int l){/*{{{*/
|
---|
371 | double gg=6.673e-11;
|
---|
372 | int si,sj,id,s;
|
---|
373 | double R,Q,P;
|
---|
374 | Matrix *xp= new Matrix(1,2);
|
---|
375 | Matrix *yp= new Matrix(1,2);
|
---|
376 | Matrix *xpp= new Matrix(1,2);
|
---|
377 | Matrix *ypp= new Matrix(1,2);
|
---|
378 | Matrix *U= new Matrix(l,4);
|
---|
379 | Matrix *U1=new Matrix(1,4);
|
---|
380 | Matrix *U2=new Matrix(1,4);
|
---|
381 | Matrix *gl= new Matrix(1,l-1);
|
---|
382 | bool test=true;
|
---|
383 | for(int c=0;c<n;c++){
|
---|
384 | g->SetValue(c,0,0);
|
---|
385 | for(int a=0;a<m;a++){
|
---|
386 | test=true;
|
---|
387 | xp->SetValue(0,0,Pp->GetValue(a,0)-Pobs->GetValue(c,0));
|
---|
388 | xp->SetValue(0,1,Pp->GetValue(a,0)-Pobs->GetValue(c,0)+dx);
|
---|
389 | if(xp->GetValue(0,0)<0 && xp->GetValue(0,0)<xp->GetValue(0,1) && xp->GetValue(0,0)*xp->GetValue(0,1)>=0){
|
---|
390 | xpp->SetValue(0,0,xp->GetValue(0,1));
|
---|
391 | xpp->SetValue(0,1,xp->GetValue(0,0));
|
---|
392 | xp->MatrixAbs(xpp);
|
---|
393 | }
|
---|
394 | yp->SetValue(0,0,Pp->GetValue(a,1)-Pobs->GetValue(c,1));
|
---|
395 | yp->SetValue(0,1,Pp->GetValue(a,1)-Pobs->GetValue(c,1)+dy);
|
---|
396 | if(yp->GetValue(0,0)<0 && yp->GetValue(0,0)<yp->GetValue(0,1) && yp->GetValue(0,0)*yp->GetValue(0,1)>=0){
|
---|
397 | ypp->SetValue(0,0,yp->GetValue(0,1));
|
---|
398 | ypp->SetValue(0,1,yp->GetValue(0,0));
|
---|
399 | yp->MatrixAbs(ypp);
|
---|
400 | }
|
---|
401 | P=sqrt(xp->GetValue(0,0)*xp->GetValue(0,0)+yp->GetValue(0,0)*yp->GetValue(0,0));
|
---|
402 | if(P>dn){
|
---|
403 | test=false;
|
---|
404 | for(int i=0;i<l-1;i++){
|
---|
405 | gl->SetValue(0,i,0);
|
---|
406 | }
|
---|
407 | }
|
---|
408 | if(test==true){
|
---|
409 | si=1;
|
---|
410 | sj=1;
|
---|
411 | id=0;
|
---|
412 | for(int i=0;i<2;i++){
|
---|
413 | si*=-1;
|
---|
414 | for(int j=0;j<2;j++){
|
---|
415 | si*=-1;
|
---|
416 | s=si*sj;
|
---|
417 | for(int k=0;k<l;k++){
|
---|
418 | R=sqrt(xp->GetValue(0,i)*xp->GetValue(0,i)+yp->GetValue(0,j)*yp->GetValue(0,j)+mesh->GetValue(a,k)*mesh->GetValue(a,k));
|
---|
419 | Q=atan(xp->GetValue(0,i)*yp->GetValue(0,j)/(mesh->GetValue(a,k)*R));
|
---|
420 | U->SetValue(k,id,s*(mesh->GetValue(a,k)*Q-xp->GetValue(0,i)*log(R+yp->GetValue(0,j))-yp->GetValue(0,j)*log(R+xp->GetValue(0,i))));
|
---|
421 | }
|
---|
422 | id++;
|
---|
423 | }
|
---|
424 | }
|
---|
425 | for(int b=0;b<l-1;b++){
|
---|
426 | U->ExtractLine(U1,b);
|
---|
427 | U->ExtractLine(U2,b+1);
|
---|
428 | gl->SetValue(0,b,rho->GetValue(0,b)*(U1->MatrixInternSum()*(-1)+U2->MatrixInternSum()));
|
---|
429 | }
|
---|
430 | }
|
---|
431 | g->SetValue(c,0,g->GetValue(c,0)+gg*gl->MatrixInternSum());
|
---|
432 | }
|
---|
433 | }
|
---|
434 | delete xp;
|
---|
435 | delete yp;
|
---|
436 | delete xpp;
|
---|
437 | delete ypp;
|
---|
438 | delete gl;
|
---|
439 | delete U;
|
---|
440 | delete U1;
|
---|
441 | delete U2;
|
---|
442 | }/*}}}*/
|
---|
443 | void vec2gridsimple(Matrix *V,Matrix *V1,int nx, int ny){/*{{{*/
|
---|
444 | for(int i=0;i<ny;i++){
|
---|
445 | for (int j=0;j<nx;j++){
|
---|
446 | V1->SetValue(i,j, V->GetValue(j+nx*i,0));
|
---|
447 | }
|
---|
448 | }
|
---|
449 | }/*}}}*/
|
---|
450 | void reshape(Matrix* V,Matrix* V1,int nx,int ny){/*{{{*/
|
---|
451 | for (int i=0;i<ny;i++){
|
---|
452 | for(int j=0;j<nx;j++){
|
---|
453 | V1->SetValue(j+nx*i,0,V->GetValue(i,j));
|
---|
454 | }
|
---|
455 | }
|
---|
456 | }/*}}}*/
|
---|
457 | double misfit(Matrix* m0,Matrix* evalid,Matrix* gobs,int dlevel,Matrix* Pobs,Matrix* xobs,Matrix* yobs,Matrix* Pp,Matrix* rho1, Matrix* rho2,int dx,int dy,int dn,int nx,int ny, int mx,int my){/*{{{*/
|
---|
458 | Matrix* m1=new Matrix(mx*my,4);
|
---|
459 | Matrix* m2=new Matrix(mx*my,3);
|
---|
460 | Matrix* g1=new Matrix(nx*ny,1);
|
---|
461 | Matrix* g2=new Matrix(nx*ny,1);
|
---|
462 | Matrix* g=new Matrix(nx*ny,1);
|
---|
463 | Matrix* gcalgr=new Matrix(ny,nx);
|
---|
464 | Matrix* gcalvec=new Matrix(mx*my,1);
|
---|
465 | Matrix* df=new Matrix(mx*my,1);
|
---|
466 | Matrix* G=new Matrix(mx*my,3);
|
---|
467 | double a=0;
|
---|
468 | double b=0;
|
---|
469 | double e=0;
|
---|
470 | msplit(m0,m1,m2,dlevel);
|
---|
471 | plouff(g1,Pobs,Pp,m1,rho1,dx,dy,dn,mx*my,nx*ny,4);
|
---|
472 | plouff(g2,Pobs,Pp,m2,rho2,dx,dy,dn,mx*my,nx*ny,3);
|
---|
473 | g->MatrixSum(g1,g2);
|
---|
474 | vec2gridsimple(g,gcalgr,nx,ny);
|
---|
475 | reshape(gcalgr,gcalvec,mx,my);
|
---|
476 | for (int i=0;i<mx*my;i++){
|
---|
477 | df->SetValue(i,0,evalid->GetValue(i,0)*(gobs->GetValue(i,0)-gcalvec->GetValue(i,0)));
|
---|
478 | G->SetValue(i,0,evalid->GetValue(i,0)*Pobs->GetValue(i,0));
|
---|
479 | G->SetValue(i,1,evalid->GetValue(i,0)*Pobs->GetValue(i,1));
|
---|
480 | G->SetValue(i,2,evalid->GetValue(i,0));
|
---|
481 | }
|
---|
482 | Matrix* M = NULL;
|
---|
483 | GSLsquarefit(&M,G,df);
|
---|
484 |
|
---|
485 | for (int i=0;i<my;i++){
|
---|
486 | for(int j=0;j<mx;j++){
|
---|
487 | gcalgr->SetValue(i,j,gcalgr->GetValue(i,j)+xobs->GetValue(i,j)*M->GetValue(0,0)+yobs->GetValue(i,j)*M->GetValue(1,0)+M->GetValue(2,0));
|
---|
488 | }
|
---|
489 | }
|
---|
490 | reshape(gcalgr,g,mx,my);
|
---|
491 | for (int i=0;i<mx*my;i++){
|
---|
492 | a=a+fabs(evalid->GetValue(i,0)*(gobs->GetValue(i,0)-g->GetValue(i,0)));
|
---|
493 | b=b+fabs(evalid->GetValue(i,0)*(gobs->GetValue(i,0)+g->GetValue(i,0)));
|
---|
494 | }
|
---|
495 | e=2*a/(a+b);
|
---|
496 | return e;
|
---|
497 | }/*}}}*/
|
---|
498 | void newmodelgen(Matrix* m0,Matrix* m1,Matrix* bathy,Matrix* icethick,int mx,int my,double T,double ptval,double mmax,double mmax2,double ctr,double sd){/*{{{*/
|
---|
499 | Matrix* m1gr=new Matrix(my,mx);
|
---|
500 | Matrix* m1grsm=new Matrix(my,mx);
|
---|
501 | Matrix* m1col=new Matrix(mx*my,1);
|
---|
502 | double u=0;
|
---|
503 | double y=0;
|
---|
504 | m1->MatrixEqual(m0);
|
---|
505 | for (int i=0;i<mx*my;i++){
|
---|
506 | if(icethick->GetValue(i,0)==0){
|
---|
507 | u=double (rand())/ double(RAND_MAX);
|
---|
508 | y=signe(u-0.5)*T*(pow(1+1/T,fabs(2*u-1))-1);
|
---|
509 | m1->SetValue(i,1,m0->GetValue(i,1)+y*ptval);
|
---|
510 | if(m1->GetValue(i,1)<=m1->GetValue(i,0)){
|
---|
511 | m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
|
---|
512 | }
|
---|
513 | if(m1->GetValue(i,1)>=m1->GetValue(i,0)+mmax){
|
---|
514 | m1->SetValue(i,1,m1->GetValue(i,0)+mmax);
|
---|
515 | }
|
---|
516 | }
|
---|
517 | }
|
---|
518 | m1->ExtractColumn(m1col,1);
|
---|
519 | vec2gridsimple(m1col,m1gr,mx,my);
|
---|
520 | cout<<endl<<endl;
|
---|
521 | filtergrav(m1grsm,m1gr,ctr,sd,mx,my);
|
---|
522 | reshape(m1grsm,m1col,mx,my);
|
---|
523 | for (int i=0;i<mx*my;i++){
|
---|
524 | if(icethick->GetValue(i,0)==0){
|
---|
525 | m1->SetValue(i,1,m1col->GetValue(i,0));
|
---|
526 | }
|
---|
527 | else{
|
---|
528 | m1->SetValue(i,1,m0->GetValue(i,1));
|
---|
529 | }
|
---|
530 | if(m1->GetValue(i,1)<=m1->GetValue(i,0)){
|
---|
531 | m1->SetValue(i,1,m1->GetValue(i,0)+1e-10);
|
---|
532 | }
|
---|
533 | }
|
---|
534 |
|
---|
535 | for (int i=0;i<mx*my;i++){
|
---|
536 | if(bathy->GetValue(i,0)==0){
|
---|
537 | u=double (rand())/ double(RAND_MAX);
|
---|
538 | y=signe(u-0.5)*T*(pow(1+1/T,fabs(2*u-1))-1);
|
---|
539 | m1->SetValue(i,2,m0->GetValue(i,2)+y*ptval);
|
---|
540 | if(m1->GetValue(i,2)<=m1->GetValue(i,1)){
|
---|
541 | m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
|
---|
542 | }
|
---|
543 | if(m1->GetValue(i,2)>=m1->GetValue(i,1)+mmax2){
|
---|
544 | m1->SetValue(i,2,m1->GetValue(i,1)+mmax2);
|
---|
545 | }
|
---|
546 | }
|
---|
547 | }
|
---|
548 | m1->ExtractColumn(m1col,2);
|
---|
549 | vec2gridsimple(m1col,m1gr,mx,my);
|
---|
550 | filtergrav(m1grsm,m1gr,ctr,sd,mx,my);
|
---|
551 | reshape(m1grsm,m1col,mx,my);
|
---|
552 | for (int i=0;i<mx*my;i++){
|
---|
553 | if(bathy->GetValue(i,0)==0){
|
---|
554 | m1->SetValue(i,2,m1col->GetValue(i,0));
|
---|
555 | }
|
---|
556 | else{
|
---|
557 | m1->SetValue(i,2,m0->GetValue(i,2));
|
---|
558 | }
|
---|
559 | if(m1->GetValue(i,2)<=m1->GetValue(i,1)){
|
---|
560 | m1->SetValue(i,2,m1->GetValue(i,1)+1e-10);
|
---|
561 | }
|
---|
562 | }
|
---|
563 | delete m1gr;
|
---|
564 | delete m1grsm;
|
---|
565 | delete m1col;
|
---|
566 | }/*}}}*/
|
---|
567 | double signe(double a){/*{{{*/
|
---|
568 | if(a<0){return -1;}
|
---|
569 | else{return 1;}
|
---|
570 | }/*}}}*/
|
---|
571 | void filtergrav(Matrix* A,Matrix* Ain,double ctr,double sd,int mx,int my){/*{{{*/
|
---|
572 | A->MatrixEqual(Ain);
|
---|
573 | for (int i=1;i<my-1;i++){
|
---|
574 | for(int j=1;j<mx-1;j++){
|
---|
575 | A->SetValue(i,j,(ctr*Ain->GetValue(i,j)+sd*(Ain->GetValue(i-1,j)+Ain->GetValue(i+1,j)+Ain->GetValue(i,j-1)+Ain->GetValue(i,j+1)))/(ctr+4*sd));
|
---|
576 | }
|
---|
577 | }
|
---|
578 | }/*}}}*/
|
---|