void AveragedTurbine<Mu>::nonlocal_time_derivative(bool compute_jacobian, AssemblyContext& context, CachedValues& /* cache */ ) { libMesh::DenseSubMatrix<libMesh::Number> &Kss = context.get_elem_jacobian(this->fan_speed_var(), this->fan_speed_var()); // R_{s},{s} libMesh::DenseSubVector<libMesh::Number> &Fs = context.get_elem_residual(this->fan_speed_var()); // R_{s} const std::vector<libMesh::dof_id_type>& dof_indices = context.get_dof_indices(this->fan_speed_var()); const libMesh::Number fan_speed = context.get_system().current_solution(dof_indices[0]); const libMesh::Number output_torque = this->torque_function(libMesh::Point(0), fan_speed); Fs(0) += output_torque; if (compute_jacobian) { // FIXME: we should replace this FEM with a hook to the AD fparser stuff const libMesh::Number epsilon = 1e-6; const libMesh::Number output_torque_deriv = (this->torque_function(libMesh::Point(0), fan_speed+epsilon) - this->torque_function(libMesh::Point(0), fan_speed-epsilon)) / (2*epsilon); Kss(0,0) += output_torque_deriv * context.get_elem_solution_derivative(); } return; }
void HeatTransferStabilizationHelper::compute_res_energy_steady_and_derivs ( AssemblyContext& context, unsigned int qp, const libMesh::Real rho, const libMesh::Real Cp, const libMesh::Real k, libMesh::Real &res, libMesh::Real &d_res_dT, libMesh::Gradient &d_res_dgradT, libMesh::Tensor &d_res_dhessT, libMesh::Gradient &d_res_dU ) const { libMesh::Gradient grad_T = context.fixed_interior_gradient(this->_temp_vars.T_var(), qp); libMesh::Tensor hess_T = context.fixed_interior_hessian(this->_temp_vars.T_var(), qp); libMesh::RealGradient rhocpU( rho*Cp*context.fixed_interior_value(this->_flow_vars.u_var(), qp), rho*Cp*context.fixed_interior_value(this->_flow_vars.v_var(), qp) ); if(context.get_system().get_mesh().mesh_dimension() == 3) rhocpU(2) = rho*Cp*context.fixed_interior_value(this->_flow_vars.w_var(), qp); res = rhocpU*grad_T - k*(hess_T(0,0) + hess_T(1,1) + hess_T(2,2)); d_res_dT = 0; d_res_dgradT = rhocpU; d_res_dhessT = 0; d_res_dhessT(0,0) = -k; d_res_dhessT(1,1) = -k; d_res_dhessT(2,2) = -k; d_res_dU = rho * Cp * grad_T; }
libMesh::Real HeatTransferStabilizationHelper::compute_res_energy_steady( AssemblyContext& context, unsigned int qp, const libMesh::Real rho, const libMesh::Real Cp, const libMesh::Real k ) const { libMesh::Gradient grad_T = context.fixed_interior_gradient(this->_temp_vars.T_var(), qp); libMesh::Tensor hess_T = context.fixed_interior_hessian(this->_temp_vars.T_var(), qp); libMesh::RealGradient rhocpU( rho*Cp*context.fixed_interior_value(this->_flow_vars.u_var(), qp), rho*Cp*context.fixed_interior_value(this->_flow_vars.v_var(), qp) ); if(context.get_system().get_mesh().mesh_dimension() == 3) rhocpU(2) = rho*Cp*context.fixed_interior_value(this->_flow_vars.w_var(), qp); return rhocpU*grad_T - k*(hess_T(0,0) + hess_T(1,1) + hess_T(2,2)); }
libMesh::Real SpalartAllmarasStabilizationHelper::compute_res_spalart_steady( AssemblyContext& context, unsigned int qp, const libMesh::Real rho, const libMesh::Real mu, const libMesh::Real distance_qp ) const { // The flow velocity libMesh::Number u,v; u = context.interior_value(this->_flow_vars.u_var(), qp); v = context.interior_value(this->_flow_vars.v_var(), qp); libMesh::NumberVectorValue U(u,v); if ( context.get_system().get_mesh().mesh_dimension() == 3 ) U(2) = context.interior_value(this->_flow_vars.w_var(), qp); libMesh::RealGradient grad_u = context.fixed_interior_gradient(this->_flow_vars.u_var(), qp); libMesh::RealGradient grad_v = context.fixed_interior_gradient(this->_flow_vars.v_var(), qp); libMesh::Number nu_value = context.interior_value(this->_turbulence_vars.nu_var(), qp); libMesh::RealGradient grad_nu = context.fixed_interior_gradient(this->_turbulence_vars.nu_var(), qp); libMesh::RealTensor hess_nu = context.fixed_interior_hessian(this->_turbulence_vars.nu_var(), qp); // The convection term libMesh::Number rhoUdotGradnu = rho*(U*grad_nu); // The diffusion term libMesh::Number inv_sigmadivnuplusnuphysicalGradnu = (1./this->_sa_params.get_sigma())*(grad_nu*grad_nu + ((nu_value + mu)*(hess_nu(0,0) + hess_nu(1,1) + (this->_dim == 3)?hess_nu(2,2):0)) + this->_sa_params.get_cb2()*grad_nu*grad_nu); // The source term libMesh::Real vorticity_value_qp = this->_spalart_allmaras_helper.vorticity(context, qp); libMesh::Real S_tilde = this->_sa_params.source_fn(nu_value, mu, distance_qp, vorticity_value_qp); libMesh::Real source_term = this->_sa_params.get_cb1()*S_tilde*nu_value; // The destruction term libMesh::Real fw = this->_sa_params.destruction_fn(nu_value, distance_qp, S_tilde); libMesh::Real destruction_term = this->_sa_params.get_cw1()*fw*pow(nu_value/distance_qp, 2.); return rhoUdotGradnu + source_term + inv_sigmadivnuplusnuphysicalGradnu - destruction_term; }
void PracticeCDRinv::element_time_derivative( bool compute_jacobian, AssemblyContext& context, CachedValues& /*cache*/ ){ // The number of local degrees of freedom in each variable. const unsigned int n_c_dofs = context.get_dof_indices(_c_var).size(); // We get some references to cell-specific data that // will be used to assemble the linear system. // Element Jacobian * quadrature weights for interior integration. const std::vector<libMesh::Real> &JxW = context.get_element_fe(_c_var)->get_JxW(); // The temperature shape function gradients (in global coords.) // at interior quadrature points. const std::vector<std::vector<libMesh::RealGradient> >& dphi = context.get_element_fe(_c_var)->get_dphi(); const std::vector<std::vector<libMesh::Real> >& phi = context.get_element_fe(_c_var)->get_phi(); const std::vector<libMesh::Point>& q_points = context.get_element_fe(_c_var)->get_xyz(); libMesh::DenseSubMatrix<libMesh::Number> &J_c_zc = context.get_elem_jacobian(_c_var, _zc_var); libMesh::DenseSubMatrix<libMesh::Number> &J_c_c = context.get_elem_jacobian(_c_var, _c_var); libMesh::DenseSubMatrix<libMesh::Number> &J_zc_c = context.get_elem_jacobian(_zc_var, _c_var); libMesh::DenseSubMatrix<libMesh::Number> &J_zc_fc = context.get_elem_jacobian(_zc_var, _fc_var); libMesh::DenseSubMatrix<libMesh::Number> &J_fc_zc = context.get_elem_jacobian(_fc_var, _zc_var); libMesh::DenseSubMatrix<libMesh::Number> &J_fc_fc = context.get_elem_jacobian(_fc_var, _fc_var); libMesh::DenseSubVector<libMesh::Number> &Rc = context.get_elem_residual( _c_var );; libMesh::DenseSubVector<libMesh::Number> &Rzc = context.get_elem_residual( _zc_var ); libMesh::DenseSubVector<libMesh::Number> &Rfc = context.get_elem_residual( _fc_var ); // Now we will build the element Jacobian and residual. // Constructing the residual requires the solution and its // gradient from the previous timestep. This must be // calculated at each quadrature point by summing the // solution degree-of-freedom values by the appropriate // weight functions. unsigned int n_qpoints = context.get_element_qrule().n_points(); for (unsigned int qp=0; qp != n_qpoints; qp++){ libMesh::Number c = context.interior_value(_c_var, qp), zc = context.interior_value(_zc_var, qp), fc = context.interior_value(_fc_var, qp); libMesh::Gradient grad_c = context.interior_gradient(_c_var, qp), grad_zc = context.interior_gradient(_zc_var, qp), grad_fc = context.interior_gradient(_fc_var, qp); //location of quadrature point const libMesh::Real ptx = q_points[qp](0); const libMesh::Real pty = q_points[qp](1); int xind, yind; libMesh::Real xdist = 1.e10; libMesh::Real ydist = 1.e10; for(int ii=0; ii<x_pts.size(); ii++){ libMesh::Real tmp = std::abs(ptx - x_pts[ii]); if(xdist > tmp){ xdist = tmp; xind = ii; } else break; } for(int jj=0; jj<y_pts[xind].size(); jj++){ libMesh::Real tmp = std::abs(pty - y_pts[xind][jj]); if(ydist > tmp){ ydist = tmp; yind = jj; } else break; } libMesh::Real u = vel_field[xind][yind](0); libMesh::Real v = vel_field[xind][yind](1); libMesh::NumberVectorValue U (u, v); // First, an i-loop over the degrees of freedom. for (unsigned int i=0; i != n_c_dofs; i++){ Rc(i) += JxW[qp]*(-_k*grad_zc*dphi[i][qp] + U*grad_zc*phi[i][qp] + 2*_R*zc*c*phi[i][qp]); Rzc(i) += JxW[qp]*(-_k*grad_c*dphi[i][qp] - U*grad_c*phi[i][qp] + _R*c*c*phi[i][qp] + fc*phi[i][qp]); Rfc(i) += JxW[qp]*(_beta*grad_fc*dphi[i][qp] + zc*phi[i][qp]); if (compute_jacobian){ for (unsigned int j=0; j != n_c_dofs; j++){ J_c_zc(i,j) += JxW[qp]*(-_k*dphi[j][qp]*dphi[i][qp] + U*dphi[j][qp]*phi[i][qp] + 2*_R*phi[j][qp]*c*phi[i][qp]); J_c_c(i,j) += JxW[qp]*(2*_R*zc*phi[j][qp]*phi[i][qp]); J_zc_c(i,j) += JxW[qp]*(-_k*dphi[j][qp]*dphi[i][qp] - U*dphi[j][qp]*phi[i][qp] + 2*_R*c*phi[j][qp]*phi[i][qp]); J_zc_fc(i,j) += JxW[qp]*(phi[j][qp]*phi[i][qp]); J_fc_zc(i,j) += JxW[qp]*(phi[j][qp]*phi[i][qp]); J_fc_fc(i,j) += JxW[qp]*(_beta*dphi[j][qp]*dphi[i][qp]); } // end of the inner dof (j) loop } // end - if (compute_jacobian && context.get_elem_solution_derivative()) } // end of the outer dof (i) loop } // end of the quadrature point (qp) loop for(unsigned int dnum=0; dnum<datavals.size(); dnum++){ libMesh::Point data_point = datapts[dnum]; if(context.get_elem().contains_point(data_point)){ libMesh::Number cpred = context.point_value(_c_var, data_point); libMesh::Number cstar = datavals[dnum]; unsigned int dim = context.get_system().get_mesh().mesh_dimension(); libMesh::FEType fe_type = context.get_element_fe(_c_var)->get_fe_type(); //go between physical and reference element libMesh::Point c_master = libMesh::FEInterface::inverse_map(dim, fe_type, &context.get_elem(), data_point); std::vector<libMesh::Real> point_phi(n_c_dofs); for (unsigned int i=0; i != n_c_dofs; i++){ //get value of basis function at mapped point in reference (master) element point_phi[i] = libMesh::FEInterface::shape(dim, fe_type, &context.get_elem(), i, c_master); } for (unsigned int i=0; i != n_c_dofs; i++){ Rc(i) += (cpred - cstar)*point_phi[i]; if (compute_jacobian){ for (unsigned int j=0; j != n_c_dofs; j++) J_c_c(i,j) += point_phi[j]*point_phi[i] ; } } } } return; }