Skip to content

Commit

Permalink
Merge pull request #1559 from JacquesCarette/inputParams
Browse files Browse the repository at this point in the history
Improvements to input parameters code generation
  • Loading branch information
JacquesCarette authored Jun 18, 2019
2 parents e9b5fb1 + e37638f commit ca7a883
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 90 deletions.
63 changes: 40 additions & 23 deletions code/drasil-code/Language/Drasil/Code/Imperative/Import.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import Data.List (nub, intersperse, (\\), stripPrefix)
import System.Directory (setCurrentDirectory, createDirectoryIfMissing, getCurrentDirectory)
import Data.Map (member)
import qualified Data.Map as Map (lookup)
import Data.Maybe (fromMaybe, maybe, maybeToList, catMaybes)
import Data.Maybe (fromMaybe, maybe, maybeToList, catMaybes, mapMaybe)
import Control.Applicative ((<$>))
import Control.Monad (when,liftM2,liftM3,zipWithM)
import Control.Monad.Reader (Reader, ask, runReader, withReader)
Expand Down Expand Up @@ -177,41 +177,45 @@ liftS = fmap (: [])

genInputModClass :: (RenderSym repr) => Reader (State repr) [repr (Module repr)]
genInputModClass = do
inputClass <- genInputClass
derived <- genInputDerived
constrs <- genInputConstraints
let dl = maybeToList derived
let ic = maybeToList inputClass
dl = maybeToList derived
cl = maybeToList constrs
sequence [ genModule "InputParameters" Nothing (Just $ liftS genInputClass),
sequence [ genModule "InputParameters" Nothing (Just $ return ic),
genModule "DerivedValues" (Just $ return dl) Nothing,
genModule "InputConstraints" (Just $ return cl) Nothing
]

genInputModNoClass :: (RenderSym repr) => Reader (State repr)
[repr (Module repr)]
genInputModNoClass = do
g <- ask
let ins = inputs $ codeSpec g
inpDer <- genInputDerived
inpConstr <- genInputConstraints
return [ buildModule "InputParameters" []
(map (\x -> varDecDef (codeName x) (convType $ codeType x)
(getDefaultValue $ codeType x)) ins)
return [ buildModule "InputParameters" [] []
(catMaybes [inpDer, inpConstr])
[]
]

genInputClass :: (RenderSym repr) => Reader (State repr) (repr (Class repr))
genInputClass :: (RenderSym repr) => Reader (State repr) (Maybe (repr (Class
repr)))
genInputClass = do
g <- ask
let ins = inputs $ codeSpec g
inputVars =
map (\x -> pubMVar 0 (codeName x) (convType $ codeType x)) ins
varsList = map (objVarSelf . codeName) ins
vals = map (getDefaultValue . codeType) ins
asgs <- zipWithM assign' varsList vals
return $ pubClass "InputParameters" Nothing inputVars
[ constructor "InputParameters" [] (body [block (initLogFileVar (logKind g)
++ asgs)]) ]
let ins = inputs $ codeSpec g
genClass :: (RenderSym repr) => [String] -> Reader (State repr) (Maybe
(repr (Class repr)))
genClass [] = return Nothing
genClass _ = do
let inputVars = map (\x -> pubMVar 0 (codeName x) (convType $
codeType x)) ins
varsList = map (objVarSelf . codeName) ins
vals = map (getDefaultValue . codeType) ins
asgs <- zipWithM assign' varsList vals
return $ Just $ pubClass "InputParameters" Nothing inputVars
[ constructor "InputParameters" [] (body [block (initLogFileVar
(logKind g) ++ asgs)]) ]
genClass $ mapMaybe (\x -> Map.lookup (codeName x) (eMap $ codeSpec g)) ins

genInputConstraints :: (RenderSym repr) => Reader (State repr)
(Maybe (repr (Method repr)))
Expand Down Expand Up @@ -409,18 +413,31 @@ genMain = genModule "Control" (Just $ liftS genMainFunc) Nothing
genMainFunc :: (RenderSym repr) => Reader (State repr) (repr (Method repr))
genMainFunc =
let l_filename = "inputfile"
l_params = "inParams"
in do
g <- ask
ip <- getInputDecl
gi <- getInputCall
dv <- getDerivedCall
ic <- getConstraintCall
varDef <- mapM getCalcCall (execOrder $ codeSpec g)
wo <- getOutputCall
return $ mainMethod "" $ bodyStatements $ [
varDecDef l_filename string $ arg 0 ,
extObjDecNewVoid l_params "InputParameters" (obj "InputParameters")] ++
catMaybes ([gi, dv, ic] ++ varDef ++ [wo])
return $ mainMethod "" $ bodyStatements $
varDecDef l_filename string (arg 0) :
catMaybes ([ip, gi, dv, ic] ++ varDef ++ [wo])

getInputDecl :: (RenderSym repr) => Reader (State repr) (Maybe (repr (
Statement repr)))
getInputDecl = do
g <- ask
let l_params = "inParams"
getDecl :: (RenderSym repr) => Structure -> [CodeChunk] -> Maybe (repr
(Statement repr))
getDecl _ [] = Nothing
getDecl Loose ins = Just $ multi $ map (\x -> varDecDef (codeName x)
(convType $ codeType x) (getDefaultValue $ codeType x)) ins
getDecl AsClass _ = Just $ extObjDecNewVoid l_params "InputParameters"
(obj "InputParameters")
return $ getDecl (inStruct g) (inputs $ codeSpec g)

getFuncCall :: (RenderSym repr) => String -> Reader (State repr)
[ParamData repr] -> Reader (State repr) (Maybe (repr (Value repr)))
Expand Down
31 changes: 19 additions & 12 deletions code/drasil-code/Language/Drasil/CodeSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ codeSpec SI {_sys = sys
constMap = assocToMap const',
constants = const',
mods = mods',
dMap = modDepMap db mem mods' exOrder,
dMap = modDepMap db mem mods' allInputs exOrder,
sysinfodb = db
}

Expand Down Expand Up @@ -239,7 +239,7 @@ type ModExportMap = Map.Map String String
modExportMap :: Choices -> ConstraintMap -> [Mod] -> [Input] -> [Const] -> [Derived] -> [Output] -> ModExportMap
modExportMap chs cm ms ins _ ds outs = Map.fromList $ concatMap mpair ms
where mpair (Mod n fs) = map fname fs `zip` repeat n
++ map codeName ins `zip` repeat "InputParameters"
++ getExportInput chs (ins ++ map codevar ds)
++ getExportDerived chs ds
++ getExportConstraints chs (getConstraints cm
(ins ++ map codevar ds))
Expand All @@ -249,9 +249,9 @@ modExportMap chs cm ms ins _ ds outs = Map.fromList $ concatMap mpair ms

type ModDepMap = Map.Map String [String]

modDepMap :: ChunkDB -> ModExportMap -> [Mod] -> [Def] -> ModDepMap
modDepMap sm mem ms eo = Map.fromList $ map (\(Mod n _) -> n) ms `zip` map getModDep ms
++ [("Control", getDepsControl mem eo),
modDepMap :: ChunkDB -> ModExportMap -> [Mod] -> [Input] -> [Def] -> ModDepMap
modDepMap sm mem ms ins eo = Map.fromList $ map (\(Mod n _) -> n) ms `zip` map getModDep ms
++ [("Control", getDepsControl mem ins eo),
("DerivedValues", [ "InputParameters" ] ),
("InputConstraints", [ "InputParameters" ] )] -- hardcoded for now
-- will fix later
Expand Down Expand Up @@ -342,6 +342,12 @@ getExecOrder d k' n' sm = getExecOrder' [] d k' (n' \\ k')

type Export = (String, String)

getExportInput :: Choices -> [Input] -> [Export]
getExportInput _ [] = []
getExportInput chs ins = inExp $ inputStructure chs
where inExp Loose = []
inExp AsClass = map codeName ins `zip` repeat "InputParameters"

getExportDerived :: Choices -> [Derived] -> [Export]
getExportDerived _ [] = []
getExportDerived chs _ = [("derived_values", dMod $ inputStructure chs)]
Expand All @@ -358,13 +364,14 @@ getExportOutput :: [Output] -> [Export]
getExportOutput [] = []
getExportOutput _ = [("write_output", "OutputFormat")]

getDepsControl :: ModExportMap -> [Def] -> [String]
getDepsControl mem eo = let inf = Map.lookup (funcPrefix ++ "get_input") mem
dv = Map.lookup "derived_values" mem
ic = Map.lookup "input_constraints" mem
wo = Map.lookup "write_output" mem
calcs = map (\x -> Map.lookup (codeName x) mem) eo
in nub $ catMaybes ([inf, dv, ic, wo] ++ calcs) ++ ["InputParameters"]
getDepsControl :: ModExportMap -> [Input] -> [Def] -> [String]
getDepsControl mem ins eo = let ip = map (\x -> Map.lookup (codeName x) mem) ins
inf = Map.lookup (funcPrefix ++ "get_input") mem
dv = Map.lookup "derived_values" mem
ic = Map.lookup "input_constraints" mem
wo = Map.lookup "write_output" mem
calcs = map (\x -> Map.lookup (codeName x) mem) eo
in nub $ catMaybes (ip ++ [inf, dv, ic, wo] ++ calcs)

subsetOf :: (Eq a) => [a] -> [a] -> Bool
xs `subsetOf` ys = all (`elem` ys) xs
Expand Down
2 changes: 1 addition & 1 deletion code/stable/glassbr/src/cpp/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ using std::vector;
using std::ifstream;
using std::ofstream;

#include "InputParameters.hpp"
#include "InputFormat.hpp"
#include "DerivedValues.hpp"
#include "InputConstraints.hpp"
#include "OutputFormat.hpp"
#include "Calculations.hpp"
#include "InputParameters.hpp"

int main(int argc, const char *argv[]) {
string inputfile = argv[1];
Expand Down
2 changes: 1 addition & 1 deletion code/stable/glassbr/src/python/Control.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
import sys
import math

import InputParameters
import InputFormat
import DerivedValues
import InputConstraints
import OutputFormat
import Calculations
import InputParameters

inputfile = sys.argv[1]
inParams = InputParameters.InputParameters()
Expand Down
15 changes: 14 additions & 1 deletion code/stable/nopcm/src/cpp/Control.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,20 @@ using std::ofstream;

int main(int argc, const char *argv[]) {
string inputfile = argv[1];
InputParameters inParams = InputParameters();
double A_C = 0.0;
double C_W = 0.0;
double h_C = 0.0;
double T_init = 0.0;
double t_final = 0.0;
double L = 0.0;
double T_C = 0.0;
double t_step = 0.0;
double rho_W = 0.0;
double D = 0.0;
double A_tol = 0.0;
double R_tol = 0.0;
double T_W = 0.0;
double E_W = 0.0;
func_get_input(inputfile, A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, A_tol, R_tol, T_W, E_W);
input_constraints(A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, T_W, E_W);
write_output(T_W, E_W);
Expand Down
2 changes: 0 additions & 2 deletions code/stable/nopcm/src/cpp/InputFormat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ using std::vector;
using std::ifstream;
using std::ofstream;

#include "InputParameters.hpp"

void func_get_input(string filename, double L, double D, double A_C, double T_C, double rho_W, double C_W, double h_C, double T_init, double t_step, double t_final, double A_tol, double R_tol) {
ifstream infile;
string line;
Expand Down
2 changes: 0 additions & 2 deletions code/stable/nopcm/src/cpp/InputFormat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ using std::vector;
using std::ifstream;
using std::ofstream;

#include "InputParameters.hpp"

void func_get_input(string filename, double L, double D, double A_C, double T_C, double rho_W, double C_W, double h_C, double T_init, double t_step, double t_final, double A_tol, double R_tol);

#endif
15 changes: 14 additions & 1 deletion code/stable/nopcm/src/csharp/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ public class Control {

public static void Main(string[] args) {
string inputfile = args[0];
InputParameters inParams = new InputParameters();
double A_C = 0.0;
double C_W = 0.0;
double h_C = 0.0;
double T_init = 0.0;
double t_final = 0.0;
double L = 0.0;
double T_C = 0.0;
double t_step = 0.0;
double rho_W = 0.0;
double D = 0.0;
double A_tol = 0.0;
double R_tol = 0.0;
double T_W = 0.0;
double E_W = 0.0;
InputFormat.func_get_input(inputfile, A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, A_tol, R_tol, T_W, E_W);
InputParameters.input_constraints(A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, T_W, E_W);
OutputFormat.write_output(T_W, E_W);
Expand Down
14 changes: 0 additions & 14 deletions code/stable/nopcm/src/csharp/InputParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
using System.Collections.Generic;

public class InputParameters {
public static double A_C = 0.0;
public static double C_W = 0.0;
public static double h_C = 0.0;
public static double T_init = 0.0;
public static double t_final = 0.0;
public static double L = 0.0;
public static double T_C = 0.0;
public static double t_step = 0.0;
public static double rho_W = 0.0;
public static double D = 0.0;
public static double A_tol = 0.0;
public static double R_tol = 0.0;
public static double T_W = 0.0;
public static double E_W = 0.0;

public static void input_constraints(double A_C, double C_W, double h_C, double T_init, double t_final, double L, double T_C, double t_step, double rho_W, double D, double T_W, double E_W) {
if (!((inParams.A_C <= 100000))) {
Expand Down
15 changes: 14 additions & 1 deletion code/stable/nopcm/src/java/SWHS/Control.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@ public class Control {

public static void main(String[] args) throws Exception {
String inputfile = args[0];
InputParameters inParams = new InputParameters();
double A_C = 0.0;
double C_W = 0.0;
double h_C = 0.0;
double T_init = 0.0;
double t_final = 0.0;
double L = 0.0;
double T_C = 0.0;
double t_step = 0.0;
double rho_W = 0.0;
double D = 0.0;
double A_tol = 0.0;
double R_tol = 0.0;
double T_W = 0.0;
double E_W = 0.0;
InputFormat.func_get_input(inputfile, A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, A_tol, R_tol, T_W, E_W);
InputParameters.input_constraints(A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, T_W, E_W);
OutputFormat.write_output(T_W, E_W);
Expand Down
14 changes: 0 additions & 14 deletions code/stable/nopcm/src/java/SWHS/InputParameters.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,6 @@
import java.util.ArrayList;

public class InputParameters {
public static double A_C = 0.0;
public static double C_W = 0.0;
public static double h_C = 0.0;
public static double T_init = 0.0;
public static double t_final = 0.0;
public static double L = 0.0;
public static double T_C = 0.0;
public static double t_step = 0.0;
public static double rho_W = 0.0;
public static double D = 0.0;
public static double A_tol = 0.0;
public static double R_tol = 0.0;
public static double T_W = 0.0;
public static double E_W = 0.0;

public static void input_constraints(double A_C, double C_W, double h_C, double T_init, double t_final, double L, double T_C, double t_step, double rho_W, double D, double T_W, double E_W) throws Exception {
if (!((inParams.A_C <= 100000))) {
Expand Down
15 changes: 14 additions & 1 deletion code/stable/nopcm/src/python/Control.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@
import OutputFormat

inputfile = sys.argv[1]
inParams = InputParameters.InputParameters()
A_C = 0.0
C_W = 0.0
h_C = 0.0
T_init = 0.0
t_final = 0.0
L = 0.0
T_C = 0.0
t_step = 0.0
rho_W = 0.0
D = 0.0
A_tol = 0.0
R_tol = 0.0
T_W = 0.0
E_W = 0.0
InputFormat.func_get_input(inputfile, A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, A_tol, R_tol, T_W, E_W)
InputParameters.input_constraints(A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, T_W, E_W)
OutputFormat.write_output(T_W, E_W)
Expand Down
2 changes: 0 additions & 2 deletions code/stable/nopcm/src/python/InputFormat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
import sys
import math

import InputParameters

def func_get_input(filename, L, D, A_C, T_C, rho_W, C_W, h_C, T_init, t_step, t_final, A_tol, R_tol):
lines = []
linetokens = []
Expand Down
15 changes: 0 additions & 15 deletions code/stable/nopcm/src/python/InputParameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@
import sys
import math

A_C = 0.0
C_W = 0.0
h_C = 0.0
T_init = 0.0
t_final = 0.0
L = 0.0
T_C = 0.0
t_step = 0.0
rho_W = 0.0
D = 0.0
A_tol = 0.0
R_tol = 0.0
T_W = 0.0
E_W = 0.0

def input_constraints(A_C, C_W, h_C, T_init, t_final, L, T_C, t_step, rho_W, D, T_W, E_W):
if (not((inParams.A_C <= 100000))) :
print("Warning: constraint violated")
Expand Down

0 comments on commit ca7a883

Please sign in to comment.