Skip to content

Commit

Permalink
Merge pull request #27 from SeisSol/lua-more-input
Browse files Browse the repository at this point in the history
Fix Lua input, add x["variableName"] lookup
  • Loading branch information
krenzland committed Feb 15, 2022
2 parents 4c87ef3 + 486d089 commit 9ca6daa
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 6 deletions.
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
project(easi)
cmake_minimum_required(VERSION 3.13)
set(EASI_VERSION_MAJOR 1)
set(EASI_VERSION_MINOR 1)
set(EASI_VERSION_PATCH 2)
set(EASI_VERSION_MINOR 2)
set(EASI_VERSION_PATCH 0)

include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
Expand Down Expand Up @@ -231,6 +231,7 @@ endif ()

if (LUA)
easi_add_test(4_lua)
easi_add_test(6_lua)
endif()


Expand Down
1 change: 1 addition & 0 deletions examples/4_lua.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ components:
function: |
function f (x)
io.write(x[1], " ", x[2], " ", x[3], "\n") -- optional debug output
io.write(x["x"], " ", x["y"], " ", x["z"], "\n") -- you can also access arrays like this
return {
rho = 1600. + 59.5 * (x[2] ^ (1./3.)),
Expand Down
19 changes: 19 additions & 0 deletions examples/6_lua.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
!EvalModel
parameters: [elevation, z]
model: !Switch
[elevation]: !ConstantMap
map:
elevation: 100.
[z]: !AffineMap
matrix:
z: [0.0, 0.0, 1.0]
translation:
z: 0.0
components: !LuaMap
returns: [effectiveConfiningStress]
function: |
function f (x)
return {
effectiveConfiningStress = 2670.0 * 0.21 * 9.8 * math.min(-100.0, x["elevation"] + x["z"])
}
end
3 changes: 2 additions & 1 deletion include/easi/component/LuaMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class LuaMap : public Map {
private:
double executeLuaFunction(Matrix<double> x, unsigned coordIdx, unsigned funcIdx);
std::string function;
std::vector<std::string> idxToNameMap;
std::vector<std::string> idxToInputName;
std::vector<std::string> idxToOutputName;
lua_State* luaState{nullptr};

};
Expand Down
15 changes: 12 additions & 3 deletions src/component/LuaMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,16 @@ double LuaMap::executeLuaFunction(Matrix<double> x,

// Add table as input: x holds coordinates
lua_newtable(luaState);
for (int i = 0; i < 3; ++i) {
for (int i = 0; i < x.cols(); ++i) {
// Support x[1] indexing
lua_pushnumber(luaState, i+1);
lua_pushnumber(luaState, x(coordIdx, i));
lua_rawset(luaState, -3);

// Support x["x"] indexing
lua_pushstring(luaState, idxToInputName[i].data());
lua_pushnumber(luaState, x(coordIdx, i));
lua_rawset(luaState, -3);
}

if (lua_pcall(luaState, 1, 1, 0) != 0) {
Expand All @@ -62,7 +68,7 @@ double LuaMap::executeLuaFunction(Matrix<double> x,
<< std::endl;
std::abort();
}
return getField(luaState, idxToNameMap[funcIdx]);
return getField(luaState, idxToOutputName[funcIdx]);
}

Matrix<double> LuaMap::map(Matrix<double>& x) {
Expand All @@ -84,8 +90,11 @@ void LuaMap::setMap(const std::set<std::string>& in,
setIn(in);
setOut(out);
function = newFunction;
for (const auto& i: in) {
idxToInputName.push_back(i);
}
for (const auto& o : out) {
idxToNameMap.push_back(o);
idxToOutputName.push_back(o);
}
}

Expand Down
34 changes: 34 additions & 0 deletions tests/6_lua.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "easitest.h"
#include <cmath>

double effectiveConfiningStress(double z) {
const double elevation = 100;
return 2670.0 * 0.21 * 9.8 * std::min(-100.0, elevation + z);
}

int main(int argc, char** argv) {
assert(argc == 2);

easi::Query query = createQuery<3>({
{0, {0.0, 0.0, -180.0}},
{0, {0.0, 0.0, -190.0}},
{0, {0.0, 0.0, -200.0}},
{0, {0.0, 0.0, -210.0}},
{0, {0.0, 0.0, -220.0}},
{0, {0.0, 0.0, -230.0}}
});
std::vector<std::string> parameters{"effectiveConfiningStress"};
auto stress = genericModel(argv[1], query, parameters);

double curVal = -180;
for (auto& s : stress[0]) {
constexpr double eps = 10e-15;
assert(std::abs(s - effectiveConfiningStress(curVal)) <= eps);
curVal -= 10.0;
}



return 0;
}

0 comments on commit 9ca6daa

Please sign in to comment.