From 21f80fddd748c60732e6f1ee246d4b7986b79cc2 Mon Sep 17 00:00:00 2001 From: vavrines Date: Wed, 3 Jul 2024 08:31:30 +0800 Subject: [PATCH] Fix pythoncall issue --- example/cavity_sim.jl | 12 ++++++------ src/KitAMR.jl | 12 ++++++++---- src/postprocess.jl | 8 ++++---- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/example/cavity_sim.jl b/example/cavity_sim.jl index 5b46dc0..7d48de0 100644 --- a/example/cavity_sim.jl +++ b/example/cavity_sim.jl @@ -1,7 +1,7 @@ """ -To execute the code: -- from the main directory: mpiexecjl -n N --project=. julia example/cavity_sim.jl -- from the file directory: mpiexecjl -n N --project=.. julia cavity_sim.jl +To execute the code (assuming 16 cores are going to be used): +- from the main directory: mpiexecjl -n 16 --project=. julia example/cavity_sim.jl +- from the file directory: mpiexecjl -n 16 --project=.. julia cavity_sim.jl """ using KitAMR, MPI, CairoMakie @@ -58,7 +58,7 @@ end KitAMR.save_VS_final(DVM_data) meshes = KitAMR.collect_mesh(DVM_data) solutions = KitAMR.collect_solution(ps4est, DVM_data) -# write_result!(ps4est,DVM_data,"write_test") + if MPI.Comm_rank(MPI.COMM_WORLD) == 0 x, y = KitAMR.reshape_mesh(meshes) f = Figure() @@ -66,13 +66,13 @@ if MPI.Comm_rank(MPI.COMM_WORLD) == 0 KitAMR.mesh_plot(x, y, ax) save("mesh.png", f) - #=x, y, variable = KitAMR.reshape_solutions(solutions, DVM_data.global_data, :prim, 4) + x, y, variable = KitAMR.reshape_solutions(solutions, DVM_data.global_data, :prim, 4) variable = 1 ./ variable f = Figure() ax = Axis(f[1, 1]) co = contourf!(x, y, variable) Colorbar(f[1, 2], co) - save("test_vs_adaptive.png", f)=# + save("test_vs_adaptive.png", f) end KitAMR.finalize!(ps4est, DVM_data) diff --git a/src/KitAMR.jl b/src/KitAMR.jl index ac3ba55..2c24789 100644 --- a/src/KitAMR.jl +++ b/src/KitAMR.jl @@ -8,14 +8,11 @@ using LinearAlgebra using Parameters using SpecialFunctions using StaticArrays +using PythonCall using Reexport @reexport using P4est -using PythonCall -scipy = pyimport("scipy") -np = pyimport("numpy") - include("../lib/P4estTypes/src/P4estTypes.jl") using .P4estTypes @@ -39,4 +36,11 @@ include("partition.jl") include("postprocess.jl") include("finalize.jl") +const np = Ref{Py}() +const scipy = Ref{Py}() +function __init__() + np[] = pyimport("numpy") + scipy[] = pyimport("scipy") +end + end # module diff --git a/src/postprocess.jl b/src/postprocess.jl index 7078d17..23a8a72 100644 --- a/src/postprocess.jl +++ b/src/postprocess.jl @@ -74,12 +74,12 @@ function reshape_solutions( midpoints[i, :] .= solutions[i].midpoint z[i] = getfield(solutions[i], field)[index] end - itp = scipy.interpolate.CloughTocher2DInterpolator(midpoints, z) + itp = scipy[].interpolate.CloughTocher2DInterpolator(midpoints, z) dx = (xmax - xmin) / Nx / 2^DVM_PS_MAXLEVEL dy = (ymax - ymin) / Ny / 2^DVM_PS_MAXLEVEL X = collect(xmin+EPS+dx/2:dx:xmax-EPS-dx/2) Y = collect(ymin+EPS+dy/2:dy:ymax-EPS-dy/2) - pyZ = np.zeros((length(X), length(Y))) + pyZ = np[].zeros((length(X), length(Y))) for i in eachindex(X) for j in eachindex(Y) pyZ[i-1, j-1] = itp(X[i], Y[j])[] @@ -495,12 +495,12 @@ function reshape_solutions_vs(midpoint::AM, df::AM, global_data::Global_Data) Nx, Ny = global_data.vs_trees_num xmin, xmax, ymin, ymax = global_data.quadrature z = @view(df[:, 1]) - itp = scipy.interpolate.CloughTocher2DInterpolator(midpoint, PyArray(z)) + itp = scipy[].interpolate.CloughTocher2DInterpolator(midpoint, PyArray(z)) dx = (xmax - xmin) / Nx / 2^DVM_VS_MAXLEVEL dy = (ymax - ymin) / Ny / 2^DVM_VS_MAXLEVEL X = collect(xmin+EPS+dx/2:dx:xmax-EPS-dx/2) Y = collect(ymin+EPS+dy/2:dy:ymax-EPS-dy/2) - pyZ = np.zeros((length(X), length(Y))) + pyZ = np[].zeros((length(X), length(Y))) for i in eachindex(X) for j in eachindex(Y) pyZ[i-1, j-1] = itp(X[i], Y[j])[]