示例#1
0
文件: main.cpp 项目: NTFrs/pacs_proj
void Opzione<dim>::estimate_doubling(double time,  Vector<float> & errors) {

	Triangulation<dim> old_tria;
	old_tria.copy_triangulation(triangulation);
	FE_Q<dim> old_fe(1);
	DoFHandler<dim> old_dof(old_tria);
	old_dof.distribute_dofs(old_fe);
	Vector<double> old_solution=solution;
	{
	 Functions::FEFieldFunction<dim>	moveSol(old_dof,  old_solution);

	 triangulation.refine_global(1);
	 setup_system();
	 VectorTools::interpolate(dof_handler, moveSol, solution);
 }
	assemble_system();
	solve_one_step(time);
	{
	 Functions::FEFieldFunction<dim> moveSol(dof_handler, solution); 
	 cerr<< "dof size "<< dof_handler.n_dofs()<< " solution size "<< solution.size()<< endl;
	 cerr<< "olddof size "<< old_dof.n_dofs()<< " oldsolution size "<< old_solution.size()<< endl;

	 Vector<double> temp(old_dof.n_dofs());
	 cerr<< "this one2\n";
	 VectorTools::interpolate(old_dof, moveSol, temp);
	 cerr<< "this one2\n";
	 solution=temp;
 }
	triangulation.clear();
	triangulation.copy_triangulation(old_tria);
	setup_system();
	assemble_system();

	typename DoFHandler<dim>::active_cell_iterator cell=dof_handler.begin_active(),  endc=dof_handler.end();
	vector<types::global_dof_index> local_dof_indices(fe.dofs_per_cell);
	errors.reinit(old_tria.n_active_cells());
	double err(0);
	unsigned ind(0),  count(0);
	for (;cell !=endc;++cell) {
	 err=0;
	 cell->get_dof_indices(local_dof_indices);
	 for (unsigned i=0;i<fe.dofs_per_cell;++i) {
	  ind=local_dof_indices[i];
	  err+=(solution[ind]-old_solution[ind])*(solution[ind]-old_solution[ind]);
	}
	 errors[count]=(err);
	 count++;
   }

	solution=old_solution;
  }
