瀏覽代碼

Added support for serializing complex matrices in Yaml

submodule
T-bone 7 年之前
父節點
當前提交
79478ef492
共有 2 個文件被更改,包括 91 次插入0 次删除
  1. 55
    0
      Modules/FDEM1D/examples/CircularLoop.cpp
  2. 36
    0
      Modules/LemmaCore/include/helper.h

+ 55
- 0
Modules/FDEM1D/examples/CircularLoop.cpp 查看文件

@@ -0,0 +1,55 @@
1
+/* This file is part of Lemma, a geophysical modelling and inversion API.
2
+ * More information is available at http://lemmasoftware.org
3
+ */
4
+
5
+/* This Source Code Form is subject to the terms of the Mozilla Public
6
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
7
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
+ */
9
+
10
+/**
11
+ * @file
12
+ * @date      12/01/2017 09:38:57 PM
13
+ * @version   $Id$
14
+ * @author    Trevor Irons (ti)
15
+ * @email     tirons@egi.utah.edu
16
+ * @copyright Copyright (c) 2017, University of Utah
17
+ * @copyright Copyright (c) 2017, Trevor Irons & Lemma Software, LLC
18
+ */
19
+
20
+
21
+#include <FDEM1D>
22
+using namespace Lemma;
23
+
24
+int main(int argc, char** argv) {
25
+
26
+    if (argc < 6) {
27
+        std::cout << "Usage:\n";
28
+        std::cout << "CircularLoop <radius> <centreNorthing>  <centreEasting>"
29
+                  <<  "  <numTurns>  <npoints>\n"  ;
30
+        exit(EXIT_SUCCESS);
31
+    }
32
+
33
+    Real rad = atof(argv[1]);
34
+    Real cx = atof(argv[2]);
35
+    Real cy = atof(argv[3]);
36
+    int nt = atoi(argv[4]);
37
+    int np = atoi(argv[5]);
38
+    std::cout << rad << std::endl;
39
+
40
+    auto wire = PolygonalWireAntenna::NewSP();
41
+        wire->SetNumberOfPoints(np);
42
+        wire->SetNumberOfTurns(nt);
43
+        Real da = 2.*PI / (Real)(np-1);
44
+        for (int ip=0; ip<np-1; ++ip) {
45
+            wire->SetPoint(ip, cx+rad*std::sin(da*static_cast<Real>(ip)),
46
+                               cy+rad*std::cos(da*static_cast<Real>(ip)), -1e-3);
47
+        }
48
+        wire->SetPoint(np-1, cx, cy+rad, -1e-3);
49
+
50
+    std::cout << *wire << std::endl;
51
+
52
+    //auto wire2 = wire->Clone();
53
+    //std::cout << "copy\n" << *wire2 << std::endl;
54
+}
55
+

+ 36
- 0
Modules/LemmaCore/include/helper.h 查看文件

@@ -321,6 +321,42 @@ struct convert<Lemma::MatrixXr> {
321 321
 
322 322
 };
323 323
 
324
+template<>
325
+struct convert<Lemma::MatrixXcr> {
326
+  static Node encode(const Lemma::MatrixXcr& rhs) {
327
+    Node node;
328
+    node["rows"] = rhs.rows();
329
+    node["cols"] = rhs.cols();
330
+    for (int ir=0; ir<rhs.rows(); ++ir) {
331
+        for (int ic=0; ic<rhs.cols(); ++ic) {
332
+            node["data"][ir][ic] = rhs(ir,ic);
333
+        }
334
+        node["data"][ir].SetStyle(YAML::EmitterStyle::Flow);
335
+    }
336
+    //node.SetStyle(YAML::EmitterStyle::Block);
337
+    node.SetTag( "MatrixXcr" );
338
+    return node;
339
+  }
340
+
341
+  static bool decode(const Node& node, Lemma::MatrixXcr& rhs) {
342
+    if( node.Tag() != "MatrixXcr" ) {
343
+        return false;
344
+    }
345
+    int nc = node["cols"].as<int>();
346
+    int nr = node["rows"].as<int>();
347
+    rhs.resize(nr, nc);
348
+    for (int ir=0; ir<nr; ++ir) {
349
+        int ic=0;
350
+        for(YAML::const_iterator it=node["data"][ir].begin(); it!=node["data"][ir].end(); ++it) {
351
+            rhs(ir,ic) = it->as<Lemma::Complex>();
352
+            ++ic;
353
+        }
354
+    }
355
+    return true;
356
+  }
357
+
358
+};
359
+
324 360
 }
325 361
 
326 362
 #endif   // ----- #ifndef HELPER_INC  -----

Loading…
取消
儲存