# About \`dolfinx.fem.petsc.assemble\_matrix\` API

**URL:** <https://fenicsproject.discourse.group/t/about-dolfinx-fem-petsc-assemble-matrix-api/17220>\
**Category:** General\
**Tags:** dolfinx\
**Created:** [March 16, 2025, 2:58am UTC](https://fenicsproject.discourse.group/t/about-dolfinx-fem-petsc-assemble-matrix-api/17220 "2025-03-16T02:58:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyunfei1211](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lyunfei1211/32/6600_2.png) [@lyunfei1211](https://fenicsproject.discourse.group/u/lyunfei1211)\
**Post date:** [March 16, 2025, 2:58am UTC](https://fenicsproject.discourse.group/t/about-dolfinx-fem-petsc-assemble-matrix-api/17220/1 "2025-03-16T02:58:26Z")

</div>

There are two APIs aboutthe matrix assembling operation in dolfinx 0.9.0.

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/9/9fc3904c92779bb098ab889ee1fabd32723e0402.png)  
The above one has no parameter `A: Mat`, and the bellow has it. What is the difference between them? And again the intruction for `Parameters` has no `A: Mat`. When I use the following sentences  
 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/optimized/2X/8/8db98a5c4accc05895f6336b08dee4c9c0c5e7f2_2_690x47.png)  
the code editor prompts `Unexpected argument `.  
So how can I use the function with parameter `A: Mat`?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 16, 2025, 3:42am UTC](https://fenicsproject.discourse.group/t/about-dolfinx-fem-petsc-assemble-matrix-api/17220/2 "2025-03-16T03:42:37Z")

</div>

The one with `A` as input expects a PETS Matrix that you will add the contributions of the form `a` to. This is useful if you need too assemble the matrix multiple times, is it avoids reallocation of memory.

See for instance

> <https://github.com/FEniCS/dolfinx/blob/8297a31777a091b236ebfe3d6632b2ed9cafeefe/python/test/unit/fem/test_bcs.py#L92>

[Test problem 2: Flow past a cylinder (DFG 2D-3 benchmark) — FEniCSx tutorial](https://jsdokken.com/dolfinx-tutorial/chapter2/ns_code2.html#solving-the-time-dependent-problem) (Step 1 in temporal loop)  
[The standard way of compiling code with DOLFINx — FEniCS Workshop](http://jsdokken.com/FEniCS-workshop/src/form_compilation/alternate_form.html)

---

<div class="post-metadata">

**Author:** ![lyunfei1211](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/lyunfei1211/32/6600_2.png) [@lyunfei1211](https://fenicsproject.discourse.group/u/lyunfei1211)\
**Post date:** [March 16, 2025, 4:02am UTC](https://fenicsproject.discourse.group/t/about-dolfinx-fem-petsc-assemble-matrix-api/17220/3 "2025-03-16T04:02:57Z")

</div>

Thanks for your comments, Mr. dokken.  
By the way, when using the function

> [@dokken](#):
>
> `assemble_matrix(A, a, bcs=bcs)`

is it required to call `A.assemble()` after the sentence to get the matrix assembled?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [March 16, 2025, 6:30am UTC](https://fenicsproject.discourse.group/t/about-dolfinx-fem-petsc-assemble-matrix-api/17220/4 "2025-03-16T06:30:01Z")

</div>

That is required for both APIs. It takes care of the parallel communication/accumulation of data.
