@@ -145,13 +145,13 @@ def coarsen_equation_bc(ebc, self, coefficient_mapping=None):
145145
146146@coarsen .register (firedrake .functionspaceimpl .WithGeometryBase )
147147def coarsen_function_space (V , self , coefficient_mapping = None ):
148- if hasattr (V , "_coarse" ) and self == coarsen :
149- return V ._coarse
150- elif hasattr (V , "_fine" ) and self == refine :
151- return V ._fine
152148 # Handle MixedFunctionSpace : V.reconstruct requires MeshSequence.
153149 mesh = V .mesh () if V .index is None else V .parent .mesh ()
154150 new_mesh = self (mesh , self )
151+ if self == coarsen and hasattr (V , "_coarse" ) and V ._coarse .mesh () == new_mesh :
152+ return V ._coarse
153+ elif self == refine and hasattr (V , "_fine" ) and V ._fine .mesh () == new_mesh :
154+ return V ._fine
155155 name = V .name
156156 if name is not None :
157157 level_increment = - 1 if self == coarsen else 1
@@ -201,8 +201,14 @@ def coarsen_function(expr, self, coefficient_mapping=None):
201201
202202@coarsen .register (firedrake .NonlinearVariationalProblem )
203203def coarsen_nlvp (problem , self , coefficient_mapping = None ):
204- if hasattr (problem , "_coarse" ) and self == coarsen :
205- return problem ._coarse
204+ # Have we done this already?
205+ mh , _ = utils .get_level (problem .u .function_space ().mesh ())
206+ if self == coarsen and hasattr (problem , "_coarse" ):
207+ if mh is utils .get_level (problem ._coarse .u .function_space ().mesh ())[0 ]:
208+ return problem ._coarse
209+ elif self == refine and hasattr (problem , "_fine" ):
210+ if mh is utils .get_level (problem ._fine .u .function_space ().mesh ())[0 ]:
211+ return problem ._fine
206212
207213 def inject_on_restrict (fine , restriction , rscale , injection , coarse ):
208214 manager = get_transfer_manager (fine )
@@ -243,10 +249,13 @@ def inject_on_restrict(fine, restriction, rscale, injection, coarse):
243249 Jp = self (problem .Jp , self , coefficient_mapping = coefficient_mapping )
244250 u = coefficient_mapping [problem .u_restrict ]
245251
246- fine = problem
252+ orig = problem
247253 problem = firedrake .NonlinearVariationalProblem (F , u , bcs = bcs , J = J , Jp = Jp , is_linear = problem .is_linear ,
248254 form_compiler_parameters = problem .form_compiler_parameters )
249- fine ._coarse = problem
255+ if self == coarsen :
256+ orig ._coarse = problem
257+ elif self == refine :
258+ orig ._fine = problem
250259 return problem
251260
252261
@@ -282,9 +291,8 @@ def coarsen_snescontext(context, self, coefficient_mapping=None):
282291 coefficient_mapping = {}
283292
284293 # Have we already done this?
285- coarse = context ._coarse
286- if coarse is not None :
287- return coarse
294+ if self == coarsen and context ._coarse is not None :
295+ return context ._coarse
288296
289297 problem = self (context ._problem , self , coefficient_mapping = coefficient_mapping )
290298 appctx = context .appctx
0 commit comments