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 #include <iostream>
29 
30 using namespace casadi;
31 
32 int eval_dump(const std::string& name) {
33  // Load function
34  Function f = Function::load(name+".casadi");
35  f.change_option("dump_in", false);
36  f.change_option("dump_out", false);
37 
38  // Helper to format filenames
39  std::stringstream ss;
40  ss << std::setfill('0') << std::setw(6);
41 
42  std::vector<DM> inputs;
43  // Loop over all inputs
44  for (int i=0;i<1000000;++i) {
45  ss << i;
46  try {
47  inputs = f.generate_in(name+"."+ss.str()+ ".in.txt");
48  } catch (CasadiException& ex) {
49  if (i==0) {
50  casadi_warning(ex.what());
51  casadi_assert(i>0, "Could not find a single input file "
52  "with file name " + name + ".<dddddd>.in.txt");
53  }
54  // No more input files
55  break;
56  }
57  // Run function
58  std::vector<DM> res = f(inputs);
59  // Generate output file
60  f.generate_out(name+"."+ss.str()+ ".out.txt", res);
61  ss.clear();
62  }
63  return 0;
64 }
65 
66 int eval_dump_parse(const std::vector<std::string>& args) {
67  casadi_assert(args.size()>0, "Name is missing in $ casadi-cli eval_dump name.");
68  std::string name = args[0];
69  return eval_dump(name);
70 }
71 
72 int main(int argc, char* argv[]) {
73  try {
74  // Retrieve all arguments
75  std::vector<std::string> args(argv + 1, argv + argc);
76 
77  // Branch on 'command' (first argument)
78  std::set<std::string> commands = {"eval_dump"};
79  casadi_assert(args.size()>0, "Must provide a command. Use one of: " + str(commands) + ".");
80  std::string cmd = args[0];
81  if (cmd=="eval_dump") {
82  return eval_dump_parse(std::vector<std::string>(args.begin()+1, args.end()));
83  } else {
84  casadi_assert(commands.find(cmd)!=commands.end(),
85  "Unrecognised command '" + cmd + "'. Use one of: " + str(commands) + ".");
86  }
87  } catch (const std::exception& e) {
88  std::cerr << e.what() << std::endl;
89  return 1;
90  } catch (...) {
91  std::cerr << "Unknown error" << std::endl;
92  return 1;
93  }
94 }
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:1400
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:1415
static Function load(const std::string &filename)
Build function from serialization.
Definition: function.cpp:1546
void change_option(const std::string &option_name, const GenericType &option_value)
Change option after object creation for debugging.
Definition: function.cpp:1361
The casadi namespace.
Definition: archiver.cpp:28
std::string str(const T &v)
String representation, any type.