Report ElasticProblem2DOnCellV2<dim>::calculate_cells_stress () { cells_stress .clear (); dealii::QGauss<dim> quadrature_formula(2); dealii::FEValues<dim> fe_values (finite_element, quadrature_formula, dealii::update_gradients | dealii::update_quadrature_points | dealii::update_JxW_values); const uint8_t dofs_per_cell = finite_element.dofs_per_cell; const uint8_t num_quad_points = quadrature_formula.size(); typename dealii::DoFHandler<dim>::active_cell_iterator cell = this->domain.dof_handler.begin_active(); typename dealii::DoFHandler<dim>::active_cell_iterator endc = this->domain.dof_handler.end(); std::vector<unsigned int> local_dof_indices (dofs_per_cell); size_t cell_num = 0; for (; cell != endc; ++cell) { fe_values .reinit (cell); dbl area = 0.0; for(st i = 0; i < num_quad_points; ++i) area += fe_values.JxW(o); dbl mat_id = cell->material_id(); std::array<dbl,2> deform; for (auto i : {x, y}) { deform[i][j] = 0.0; for(auto i : {1, 2, 3, 4}) { dbl summ = 0.0; for (size_t q_point = 0; q_point < num_quad_points; ++q_point) summ += fe_values.shape_grad (n, q_point)[i] * fe_values.JxW(q_point); deform[i] += solution(cell->vertex_dof_index(n, 0)) * summ; }; deform[i] /= area; }; }; REPORT_USE( Report report; report.result = true; _return (report););
void ElasticProblem<dim>::assemble_system () { QGauss<dim> quadrature_formula(2); FEValues<dim> fe_values (fe, quadrature_formula, update_values | update_gradients | update_quadrature_points | update_JxW_values); TestESM<dim> test_esm; double coef[2] = {1.0, 1.0}; test_esm .set_coefficient(coef); const unsigned int dofs_per_cell = fe.dofs_per_cell; const unsigned int n_q_points = quadrature_formula.size(); FullMatrix<double> cell_matrix (dofs_per_cell, dofs_per_cell); Vector<double> cell_rhs (dofs_per_cell); std::vector<unsigned int> local_dof_indices (dofs_per_cell); std::vector<double> lambda_values (n_q_points); std::vector<double> mu_values (n_q_points); ConstantFunction<dim> lambda(1.), mu(1.); RightHandSide<dim> right_hand_side; std::vector<Vector<double> > rhs_values (n_q_points, Vector<double>(dim)); typename DoFHandler<dim>::active_cell_iterator cell = dof_handler.begin_active(), endc = dof_handler.end(); for (; cell!=endc; ++cell) { cell_matrix = 0; cell_rhs = 0; fe_values.reinit (cell); lambda.value_list (fe_values.get_quadrature_points(), lambda_values); mu.value_list (fe_values.get_quadrature_points(), mu_values); right_hand_side.vector_value_list (fe_values.get_quadrature_points(), rhs_values); for (unsigned int i=0; i<dofs_per_cell; ++i) { const unsigned int component_i = fe.system_to_component_index(i).first; for (unsigned int j=0; j<dofs_per_cell; ++j) { const unsigned int component_j = fe.system_to_component_index(j).first; // printf("i = %d, j = %d, res = %f\n", i, j, // test_esm (i, j, quadrature_formula, fe_values)); cell_matrix(i,j) += test_esm (i, j, quadrature_formula, fe_values); //// double res = 0; for (unsigned int q_point=0; q_point<n_q_points; ++q_point) { // cell_matrix(i,j) res += ( (fe_values.shape_grad(i,q_point)[component_i] * fe_values.shape_grad(j,q_point)[component_j] * lambda_values[q_point]) + (fe_values.shape_grad(i,q_point)[component_j] * fe_values.shape_grad(j,q_point)[component_i] * mu_values[q_point]) + ((component_i == component_j) ? (fe_values.shape_grad(i,q_point) * fe_values.shape_grad(j,q_point) * mu_values[q_point]) : 0) ) * fe_values.JxW(q_point); //// printf("res=%f\n", res); } } } printf("!!!!!!!!!!!!!\n"); // Assembling the right hand // side is also just as // discussed in the // introduction: for (unsigned int i=0; i<dofs_per_cell; ++i) { const unsigned int component_i = fe.system_to_component_index(i).first; for (unsigned int q_point=0; q_point<n_q_points; ++q_point) cell_rhs(i) += fe_values.shape_value(i,q_point) * rhs_values[q_point](component_i) * fe_values.JxW(q_point); } cell->get_dof_indices (local_dof_indices); for (unsigned int i=0; i<dofs_per_cell; ++i) { for (unsigned int j=0; j<dofs_per_cell; ++j) system_matrix.add (local_dof_indices[i], local_dof_indices[j], cell_matrix(i,j)); system_rhs(local_dof_indices[i]) += cell_rhs(i); } } // hanging_node_constraints.condense (system_matrix); // hanging_node_constraints.condense (system_rhs); for (size_t i = 0; i < system_matrix.m(); ++i) for (size_t j = 0; j < system_matrix.n(); ++j) if (system_matrix .el (i,j)) printf("system_matrix_old(%d,%d)=%f\n", i,j,system_matrix(i,j)); std::map<unsigned int,double> boundary_values; VectorTools::interpolate_boundary_values (dof_handler, 0, ConstantFunction<dim>(0, dim),// //ZeroFunction<dim>(dim), boundary_values); MatrixTools::apply_boundary_values (boundary_values, system_matrix, solution, system_rhs); }
void OptionBaseLogPrice<dim>::assemble_system() { double alpha(0.); double lambda(0.); if (this->model_type!=OptionBase<dim>::ModelType::BlackScholes && dim==1) { alpha=this->levy->get_part1(); lambda=this->models[0]->get_lambda(); } QGauss<dim> quadrature_formula(2*dim); FEValues<dim> fe_values (this->fe, quadrature_formula, update_values | update_gradients | update_JxW_values | update_quadrature_points); const unsigned int dofs_per_cell = (this->fe).dofs_per_cell; const unsigned int n_q_points = quadrature_formula.size(); cout<< "Assembling System\n"; cout<< "Degrees of freedom per cell: "<< dofs_per_cell<< endl; cout<< "Quadrature points per cell: "<< n_q_points<< endl; std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell); FullMatrix<double> cell_dd(dofs_per_cell); FullMatrix<double> cell_fd(dofs_per_cell); FullMatrix<double> cell_ff(dofs_per_cell); FullMatrix<double> cell_system(dofs_per_cell); SparseMatrix<double> dd_matrix; SparseMatrix<double> fd_matrix; dd_matrix.reinit(this->sparsity_pattern); fd_matrix.reinit(this->sparsity_pattern); typename DoFHandler<dim>::active_cell_iterator cell=(this->dof_handler).begin_active(), endc=(this->dof_handler).end(); Tensor< 1 , dim, double > trasp; Tensor< 2 , dim, double > sig_mat; vector<Point<dim> > quad_points(n_q_points); if (dim==1) { Tensor< 1 , dim, double > ones; for (unsigned i=0;i<dim;++i) ones[i]=1; for (; cell !=endc;++cell) { fe_values.reinit(cell); cell_dd=0; cell_fd=0; cell_ff=0; for (unsigned q_point=0;q_point<n_q_points;++q_point) for (unsigned i=0;i<dofs_per_cell;++i) for (unsigned j=0; j<dofs_per_cell;++j) { cell_dd(i, j)+=fe_values.shape_grad(i, q_point)*fe_values.shape_grad(j, q_point)*fe_values.JxW(q_point); cell_fd(i, j)+=fe_values.shape_value(i, q_point)*(ones*fe_values.shape_grad(j,q_point))*fe_values.JxW(q_point); cell_ff(i, j)+=fe_values.shape_value(i, q_point)*fe_values.shape_value(j, q_point)*fe_values.JxW(q_point); } cell->get_dof_indices (local_dof_indices); for (unsigned int i=0; i<dofs_per_cell;++i) for (unsigned int j=0; j< dofs_per_cell; ++j) { dd_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_dd(i, j)); fd_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_fd(i, j)); (this->ff_matrix).add(local_dof_indices[i], local_dof_indices[j], cell_ff(i, j)); } } double diff=(*(this->models[0])).get_vol()*(*(this->models[0])).get_vol()/2; double trasp=this->r-(*(this->models[0])).get_vol()* (*(this->models[0])).get_vol()/2-alpha; double reaz=-(this->r)-lambda; (*(this->system_matrix)).add(1/(this->dt)-0.5*reaz, this->ff_matrix); (*(this->system_matrix)).add(0.5*diff, dd_matrix); (*(this->system_matrix)).add(-0.5*trasp, fd_matrix); (this->system_M2).add(1/(this->dt)+0.5*reaz, this->ff_matrix); (this->system_M2).add(-0.5*diff, dd_matrix); (this->system_M2).add(0.5*trasp, fd_matrix); } else { // building tensors Tensor< 2 , dim, double > sigma_matrix; sigma_matrix[0][0]=(*(this->models[0])).get_vol()*(*(this->models[0])).get_vol(); sigma_matrix[1][1]=(*(this->models[1])).get_vol()*(*(this->models[1])).get_vol(); sigma_matrix[0][1]=(*(this->models[0])).get_vol()*(*(this->models[1])).get_vol()* (this->rho); sigma_matrix[1][0]=(*(this->models[0])).get_vol()*(*(this->models[1])).get_vol()* (this->rho); /* Tensor< 1 , dim, double > ones; for (unsigned i=0;i<dim;++i) ones[i]=1; */ Tensor< 1, dim, double > trasp; trasp[0]=this->r-(*(this->models[0])).get_vol()*(*(this->models[0])).get_vol()/2-alpha; trasp[1]=this->r-(*(this->models[1])).get_vol()*(*(this->models[1])).get_vol()/2-alpha; for (; cell !=endc;++cell) { fe_values.reinit(cell); cell_dd=0; cell_fd=0; cell_ff=0; cell_system=0; for (unsigned q_point=0;q_point<n_q_points;++q_point) for (unsigned i=0;i<dofs_per_cell;++i) for (unsigned j=0; j<dofs_per_cell;++j) { // mass matrix cell_ff(i, j)+=fe_values.shape_value(i, q_point)*fe_values.shape_value(j, q_point)*fe_values.JxW(q_point); // system matrix cell_system(i, j)+=fe_values.JxW(q_point)* (0.5*fe_values.shape_grad(i, q_point)*sigma_matrix*fe_values.shape_grad(j, q_point)- fe_values.shape_value(i, q_point)*(trasp*fe_values.shape_grad(j,q_point))+ (1/(this->dt)+this->r+lambda+lambda)* fe_values.shape_value(i, q_point)*fe_values.shape_value(j, q_point)); } cell->get_dof_indices (local_dof_indices); for (unsigned int i=0; i<dofs_per_cell;++i) for (unsigned int j=0; j< dofs_per_cell; ++j) { (this->ff_matrix).add(local_dof_indices[i], local_dof_indices[j], cell_ff(i, j)); (*(this->system_matrix)).add(local_dof_indices[i], local_dof_indices[j], cell_system(i, j)); } } (this->system_M2).add(1/(this->dt), this->ff_matrix); } return; }