Chi-Tech
chi_ffinter_line_exportpython.cc
Go to the documentation of this file.
1#include "chi_ffinter_line.h"
2
4
5#include "chi_runtime.h"
6#include "chi_mpi.h"
7#include "chi_log.h"
8
9#include <fstream>
10
11//###################################################################
12/***/
14ExportPython(std::string base_name)
15{
16 std::ofstream ofile;
17
18 std::string fileName = base_name;
19 fileName = fileName + std::to_string(Chi::mpi.location_id);
20 fileName = fileName + std::string(".py");
21 ofile.open(fileName);
22
23 ofile
24 << "import numpy as np\n"
25 "import matplotlib.pyplot as plt\n"
26 << "\n";
27
28 std::string offset;
29 std::string submod_name;
30 if (Chi::mpi.location_id == 0)
31 {
32 submod_name = base_name;
33 submod_name = submod_name + std::to_string(Chi::mpi.location_id+1);
34
35 if (Chi::mpi.process_count>1)
36 {
37 ofile << "import " << submod_name << "\n\n";
38 }
39
40 for (int ff=0; ff < field_functions_.size(); ff++)
41 {
42 ofile
43 << "data" << ff << "=np.zeros([" << interpolation_points_.size()
44 << ",5])\n";
45 }
46 for (int ca=0; ca < custom_arrays_.size(); ca++)
47 {
48 int ff = ca + field_functions_.size();
49 ofile
50 << "data" << ff << "=np.zeros([" << interpolation_points_.size()
51 << ",5])\n";
52 }
53
54 offset = std::string("");
55 }
56 else if (Chi::mpi.process_count>1)
57 {
58
59 if (Chi::mpi.location_id != (Chi::mpi.process_count-1))
60 {
61 submod_name = base_name;
62 submod_name = submod_name + std::to_string(Chi::mpi.location_id+1);
63
64 ofile << "import " << submod_name << "\n\n";
65 }
66
67
68 }
69
70 for (int ff=0; ff < field_functions_.size(); ff++)
71 {
72 const auto& ff_ctx = ff_contexts_[ff];
73
74 if (Chi::mpi.process_count>1 and Chi::mpi.location_id!=0)
75 {
76 ofile
77 << "def AddData" << ff << "(data" << ff << "):\n";
78
79 offset = std::string(" ");
80 }
81 for (int p=0; p < interpolation_points_.size(); p++)
82 {
83 if ((not ff_ctx.interpolation_points_has_ass_cell[p]) &&
84 (Chi::mpi.location_id != 0))
85 {
86 continue;
87 }
88
89 ofile << offset << "data" << ff << "[" << p << ",0] = "
90 << interpolation_points_[p].x << "\n";
91 ofile << offset << "data" << ff << "[" << p << ",1] = "
92 << interpolation_points_[p].y << "\n";
93 ofile << offset << "data" << ff << "[" << p << ",2] = "
94 << interpolation_points_[p].z << "\n";
95
96 double d = delta_d_ * p;
97
98 ofile << offset << "data" << ff << "[" << p << ",3] = "
99 << d << "\n";
100 ofile << offset << "data" << ff << "[" << p << ",4] = "
101 << ff_ctx.interpolation_points_values[p] << "\n";
102
103
104 }
105
106 ofile << offset << "done=True\n";
107 ofile << "\n\n";
108 if ((Chi::mpi.process_count>1) &&
109 (Chi::mpi.location_id != (Chi::mpi.process_count-1)))
110 {
111 ofile << offset << submod_name
112 << ".AddData" << ff << "(data" << ff << ")\n";
113 }
114
115
116 }
117
118 for (int ca=0; ca < custom_arrays_.size(); ca++)
119 {
120 int ff = ca + field_functions_.size();
121
122 if (Chi::mpi.process_count>1 and Chi::mpi.location_id!=0)
123 {
124 ofile
125 << "def AddData" << ff << "(data" << ff << "):\n";
126
127 offset = std::string(" ");
128 }
129
130 std::string op("= ");
131 if (Chi::mpi.location_id != 0) op = std::string("+= ");
132
133 for (int p=0; p < interpolation_points_.size(); p++)
134 {
135 ofile << offset << "data" << ff << "[" << p << ",0] = "
136 << interpolation_points_[p].x << "\n";
137 ofile << offset << "data" << ff << "[" << p << ",1] = "
138 << interpolation_points_[p].y << "\n";
139 ofile << offset << "data" << ff << "[" << p << ",2] = "
140 << interpolation_points_[p].z << "\n";
141
142 double d = delta_d_ * p;
143 double value = 0.0;
144
145 if (p < custom_arrays_[ca].size())
146 value = custom_arrays_[ca][p];
147
148 ofile << offset << "data" << ff << "[" << p << ",3] = "
149 << d << "\n";
150 ofile << offset << "data" << ff << "[" << p << ",4] " << op
151 << value << "\n";
152 }
153 ofile << offset << "done=True\n";
154 ofile << "\n\n";
155 if ((Chi::mpi.process_count>1) &&
156 (Chi::mpi.location_id != (Chi::mpi.process_count-1)))
157 {
158 ofile << offset << submod_name
159 << ".AddData" << ff << "(data" << ff << ")\n";
160 }
161 }
162
163
164 if (Chi::mpi.location_id == 0)
165 {
166 ofile << "plt.figure(1)\n";
167 for (int ff=0; ff < field_functions_.size(); ff++)
168 {
169 ofile << "plt.plot(data" << ff << "[:,3],data"
170 << ff << "[:,4]"
171 << ",label=\"" << field_functions_[ff]->TextName() << "\""
172 << ")\n";
173 }
174 for (int ca=0; ca < custom_arrays_.size(); ca++)
175 {
176 int ff = ca + field_functions_.size();
177 ofile << "plt.plot(data" << ff << "[:,3],data"
178 << ff << "[:,4]"
179 << ",label=\"CustomArray" << ca << "\""
180 << ")\n";
181 }
182 ofile << "plt.legend()\n"
183 "plt.grid(which='major')\n";
184 ofile << "plt.show()\n";
185 }
186
187
188 ofile.close();
189
190 Chi::log.Log()
191 << "Exported Python files for field func \""
192 << field_functions_[0]->TextName()
193 << "\" to base name \""
194 << base_name << "\" Successfully";
195
196
197}
static chi::ChiLog & log
Definition: chi_runtime.h:81
static chi::MPI_Info & mpi
Definition: chi_runtime.h:78
LogStream Log(LOG_LVL level=LOG_0)
Definition: chi_log.cc:35
const int & location_id
Current process rank.
Definition: mpi_info.h:26
std::vector< chi_physics::FieldFunctionGridBasedPtr > field_functions_
void ExportPython(std::string base_name) override
std::vector< chi_mesh::Vector3 > interpolation_points_
std::vector< std::vector< double > > custom_arrays_
std::vector< FieldFunctionContext > ff_contexts_