Skip to content

Commit

Permalink
Minor changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldoclemente committed Feb 5, 2024
1 parent 6cf6b54 commit 5e4405c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
15 changes: 9 additions & 6 deletions R/function_space.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,28 +126,31 @@ setMethod("FunctionSpace", signature = c(mesh="Mesh", fe_order="missing"),
private$coeff_ <- coefficients
},
eval = function(X) {
M = dim(extract_private(private$FunctionSpace_)$mesh_$nodes())[2]
M = dim(private$FunctionSpace_$mesh()$nodes())[2]
if(is.vector(X)) X <- t(as.matrix(X))

if(dim(X)[2] != M) {
stop(paste("matrix of evaluation points should be a 2 columns matrix"))
}
evals <- NULL
if(length(extract_private(private$FunctionSpace_)$mesh_$time_nodes()) == 0){
if(length(private$FunctionSpace_$mesh()$time_nodes()) == 0){
evals <- apply(private$FunctionSpace_$basis()$eval(as.matrix(X)) %*% private$coeff_,
MARGIN=1, FUN=sum)
}else{
evals <- matrix(nrow=nrow(X),ncol=length(extract_private(private$FunctionSpace_)$mesh_$time_nodes()))
for(t in 1:length(extract_private(private$FunctionSpace_)$mesh_$time_nodes())){
evals <- matrix(nrow=nrow(X),ncol=length(private$FunctionSpace_$mesh()$time_nodes()))
for(t in 1:length(private$FunctionSpace_$mesh()$time_nodes())){
evals[,t] <- apply(private$FunctionSpace_$basis()$eval(as.matrix(X)) %*% private$coeff_[,t],
MARGIN=1, FUN=sum)
}
}
return(evals)
},
set_coefficients = function(coefficients){
if( nrow(coefficients) != private$FunctionSpace_$basis()$size())
stop("Input parameter dimension is different from function space size.")
if( ((length(private$FunctionSpace_$mesh()$times_nodes) == 0) &&
nrow(coefficients) != private$FunctionSpace_$basis()$size()) |
((ncol(coefficients) != length(private$FunctionSpace_$mesh()$times_nodes())) &&
nrow(coefficients) != private$FunctionSpace_$basis()$size()))
stop("Wrong input parameter dimensions.")
private$coeff_ <- coefficients
},
coefficients = function(){
Expand Down
12 changes: 9 additions & 3 deletions tests/parabolic.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ plot(mesh)
# create Functional Space
Vh <- FunctionSpace(mesh)

exact_solution <- function(points,t){
exact_solution <- function(points,times){
res <- matrix(0, nrow=nrow(points), ncol=length(times))
for( t in 1:length(times)){
res[,t] = sin( 2.* pi * points[,1]) * sin(2.*pi* points[,2]) * exp(-times[t])
Expand Down Expand Up @@ -54,7 +54,7 @@ u0 <- function(points){
}

## create pde
pde <- Pde(L, 0.)
pde <- Pde(L, f)
pde$set_boundary_condition(g)
pde$set_initial_condition(u0)

Expand Down Expand Up @@ -86,10 +86,16 @@ max(abs( u$eval(points) - as.matrix(exact_solution(points))))
# ## plot solution
options(warn=-1)
plot(u)

plot(u) %>% layout(scene =list(camera=list(eye=list(z=1))))
plot(u, showscale=FALSE) # no color bar :)

# ------------------------------------------------------------------------------

u_exact <- Function(Vh)
u_exact$set_coefficients(exact_solution(Vh$basis()$dofs_coordinates(), times))
plot(u_exact) %>% layout(scene =list(camera=list(eye=list(z=1))))
plot(u_exact, showscale=FALSE) # no color bar :)

plot(u)

# contour
Expand Down

0 comments on commit 5e4405c

Please sign in to comment.