computing the convergence rate in fenics
I am new to fenics and I really need help to debug my code. Basically I am trying to solve a time dependent partial deferential equation and I would like to compute the convergence rate of the error norms that I have. however I keep get this error which I don't understand what does it mean or even how to get over it!
rates[degree][error_type] =
KeyError: 1
and this is my full code
T = 2.0 # final time
beta = 1.2 # parameter beta
tol = 3E-16
bound_a=0.0
bound_b=1.0
r=1.0
m=0.1
num_levels=3
max_degree=3
h = {} # discretization parameter: h[degree][level]
E = {} # error measure(s): E[degree][level][error_type]
# Iterate over degrees and mesh refinement levels
degrees = range(1, max_degree + 1)
for degree in degrees:
h[degree] =
E[degree] =
for i in range(num_levels):
num_steps = pow(4,i) # number of time steps
dt = T / num_steps # time step size
# Create mesh and define function space
mesh_division= int(sqrt(num_steps))
mesh = IntervalMesh(mesh_division,bound_a,bound_b)
h[degree].append(1.0 / mesh_division)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
u_D = Expression('(b-a)/2 + beta*t +(x[0]-a)*(x[0]-b)', degree=1, beta=beta,a=bound_a,b=bound_b, t=0.0)
def boundary(x, on_boundary):
return on_boundary and (abs((x[0]-bound_a)*(x[0]-bound_b))<tol)
bc = DirichletBC(V, u_D, boundary)
# Define initial value
u_n = interpolate(u_D, V)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0.0)
F = u*v*dx + dt*inner(grad(u), grad(v))*dx + dt*inner(grad(u_n), grad(u))*dx -dt*r*(m-u_n)*v*dx- (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
# Time-stepping
u = Function(V)
time = 0.0
for n in range(num_steps):
# Update current time
time += dt
u_D.t = time
# Compute solution
solve(a == L, u, bc)
u_e= interpolate(u_D, V)
def compute_errors(u_e, u):
# Infinity norm based on nodal values
E1 = abs(u_e.vector().get_local() - u.vector().get_local()).max()
# L2 norm
E2 = errornorm(u_e, u, norm_type='L2')
# H1 seminorm
E3 = errornorm(u_e, u, norm_type='H10')
# Collect error measures in a dictionary with self-explanatory keys
errors = {'infinity norm': E1,
'L2 norm': E2,
'H10 seminorm': E3}
return errors
errors = compute_errors(u_e, u)
E[degree].append(errors)
#Compute convergence rates
etypes = list(E[1][0].keys())
rates = {}
for error_type in sorted(etypes):
rates[degree][error_type] =
for i in range(1, num_levels):
Ei = E[degree][i][error_type]
r = ln(Ei / Eim1) / ln(h[degree][i] / h[degree][i - 1])
rates[degree][error_type].append(round(r, 2))
# Update previous solution
u_n.assign(u)
any help will be appreciated. Many thanks
python fenics
add a comment |
I am new to fenics and I really need help to debug my code. Basically I am trying to solve a time dependent partial deferential equation and I would like to compute the convergence rate of the error norms that I have. however I keep get this error which I don't understand what does it mean or even how to get over it!
rates[degree][error_type] =
KeyError: 1
and this is my full code
T = 2.0 # final time
beta = 1.2 # parameter beta
tol = 3E-16
bound_a=0.0
bound_b=1.0
r=1.0
m=0.1
num_levels=3
max_degree=3
h = {} # discretization parameter: h[degree][level]
E = {} # error measure(s): E[degree][level][error_type]
# Iterate over degrees and mesh refinement levels
degrees = range(1, max_degree + 1)
for degree in degrees:
h[degree] =
E[degree] =
for i in range(num_levels):
num_steps = pow(4,i) # number of time steps
dt = T / num_steps # time step size
# Create mesh and define function space
mesh_division= int(sqrt(num_steps))
mesh = IntervalMesh(mesh_division,bound_a,bound_b)
h[degree].append(1.0 / mesh_division)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
u_D = Expression('(b-a)/2 + beta*t +(x[0]-a)*(x[0]-b)', degree=1, beta=beta,a=bound_a,b=bound_b, t=0.0)
def boundary(x, on_boundary):
return on_boundary and (abs((x[0]-bound_a)*(x[0]-bound_b))<tol)
bc = DirichletBC(V, u_D, boundary)
# Define initial value
u_n = interpolate(u_D, V)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0.0)
F = u*v*dx + dt*inner(grad(u), grad(v))*dx + dt*inner(grad(u_n), grad(u))*dx -dt*r*(m-u_n)*v*dx- (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
# Time-stepping
u = Function(V)
time = 0.0
for n in range(num_steps):
# Update current time
time += dt
u_D.t = time
# Compute solution
solve(a == L, u, bc)
u_e= interpolate(u_D, V)
def compute_errors(u_e, u):
# Infinity norm based on nodal values
E1 = abs(u_e.vector().get_local() - u.vector().get_local()).max()
# L2 norm
E2 = errornorm(u_e, u, norm_type='L2')
# H1 seminorm
E3 = errornorm(u_e, u, norm_type='H10')
# Collect error measures in a dictionary with self-explanatory keys
errors = {'infinity norm': E1,
'L2 norm': E2,
'H10 seminorm': E3}
return errors
errors = compute_errors(u_e, u)
E[degree].append(errors)
#Compute convergence rates
etypes = list(E[1][0].keys())
rates = {}
for error_type in sorted(etypes):
rates[degree][error_type] =
for i in range(1, num_levels):
Ei = E[degree][i][error_type]
r = ln(Ei / Eim1) / ln(h[degree][i] / h[degree][i - 1])
rates[degree][error_type].append(round(r, 2))
# Update previous solution
u_n.assign(u)
any help will be appreciated. Many thanks
python fenics
add a comment |
I am new to fenics and I really need help to debug my code. Basically I am trying to solve a time dependent partial deferential equation and I would like to compute the convergence rate of the error norms that I have. however I keep get this error which I don't understand what does it mean or even how to get over it!
rates[degree][error_type] =
KeyError: 1
and this is my full code
T = 2.0 # final time
beta = 1.2 # parameter beta
tol = 3E-16
bound_a=0.0
bound_b=1.0
r=1.0
m=0.1
num_levels=3
max_degree=3
h = {} # discretization parameter: h[degree][level]
E = {} # error measure(s): E[degree][level][error_type]
# Iterate over degrees and mesh refinement levels
degrees = range(1, max_degree + 1)
for degree in degrees:
h[degree] =
E[degree] =
for i in range(num_levels):
num_steps = pow(4,i) # number of time steps
dt = T / num_steps # time step size
# Create mesh and define function space
mesh_division= int(sqrt(num_steps))
mesh = IntervalMesh(mesh_division,bound_a,bound_b)
h[degree].append(1.0 / mesh_division)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
u_D = Expression('(b-a)/2 + beta*t +(x[0]-a)*(x[0]-b)', degree=1, beta=beta,a=bound_a,b=bound_b, t=0.0)
def boundary(x, on_boundary):
return on_boundary and (abs((x[0]-bound_a)*(x[0]-bound_b))<tol)
bc = DirichletBC(V, u_D, boundary)
# Define initial value
u_n = interpolate(u_D, V)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0.0)
F = u*v*dx + dt*inner(grad(u), grad(v))*dx + dt*inner(grad(u_n), grad(u))*dx -dt*r*(m-u_n)*v*dx- (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
# Time-stepping
u = Function(V)
time = 0.0
for n in range(num_steps):
# Update current time
time += dt
u_D.t = time
# Compute solution
solve(a == L, u, bc)
u_e= interpolate(u_D, V)
def compute_errors(u_e, u):
# Infinity norm based on nodal values
E1 = abs(u_e.vector().get_local() - u.vector().get_local()).max()
# L2 norm
E2 = errornorm(u_e, u, norm_type='L2')
# H1 seminorm
E3 = errornorm(u_e, u, norm_type='H10')
# Collect error measures in a dictionary with self-explanatory keys
errors = {'infinity norm': E1,
'L2 norm': E2,
'H10 seminorm': E3}
return errors
errors = compute_errors(u_e, u)
E[degree].append(errors)
#Compute convergence rates
etypes = list(E[1][0].keys())
rates = {}
for error_type in sorted(etypes):
rates[degree][error_type] =
for i in range(1, num_levels):
Ei = E[degree][i][error_type]
r = ln(Ei / Eim1) / ln(h[degree][i] / h[degree][i - 1])
rates[degree][error_type].append(round(r, 2))
# Update previous solution
u_n.assign(u)
any help will be appreciated. Many thanks
python fenics
I am new to fenics and I really need help to debug my code. Basically I am trying to solve a time dependent partial deferential equation and I would like to compute the convergence rate of the error norms that I have. however I keep get this error which I don't understand what does it mean or even how to get over it!
rates[degree][error_type] =
KeyError: 1
and this is my full code
T = 2.0 # final time
beta = 1.2 # parameter beta
tol = 3E-16
bound_a=0.0
bound_b=1.0
r=1.0
m=0.1
num_levels=3
max_degree=3
h = {} # discretization parameter: h[degree][level]
E = {} # error measure(s): E[degree][level][error_type]
# Iterate over degrees and mesh refinement levels
degrees = range(1, max_degree + 1)
for degree in degrees:
h[degree] =
E[degree] =
for i in range(num_levels):
num_steps = pow(4,i) # number of time steps
dt = T / num_steps # time step size
# Create mesh and define function space
mesh_division= int(sqrt(num_steps))
mesh = IntervalMesh(mesh_division,bound_a,bound_b)
h[degree].append(1.0 / mesh_division)
V = FunctionSpace(mesh, 'P', 1)
# Define boundary condition
u_D = Expression('(b-a)/2 + beta*t +(x[0]-a)*(x[0]-b)', degree=1, beta=beta,a=bound_a,b=bound_b, t=0.0)
def boundary(x, on_boundary):
return on_boundary and (abs((x[0]-bound_a)*(x[0]-bound_b))<tol)
bc = DirichletBC(V, u_D, boundary)
# Define initial value
u_n = interpolate(u_D, V)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0.0)
F = u*v*dx + dt*inner(grad(u), grad(v))*dx + dt*inner(grad(u_n), grad(u))*dx -dt*r*(m-u_n)*v*dx- (u_n + dt*f)*v*dx
a, L = lhs(F), rhs(F)
# Time-stepping
u = Function(V)
time = 0.0
for n in range(num_steps):
# Update current time
time += dt
u_D.t = time
# Compute solution
solve(a == L, u, bc)
u_e= interpolate(u_D, V)
def compute_errors(u_e, u):
# Infinity norm based on nodal values
E1 = abs(u_e.vector().get_local() - u.vector().get_local()).max()
# L2 norm
E2 = errornorm(u_e, u, norm_type='L2')
# H1 seminorm
E3 = errornorm(u_e, u, norm_type='H10')
# Collect error measures in a dictionary with self-explanatory keys
errors = {'infinity norm': E1,
'L2 norm': E2,
'H10 seminorm': E3}
return errors
errors = compute_errors(u_e, u)
E[degree].append(errors)
#Compute convergence rates
etypes = list(E[1][0].keys())
rates = {}
for error_type in sorted(etypes):
rates[degree][error_type] =
for i in range(1, num_levels):
Ei = E[degree][i][error_type]
r = ln(Ei / Eim1) / ln(h[degree][i] / h[degree][i - 1])
rates[degree][error_type].append(round(r, 2))
# Update previous solution
u_n.assign(u)
any help will be appreciated. Many thanks
python fenics
python fenics
asked Nov 13 '18 at 9:00
robyroby
2317
2317
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277238%2fcomputing-the-convergence-rate-in-fenics%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277238%2fcomputing-the-convergence-rate-in-fenics%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown