Changeset 18345
- Timestamp:
- 08/08/14 08:34:32 (11 years ago)
- Location:
- issm/trunk-jpl/src/c
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/analyses/MasstransportAnalysis.cpp
r18344 r18345 805 805 *pMlff=Mlff; 806 806 }/*}}}*/ 807 void MasstransportAnalysis::MassMatrix(Matrix<IssmDouble>** pMff,FemModel* femmodel){/*{{{*/ 808 809 /*Initialize Mass matrix*/ 810 Matrix<IssmDouble> *Mff = NULL; 811 AllocateSystemMatricesx(&Mff,NULL,NULL,NULL,femmodel); 812 813 /*Create and assemble matrix*/ 814 for(int i=0;i<femmodel->elements->Size();i++){ 815 Element* element = dynamic_cast<Element*>(femmodel->elements->GetObjectByOffset(i)); 816 ElementMatrix* MLe = this->CreateMassMatrix(element); 817 if(MLe){ 818 MLe->AddToGlobal(Mff); 819 } 820 delete MLe; 821 } 822 Mff->Assemble(); 823 824 /*Assign output pointer*/ 825 *pMff=Mff; 826 }/*}}}*/ 807 827 ElementMatrix* MasstransportAnalysis::CreateMassMatrix(Element* element){/*{{{*/ 808 828 … … 866 886 /*Assign output pointer*/ 867 887 *pKff=Kff; 868 *pKfs=Kfs; 888 if(pKfs){ 889 *pKfs=Kfs; 890 } 891 else{ 892 delete Kfs; 893 } 869 894 }/*}}}*/ 870 895 ElementMatrix* MasstransportAnalysis::CreateFctKMatrix(Element* element){/*{{{*/ -
issm/trunk-jpl/src/c/analyses/MasstransportAnalysis.h
r18344 r18345 39 39 /*FCT*/ 40 40 void LumpedMassMatrix(Vector<IssmDouble>** pMLff,FemModel* femmodel); 41 void MassMatrix(Matrix<IssmDouble>** pMff,FemModel* femmodel); 41 42 void FctKMatrix(Matrix<IssmDouble>** pKff,Matrix<IssmDouble>** pKfs,FemModel* femmodel); 42 43 ElementMatrix* CreateMassMatrix(Element* element); -
issm/trunk-jpl/src/c/solutionsequences/solutionsequence_fct.cpp
r18344 r18345 11 11 12 12 /*intermediary: */ 13 Vector<IssmDouble>* Ml f= NULL;14 Matrix<IssmDouble>* K ff= NULL;15 Matrix<IssmDouble>* Kfs= NULL;16 Vector<IssmDouble>* ug 17 Vector<IssmDouble>* uf 18 Vector<IssmDouble>* ys 13 Vector<IssmDouble>* Ml = NULL; 14 Matrix<IssmDouble>* K = NULL; 15 Matrix<IssmDouble>* Mc = NULL; 16 Vector<IssmDouble>* ug = NULL; 17 Vector<IssmDouble>* uf = NULL; 18 Vector<IssmDouble>* ys = NULL; 19 19 20 20 IssmDouble theta,deltat; … … 41 41 42 42 /*Create lumped mass matrix*/ 43 analysis->LumpedMassMatrix(&Mlf,femmodel); 44 analysis->FctKMatrix(&Kff,&Kfs,femmodel); 45 46 /*Create Dff Matrix*/ 43 analysis->LumpedMassMatrix(&Ml,femmodel); 44 analysis->MassMatrix(&Mc,femmodel); 45 analysis->FctKMatrix(&K,NULL,femmodel); 46 47 /*Create D Matrix*/ 47 48 #ifdef _HAVE_PETSC_ 48 Mat Kff_transp = NULL; 49 Mat Dff_petsc = NULL; 50 Mat LHS = NULL; 51 Mat Kff_petsc = Kff->pmatrix->matrix; 52 Vec Mlf_petsc = Mlf->pvector->vector; 53 MatTranspose(Kff_petsc,MAT_INITIAL_MATRIX,&Kff_transp); 54 MatDuplicate(Kff_petsc,MAT_SHARE_NONZERO_PATTERN,&Dff_petsc); 55 MatGetOwnershipRange(Kff_transp,&rstart,&rend); 49 Mat K_transp = NULL; 50 Mat D_petsc = NULL; 51 Mat LHS = NULL; 52 Mat K_petsc = K->pmatrix->matrix; 53 Vec Ml_petsc = Ml->pvector->vector; 54 Mat Mc_petsc = Mc->pmatrix->matrix; 55 MatTranspose(K_petsc,MAT_INITIAL_MATRIX,&K_transp); 56 MatDuplicate(K_petsc,MAT_SHARE_NONZERO_PATTERN,&D_petsc); 57 MatGetOwnershipRange(K_transp,&rstart,&rend); 56 58 for(int row=rstart; row<rend; row++){ 57 59 diagD = 0.; 58 MatGetRow(K ff_petsc ,row,&ncols, (const int**)&cols, (const double**)&vals);59 MatGetRow(K ff_transp,row,&ncols2,(const int**)&cols2,(const double**)&vals2);60 MatGetRow(K_petsc ,row,&ncols, (const int**)&cols, (const double**)&vals); 61 MatGetRow(K_transp,row,&ncols2,(const int**)&cols2,(const double**)&vals2); 60 62 _assert_(ncols==ncols2); 61 63 for(int j=0; j<ncols; j++) { 62 64 _assert_(cols[j]==cols2[j]); 63 65 d = max(max(-vals[j],-vals2[j]),0.); 64 MatSetValues(D ff_petsc,1,&row,1,&cols[j],(const double*)&d,INSERT_VALUES);66 MatSetValues(D_petsc,1,&row,1,&cols[j],(const double*)&d,INSERT_VALUES); 65 67 if(cols[j]!=row) diagD -= d; 66 68 } 67 MatSetValues(D ff_petsc,1,&row,1,&row,(const double*)&diagD,INSERT_VALUES);68 MatRestoreRow(K ff_petsc, row,&ncols, (const int**)&cols, (const double**)&vals);69 MatRestoreRow(K ff_transp,row,&ncols2,(const int**)&cols2,(const double**)&vals2);70 } 71 MatAssemblyBegin(D ff_petsc,MAT_FINAL_ASSEMBLY);72 MatAssemblyEnd( D ff_petsc,MAT_FINAL_ASSEMBLY);73 MatFree(&K ff_transp);69 MatSetValues(D_petsc,1,&row,1,&row,(const double*)&diagD,INSERT_VALUES); 70 MatRestoreRow(K_petsc, row,&ncols, (const int**)&cols, (const double**)&vals); 71 MatRestoreRow(K_transp,row,&ncols2,(const int**)&cols2,(const double**)&vals2); 72 } 73 MatAssemblyBegin(D_petsc,MAT_FINAL_ASSEMBLY); 74 MatAssemblyEnd( D_petsc,MAT_FINAL_ASSEMBLY); 75 MatFree(&K_transp); 74 76 75 77 /*Create LHS: [ML − theta*detlat *(K+D)^n+1]*/ 76 MatDuplicate(K ff_petsc,MAT_SHARE_NONZERO_PATTERN,&LHS);78 MatDuplicate(K_petsc,MAT_SHARE_NONZERO_PATTERN,&LHS); 77 79 for(int row=rstart; row<rend; row++){ 78 MatGetRow(K ff_petsc,row,&ncols, (const int**)&cols, (const double**)&vals);79 MatGetRow(D ff_petsc,row,&ncols2,(const int**)&cols2,(const double**)&vals2);80 MatGetRow(K_petsc,row,&ncols, (const int**)&cols, (const double**)&vals); 81 MatGetRow(D_petsc,row,&ncols2,(const int**)&cols2,(const double**)&vals2); 80 82 _assert_(ncols==ncols2); 81 83 for(int j=0; j<ncols; j++) { … … 83 85 d = -theta*deltat*(vals[j] + vals2[j]); 84 86 if(cols[j]==row){ 85 VecGetValues(Ml f_petsc,1,(const int*)&cols[j],&mi);87 VecGetValues(Ml_petsc,1,(const int*)&cols[j],&mi); 86 88 d += mi; 87 89 } … … 89 91 MatSetValues(LHS,1,&row,1,&cols[j],(const double*)&d,INSERT_VALUES); 90 92 } 91 MatRestoreRow(K ff_petsc,row,&ncols, (const int**)&cols, (const double**)&vals);92 MatRestoreRow(D ff_petsc,row,&ncols2,(const int**)&cols2,(const double**)&vals2);93 MatRestoreRow(K_petsc,row,&ncols, (const int**)&cols, (const double**)&vals); 94 MatRestoreRow(D_petsc,row,&ncols2,(const int**)&cols2,(const double**)&vals2); 93 95 } 94 96 … … 118 120 VecDuplicate(u,&Du); 119 121 VecDuplicate(u,&RHS); 120 MatMult(K ff_petsc,u,Ku);121 MatMult(D ff_petsc,u,Du);122 VecPointwiseMult(RHS,Ml f_petsc,u);122 MatMult(K_petsc,u,Ku); 123 MatMult(D_petsc,u,Du); 124 VecPointwiseMult(RHS,Ml_petsc,u); 123 125 VecAXPBYPCZ(RHS,(1-theta)*deltat,(1-theta)*deltat,1,Ku,Du);// RHS = M*u + (1-theta)*deltat*K*u + (1-theta)*deltat*D*u 124 126 VecFree(&Ku); 125 127 VecFree(&Du); 126 MatFree(&Dff_petsc);127 128 delete uf; 128 129 … … 138 139 } 139 140 } 141 VecAssemblyBegin(RHS); 142 VecAssemblyEnd( RHS); 140 143 141 144 /*Go solve!*/ … … 143 146 MatFree(&LHS); 144 147 VecFree(&RHS); 148 149 /*Richardson to calculate udot*/ 150 Vec udot = NULL; 151 VecDuplicate(u,&udot); 152 VecZeroEntries(udot); 153 Vec temp1 = NULL; VecDuplicate(u,&temp1); 154 Vec temp2 = NULL; VecDuplicate(u,&temp2); 155 for(int i=0;i<5;i++){ 156 /*udot_new = udot_old + Ml^-1 (K^(n+1) u -- Mc udot_old)*/ 157 MatMult(Mc_petsc,udot,temp1); 158 MatMult(K_petsc, u, temp2); 159 VecAXPBY(temp2,-1.,1.,temp1); // temp2 = (K^(n+1) u -- Mc udot_old) 160 VecPointwiseDivide(temp1,temp2,Ml_petsc); //temp1 = Ml^-1 temp2 161 VecAXPBY(udot,1.,1.,temp1); 162 } 163 VecFree(&temp1); 164 VecFree(&temp2); 165 166 /*Serialize u and udot*/ 167 IssmDouble* udot_serial = NULL; 168 IssmDouble* u_serial = NULL; 169 VecToMPISerial(&udot_serial,udot,IssmComm::GetComm()); 170 VecToMPISerial(&u_serial ,u ,IssmComm::GetComm()); 171 172 173 /*Anti diffusive fluxes*/ 174 Mat F = NULL; 175 Vec Fbar = NULL; 176 VecDuplicate(u,&Fbar); 177 MatDuplicate(D_petsc,MAT_SHARE_NONZERO_PATTERN,&F); 178 for(int row=rstart; row<rend; row++){ 179 MatGetRow(Mc_petsc,row,&ncols, (const int**)&cols, (const double**)&vals); 180 MatGetRow(D_petsc ,row,&ncols2,(const int**)&cols2,(const double**)&vals2); 181 _assert_(ncols==ncols2); 182 mi = 0.; 183 for(int j=0; j<ncols; j++) { 184 _assert_(cols[j]==cols2[j]); 185 d = vals[j]*(udot_serial[row] - udot_serial[cols[j]]) + vals2[j]*(u_serial[row] - u_serial[cols[j]]); 186 if(row!=cols[j]) mi += d; 187 MatSetValues(F,1,&row,1,&cols[j],(const double*)&d,INSERT_VALUES); 188 189 } 190 VecSetValue(Fbar,row,(const double)mi,INSERT_VALUES); 191 MatRestoreRow(Mc_petsc, row,&ncols, (const int**)&cols, (const double**)&vals); 192 MatRestoreRow(D_petsc,row,&ncols2,(const int**)&cols2,(const double**)&vals2); 193 } 194 MatAssemblyBegin(F,MAT_FINAL_ASSEMBLY); 195 MatAssemblyEnd( F,MAT_FINAL_ASSEMBLY); 196 VecAssemblyBegin(Fbar); 197 VecAssemblyEnd( Fbar); 198 199 MatFree(&D_petsc); 200 MatFree(&F); 201 delete Mc; 202 xDelete<IssmDouble>(udot_serial); 203 xDelete<IssmDouble>(u_serial); 204 205 /*Compute solution u^n+1 = u_L + deltat Ml^-1 fbar*/ 206 VecDuplicate(u,&temp1); 207 VecPointwiseDivide(temp1,Fbar,Ml_petsc); //temp1 = Ml^-1 temp2 208 VecAXPBY(udot,1.,1.,temp1); 209 VecAXPY(u,deltat,temp1); 210 VecFree(&Fbar); 211 VecFree(&udot); 212 VecFree(&temp1); 213 145 214 uf =new Vector<IssmDouble>(u); 146 215 VecFree(&u); … … 154 223 #endif 155 224 156 delete Mlf; 157 delete Kff; 158 delete Kfs; 225 delete Ml; 226 delete K; 159 227 delete analysis; 160 228
Note:
See TracChangeset
for help on using the changeset viewer.