Skip to content

AMR error after Stokes solve. #48

Closed

Description

@bknight1

Unable to adapt mesh with variable (e.g. strain rate) after stokes solve. Most likely due to variable not being the velocity or pressure field. I'm currently on the latest development branch with PETSc v2.24.4 that is built with pragmatic, mmg & parmmg. Here is the error message:

[0]PETSC ERROR: --------------------- Error Message --------------------------------------------------------------
[0]PETSC ERROR: Argument out of range
[0]PETSC ERROR: Invalid field number 2; not in [0, 2)
[0]PETSC ERROR: WARNING! There are unused option(s) set! Could be the program crashed before usage or a spelling mistake, etc!

Activity

  1. lmoresi commented on Feb 24, 2026

    @lmoresi
    Member

    Can you add more information so we can reproduce this - we're continually debugging this part of the code which is difficult to get right and changes quite a bit with petsc updates.

  2. bknight1 commented on Mar 2, 2026

    @bknight1
    MemberAuthor

    Here's a basic example using the Stokes sinker, adapting based on the SR field:

    
    # %%
    import sympy
    import underworld3 as uw
    from underworld3 import meshing, discretisation, systems, constitutive_models, adaptivity
    
    # %%
    mesh = meshing.UnstructuredSimplexBox(
        cellSize=0.02,
        minCoords=(0.0, 0.0),
        maxCoords=(1.0, 1.0),
        qdegree=2,
    )
    
    # %%
    v = discretisation.MeshVariable("U", mesh, mesh.dim, vtype=uw.VarType.VECTOR, degree=2)
    p = discretisation.MeshVariable("P", mesh, 1, vtype=uw.VarType.SCALAR, degree=1)
    sr = discretisation.MeshVariable("SR", mesh, 1, vtype=uw.VarType.SCALAR, degree=1)
    
    uw.pprint(0, "Setup basic Stokes sinker model")
    # %%
    x, y = mesh.X
    x0, y0, r = 0.5, 0.7, 0.15
    rho_light, rho_heavy = 0, 1
    g = 1
    
    # %%
    rho = sympy.Piecewise(
        (rho_heavy, (x - x0) ** 2 + (y - y0) ** 2 < r**2),
        (rho_light, True),
    )
    
    # %%
    stokes = systems.Stokes(mesh, velocityField=v, pressureField=p)
    stokes.constitutive_model = constitutive_models.ViscousFlowModel
    stokes.bodyforce = sympy.Matrix([0, -rho * g])
    
    # %%
    for wall in ("Left", "Right", "Bottom", "Top"):
        stokes.add_dirichlet_bc((0.0, 0.0), wall)
    
    # %%
    uw.pprint(0, "Solve basic Stokes sinker model")
    
    # %%
    stokes.petsc_options["ksp_monitor"] = None
    stokes.solve()
    
    # %%
    uw.pprint(0, "Save output")
    
    # %%
    sr.data[:, 0] = uw.function.evaluate(stokes.Unknowns.Einv2, sr.coords).reshape(-1)
    
    # %%
    mesh.petsc_save_checkpoint(0, [sr, v, p] )
    
    # %%
    uw.pprint(0, "Begin mesh adaptation")
    
    
    # %%
    metric = adaptivity.metric_from_field(sr, h_min=0.01, h_max=0.1)
    mesh.adapt(metric)
    
    
    # %%
    uw.pprint(0, "Finish mesh adaptation")
    
  3. added a commit that references this issue on Mar 3, 2026
    6b8f225
  4. lmoresi commented on Mar 3, 2026

    @lmoresi
    Member

    Fixed in PR #67 (merged to development). The adapt() method now destroys all old variable lvecs upfront before reinitializing, preventing stale field_id errors in _setup_ds(). The metric variable is also preserved through adaptation.

  5. bknight1 commented on Mar 3, 2026

    @bknight1
    MemberAuthor

    Confirmed working. Thanks, Louis

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions