I used to have a decent setup that worked as follows: 1. Build node->element table (For a given node, which elements contain this node. You may already have this) OR build a node connectivity table (For a given node, which nodes are connected). 2. Build element->node table (For a given element, which nodes are contained in this element. You probably already have this) 4. Loop over nodes and get the global DOFs contained in that node. For those DOF id rows, add the number of nodal DOFs for each unique node connected to the current node using the node->element and element->node table OR the node connectivity table. 5. Loop over elements and add the number of elemental DOFs to each nodal DOF global id row contained in this element using element->node table. Also add the number of elemental DOF's and the sum of the nodal DOF's to the elemental global DOF id rows. 6. Add contributions of multi-point constraints, i.e. Lagrange multiplier DOF's.
Note that in parallel you may have to scatter values to global DOF ids owned by off-processors to get an accurate Onz. This setup as a whole can be pretty fast but can scale poorly if you don't have a good way of developing the node-element table or node connectivity since it requires some loops within loops. Another way is to use the PETSc preallocation macros such as MatPreallocateSet. You can essentially do a "dry run" of a Jacobian Matrix assembly into the preallocation macros. They can be tricky to use, so if you have problems you can simply look at the PETSc documentation for those macros and hand code them yourself. This strategy will overestimate the memory, but a matrix inversion will dwarf how much this will waste. I vaguely remember a PETSc archive asking how to free the unneeded memory if this is absolutely necessary, but I don't think anything really worked without a full matrix copy. If someone by chance knows how the Trilinos "OptimizeStorage" routine works for Epetra matricies they could potentially shed some light on how to do this - if it is even possible. Dave Makhija On Tue, Sep 7, 2010 at 2:24 PM, stali <stali at purdue.edu> wrote: > Petsc-users > > How can I efficiently calculate the _exact_ number of non-zeros that would > be in the global sparse (stiffness) matrix given an unstructured mesh? > > Thanks >