示例#2
0
文件: step4.cpp 项目: spacy-dev/Spacy
void Step4< dim >::assemble_system()
{
    QGauss< dim > quadrature_formula( 2 );
    const RightHandSide< dim > right_hand_side;
    FEValues< dim > fe_values( fe, quadrature_formula, update_values | update_gradients |
                                                           update_quadrature_points |
                                                           update_JxW_values );
    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< types::global_dof_index > local_dof_indices( dofs_per_cell );
    typename DoFHandler< dim >::active_cell_iterator cell = dof_handler.begin_active(),
                                                     endc = dof_handler.end();
    for ( ; cell != endc; ++cell )
    {
        fe_values.reinit( cell );
        cell_matrix = 0;
        cell_rhs = 0;
        for ( unsigned int q_index = 0; q_index < n_q_points; ++q_index )
            for ( unsigned int i = 0; i < dofs_per_cell; ++i )
            {
                for ( unsigned int j = 0; j < dofs_per_cell; ++j )
                    cell_matrix( i, j ) +=
                        ( fe_values.shape_grad( i, q_index ) * fe_values.shape_grad( j, q_index ) *
                          fe_values.JxW( q_index ) );
                cell_rhs( i ) += ( fe_values.shape_value( i, q_index ) *
                                   right_hand_side.value( fe_values.quadrature_point( q_index ) ) *
                                   fe_values.JxW( q_index ) );
            }
        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 );
        }
    }

    std::map< types::global_dof_index, double > boundary_values;
    VectorTools::interpolate_boundary_values( dof_handler, 0, BoundaryValues< dim >(),
                                              boundary_values );
    MatrixTools::apply_boundary_values( boundary_values, system_matrix, solution, system_rhs );
}
示例#3
0
文件: main.cpp 项目: NTFrs/pacs_proj
void Opzione<dim>::assemble_system() {


	QGauss<dim> quadrature_formula(4);
	FEValues<dim> fe_values (fe, quadrature_formula, update_values   | update_gradients |
	 update_JxW_values | update_quadrature_points);

	const unsigned int   dofs_per_cell = 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_mat(dofs_per_cell);

	typename DoFHandler<dim>::active_cell_iterator
	cell=dof_handler.begin_active(),
	endc=dof_handler.end();
	Tensor< 1 , dim, double > trasp;
	Tensor<2, dim,  double > sig_mat;
	// 	Tensor< 1 , dim, double > increasing;
	vector<Point<dim> > quad_points(n_q_points);

	for (; cell !=endc;++cell) {
	 fe_values.reinit(cell);
	 cell_ff=0;
	 cell_mat=0;

	 quad_points=fe_values.get_quadrature_points();

	 for (unsigned q_point=0;q_point<n_q_points;++q_point) {

	  trasp[0]=par.sigma1*par.sigma1*quad_points[q_point][0]
	  +0.5*par.rho*par.sigma1*par.sigma2*quad_points[q_point][0]+(alpha_x-par.r)*quad_points[q_point][0];
	  trasp[1]=par.sigma2*par.sigma2*quad_points[q_point][1]
	  +0.5*par.rho*par.sigma1*par.sigma2*quad_points[q_point][1]+(alpha_y-par.r)*quad_points[q_point][1];

	  sig_mat[0][0]=0.5*par.sigma1*par.sigma1
	  *quad_points[q_point][0]*quad_points[q_point][0];
	  sig_mat[1][1]=0.5*par.sigma2*par.sigma2
	  *quad_points[q_point][1]*quad_points[q_point][1];
	  sig_mat[0][1]=0.5*par.rho*par.sigma1*par.sigma2*
	  quad_points[q_point][0]*quad_points[q_point][1];
	  sig_mat[1][0]=sig_mat[0][1];


	  for (unsigned i=0;i<dofs_per_cell;++i)
	  for (unsigned j=0; j<dofs_per_cell;++j) {


	   cell_mat(i, j)+=fe_values.JxW(q_point)*(
		(1/time_step+par.r+par.lambda1+par.lambda2)*fe_values.shape_value(i, q_point)*fe_values.shape_value(j,q_point)
		+fe_values.shape_grad(i, q_point)*sig_mat*fe_values.shape_grad(j, q_point)
		+fe_values.shape_value(i, q_point)*trasp*fe_values.shape_grad(j, 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);
	 constraints.distribute_local_to_global(cell_mat, local_dof_indices, system_matrix);
	 constraints.distribute_local_to_global(cell_ff, local_dof_indices, ff_matrix);
// 	 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_mat(i, j));
// 	  ff_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_ff(i, j));

// 	}
   }

	system_M2.add(1/time_step, ff_matrix);

  }
示例#4
0
文件: main.cpp 项目: NTFrs/pacs_proj
void Opzione<dim>::Levy_integral_part2(Vector<double> &J_x, Vector<double> &J_y) {

	double grid_tol(1.0e-6);
	J_x.reinit(solution.size());
	J_y.reinit(solution.size());

	unsigned int N(solution.size());

	QGauss<1> quad1D(3);    
	FEFaceValues<dim> fe_face(fe, quad1D, update_values  | update_quadrature_points | update_JxW_values);

	const unsigned int n_q_points=quad1D.size();

	Point<dim> z, karg;

	typename DoFHandler<dim>::active_cell_iterator cell=dof_handler.begin_active(),  endc=dof_handler.end();
	const unsigned int   dofs_per_cell = fe.dofs_per_cell;
	vector<bool> used(N, false);
	std::vector<types::global_dof_index> local_dof_indices(dofs_per_cell);

	for (; cell !=endc;++cell) {

	 cell->get_dof_indices(local_dof_indices);

	 for (unsigned int j=0;j<dofs_per_cell;++j) {
	  unsigned int it=local_dof_indices[j];
	  //                 cout << "Nodo globale "<< it<< endl;
	  if (used[it]==false)
	  {

	   used[it]=true;
	   Point<dim> actual_vertex=cell->vertex(j);
	   // 		cout<< "At point N "<< it<<" wich is "<<  actual_vertex<< endl;
	   typename DoFHandler<dim>::active_cell_iterator inner_cell=dof_handler.begin_active();
	   bool left(false),  bottom(false);
	   if (fabs(actual_vertex[0]-Smin[0])<grid_tol) {
		left=true;
	  // 		cout<< "it's a left node\n";
	  }
	   if (fabs(actual_vertex[1]-Smin[1])<grid_tol) {
		bottom=true;
	  // 	    cout<< "it's a bottom node \n";
	  }
	   for (;inner_cell !=endc;++inner_cell)
	   {
		if (left && inner_cell->face(0)->at_boundary())
		{
	  // 		  cout<< "\t boundary cell left\n";
	  unsigned actual_face(0);
	  fe_face.reinit(inner_cell, actual_face);
	  vector<Point <dim> > quad_points=fe_face.get_quadrature_points();

	  vector<double> sol_values(n_q_points);
	  fe_face.get_function_values(solution,  sol_values);

	  for (unsigned q_point=0;q_point<n_q_points;++q_point) {
	   z=quad_points[q_point];
	   karg(1)=log(z(1)/actual_vertex(1));
	   J_y[it]+=fe_face.JxW(q_point)*k_y.value(karg)*sol_values[q_point]/z(1);
	 }

  }

		if (bottom && inner_cell->face(2)->at_boundary())
		{
	  // 		  cout<< "\t boundary cell bottom\n";
	  unsigned actual_face(2);
	  fe_face.reinit(inner_cell, actual_face);
	  vector<Point <dim> > quad_points=fe_face.get_quadrature_points();

	  vector<double> sol_values(n_q_points);
	  fe_face.get_function_values(solution,  sol_values);

	  for (unsigned q_point=0;q_point<n_q_points;++q_point) {
	   z=quad_points[q_point];
	   karg(0)=log(z(0)/actual_vertex(0));
	   J_x[it]+=fe_face.JxW(q_point)*k_x.value(karg)*sol_values[q_point]/z(0);
	 }

  }


		if (fabs(inner_cell->face(3)->center()(1)-actual_vertex(1))<grid_tol) 
		{
	  // 		  cout<< "\t\t operating on upper face\n";
	  unsigned face(3);
	  fe_face.reinit(inner_cell, face);
	  vector<Point <dim> > quad_points=fe_face.get_quadrature_points();

	  vector<double> sol_values(n_q_points);
	  fe_face.get_function_values(solution,  sol_values);

	  for (unsigned q_point=0;q_point<n_q_points;++q_point) {
	   z=quad_points[q_point];
	   karg(0)=log(z(0)/actual_vertex(0));

	   J_x[it]+=fe_face.JxW(q_point)*k_x.value(karg)*sol_values[q_point]/z(0);
	 }

  }

		if (fabs(inner_cell->face(1)->center()(0)-actual_vertex(0))<grid_tol) 
		{
	  // 			cout<< "\t\t Operating on right face\n";
	  unsigned face(1);
	  fe_face.reinit(inner_cell, face);
	  vector<Point <dim> > quad_points=fe_face.get_quadrature_points();

	  vector<double> sol_values(n_q_points);
	  fe_face.get_function_values(solution,  sol_values);

	  for (unsigned q_point=0;q_point<n_q_points;++q_point) {
	   z=quad_points[q_point];
	   karg(1)=log(z(1)/actual_vertex(1));

	   J_y[it]+=fe_face.JxW(q_point)*k_y.value(karg)*sol_values[q_point]/z(1);
	 }

  }


	}
   }

	}
   }
  }
示例#5
0
文件: main.cpp 项目: NTFrs/pacs_proj
void Opzione<dim>::assemble_system() {
        
        QGauss<dim> quadrature_formula(4); // 2 nodes, 2d -> 4 quadrature points per cell
        
	FEValues<dim> fe_values (fe, quadrature_formula, update_values   | update_gradients | update_quadrature_points |
                                 update_JxW_values | update_jacobians); //  update_quadrature_points ?
        
	const unsigned int   dofs_per_cell = 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_ff(dofs_per_cell);
    FullMatrix<double> cell_system(dofs_per_cell);
        
	typename DoFHandler<dim>::active_cell_iterator
	cell=dof_handler.begin_active(),
	endc=dof_handler.end();
	
        // building tensors
        Tensor< dim , dim, double > sigma_matrix;
        
        sigma_matrix[0][0]=0.;
        sigma_matrix[1][1]=0.;
        sigma_matrix[0][1]=0.;
        sigma_matrix[1][0]=0.;
        
        
        Tensor< 1, dim, double > trasp;
		Tensor< 1, dim, double > divSIG;
        trasp[0]=0.;
        trasp[1]=0.;
		divSIG[1]=0.;
//         const Coefficient<dim> coefficient(par.r, par.S0);
	        std::vector<Point<dim> >    quad_points (n_q_points);
        
        // cell loop
        for (; cell !=endc;++cell) {
                
                fe_values.reinit(cell);
                
                cell_ff=0;
                cell_system=0;
                
                quad_points=fe_values.get_quadrature_points();
                
                for (unsigned q_point=0;q_point<n_q_points;++q_point) {
					

					trasp[0]=par.r*quad_points[q_point][0];
					trasp[1]=quad_points[q_point][0];
					// 									cerr<< "got to here\n";
					sigma_matrix[0][0]=0.5*par.sigma*par.sigma*
					quad_points[q_point][0]*quad_points[q_point][0];
					// 									cout<< fe_values.jacobian(q_point)[1][1]<< "\n";
					// 										sigma_matrix[1][1]=0.5*fabs(trasp[1])/fe_values.jacobian(q_point)[1][1];

					divSIG[0]=par.sigma*par.sigma*quad_points[q_point][0];

					
					
					
                        for (unsigned i=0;i<dofs_per_cell;++i)
                                for (unsigned j=0; j<dofs_per_cell;++j) {
                                        
// 									cerr<< "but not here";
                                        cell_ff(i, j)+=fe_values.shape_value(i, q_point)*fe_values.shape_value(j, q_point)*fe_values.JxW(q_point);
                                        cell_system(i, j)+=fe_values.JxW(q_point)*
                                        (fe_values.shape_grad(i, q_point)*sigma_matrix*fe_values.shape_grad(j, q_point)+
                                        fe_values.shape_value(i, q_point)*(divSIG-trasp)*fe_values.shape_grad(j, q_point)
                                        +(1/time_step+par.r)*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) {
                                
                                ff_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_ff(i, j));
                                system_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_system(i, j));
                                
                        }
                
        }
        /*
        cout<<"dd_matrix\n";
        dd_matrix.print(cout);
        cout<<"\nfd_matrix\n";
        fd_matrix.print(cout);
        cout<<"ff_matrix\n";
        ff_matrix.print(cout);*/
        
        system_M2.add(1/time_step, ff_matrix);
        
#ifdef __VERBOSE__
        cout<<"system_matrix\n";
        system_matrix.print_formatted(cout);
        cout<<"system_M2\n";
        system_M2.print_formatted(cout);
#endif
}
示例#6
0
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);
}
示例#7
0
void OptionBasePrice<dim>::assemble_system()
{
        using namespace std;
        using namespace dealii;
        
        std::vector<double> alpha(dim,0.);
        
        if (this->model_type!=OptionBase<dim>::ModelType::BlackScholes)
                this->levy->get_alpha(alpha);
        
        double lambda=0.;
        if (this->model_type!=OptionBase<dim>::ModelType::BlackScholes)
                for (unsigned i=0; i<dim; ++i)
                        lambda+=this->models[i]->get_lambda();
        
        dealii::QGauss<dim> quadrature_formula(2*dim);
	dealii::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();
        
	if (this->verbose) {
                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);
        
	dealii::FullMatrix<double> cell_ff(dofs_per_cell);
	dealii::FullMatrix<double> cell_mat(dofs_per_cell);
        
	typename DoFHandler<dim>::active_cell_iterator
	cell=(this->dof_handler).begin_active(),
	endc=(this->dof_handler).end();
	dealii::Tensor< 1 , dim, double > trasp;
	dealii::Tensor< 2 , dim, double > sig_mat;
	
	vector<dealii::Point<dim> > quad_points(n_q_points);
        
        for (; cell !=endc;++cell) {
                fe_values.reinit(cell);
                cell_ff=0;
                cell_mat=0;
                
                quad_points=fe_values.get_quadrature_points();
                
                for (unsigned q_point=0;q_point<n_q_points;++q_point) {
                        
                        tools::make_trasp<dim>(trasp, this->models, this->r, this->rho, alpha, quad_points[q_point]);
                        tools::make_diff<dim>(sig_mat, this->models, this->rho, quad_points[q_point]);
                        
                        for (unsigned i=0;i<dofs_per_cell;++i)
                                for (unsigned j=0; j<dofs_per_cell;++j) {
                                        
                                        
                                        cell_mat(i, j)+=fe_values.JxW(q_point)*
                                        (
                                         (1/(this->dt)+(this->r)+lambda)*fe_values.shape_value(i, q_point)*fe_values.shape_value(j,q_point)
                                         +fe_values.shape_grad(i, q_point)*sig_mat*fe_values.shape_grad(j, q_point)
                                         -fe_values.shape_value(i, q_point)*trasp*fe_values.shape_grad(j, 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);
                
                auto pointer=static_cast<SparseMatrix<double> *> (&(this->system_matrix));
                this->constraints.distribute_local_to_global(cell_mat, local_dof_indices, *pointer);
                this->constraints.distribute_local_to_global(cell_ff, local_dof_indices, this->ff_matrix);
        }
        
	(this->system_M2).add(1./(this->dt), this->ff_matrix);
        
        if (this->verbose) {
                cout<<"Done!\n";
        }
        
        return;
        
}
示例#8
0
void LevyIntegralPrice<2>::compute_J(dealii::Vector<double> & sol, dealii::DoFHandler<2> & dof_handler, dealii::FE_Q<2> & fe, std::vector< dealii::Point<2> > const & vertices) {
	using namespace dealii;
	using namespace std;
        
        
        const unsigned N(dof_handler.n_dofs());
        
        //we clear these dealii Vectors to be all zero
	j1.reinit(N);
        j2.reinit(N);
        
        //we need a one dimensional quadrature formula, as well as the object that handles the values of FE on the faces of cells
	QGauss<1> quad1D(3);    
	FEFaceValues<2> fe_face(fe, quad1D, update_values  | update_quadrature_points | update_JxW_values);
        
	const unsigned int n_q_points=quad1D.size();
        
        //an iterator over cells and the final condition
	typename DoFHandler<2>::active_cell_iterator cell=dof_handler.begin_active(), endc=dof_handler.end();
        
        
	double z, karg;
        
	//vectors that will contain values of the solution and quadrature points on the current face 
	std::vector<Point <2> > quad_points(n_q_points);
	std::vector<double> sol_values(n_q_points);
        
	Point<2> actual_vertex;
	
        //we loop over all cells
        for (;cell !=endc;++cell) {
                
                //if this cell is at the left boundary,  we need to add the contribution to the vertices on this boundary.
		if (cell->face(0)->at_boundary()) {
                        
                        fe_face.reinit(cell,0);
                        quad_points=fe_face.get_quadrature_points();
                        
                        fe_face.get_function_values(sol,  sol_values);
                        
                        for (unsigned it=0;it<N;++it)
			{
                                actual_vertex=vertices[it];
                                if (fabs(actual_vertex(0)-this->lower_limit(0))<constants::grid_toll) {
                                        for (unsigned q_point=0;q_point<n_q_points;++q_point) {
                                                z=quad_points[q_point](1);
                                                karg=log(z/actual_vertex(1));
                                                j2[it]+=fe_face.JxW(q_point)*((*mods[1]).density(karg))*(sol_values[q_point])/z;
                                        }
                                }
                                
			}
		}
		//same for the bottom boundary
		if (cell->face(2)->at_boundary()) {
                        
                        fe_face.reinit(cell, 2);
                        quad_points=fe_face.get_quadrature_points();
			fe_face.get_function_values(sol,  sol_values);
			
			for (unsigned it=0;it<N;++it)
                        {
				actual_vertex=vertices[it];
				if (fabs(actual_vertex(1)-this->lower_limit(1))<constants::grid_toll) {
					for (unsigned q_point=0;q_point<n_q_points;++q_point) {
                                                z=quad_points[q_point](0);
                                                karg=log(z/actual_vertex(0));
                                                j1[it]+=fe_face.JxW(q_point)*((*mods[0]).density(karg))*(sol_values[q_point])/z;
					}
                                        
                                }
                                
                                
                        }
                }
		
		//now the upper side of the cell (number 3)
		{
                        //since this call is costly,  we save its value
                        double center(cell->face(3)->center()(1));
                        //we tell fe_face wich face we are on
                        fe_face.reinit(cell, 3);
                        
                        //we save the quadrature points and values of the solution since they will be used multiple times
                        quad_points=fe_face.get_quadrature_points();
                        fe_face.get_function_values(sol,  sol_values);
                        
                        //loop over all vertices
                        for (unsigned it=0;it<N;++it) {
                                actual_vertex=vertices[it];
                                //only the vertices whoose y coordiate is on this face get the contribute to j1[it]
                                if (fabs(actual_vertex(1)-center)<constants::grid_toll)
                                {
                                        //we finally calculate the contribute to j1
                                        for (unsigned q_point=0;q_point<n_q_points;++q_point) {
                                                z=quad_points[q_point](0);
                                                karg=log(z/actual_vertex(0));
                                                j1[it]+=fe_face.JxW(q_point)*((*mods[0]).density(karg))*(sol_values[q_point])/z;
                                        }
                                }
                                
                        }
		}
		{	  
                        //the same but on the right face,  inverting x and y coordinates
                        double center(cell->face(1)->center()(0));
                        fe_face.reinit(cell, 1);
                        
                        quad_points=fe_face.get_quadrature_points();
                        
                        fe_face.get_function_values(sol,  sol_values);
                        
                        for (unsigned it=0;it<N;++it) {
                                actual_vertex=vertices[it];
                                if (fabs(actual_vertex(0)-center)<constants::grid_toll)
                                {
                                        for (unsigned q_point=0;q_point<n_q_points;++q_point) {
                                                z=quad_points[q_point](1);
                                                karg=log(z/actual_vertex(1));
                                                j2[it]+=fe_face.JxW(q_point)*((*mods[1]).density(karg))*(sol_values[q_point])/z;
                                        }
                                }
                                
                        }
		}
                
                
		
        }
        
        j_ran=true;
        
}
示例#9
0
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;
        
}
示例#10
0
文件: main.cpp 项目: NTFrs/pacs_proj
void Opzione<dim>::assemble_system() {
        
	Levy_integral_part1();
        
	QGauss<dim> quadrature_formula(2);                  // 2 nodes, 2d -> 4 quadrature points per cell
        
	FEValues<dim> fe_values (fe, quadrature_formula, update_values   | update_gradients |
                                 update_JxW_values);
        
	const unsigned int   dofs_per_cell = 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);
        
	typename DoFHandler<dim>::active_cell_iterator
	cell=dof_handler.begin_active(),
	endc=dof_handler.end();
        
	// building tensors
	Tensor< dim , dim, double > sigma_matrix;
        
	sigma_matrix[0][0]=par.sigma1*par.sigma1;
	sigma_matrix[1][1]=par.sigma2*par.sigma2;
	sigma_matrix[0][1]=par.sigma1*par.sigma2*par.ro;
	sigma_matrix[1][0]=par.sigma1*par.sigma2*par.ro;
	/*
         Tensor< 1 , dim, double > ones;
         for (unsigned i=0;i<dim;++i)
         ones[i]=1;
	 */
	Tensor< 1, dim, double > trasp;
	trasp[0]=par.r-par.sigma1*par.sigma1/2-alpha1;
	trasp[1]=par.r-par.sigma2*par.sigma2/2-alpha2;
        
	// cell loop
	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/time_step+par.r+par.lambda1+par.lambda2)*
                                         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) {
                                
                                ff_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_ff(i, j));
                                system_matrix.add(local_dof_indices[i], local_dof_indices[j], cell_system(i, j));
                                
                        }
                
        }
        
	system_M2.add(1/time_step, ff_matrix);
        
#ifdef __VERBOSE__
	cout<<"system_matrix\n";
	system_matrix.print_formatted(cout);
	cout<<"system_M2\n";
	system_M2.print_formatted(cout);
#endif
}