Skip to content

MethodError: no method matching Reactant.MLIR.IR.Type for a custom struct #2733

@varchasgopalaswamy

Description

@varchasgopalaswamy

Hi,

I think this is probably an error on my end since I am a Julia novice.

struct GridBlock
    r_faces
    nr
    ntheta
    theta_faces
    theta_max
    theta_min
end
function GridBlock(r_faces, ntheta, theta_max, theta_min)
    nr = length(r_faces) - 1
    if nr < 4
        throw(ArgumentError("GridBlock must have at least 4 radial zones."))
    end
    if length(r_faces) != nr + 1
        throw(ArgumentError("Length of r_faces must be nr + 1."))
    end
    if ntheta < 1
        throw(ArgumentError("GridBlock must have at least 1 theta zone."))
    end
    theta_faces = similar(r_faces, (ntheta + 1,))
    allowscalar() do
        @trace for j in 1:(ntheta+1)
            theta_faces[j] = theta_min + (j - 1) * (theta_max - theta_min) / ntheta
        end
    end
    GridBlock(r_faces, nr, ntheta, theta_faces, theta_max, theta_min)
end

struct SMRGrid
    nblocks
    total_nr
    max_ntheta
    max_theta
    min_theta
    blocks::Vector{GridBlock}
end
function SMRGrid(blocks::Vector{GridBlock})
    if isempty(blocks)
        throw(ArgumentError("SMRGrid must contain at least one block."))
    end
    max_ntheta = maximum(block.ntheta for block in blocks)
    total_nr = sum(block.nr for block in blocks)
    nblocks = length(blocks)
    allowscalar() do
        @trace track_numbers = false for i in 1:nblocks-1
            # Check contiguous blocks in radius
            if !isapprox(blocks[i].r_faces[end], blocks[i+1].r_faces[1])
                throw(ArgumentError("Blocks must be contiguous in radius."))
            end

            # Check theta transition constraints
            ntheta1 = blocks[i].ntheta
            ntheta2 = blocks[i+1].ntheta
            if ntheta1 > ntheta2 * 2 || ntheta2 > ntheta1 * 2
                throw(ArgumentError("Theta zones must transition by at most a factor of 2 between adjacent blocks."))
            end
        end
    end
    SMRGrid(nblocks, total_nr, max_ntheta, blocks[1].theta_max, blocks[1].theta_min, blocks)
end

when I test it with

@testset "SMRGrid" begin
    theta_max = Reactant.to_rarray(Float64(pi); track_numbers=true)

    r1 = [0.0, 1.0, 2.0, 3.0, 4.0]
    b1 = Reactant.to_rarray(GridBlock(r1, 1, theta_max, 0.0))

    r2 = [4.0, 5.0, 6.0, 7.0, 8.0]
    b2 = Reactant.to_rarray(GridBlock(r2, 2, theta_max, 0.0))

    r3 = [8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0]
    b3 = Reactant.to_rarray(GridBlock(r3, 4, theta_max, 0.0))

    grid_elements = Reactant.to_rarray([b1, b2, b3])
    grid = @jit SMRGrid(grid_elements)
    @test length(grid.blocks) == 3
    @test grid.total_nr == 14
    @test grid.max_ntheta == 4

end

I get the error

MethodError: no method matching Reactant.MLIR.IR.Type(::Type{GridBlock})
  The type `Reactant.MLIR.IR.Type` exists, but no method is defined for this combination of argument types when trying to construct it.

I'm using Reactant 0.2.240. Is there a way to resolve this?

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