Skip to content

Commit 02068bd

Browse files
Allow running of external models in same working directory (#100)
* Extract datetime as keyword * Run external models in the same workdir This allows to reuse files or pass output files from one model to the next as input. * Reenable tests * Fix permissions of unix solver * Add second solver for Mac * Update mac test solver * Update mac binary * Update squared-mac file 2 --------- Co-authored-by: andrea perin <andrea.perin@irz.uni-hannover.de>
1 parent 4bc1754 commit 02068bd

File tree

7 files changed

+92
-14
lines changed

7 files changed

+92
-14
lines changed

src/models/externalmodel.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ struct ExternalModel <: UQModel
2424
end
2525
end
2626

27-
function evaluate!(m::ExternalModel, df::DataFrame)
28-
datetime = Dates.format(now(), "YYYY-mm-dd-HH-MM-SS")
29-
27+
function evaluate!(
28+
m::ExternalModel,
29+
df::DataFrame;
30+
datetime::String=Dates.format(now(), "YYYY-mm-dd-HH-MM-SS"),
31+
)
3032
n = size(df, 1)
3133
digits = ndigits(n)
3234

src/models/model.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,15 @@ function evaluate!(m::ParallelModel, df::DataFrame)
2626
return nothing
2727
end
2828

29-
function evaluate!(models::Vector{T} where {T<:UQModel}, df::DataFrame)
29+
function evaluate!(models::Vector{<:UQModel}, df::DataFrame)
30+
# use same working directory for all external models
31+
datetime = Dates.format(now(), "YYYY-mm-dd-HH-MM-SS")
3032
for m in models
31-
evaluate!(m, df)
33+
if isa(m, ExternalModel)
34+
evaluate!(m, df; datetime=datetime)
35+
else
36+
evaluate!(m, df)
37+
end
3238
end
3339
return nothing
3440
end

test/models/externalmodel.jl

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
numberformats = Dict(:x => FormatSpec(".8e"), :* => FormatSpec(".8e"))
77

8-
r = Extractor(
8+
radius = Extractor(
99
base -> begin
1010
map(x -> parse(Float64, x), readlines(joinpath(base, "out.txt")))[1]
1111
end, :r
@@ -20,7 +20,7 @@
2020
binary = joinpath(pwd(), "solvers/bin/radius")
2121
end
2222

23-
opensees = Solver(binary, "", "in.txt")
23+
solver = Solver(binary, "", "in.txt")
2424

2525
open(joinpath(sourcedir, "in.txt"), "w") do input
2626
println(input, "{{{ :x }}}")
@@ -30,7 +30,7 @@
3030
x = RandomVariable(Uniform(0, 1), :x)
3131
y = RandomVariable(Uniform(0, 1), :y)
3232

33-
df = sample([x, y], 1)
33+
df = sample([x, y], 5)
3434

3535
@testset "No Cleanup" begin
3636
ext = ExternalModel(
@@ -39,13 +39,13 @@
3939
extrafiles,
4040
numberformats,
4141
tempname(),
42-
[r],
43-
opensees,
42+
[radius],
43+
solver,
4444
false,
4545
)
4646
evaluate!(ext, df)
47-
@test length(readdir(readdir(ext.workdir; join=true)[1])) != 0
48-
@test isapprox(df.r, sqrt.(df.x .^ 2 + df.y .^ 2))
47+
@test length(readdir(readdir(ext.workdir; join=true)[1])) == 5
48+
@test isapprox(df.r, sqrt.(df.x .^ 2 .+ df.y .^ 2))
4949
end
5050

5151
@testset "Cleanup" begin
@@ -55,12 +55,59 @@
5555
extrafiles,
5656
numberformats,
5757
tempname(),
58-
[r],
59-
opensees,
58+
[radius],
59+
solver,
6060
true,
6161
)
6262
evaluate!(ext, df)
6363
@test length(readdir(readdir(ext.workdir; join=true)[1])) == 0
6464
@test isapprox(df.r, sqrt.(df.x .^ 2 + df.y .^ 2))
6565
end
66+
67+
@testset "Reuse model output" begin
68+
binary2 = ""
69+
if Sys.iswindows()
70+
binary2 = joinpath(pwd(), "solvers/bin/squared.exe")
71+
elseif Sys.isapple()
72+
binary2 = joinpath(pwd(), "solvers/bin/squared-mac")
73+
else
74+
binary2 = joinpath(pwd(), "solvers/bin/squared")
75+
end
76+
77+
solver2 = Solver(binary2, "", "out.txt")
78+
79+
workdir = tempname()
80+
81+
ext1 = ExternalModel(
82+
sourcedir,
83+
sourcefiles,
84+
extrafiles,
85+
numberformats,
86+
workdir,
87+
[radius],
88+
solver,
89+
false,
90+
)
91+
92+
squared = Extractor(
93+
base -> begin
94+
map(x -> parse(Float64, x), readlines(joinpath(base, "out-squared.txt")))[1]
95+
end,
96+
:r2,
97+
)
98+
99+
ext2 = ExternalModel(
100+
"",
101+
String[],
102+
extrafiles,
103+
Dict{Symbol,FormatSpec}(),
104+
workdir,
105+
[squared],
106+
solver2,
107+
true,
108+
)
109+
110+
evaluate!([ext1, ext2], df)
111+
@test df.r2 df.x .^ 2 + df.y .^ 2
112+
end
66113
end

test/solvers/bin/squared

15.8 KB
Binary file not shown.

test/solvers/bin/squared-mac

32.4 KB
Binary file not shown.

test/solvers/bin/squared.exe

130 KB
Binary file not shown.

test/solvers/src/squared.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
#include <math.h>
3+
4+
int main(int argc, char *argv[])
5+
{
6+
FILE *input = fopen(argv[1], "r");
7+
8+
double x;
9+
10+
fscanf(input, "%lf", &x);
11+
12+
fclose(input);
13+
14+
double y = pow(x, 2);
15+
16+
FILE *output = fopen("out-squared.txt", "w");
17+
18+
fprintf(output, "%.16f", y);
19+
20+
fclose(output);
21+
22+
return 0;
23+
}

0 commit comments

Comments
 (0)