Morpho-lang / Morpho-lang/morpho-optimize4

ProblemAdapter dispatch depends on import order of `optimize4` and `meshgen`

Open
#2 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1
Forks
0
PR merge metrics
No merged PRs in 30d

Description

Summary

ProblemAdapter(problem, field) fails with a multiple-dispatch error when optimize4 is imported before meshgen. The same code succeeds if the import order is reversed.

Minimal Example
import optimize4
import meshgen

var dom = CircularDomain([0,0], 1)
var mg = MeshGen(dom, [-1..1:0.2, -1..1:0.2])
var mesh = ChangeMeshDimension(mg.build(), 3)

var u = Field(mesh, 1)

fn energyDensity(x, u) {
    return u*u
}

var problem = OptimizationProblem(mesh)
var energy = AreaIntegral(energyDensity, u)
problem.addenergy(energy)

print "About to construct ProblemAdapter(problem, u)..."
var adapter = ProblemAdapter(problem, u)
print "ProblemAdapter constructed successfully: ${adapter}"
Actual behavior

The script fails with:

About to construct ProblemAdapter(problem, u)...
Error 'MltplDsptchFld': Multiple dispatch could not find an implementation that matches these arguments.
  from global at line 34
Expected behavior

ProblemAdapter(problem, u) should construct a valid adapter, independent of whether optimize4 or meshgen is imported first.

Workaround

Changing the import order fixes the issue:

import meshgen
import optimize4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the minimal example with both import orders, focusing on ProblemAdapter(problem, u) and the MltplDsptchFld error. Inspect how optimize4 and meshgen register the relevant multiple-dispatch implementations. Done means ProblemAdapter constructs successfully regardless of which module is imported first.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.