casadi_cli.cpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #include "function.hpp"
27 #include <iomanip>
28 
29 using namespace casadi;
30 
31 int eval_dump(const std::string& name) {
32  // Load function
33  Function f = Function::load(name+".casadi");
34  f.change_option("dump_in", false);
35  f.change_option("dump_out", false);
36 
37  // Helper to format filenames
38  std::stringstream ss;
39  ss << std::setfill('0') << std::setw(6);
40 
41  std::vector<DM> inputs;
42  // Loop over all inputs
43  for (int i=0;i<1000000;++i) {
44  ss << i;
45  try {
46  inputs = f.generate_in(name+"."+ss.str()+ ".in.txt");
47  } catch (CasadiException& ex) {
48  if (i==0) {
49  casadi_warning(ex.what());
50  casadi_assert(i>0, "Could not find a single input file "
51  "with file name " + name + ".<dddddd>.in.txt");
52  }
53  // No more input files
54  break;
55  }
56  // Run function
57  std::vector<DM> res = f(inputs);
58  // Generate output file
59  f.generate_out(name+"."+ss.str()+ ".out.txt", res);
60  ss.clear();
61  }
62  return 0;
63 }
64 
65 int eval_dump_parse(const std::vector<std::string>& args) {
66  casadi_assert(args.size()>0, "Name is missing in $ casadi-cli eval_dump name.");
67  std::string name = args[0];
68  return eval_dump(name);
69 }
70 
71 int main(int argc, char* argv[]) {
72  // Retrieve all arguments
73  std::vector<std::string> args(argv + 1, argv + argc);
74 
75  // Branch on 'command' (first argument)
76  std::set<std::string> commands = {"eval_dump"};
77  casadi_assert(args.size()>0, "Must provide a command. Use one of: " + str(commands) + ".");
78  std::string cmd = args[0];
79  if (cmd=="eval_dump") {
80  return eval_dump_parse(std::vector<std::string>(args.begin()+1, args.end()));
81  } else {
82  casadi_assert(commands.find(cmd)!=commands.end(),
83  "Unrecognised command '" + cmd + "'. Use one of: " + str(commands) + ".");
84  }
85 }
Casadi exception class.
Definition: exception.hpp:77
const char * what() const override
Display error.
Definition: exception.hpp:90
Function object.
Definition: function.hpp:60
void generate_in(const std::string &fname, const std::vector< DM > &arg)
Export an input file that can be passed to generate C code with a main.
Definition: function.cpp:1202
void generate_out(const std::string &fname, const std::vector< DM > &arg)
Export an output file that can be checked with generated C code output.
Definition: function.cpp:1217
static Function load(const std::string &filename)
Build function from serialization.
Definition: function.cpp:1349
void change_option(const std::string &option_name, const GenericType &option_value)
Change option after object creation for debugging.
Definition: function.cpp:1172
The casadi namespace.
Definition: archiver.cpp:28
std::string str(const T &v)
String representation, any type.