"Fossies" - the Fresh Open Source Software Archive

Member "gama-2.05/tests/gama-g3/scripts/check_adjustment.cpp" (10 May 2019, 4743 Bytes) of package /linux/privat/gama-2.05.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 #include <iostream>
    2 #include <fstream>
    3 #include <streambuf>
    4 #include <cmath>
    5 #include <string>
    6 #include <list>
    7 #include <map>
    8 #include <gnu_gama/xml/dataparser.h>
    9 #include <gnu_gama/size_to.h>
   10 
   11 using std::cerr;
   12 using std::cout;
   13 using std::endl;
   14 using std::string;
   15 using std::abs;
   16 using GNU_gama::size_to;
   17 
   18 namespace
   19 {
   20   int errors = 0;
   21 
   22   string pad(string t)
   23   {
   24     string s( 7, ' ');
   25     string p(15, ' ');
   26     for (std::size_t i=0; i<p.length() && i<t.length(); i++) {
   27         p[i] = t[i];
   28       }
   29     s += p;
   30     s += ' ';
   31     return s;
   32   }
   33 
   34   void tst(string s, int x, int y)
   35   {
   36     cout << pad(s);
   37     bool ok = (x == y);
   38     if (ok) {
   39         cout << "passed";
   40       }
   41     else {
   42         cout << "failed";
   43         errors++;
   44       }
   45     cout << "\t" << x << "\t" << y << endl;
   46   };
   47 
   48   void tst(string s, double x, double y, double limit=1e-6)
   49   {
   50     cout << pad(s);
   51     bool ok = (abs(x - y) < limit);
   52     if (ok) {
   53         cout << "passed";
   54       }
   55     else {
   56         cout << "failed";
   57         errors++;
   58       }
   59     cout << "\t" << x << "\t" << y << endl;
   60   };
   61 
   62   struct Adj3res {
   63     double ax {0}, ay{0}, az{0};
   64   };
   65 
   66 } /* unnamed namespace */
   67 
   68 int main(int argc, char* argv[])
   69 {
   70   if (argc != 3) {
   71       cerr << "wrong number of arguments, must be 2: input.xml output.xml\n";
   72       return 1;
   73     }
   74 
   75   std::list<GNU_gama::DataObject::Base*> objects;
   76 
   77   for (int i=1; i<=2; i++)
   78     {
   79       std::ifstream fb(argv[i]);
   80       if (!fb.is_open()){
   81           cerr << "file " << argv[i] << " is not open\n";
   82           return 1;
   83         }
   84       std::string text((std::istreambuf_iterator<char>(fb)),
   85                        std::istreambuf_iterator<char>());
   86 
   87       GNU_gama::DataParser parser(objects);
   88       try {
   89         parser.xml_parse(text.c_str(), size_to<int>(text.length()), 0);
   90         parser.xml_parse("", 0, 1);
   91       }
   92       catch(const GNU_gama::Exception::parser& p)
   93       {
   94         cerr << "\nXML parser error on line " << p.line
   95              << " of input data  "
   96              << "\t(error code " << p.error_code << ")\n"
   97              << p.str << "\n\n";
   98         return 1;
   99       }
  100       catch(...)
  101       {
  102         cerr << "catch ... \n";
  103         return 1;
  104       }
  105     }
  106 
  107   GNU_gama::DataObject::g3_adj_results* adjres1 {nullptr};
  108   GNU_gama::DataObject::g3_adj_results* adjres2 {nullptr};
  109   int n = 0;
  110   for (auto obj : objects) {
  111       if (auto t = dynamic_cast<GNU_gama::DataObject::g3_adj_results*>(obj)) {
  112           switch (++n) {
  113             case 1: adjres1 = t;
  114               break;
  115             case 2: adjres2 = t;
  116               break;
  117             }
  118         }
  119     }
  120   if (n != 2)
  121     {
  122       cerr << "wrong number of objects\n";
  123       return 1;
  124     }
  125 
  126   auto a = adjres1->adjres;
  127   auto b = adjres2->adjres;
  128 
  129   cout << "\ngama-g3 adjustment results diff" << endl;
  130   cout << "file 1 : " << argv[1] << "\t " << a->algorithm << endl;
  131   cout << "file 2 : " << argv[2] << "\t " << b->algorithm << endl;
  132 
  133   tst("parameters     ", stoi(a->parameters),     stoi(b->parameters));
  134   tst("equations      ", stoi(a->equations),      stoi(b->equations));
  135   tst("redundancy     ", stoi(a->redundancy),     stoi(b->redundancy));
  136   tst("defect         ", stoi(a->defect),         stoi(b->defect));
  137   tst("sum of sqrs    ", stod(a->sum_of_squares), stod(b->sum_of_squares));
  138   tst("apriori var    ", stod(a->apriori_var),    stod(b->apriori_var));
  139   tst("aposteriori var", stod(a->aposteriori_var),stod(b->aposteriori_var));
  140 
  141   using AdjMap = std::map<std::string, Adj3res>;
  142   AdjMap points;
  143 
  144   for (auto p : a->points) {
  145       Adj3res& point = points[p.id];
  146       if (p.x_adjusted.empty()) continue;
  147       point.ax = stod(p.x_adjusted);
  148       point.ay = stod(p.y_adjusted);
  149       point.az = stod(p.z_adjusted);
  150     }
  151   for (auto p : b->points) {
  152       if (p.x_adjusted.empty()) continue;
  153       Adj3res& point = points[p.id];
  154       point.ax -= stod(p.x_adjusted);
  155       point.ay -= stod(p.y_adjusted);
  156       point.az -= stod(p.z_adjusted);
  157     }
  158   double maxdiff = 0;
  159   string maxdiff_id = points.begin()->first;
  160   for (auto m : points) {
  161 
  162       if (abs(m.second.ax) > abs(maxdiff)) {
  163           maxdiff_id = m.first;
  164           maxdiff = m.second.ax;
  165         }
  166       if (abs(m.second.ay) > abs(maxdiff)) {
  167           maxdiff_id = m.first;
  168           maxdiff = m.second.ay;
  169         }
  170       if (abs(m.second.az) > abs(maxdiff)) {
  171           maxdiff_id = m.first;
  172           maxdiff = m.second.ay;
  173         }
  174     }
  175 
  176   if (abs(maxdiff) > 1e-4) {
  177       errors++;
  178       cout << "max diff in adjusted coordinates XYZ " << maxdiff << endl;
  179     }
  180 
  181   cout << pad("adjusted XYZ");
  182   if (abs(maxdiff) < 1e-4) {
  183       cout << "passed";
  184     }
  185   else {
  186       cout << "failed";
  187       errors++;
  188     }
  189   cout << "\t" << maxdiff << endl;
  190 
  191   return errors;
  192 }