# Applying Dirichlet boundary conditions to assembled forcing terms

**URL:** https://discourse.julialang.org/t/applying-dirichlet-boundary-conditions-to-assembled-forcing-terms/127092
**Category:** General Usage
**Tags:** gridap
**Created:** [March 18, 2025, 11:30am UTC](https://discourse.julialang.org/t/applying-dirichlet-boundary-conditions-to-assembled-forcing-terms/127092 "2025-03-18T11:30:07Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Simons](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Simons](https://discourse.julialang.org/u/Simons)
#### Post date: [March 18, 2025, 11:30am UTC](https://discourse.julialang.org/t/applying-dirichlet-boundary-conditions-to-assembled-forcing-terms/127092/1 "2025-03-18T11:30:07Z")

</div>

Hi,

I’m interested in solving a sequence of finite element problems a(u\_n,v) = b\_n(v) (A x\_n = b\_n in assembled form), all of which have the same boundary conditions. Since A is constant, I want to avoid re-assembling A at each iteration (which I believe happens e.g. when using AffineFEOperator as in the tutorials), and only assemble b\_n while imposing the boundary conditions.  
How can I achieve that? Tangentially, what are the outputs of assemble\_vector and assemble\_matrix? Do they include the specific boundary values?

Thanks in advance!

---

<div class="post-metadata">

### Author: ![Simons](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@Simons](https://discourse.julialang.org/u/Simons)
#### Post date: [March 18, 2025, 12:56pm UTC](https://discourse.julialang.org/t/applying-dirichlet-boundary-conditions-to-assembled-forcing-terms/127092/2 "2025-03-18T12:56:32Z")

</div>

Found a suitable approach.

MWE

```julia
using Gridap

n = 32
domain = (0, 1)
partition = (n,)
model = CartesianDiscreteModel(domain, partition)

order = 1
reffe = ReferenceFE(lagrangian, Float64, order)

V = TestFESpace(model, reffe; conformity=:H1, dirichlet_tags="boundary")
g(x) = 1.0
U = TrialFESpace(V, g)

degree = order+1
Ω = Triangulation(model)
dΩ = Measure(Ω, degree)

# Initial condition
function initial_condition(x)
    return 1 .- exp(-100 * ((x[1] - 0.5)^2))
end
u0 = interpolate(initial_condition, U)

bform(u, v) = ∫(∇(u) ⋅ ∇(v)) * dΩ
lform(v) = ∫(u0 * v) * dΩ

op = AffineFEOperator(bform, lform, U, V)
f1 = assemble_vector(lform, V)

ug = FEFunction(U, zeros(31), ones(2)) # define function with g_∂D = 1. and zeros elsewhere.      
bform_ug(v) = ∫(∇(ug) ⋅ ∇(v))dΩ # 
bcorr = assemble_vector(bform_ug, V)

A1, b1 = assemble_matrix(bform, U, V), assemble_vector(lform, V)
A2, b2 = op.op.matrix, op.op.vector

@assert norm(b2 - f1 + bcorr) < length(ug.dirichlet_values) * 1e-14
@assert A1 == A2

```
