void TPZNonDarcyFlow::Solution(TPZMaterialData &data, int var, TPZVec<REAL> &Solout) { TPZFMatrix<STATE> &dsol = data.dsol[0]; REAL p = data.sol[0][0]; REAL mu,rho; this->ReturnVisc(p,mu); this->ReturnRho(p,rho); REAL kh = globData.fKh; REAL kv = globData.fKv; switch(var) { case 0: { Solout[0] = p; } break; case 1: { Solout[0] = - (kh * rho) / mu * dsol(0,0) * 2. * M_PI * data.x[0]; // flux in 1r; // in SI m2/s Solout[1] = - (kv * rho) / mu * dsol(1,0) * 2. * M_PI * data.x[0]; // flux in 1z; // in SI m2/s } break; default: TPZMaterial::Solution(data,var,Solout); } }
void MAST::NonlinearImplicitAssembly:: _check_element_numerical_jacobian(MAST::ElementBase& e, RealVectorX& sol) { RealVectorX dsol, res0, dres; RealMatrixX jac0, jac, dummy; unsigned int ndofs = (unsigned int)sol.size(); res0.setZero(ndofs); dres.setZero(ndofs); jac0.setZero(ndofs, ndofs); jac.setZero(ndofs, ndofs); e.set_solution(sol); _elem_calculations(e, true, res0, jac0); Real delta = 1.0e-5; for (unsigned int i=0; i<sol.size(); i++) { dsol = sol; dsol(i) += delta; e.set_solution(dsol); _elem_calculations(e, false, dres, dummy); jac.col(i) = (dres-res0)/delta; } // write the numerical and analytical jacobians std::cout << "Analytical Jacobian: " << std::endl << jac0 << std::endl << std::endl << "Numerical Jacobian: " << std::endl << jac << std::endl << std::endl; }
void TPZIncNavierStokesKEps::Contribute(TPZMaterialData &data, REAL weight, TPZFMatrix<REAL> &ek, TPZFMatrix<REAL> &ef){ int numbersol = data.sol.size(); if (numbersol != 1) { DebugStop(); } TPZFMatrix<REAL> &dphi = data.dphix; // TPZFMatrix<REAL> &dphiL = data.dphixl; // TPZFMatrix<REAL> &dphiR = data.dphixr; TPZFMatrix<REAL> &phi = data.phi; // TPZFMatrix<REAL> &phiL = data.phil; // TPZFMatrix<REAL> &phiR = data.phir; // TPZManVector<REAL,3> &normal = data.normal; // TPZManVector<REAL,3> &x = data.x; // int &POrder=data.p; // int &LeftPOrder=data.leftp; // int &RightPOrder=data.rightp; TPZVec<REAL> &sol=data.sol[0]; // TPZVec<REAL> &solL=data.soll; // TPZVec<REAL> &solR=data.solr; TPZFMatrix<REAL> &dsol=data.dsol[0]; // TPZFMatrix<REAL> &dsolL=data.dsoll; // TPZFMatrix<REAL> &dsolR=data.dsolr; // REAL &faceSize=data.HSize; // TPZFMatrix<REAL> &daxesdksi=data.daxesdksi; // TPZFMatrix<REAL> &axes=data.axes; REAL valor; const int dim = this->Dimension(); const int nstate = this->NStateVariables(); //Getting state variables REAL K = sol[EK]; REAL Eps = sol[EEpsilon]; REAL Pressure = sol[EPressure]; TPZManVector<REAL,3> V(dim); int i,j; for(i = 0; i < dim; i++) V[i] = sol[EVx+i]; //Getting Grad[state variables] TPZManVector<REAL,3> GradK(dim,1); for(i = 0; i < dim; i++) GradK[i] = dsol(i, EK); TPZManVector<REAL,3> GradEps(dim,1); for(i = 0; i < dim; i++) GradEps[i] = dsol(i, EEpsilon); TPZManVector<REAL,3> GradPressure(dim,1); for(i = 0; i < dim; i++) GradPressure[i] = dsol(i, EPressure); TPZFNMatrix<9> GradV(dim,dim); for(i = 0; i < dim; i++){ for(j = 0; j < dim; j++){ GradV(i,j) = dsol(j,EVx+i); } } //Constants: REAL Rt = K*K /(Eps*fMU/fRHO); REAL muT = fRHO * fCmu * (K*K/Eps) * exp(-2.5/(1.+Rt/50.)); //TURBULENCE RESIDUALS //CONSERVATION OF K const int nShape = phi.Rows(); TPZFNMatrix<9> S(dim,dim); for(i = 0; i < dim; i++){ for(j = 0; j < dim; j++){ S(i,j) = 0.5 * (GradV(i,j) + GradV(j,i) ); } } REAL Diss = 2. * fMU * (1. / (4. * K) ) * this->Dot(GradK, GradK); TPZManVector<REAL,3> GradPhi(dim); for(i = 0; i < nShape; i++){ int k; for(k = 0; k < dim; k++) GradPhi[k] = dphi(k,i); ef(i*nstate+EK) += -1. * fRHO * this->Dot(V, GradPhi) * K + (fMU + muT / fSigmaK) * Dot(GradK,GradPhi) -2.0 * muT * this->Dot(S,GradV)*phi[i] + fRHO * Eps * phi[i] -1. * Diss; valor = -1. * fRHO * this->Dot(V, GradPhi) * K + (fMU + muT / fSigmaK) * Dot(GradK,GradPhi) -2.0 * muT * this->Dot(S,GradV)*phi[i] + fRHO * Eps * phi[i] -1. * Diss; } //CONSERVATION OF EPSILON for(i = 0; i < nShape; i++){ int k; for(k = 0; k < dim; k++) GradPhi[k] = dphi(k,i); ef(i*nstate+EEpsilon) += -1. * fRHO * this->Dot(V, GradPhi) * Eps + (fMU + muT / fSigmaEps) * this->Dot(GradEps, GradPhi) -1. * fCepsilon1 * (Eps/K) * 2. * muT * this->Dot(S,GradV) * phi[i] + fCepsilon2 * (Eps*Eps/K) * phi[i]; valor = -1. * fRHO * this->Dot(V, GradPhi) * Eps + (fMU + muT / fSigmaEps) * this->Dot(GradEps, GradPhi) -1. * fCepsilon1 * (Eps/K) * 2. * muT * this->Dot(S,GradV) * phi[i] + fCepsilon2 * (Eps*Eps/K) * phi[i]; } //INCOMPRESSIBLE NAVIERS-STOKES RESIDUALS //CONTINUITY EQUATION for(i = 0; i < nShape; i++){ REAL trGradV = 0.; int k; for(k = 0; k < dim; k++) trGradV += GradV(k,k); ef(i*nstate+EPressure) += trGradV * phi[i]; valor = trGradV * phi[i]; } //CONSERVATION OF LINEAR MOMENTUM TPZFNMatrix<9> T(dim,dim); //T = -p I + (mu + muT) * 2 * S T = S; T *= (fMU + muT ) *2.; for(i = 0; i < dim; i++) T(i,i) += -1. * Pressure; for(j = 0; j < dim; j++){ for(i = 0; i < nShape; i++){ int k; for(k = 0; k < dim; k++) GradPhi[k] = dphi(k,i); valor = fRHO * this->Dot(V, GradV, j) * phi[i] -1. * this->Dot(GradPhi, T, j) -1. * fBodyForce[j] * phi[i]; ef(i*nstate+ EVx+j) += valor; } } }//method
int main() { #ifdef _AUTODIFF #ifdef FAD const int dim = 3; const int nstate = dim+2; const int nphi = 6; // emulating state variables TPZVec<TPZDiffMatrix<REAL> > Ai, Tau; TPZVec<TPZVec<REAL> > TauDiv; TPZVec<TPZDiffMatrix<REAL> > TaudDiv; /*char ArtDiff[32]="LS";*/ TPZArtDiffType artDiffType = LeastSquares_AD; TPZFMatrix dsol(dim,nstate); TPZFMatrix dphi(dim,nphi); TPZVec<REAL> phi(nphi); TPZVec<REAL> sol(nstate); TPZVec<FADREAL> FADsol(nstate); TPZVec<FADREAL> FADdsol(nstate*dim); TPZVec<TPZVec<FADREAL> > FADTauDiv; // generating data TPZArtDiff ArtDiff(artDiffType, 1.4); //solution sol[0]=2.; sol[1]=3.; sol[2]=5.; sol[3]=7.; sol[4]=11.; //phi phi[0]=2.3; phi[1]=3.5; phi[2]=5.7; phi[3]=7.11; phi[4]=11.13; phi[5]=13.17; //dphi int i; int j; for(i=0;i<dim;i++) for(j=0;j<nphi;j++) dphi(i,j)=45.8*i-3.2*j; // any choice //dsol for(i=0;i<dim;i++) for(j=0;j<nstate;j++) dsol(i,j)=49.8*i-3.1*j; // any choice int k, l; //FADsol for(i=0;i<nstate;i++) { FADsol[i]=sol[i]; FADsol[i].diff(0,nphi*nstate); FADsol[i].fastAccessDx(0)=0.; for(j=0;j<nphi;j++)FADsol[i].fastAccessDx(i+j*nstate)=phi[j]; } /* FADREAL teste; for(i=0;i<FADsol.NElements();i++)teste+=FADsol[i]; cout << "\n\nFADsol\n" << teste; cout << "\n\nphi\n" << phi; */ //FADdSol for(k=0;k<dim;k++) for(i=0;i<nstate;i++) { FADdsol[k+i*dim]=dsol(k,i); FADdsol[k+i*dim].diff(0,nphi*nstate); FADdsol[k+i*dim].fastAccessDx(0)=0.; for(j=0;j<nphi;j++) FADdsol[k+i*dim].fastAccessDx(i+j*nstate)=dphi(k,j); } /* cout << "\n\nFADdsol\n"; teste=0; for(i=0;i<FADdsol.NElements();i+=3)teste+=FADdsol[i]; cout << teste << endl; teste=0; for(i=1;i<FADdsol.NElements();i+=3)teste+=FADdsol[i]; cout << teste << endl; teste=0; for(i=2;i<FADdsol.NElements();i+=3)teste+=FADdsol[i]; cout << teste << endl; //cout << "\n\nFADdsol\n" << FADdsol; cout << "\n\ndphi\n" << dphi; */ TPZFMatrix Jac(dim,dim,1.); ArtDiff.PrepareFastDiff(dim,Jac, sol, dsol, dphi, TauDiv, &TaudDiv); ArtDiff.PrepareFastDiff(dim, Jac, FADsol, FADdsol, FADTauDiv); cout << "\n\nFADTauDiv\n" << FADTauDiv; cout << "\n\nTauDiv\n" << TauDiv; cout << "\n\nTaudDiv\n" << TaudDiv; #endif #endif return 0; }