📄 ex8.php
字号:
The penalty parameter.</div><div class ="fragment"><pre> const Real penalty = 1.e10; </pre></div><div class = "comment">Here we apply sinusoidal pressure values for 0<t<0.002at one end of the pipe-mesh.</div><div class ="fragment"><pre> Real p_value; if (current_time < .002 ) p_value = sin(2*pi*current_time/.002); else p_value = .0; </pre></div><div class = "comment">Now add the contributions to the matrix and the rhs.</div><div class ="fragment"><pre> rhs.add(dn, p_value*penalty); </pre></div><div class = "comment">Add the panalty parameter to the global matrixif desired.</div><div class ="fragment"><pre> if (do_for_matrix) matrix.add(dn, dn, penalty); } } // loop n_cnt }</pre></div><a name="nocomments"></a> <br><br><br> <h1> The program without comments: </h1> <pre> #include <iostream> #include <fstream> #include <algorithm> #include <stdio.h> #include <math.h> #include <B><FONT COLOR="#BC8F8F">"libmesh.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"gmv_io.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"newmark_system.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"equation_systems.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"fe.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"quadrature_gauss.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dense_matrix.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dense_vector.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"sparse_matrix.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"numeric_vector.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"dof_map.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"node.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"elem.h"</FONT></B> #include <B><FONT COLOR="#BC8F8F">"mesh_data.h"</FONT></B> <B><FONT COLOR="#228B22">void</FONT></B> assemble_wave(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name); <B><FONT COLOR="#228B22">void</FONT></B> apply_initial(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name); <B><FONT COLOR="#228B22">void</FONT></B> fill_dirichlet_bc(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name); <B><FONT COLOR="#228B22">int</FONT></B> main (<B><FONT COLOR="#228B22">int</FONT></B> argc, <B><FONT COLOR="#228B22">char</FONT></B>** argv) { <B><FONT COLOR="#5F9EA0">libMesh</FONT></B>::init (argc, argv); { <B><FONT COLOR="#A020F0">if</FONT></B> (argc < 2) { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"Usage: "</FONT></B> << argv[0] << <B><FONT COLOR="#BC8F8F">" [meshfile]"</FONT></B> << std::endl; error(); } <B><FONT COLOR="#A020F0">else</FONT></B> { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Running "</FONT></B> << argv[0]; <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">int</FONT></B> i=1; i<argc; i++) <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">" "</FONT></B> << argv[i]; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << std::endl << std::endl; } #ifdef HAVE_PETSC <B><FONT COLOR="#A020F0">if</FONT></B> ((libMesh::on_command_line(<B><FONT COLOR="#BC8F8F">"--use-laspack"</FONT></B>)) || (libMesh::on_command_line(<B><FONT COLOR="#BC8F8F">"--disable-petsc"</FONT></B>))) #endif { <B><FONT COLOR="#5F9EA0">std</FONT></B>::cerr << <B><FONT COLOR="#BC8F8F">"WARNING! It appears you are using the\n"</FONT></B> << <B><FONT COLOR="#BC8F8F">"LasPack solvers. ex8 is known not to converge\n"</FONT></B> << <B><FONT COLOR="#BC8F8F">"using LasPack, but should work OK with PETSc.\n"</FONT></B> << <B><FONT COLOR="#BC8F8F">"If possible, download and install the PETSc\n"</FONT></B> << <B><FONT COLOR="#BC8F8F">"library from www-unix.mcs.anl.gov/petsc/petsc-2/\n"</FONT></B> << std::endl; } <B><FONT COLOR="#5F9EA0">std</FONT></B>::string mesh_file = argv[1]; <B><FONT COLOR="#5F9EA0">std</FONT></B>::cout << <B><FONT COLOR="#BC8F8F">"Mesh file is: "</FONT></B> << mesh_file << std::endl; <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim = 3; Mesh mesh (dim); MeshData mesh_data(mesh); mesh.read(mesh_file, &mesh_data); mesh.print_info(); <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> result_node = 274; <B><FONT COLOR="#228B22">const</FONT></B> Real delta_t = .0000625; <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_time_steps = 300; EquationSystems equation_systems (mesh); { equation_systems.add_system<NewmarkSystem> (<B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); NewmarkSystem & t_system = equation_systems.get_system<NewmarkSystem> (<B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); t_system.add_variable(<B><FONT COLOR="#BC8F8F">"p"</FONT></B>, FIRST); t_system.attach_assemble_function (assemble_wave); t_system.attach_init_function (apply_initial); t_system.set_newmark_parameters(delta_t); equation_systems.parameters.set<Real>(<B><FONT COLOR="#BC8F8F">"speed"</FONT></B>) = 1000.; equation_systems.parameters.set<Real>(<B><FONT COLOR="#BC8F8F">"fluid density"</FONT></B>) = 1000.; equation_systems.parameters.set<Real>(<B><FONT COLOR="#BC8F8F">"time"</FONT></B>) = 0.; equation_systems.init(); equation_systems.print_info(); } <B><FONT COLOR="#5F9EA0">std</FONT></B>::ofstream res_out(<B><FONT COLOR="#BC8F8F">"pressure_node.res"</FONT></B>); <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> res_node_no = result_node; <B><FONT COLOR="#228B22">const</FONT></B> Node& res_node = mesh.node(res_node_no-1); <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dof_no = res_node.dof_number(0,0,0); NewmarkSystem& t_system = equation_systems.get_system<NewmarkSystem> (<B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); t_system.assemble(); Real t_time = 0.; res_out << <B><FONT COLOR="#BC8F8F">"# pressure at node "</FONT></B> << res_node_no << <B><FONT COLOR="#BC8F8F">"\n"</FONT></B> << <B><FONT COLOR="#BC8F8F">"# time\tpressure\n"</FONT></B> << t_time << <B><FONT COLOR="#BC8F8F">"\t"</FONT></B> << 0 << std::endl; <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> time_step=0; time_step<n_time_steps; time_step++) { t_time += delta_t; equation_systems.parameters.set<Real>(<B><FONT COLOR="#BC8F8F">"time"</FONT></B>) = t_time; t_system.update_rhs(); <B><FONT COLOR="#A020F0">if</FONT></B> (time_step == 0) { equation_systems.parameters.set<<B><FONT COLOR="#228B22">bool</FONT></B>>(<B><FONT COLOR="#BC8F8F">"Newmark set BC for Matrix"</FONT></B>) = true; fill_dirichlet_bc(equation_systems, <B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); equation_systems.parameters.set<<B><FONT COLOR="#228B22">bool</FONT></B>>(<B><FONT COLOR="#BC8F8F">"Newmark set BC for Matrix"</FONT></B>) = false; } <B><FONT COLOR="#A020F0">else</FONT></B> fill_dirichlet_bc(equation_systems, <B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); t_system.solve(); <B><FONT COLOR="#A020F0">if</FONT></B> (time_step == 30 || time_step == 60 || time_step == 90 || time_step == 120 ) { <B><FONT COLOR="#228B22">char</FONT></B> buf[14]; sprintf (buf, <B><FONT COLOR="#BC8F8F">"out.%03d.gmv"</FONT></B>, time_step); GMVIO(mesh).write_equation_systems (buf, equation_systems); } t_system.update_u_v_a(); NumericVector<Number> &displacement = t_system.get_vector(<B><FONT COLOR="#BC8F8F">"displacement"</FONT></B>); <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector<Number> global_displacement(displacement.size()); displacement.localize(global_displacement); res_out << t_time << <B><FONT COLOR="#BC8F8F">"\t"</FONT></B> << global_displacement[dof_no] << std::endl; } } <B><FONT COLOR="#A020F0">return</FONT></B> libMesh::close (); } <B><FONT COLOR="#228B22">void</FONT></B> assemble_wave(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name) { assert (system_name == <B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); <B><FONT COLOR="#228B22">const</FONT></B> Mesh& mesh = es.get_mesh(); <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dim = mesh.mesh_dimension(); <B><FONT COLOR="#228B22">const</FONT></B> Real speed = es.parameters.get<Real>(<B><FONT COLOR="#BC8F8F">"speed"</FONT></B>); <B><FONT COLOR="#228B22">const</FONT></B> Real rho = es.parameters.get<Real>(<B><FONT COLOR="#BC8F8F">"fluid density"</FONT></B>); NewmarkSystem & t_system = es.get_system<NewmarkSystem> (system_name); FEType fe_type = t_system.get_dof_map().variable_type(0); SparseMatrix<Number>& stiffness = t_system.get_matrix(<B><FONT COLOR="#BC8F8F">"stiffness"</FONT></B>); SparseMatrix<Number>& damping = t_system.get_matrix(<B><FONT COLOR="#BC8F8F">"damping"</FONT></B>); SparseMatrix<Number>& mass = t_system.get_matrix(<B><FONT COLOR="#BC8F8F">"mass"</FONT></B>); NumericVector<Number>& force = t_system.get_vector(<B><FONT COLOR="#BC8F8F">"force"</FONT></B>); SparseMatrix<Number>& matrix = *t_system.matrix; DenseMatrix<Number> zero_matrix; AutoPtr<FEBase> fe (FEBase::build(dim, fe_type)); QGauss qrule (dim, SECOND); fe->attach_quadrature_rule (&qrule); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Real>& JxW = fe->get_JxW(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<std::vector<Real> >& phi = fe->get_phi(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<std::vector<RealGradient> >& dphi = fe->get_dphi(); <B><FONT COLOR="#228B22">const</FONT></B> DofMap& dof_map = t_system.get_dof_map(); DenseMatrix<Number> Ke, Ce, Me; DenseVector<Number> Fe; <B><FONT COLOR="#5F9EA0">std</FONT></B>::vector<<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B>> dof_indices; <B><FONT COLOR="#5F9EA0">MeshBase</FONT></B>::const_element_iterator el = mesh.elements_begin(); <B><FONT COLOR="#228B22">const</FONT></B> MeshBase::const_element_iterator end_el = mesh.elements_end(); <B><FONT COLOR="#A020F0">for</FONT></B> ( ; el != end_el; ++el) { <B><FONT COLOR="#228B22">const</FONT></B> Elem* elem = *el; dof_map.dof_indices (elem, dof_indices); fe->reinit (elem); { <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_dof_indices = dof_indices.size(); Ke.resize (n_dof_indices, n_dof_indices); Ce.resize (n_dof_indices, n_dof_indices); Me.resize (n_dof_indices, n_dof_indices); zero_matrix.resize (n_dof_indices, n_dof_indices); Fe.resize (n_dof_indices); } <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> qp=0; qp<qrule.n_points(); qp++) { <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> i=0; i<phi.size(); i++) <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> j=0; j<phi.size(); j++) { Ke(i,j) += JxW[qp]*(dphi[i][qp]*dphi[j][qp]); Me(i,j) += JxW[qp]*phi[i][qp]*phi[j][qp] *1./(speed*speed); } <I><FONT COLOR="#B22222">// end of the matrix summation loop </FONT></I> } <I><FONT COLOR="#B22222">// end of quadrature point loop</FONT></I> { <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> side=0; side<elem->n_sides(); side++) <B><FONT COLOR="#A020F0">if</FONT></B> (!true) { AutoPtr<FEBase> fe_face (FEBase::build(dim, fe_type)); QGauss qface(dim-1, SECOND); fe_face->attach_quadrature_rule (&qface); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<std::vector<Real> >& phi_face = fe_face->get_phi(); <B><FONT COLOR="#228B22">const</FONT></B> std::vector<Real>& JxW_face = fe_face->get_JxW(); fe_face->reinit(elem, side); <B><FONT COLOR="#228B22">const</FONT></B> Real acc_n_value = 1.0; <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> qp=0; qp<qface.n_points(); qp++) { <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> i=0; i<phi_face.size(); i++) { Fe(i) += acc_n_value*rho *phi_face[i][qp]*JxW_face[qp]; } } <I><FONT COLOR="#B22222">// end face quadrature point loop </FONT></I> } <I><FONT COLOR="#B22222">// end if (elem->neighbor(side) == NULL)</FONT></I> } <I><FONT COLOR="#B22222">// end boundary condition section </FONT></I> stiffness.add_matrix (Ke, dof_indices); damping.add_matrix (Ce, dof_indices); mass.add_matrix (Me, dof_indices); force.add_vector (Fe, dof_indices); matrix.add_matrix(zero_matrix, dof_indices); } <I><FONT COLOR="#B22222">// end of element loop</FONT></I> <B><FONT COLOR="#A020F0">return</FONT></B>; } <B><FONT COLOR="#228B22">void</FONT></B> apply_initial(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name) { NewmarkSystem & t_system = es.get_system<NewmarkSystem> (system_name); NumericVector<Number>& pres_vec = t_system.get_vector(<B><FONT COLOR="#BC8F8F">"displacement"</FONT></B>); NumericVector<Number>& vel_vec = t_system.get_vector(<B><FONT COLOR="#BC8F8F">"velocity"</FONT></B>); NumericVector<Number>& acc_vec = t_system.get_vector(<B><FONT COLOR="#BC8F8F">"acceleration"</FONT></B>); pres_vec.zero(); vel_vec.zero(); acc_vec.zero(); } <B><FONT COLOR="#228B22">void</FONT></B> fill_dirichlet_bc(EquationSystems& es, <B><FONT COLOR="#228B22">const</FONT></B> std::string& system_name) { assert (system_name == <B><FONT COLOR="#BC8F8F">"Wave"</FONT></B>); NewmarkSystem & t_system = es.get_system<NewmarkSystem> (system_name); SparseMatrix<Number>& matrix = *t_system.matrix; NumericVector<Number>& rhs = *t_system.rhs; <B><FONT COLOR="#228B22">const</FONT></B> Mesh& mesh = es.get_mesh(); <B><FONT COLOR="#228B22">const</FONT></B> Real pi = libMesh::pi; <B><FONT COLOR="#228B22">const</FONT></B> <B><FONT COLOR="#228B22">bool</FONT></B> do_for_matrix = es.parameters.get<<B><FONT COLOR="#228B22">bool</FONT></B>>(<B><FONT COLOR="#BC8F8F">"Newmark set BC for Matrix"</FONT></B>); <B><FONT COLOR="#228B22">const</FONT></B> Real current_time = es.parameters.get<Real>(<B><FONT COLOR="#BC8F8F">"time"</FONT></B>); <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_nodes = mesh.n_nodes(); <B><FONT COLOR="#A020F0">for</FONT></B> (<B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> n_cnt=0; n_cnt<n_nodes; n_cnt++) { <B><FONT COLOR="#228B22">const</FONT></B> Node& curr_node = mesh.node(n_cnt); <B><FONT COLOR="#228B22">const</FONT></B> Real z_coo = 4.; <B><FONT COLOR="#A020F0">if</FONT></B> (fabs(curr_node(2)-z_coo) < TOLERANCE) { <B><FONT COLOR="#228B22">unsigned</FONT></B> <B><FONT COLOR="#228B22">int</FONT></B> dn = curr_node.dof_number(0,0,0); <B><FONT COLOR="#228B22">const</FONT></B> Real penalty = 1.e10; Real p_value; <B><FONT COLOR="#A020F0">if</FONT></B> (current_time < .002 ) p_value = sin(2*pi*current_time/.002); <B><FONT COLOR="#A020F0">else</FONT></B> p_value = .0; rhs.add(dn, p_value*penalty); <B><FONT COLOR="#A020F0">if</FONT></B> (do_for_matrix) matrix.add(dn, dn, penalty); } } <I><FONT COLOR="#B22222">// loop n_cnt</FONT></I> }</pre> <a name="output"></a> <br><br><br> <h1> The console output of the program: </h1> <pre>**************************************************************** Running Example ./ex8-devel pipe-mesh.unv*************************************************************** Running ./ex8-devel pipe-mesh.unvMesh file is: pipe-mesh.unv Mesh Information: mesh_dimension()=3 spatial_dimension()=3 n_nodes()=3977 n_elem()=3520 n_local_elem()=3520 n_active_elem()=3520 n_subdomains()=1 n_processors()=1 processor_id()=0 EquationSystems n_systems()=1 System "Wave" Type "Newmark" Variables="p" Finite Element Types="LAGRANGE" Approximation Orders="FIRST" n_dofs()=3977 n_local_dofs()=3977 n_constrained_dofs()=0 n_vectors()=9 **************************************************************** Done Running Example ./ex8-devel pipe-mesh.unv***************************************************************</pre></div><?php make_footer() ?></body></html><?php if (0) { ?>\#Local Variables:\#mode: html\#End:<?php } ?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -