mirror of
https://github.com/Cian-H/nanoconc.git
synced 2026-05-30 19:42:06 +01:00
Organized project properly and added some property testing
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
[deps]
|
||||
AirspeedVelocity = "1c8270ee-6884-45cc-9545-60fa71ec23e4"
|
||||
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
|
||||
PropCheck = "ca382230-33be-11e9-0059-d981d03070e4"
|
||||
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
|
||||
@@ -0,0 +1,83 @@
|
||||
module Benchmarks
|
||||
|
||||
include("../anchors.jl")
|
||||
include("ffi_wraps.jl")
|
||||
|
||||
import .Anchors.ROOT_DIR
|
||||
import .FFIWraps: bhmie_c, bhmie_fortran, bhmie_fortran77
|
||||
using BenchmarkTools
|
||||
|
||||
include("$ROOT_DIR/src/miemfp.jl")
|
||||
|
||||
function bench_vs_ffi()
|
||||
# Fixed testing values
|
||||
nang = UInt32(2) # Example number of angles
|
||||
|
||||
c_result = @benchmark bhmie_c(x, cxref, nang, cxs1, cxs2) setup=(
|
||||
x = rand(Float32);
|
||||
cxref = rand(ComplexF32);
|
||||
nang = UInt32($nang);
|
||||
cxs1 = rand(ComplexF32, $nang);
|
||||
cxs2 = rand(ComplexF32, $nang);
|
||||
)
|
||||
|
||||
f_result = @benchmark bhmie_fortran(x, cxref, nang, cxs1, cxs2) setup=(
|
||||
x = rand(Float32);
|
||||
cxref = rand(ComplexF32);
|
||||
nang = Int32($nang);
|
||||
cxs1 = rand(ComplexF32, $nang);
|
||||
cxs2 = rand(ComplexF32, $nang);
|
||||
)
|
||||
|
||||
f77_result = @benchmark bhmie_fortran77(x, cxref, nang, cxs1, cxs2) setup=(
|
||||
x = rand(Float32);
|
||||
cxref = rand(ComplexF32);
|
||||
nang = Int32($nang);
|
||||
cxs1 = rand(ComplexF32, $nang);
|
||||
cxs2 = rand(ComplexF32, $nang);
|
||||
)
|
||||
|
||||
j_result = @benchmark miemfp.bhmie(Float64(x), ComplexF64(cxref), nang) setup=(
|
||||
x = rand(Float32);
|
||||
cxref = rand(ComplexF32);
|
||||
nang = UInt32($nang);
|
||||
)
|
||||
|
||||
println("\nC Implementation")
|
||||
display(c_result)
|
||||
println("\nFortran Implementation")
|
||||
display(f_result)
|
||||
println("\nFortran 77 Implementation")
|
||||
display(f77_result)
|
||||
println("\nJulia Implementation")
|
||||
display(j_result)
|
||||
|
||||
return c_result, f_result, f77_result, j_result
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if abspath(PROGRAM_FILE) == @__FILE__
|
||||
result = Benchmarks.bench_vs_ffi()
|
||||
|
||||
include("../anchors.jl")
|
||||
import .Anchors.ROOT_DIR
|
||||
using Pkg
|
||||
|
||||
current_package_version = Pkg.TOML.parsefile("$ROOT_DIR/Project.toml")["version"]
|
||||
|
||||
function display_to_file(io, x)
|
||||
show(IOContext(io, :limit => false), "text/plain", x)
|
||||
end
|
||||
|
||||
open("$ROOT_DIR/benchmarks/$current_package_version.txt", "w") do io
|
||||
println(io, "C Implementation")
|
||||
display_to_file(io, result[1])
|
||||
println(io, "\n\nFortran Implementation")
|
||||
display_to_file(io, result[2])
|
||||
println(io, "\n\nFortran 77 Implementation")
|
||||
display_to_file(io, result[3])
|
||||
println(io, "\n\nJulia Implementation")
|
||||
display_to_file(io, result[4])
|
||||
end
|
||||
end
|
||||
Executable
+58
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
# This bash script will pull and compile the other implementations of the
|
||||
# bhmie algorithm (found at http://scatterlib.wikidot.com/mie)
|
||||
# These implementations will then be tested and benchmarked against the local
|
||||
# implementation to ensure that they are correct and (ideally) faster.
|
||||
|
||||
# First, let's create a base URL for the scatterlib wiki codebases
|
||||
base_url="http://scatterlib.wikidot.com/local--files/codes/"
|
||||
|
||||
# Next, let's create a list of the codebases we want to pull
|
||||
codebases=("bhmie-f.zip" "bhmie-c.zip")
|
||||
|
||||
# if bhmie_dir containers bhmie-c/bhmie.so, bhmie-f/bhmie.so, and bhmie-f/bhmie_f77.so then we can skip the build
|
||||
bhmie_dir=$1
|
||||
if [ -f $bhmie_dir/bhmie-c/bhmie.so ] && [ -f $bhmie_dir/bhmie-f/bhmie.so ] && [ -f $bhmie_dir/bhmie-f/bhmie_f77.so ]; then
|
||||
echo "bhmie-c/bhmie.so, bhmie-f/bhmie.so, and bhmie-f/bhmie_f77.so already exist. Skipping build."
|
||||
exit 0
|
||||
fi
|
||||
|
||||
# Now, let's pull the codebases
|
||||
for codebase in ${codebases[@]}; do
|
||||
wget -O $bhmie_dir/$codebase $base_url/$codebase
|
||||
if [[ $codebase == *.zip ]]; then
|
||||
unzip $bhmie_dir/$codebase -d $bhmie_dir/$codebase
|
||||
fi
|
||||
done
|
||||
|
||||
# Then, let's extract any .zip files to a directory of the same name (without the .zip, obviously)
|
||||
for codebase in ${codebases[@]}; do
|
||||
if [[ $codebase == *.zip ]]; then
|
||||
unzip $bhmie_dir/$codebase -d $bhmie_dir/${codebase%.zip}
|
||||
fi
|
||||
done
|
||||
|
||||
# If the folder already existed in the archive, move that folder one level up
|
||||
# and remove the now empty folder
|
||||
for codebase in ${codebases[@]}; do
|
||||
if [[ $codebase == *.zip ]]; then
|
||||
folder=${codebase%.zip}
|
||||
if [ -d $bhmie_dir/$folder/$folder ]; then
|
||||
mv $bhmie_dir/$folder/$folder/* $bhmie_dir/$folder
|
||||
rmdir $bhmie_dir/$folder/$folder
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Next, if this has all succeeded we can delete the .zip files
|
||||
for codebase in ${codebases[@]}; do
|
||||
if [[ $codebase == *.zip ]]; then
|
||||
rm $bhmie_dir/$codebase
|
||||
fi
|
||||
done
|
||||
|
||||
# And, finally, we can compile the C, and Fortran implementations
|
||||
cd $bhmie_dir
|
||||
gcc -shared -fPIC -o bhmie-c/bhmie.so bhmie-c/bhmie.c bhmie-c/complex.c bhmie-c/nrutil.c -lm -Wno-builtin-declaration-mismatch -Wno-implicit-function-declaration
|
||||
gfortran -shared -fPIC -o bhmie-f/bhmie.so bhmie-f/bhmie.f
|
||||
gfortran -shared -fPIC -o bhmie-f/bhmie_f77.so bhmie-f/bhmie_f77.f
|
||||
@@ -0,0 +1,71 @@
|
||||
module FFIWraps
|
||||
|
||||
include("../anchors.jl")
|
||||
|
||||
if !@isdefined TEST_DIR
|
||||
include("../anchors.jl")
|
||||
import .Anchors: TEST_DIR
|
||||
end
|
||||
|
||||
BHMIELIBS_DIR = joinpath(TEST_DIR, ".cache/bhmie-libs/")
|
||||
|
||||
function __init__()
|
||||
build_script = joinpath(TEST_DIR, "build_ffi.sh")
|
||||
|
||||
mkpath(BHMIELIBS_DIR)
|
||||
|
||||
run(`$build_script $BHMIELIBS_DIR`)
|
||||
end
|
||||
|
||||
function bhmie_c(x::Float32, cxref::ComplexF32, nang::UInt32, cxs1::Vector{ComplexF32}, cxs2::Vector{ComplexF32})
|
||||
# Pre-allocate memory for the output variables
|
||||
qext = Ref{Float32}(0.0)
|
||||
qsca = Ref{Float32}(0.0)
|
||||
qback = Ref{Float32}(0.0)
|
||||
gsca = Ref{Float32}(0.0)
|
||||
|
||||
# Ensure cxs1 and cxs2 have proper sizes, as expected by the C function
|
||||
# For example, if they need to be of size `nang`, you should verify or resize them accordingly
|
||||
|
||||
# Call the C function
|
||||
ccall((:bhmie, "$BHMIELIBS_DIR/bhmie-c/bhmie.so"), Cvoid,
|
||||
(Float32, ComplexF32, UInt32, Vector{ComplexF32}, Vector{ComplexF32}, Ref{Float32}, Ref{Float32}, Ref{Float32}, Ref{Float32}),
|
||||
x, cxref, nang, cxs1, cxs2, qext, qsca, qback, gsca)
|
||||
|
||||
# Return the output variables
|
||||
return qext[], qsca[], qback[], gsca[]
|
||||
end
|
||||
|
||||
function bhmie_fortran(x::Float32, refrel::ComplexF32, nang::Int32, s1::Vector{ComplexF32}, s2::Vector{ComplexF32})
|
||||
# Pre-allocate output variables
|
||||
qext = Ref{Float32}(0.0)
|
||||
qsca = Ref{Float32}(0.0)
|
||||
qback = Ref{Float32}(0.0)
|
||||
gsca = Ref{Float32}(0.0)
|
||||
|
||||
# Call the Fortran subroutine
|
||||
ccall((:bhmie_, "$BHMIELIBS_DIR/bhmie-f/bhmie.so"), Cvoid,
|
||||
(Ref{Float32}, Ref{ComplexF32}, Ref{Int32}, Ptr{ComplexF32}, Ptr{ComplexF32}, Ref{Float32}, Ref{Float32}, Ref{Float32}, Ref{Float32}),
|
||||
x, refrel, nang, s1, s2, qext, qsca, qback, gsca)
|
||||
|
||||
# Return the modified values
|
||||
return qext[], qsca[], qback[], gsca[]
|
||||
end
|
||||
|
||||
function bhmie_fortran77(x::Float32, refrel::ComplexF32, nang::Int32, s1::Vector{ComplexF32}, s2::Vector{ComplexF32})
|
||||
# Pre-allocate output variables
|
||||
qext = Ref{Float32}(0.0)
|
||||
qsca = Ref{Float32}(0.0)
|
||||
qback = Ref{Float32}(0.0)
|
||||
gsca = Ref{Float32}(0.0)
|
||||
|
||||
# Call the Fortran subroutine
|
||||
ccall((:bhmie_, "$BHMIELIBS_DIR/bhmie-f/bhmie.so"), Cvoid,
|
||||
(Ref{Float32}, Ref{ComplexF32}, Ref{Int32}, Ptr{ComplexF32}, Ptr{ComplexF32}, Ref{Float32}, Ref{Float32}, Ref{Float32}, Ref{Float32}),
|
||||
x, refrel, nang, s1, s2, qext, qsca, qback, gsca)
|
||||
|
||||
# Return the modified values
|
||||
return qext[], qsca[], qback[], gsca[]
|
||||
end
|
||||
|
||||
end
|
||||
+90
-22
@@ -1,31 +1,99 @@
|
||||
using Test
|
||||
using PropCheck
|
||||
|
||||
include("../anchors.jl")
|
||||
|
||||
import .Anchors: TEST_DIR, SRC_DIR
|
||||
|
||||
if !@isdefined TestUtils
|
||||
include("testutils.jl")
|
||||
include(joinpath(TEST_DIR, "testutils.jl"))
|
||||
end
|
||||
if !@isdefined nanoconc
|
||||
include("../src/nanoconc.jl")
|
||||
if !@isdefined miemfp
|
||||
include(joinpath(SRC_DIR, "miemfp.jl"))
|
||||
end
|
||||
if !@isdefined FFIWraps
|
||||
include(joinpath(TEST_DIR, "ffi_wraps.jl"))
|
||||
end
|
||||
|
||||
@testset "miemfp" begin
|
||||
# function julia_vs_c(args::Tuple{Float64, Float64, Float64, UInt32, Vector{Float64}, Vector{Float64}, Vector{Float64}, Vector{Float64}})
|
||||
# x, cxref_re, cxref_im, nang, cxs1_re, cxs1_im, cxs2_re, cxs2_im = args
|
||||
function julia_vs_c(x, cxref_re, cxref_im, nang, cxs1_re, cxs1_im, cxs2_re, cxs2_im)
|
||||
cxref, cxs1, cxs2 = ComplexF64(cxref_re, cxref_im), ComplexF64.(cxs1_re, cxs1_im), ComplexF64.(cxs2_re, cxs2_im)
|
||||
x_c, cxref_c, nang_c, cxs1_c, cxs2_c = Float32(x), ComplexF32(cxref), UInt32(nang), ComplexF32.(cxs1), ComplexF32.(cxs2)
|
||||
return isapprox(
|
||||
miemfp.bhmie(x, cxref, nang),
|
||||
FFIWraps.bhmie_c(x_c, cxref_c, nang_c, cxs1_c, cxs2_c),
|
||||
rtol=0.1,
|
||||
)
|
||||
end
|
||||
|
||||
# function julia_vs_fortran(args::Tuple{Float64, Float64, Float64, UInt32, Vector{Float64}, Vector{Float64}, Vector{Float64}, Vector{Float64}})
|
||||
# x, cxref_re, cxref_im, nang, cxs1_re, cxs1_im, cxs2_re, cxs2_im = args
|
||||
function julia_vs_fortran(x, cxref_re, cxref_im, nang, cxs1_re, cxs1_im, cxs2_re, cxs2_im)
|
||||
cxref, cxs1, cxs2 = ComplexF64(cxref_re, cxref_im), ComplexF64.(cxs1_re, cxs1_im), ComplexF64.(cxs2_re, cxs2_im)
|
||||
x_f, cxref_f, nang_f, cxs1_f, cxs2_f = Float32(x), ComplexF32(cxref), Int32(nang), ComplexF32.(cxs1), ComplexF32.(cxs2)
|
||||
b = miemfp.bhmie(x, cxref, nang)
|
||||
f = FFIWraps.bhmie_fortran(x_f, cxref_f, nang_f, cxs1_f, cxs2_f)
|
||||
# open("bhmie_julia_vs_fortran.txt", "a") do io
|
||||
# println(io, "julia: ", b)
|
||||
# println(io, "fortran: ", f)
|
||||
# end
|
||||
# return is_approx(b, f)
|
||||
return isapprox(
|
||||
miemfp.bhmie(x, cxref, nang),
|
||||
FFIWraps.bhmie_fortran(x_f, cxref_f, nang_f, cxs1_f, cxs2_f),
|
||||
rtol=0.1,
|
||||
)
|
||||
end
|
||||
|
||||
# function julia_vs_fortran77(args::Tuple{Float64, Float64, Float64, UInt32, Vector{Float64}, Vector{Float64}, Vector{Float64}, Vector{Float64}})
|
||||
# x, cxref_re, cxref_im, nang, cxs1_re, cxs1_im, cxs2_re, cxs2_im = args
|
||||
function julia_vs_fortran77(x, cxref_re, cxref_im, nang, cxs1_re, cxs1_im, cxs2_re, cxs2_im)
|
||||
cxref, cxs1, cxs2 = ComplexF64(cxref_re, cxref_im), ComplexF64.(cxs1_re, cxs1_im), ComplexF64.(cxs2_re, cxs2_im)
|
||||
x_f, cxref_f, nang_f, cxs1_f, cxs2_f = Float32(x), ComplexF32(cxref), Int32(nang), ComplexF32.(cxs1), ComplexF32.(cxs2)
|
||||
return isapprox(
|
||||
miemfp.bhmie(x, cxref, nang),
|
||||
FFIWraps.bhmie_fortran77(x_f, cxref_f, nang_f, cxs1_f, cxs2_f),
|
||||
rtol=0.1,
|
||||
)
|
||||
end
|
||||
|
||||
f64_gen = PropCheck.itype(Float64)
|
||||
UInt32_gen = PropCheck.itype(UInt32)
|
||||
f64_vec_gen = PropCheck.vector(isample(1:100), f64_gen)
|
||||
bhmie_gen = PropCheck.interleave(
|
||||
f64_gen,
|
||||
f64_gen,
|
||||
f64_gen,
|
||||
UInt32_gen,
|
||||
f64_vec_gen,
|
||||
f64_vec_gen,
|
||||
f64_vec_gen,
|
||||
f64_vec_gen,
|
||||
)
|
||||
|
||||
@testset "bhmie" begin
|
||||
@testset "miemfp.bhmie" begin
|
||||
TestUtils.test_from_serialized(
|
||||
nanoconc.miemfp.bhmie,
|
||||
"test/data/Main.nanoconc.miemfp.bhmie.ser"
|
||||
)
|
||||
end
|
||||
|
||||
@testset "miemfp.mfp" begin
|
||||
TestUtils.test_from_serialized(
|
||||
nanoconc.miemfp.mfp,
|
||||
"test/data/Main.nanoconc.miemfp.mfp.ser"
|
||||
)
|
||||
end
|
||||
|
||||
@testset "miemfp.qbare" begin
|
||||
TestUtils.test_from_serialized(
|
||||
nanoconc.miemfp.qbare,
|
||||
"test/data/Main.nanoconc.miemfp.qbare.ser"
|
||||
)
|
||||
c_check = PropCheck.check(julia_vs_c, bhmie_gen)
|
||||
c_result = c_check == true
|
||||
if !c_result
|
||||
println("Fail vs C, PropCheck:")
|
||||
display(c_check)
|
||||
end
|
||||
@test c_result
|
||||
f_check = PropCheck.check(julia_vs_fortran, bhmie_gen)
|
||||
f_result = f_check == true
|
||||
if !f_result
|
||||
println("Fail vs Fortran, PropCheck:")
|
||||
display(f_check)
|
||||
end
|
||||
@test f_result
|
||||
f77_check = PropCheck.check(julia_vs_fortran77, bhmie_gen)
|
||||
f77_result = f77_check == true
|
||||
if !f77_result
|
||||
println("Fail vs Fortran77, PropCheck:")
|
||||
display(f77_check)
|
||||
end
|
||||
@test f77_result
|
||||
end
|
||||
end
|
||||
+5
-2
@@ -3,6 +3,9 @@ using Test
|
||||
include("testutils.jl")
|
||||
include("../src/nanoconc.jl")
|
||||
|
||||
include("nanoconc_tests.jl")
|
||||
# include("nanoconc_tests.jl")
|
||||
include("miemfp_tests.jl")
|
||||
include("quantumcalc_tests.jl")
|
||||
# include("quantumcalc_tests.jl")
|
||||
include("benchmarks.jl")
|
||||
|
||||
Benchmarks.bench_vs_ffi()
|
||||
Reference in New Issue
Block a user