# Form Compilation On Subdomains

**URL:** <https://fenicsproject.discourse.group/t/form-compilation-on-subdomains/4516>\
**Category:** variational formulation\
**Created:** [November 10, 2020, 11:27am UTC](https://fenicsproject.discourse.group/t/form-compilation-on-subdomains/4516 "2020-11-10T11:27:11Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![chelmes](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chelmes/32/641_2.png) [@chelmes](https://fenicsproject.discourse.group/u/chelmes)\
**Post date:** [November 10, 2020, 11:27am UTC](https://fenicsproject.discourse.group/t/form-compilation-on-subdomains/4516/1 "2020-11-10T11:27:11Z")

</div>

Hi,

I am not entirely sure whether this is the correct category. In my Fenics Code I use avoided assembly to preassemble fixed parts of my problem like so:

```
def assemble_stiffness_matrices(trial1,trial2,test1,test2,measure,subdomain=None):
    """ Assembly of stiffness matrices

    Parameters
    ----------
    trial[1,2]: UFL TrialFunctions, the Legendremoments of the flux
    test[1,2]: UFL TestFunctions, the test functions for the weak formulation
                from the same functionspace as trial[1,2]
    measure: UFL Measure, spatial measure to use in assembly of weak form
    subdomain: int, identifier for subdomain, needs to be plain python int! If
                not set, the whole domain is taken into account

    Returns
    -------
    K00, K02, K22: Assembled matrices of determined la backend, default is PETSc
    """
    if subdomain is not None:
        a_K00 = inner(grad(trial1),grad(test1)) * measure(subdomain)
        a_K20 = inner(grad(trial2),grad(test1)) * measure(subdomain)
        a_K22 = inner(grad(trial2),grad(test2)) * measure(subdomain)
    else:
        a_K00 = inner(grad(trial1),grad(test1)) * measure
        a_K20 = inner(grad(trial2),grad(test1)) * measure
        a_K22 = inner(grad(trial2),grad(test2)) * measure
    return assemble(a_K00), assemble(a_K20), assemble(a_K22)

```

Because I am interested in the solution spatially integrated over each `subdomain` I define the measure to be specific for each subdomain. When I look at the code generated by dijitso it only differs in ids for the different classes. So is there a way to use the function without the `subdomain` specification and still get results integrated over each `subdomain`, respectively, after the code is finished?

Besides the case with `subdomain != None` uses humongous amounts of RAM on FFC-compilation.  
Thanks in advance for your help.
