Chi-Tech
chi_ffinter_slice_exportpython.cc
Go to the documentation of this file.
1#include "chi_ffinter_slice.h"
2
4
5#include "chi_runtime.h"
6#include "chi_mpi.h"
7#include "chi_log.h"
8
9#include <fstream>
10
11/***/
13{
14 std::ofstream ofile;
15
16 std::string fileName = base_name;
17 fileName = fileName + std::to_string(Chi::mpi.location_id);
18 fileName = fileName + std::string(".py");
19 ofile.open(fileName);
20
21 ofile
22 << "import numpy as np\n"
23 "import matplotlib.pyplot as plt\n"
24 "import matplotlib.cm as cm\n"
25 "from matplotlib.collections import PatchCollection\n"
26 "from matplotlib.patches import Polygon\n"
27 "import matplotlib.colors as colors\n"
28 << "\n"
29 << "class Datapoint:\n"
30 " def __init__(self,x,y,value):\n"
31 " self.x = x\n"
32 " self.y = y\n"
33 " self.value = value\n\n";
34
35 ofile
36 << "class CellData:\n"
37 " def __init__(self):\n"
38 " self.data_points=[]\n"
39 " self.xy = []\n"
40 " self.c = []\n\n";
41
42 std::string offset;
43 if (Chi::mpi.location_id == 0)
44 {
45 std::string submod_name = base_name;
46 submod_name = submod_name + std::to_string(Chi::mpi.location_id+1);
47
48 if (Chi::mpi.process_count>1)
49 {
50 ofile << "import " << submod_name << "\n\n";
51 }
52
53 ofile
54 << "class BaseDataClass:\n"
55 << " def __init__(self):\n"
56 << " data_object = []\n"
57 << " self.data_object = data_object\n";
58
59 offset = std::string(" ");
60 }
61 else if (Chi::mpi.process_count>1)
62 {
63
64 if (Chi::mpi.location_id != (Chi::mpi.process_count-1))
65 {
66 std::string submod_name = base_name;
67 submod_name = submod_name + std::to_string(Chi::mpi.location_id+1);
68
69 ofile << "import " << submod_name << "\n\n";
70 }
71
72 ofile
73 << "def AddData(data_object):\n";
74
75 offset = std::string(" ");
76 }
77
78 size_t num_cells = cell_intersections_.size();
79 for (int c=0; c<num_cells; c++)
80 {
81 double x = 0.0;
82 double y = 0.0;
83 double v = 0.0;
84
85 ofile
86 << offset << "new_cell_data = CellData()\n";
87
88 size_t num_points = cell_intersections_[c].intersections.size();
89 ofile
90 << offset << "new_cell_data.xy = np.zeros(("
91 << std::to_string(num_points) << ",2))\n"
92 << offset << "new_cell_data.c = np.zeros("
93 << std::to_string(num_points) << ")\n";
94 for (int p=0; p<num_points; p++)
95 {
96 x = cell_intersections_[c].intersections[p].point2d.x;
97 y = cell_intersections_[c].intersections[p].point2d.y;
98 v = cell_intersections_[c].intersections[p].point_value;
99
100 ofile
101 << offset
102 << "new_cell_data.xy[" << std::to_string(p)
103 << ",0] = " << std::to_string(x) << "\n"
104 << offset
105 << "new_cell_data.xy[" << std::to_string(p)
106 << ",1] = " << std::to_string(y) << "\n"
107 << offset
108 << "new_cell_data.c[" << std::to_string(p)
109 << "] = " << std::to_string(v) << "\n";
110 }
111 v = cell_intersections_[c].cell_avg_value;
112 ofile
113 << offset
114 << "new_cell_data.avg = " << std::to_string(v) << "\n";
115
116
117 ofile
118 << offset << "data_object.append(new_cell_data)\n";
119 }
120
121 if (Chi::mpi.location_id != (Chi::mpi.process_count-1))
122 {
123 std::string submod_name = base_name;
124 submod_name = submod_name + std::to_string(Chi::mpi.location_id+1);
125
126 ofile
127 << offset << "data_object = "
128 << submod_name << ".AddData(data_object)\n\n";
129 }
130 if (Chi::mpi.location_id>0)
131 {
132 ofile
133 << offset << "return data_object\n";
134 }
135
136 if (Chi::mpi.location_id==0)
137 {
138 ofile
139 << "data = BaseDataClass()\n"
140 << "print(len(data.data_object))\n\n"
141 << "N = len(data.data_object)\n"
142 "\n"
143 "maxavg = 0.0\n"
144 "maxc = 0.0\n"
145 "xycount = 0\n"
146 "for c in range(0,N):\n"
147 " for i in range(0,np.size(data.data_object[c].c)):\n"
148 " xycount += 1\n"
149 " if data.data_object[c].avg>maxavg:\n"
150 " maxavg = data.data_object[c].avg\n"
151 " if data.data_object[c].c[i]>maxc:\n"
152 " maxc = data.data_object[c].c[i]\n"
153 " \n"
154 "xmax = -9999999.0\n"
155 "xmin = 9999999.0\n"
156 "ymax = -9999999.0\n"
157 "ymin = 9999999.0\n"
158 "zmax = -9999999.0\n"
159 "zmin = 9999999.0\n"
160 "x = np.zeros(xycount)\n"
161 "y = np.zeros(xycount)\n"
162 "z = np.zeros(xycount)\n"
163 "xycount = -1\n"
164 "for c in range(0,N):\n"
165 " for i in range(0,np.size(data.data_object[c].c)):\n"
166 " xycount += 1\n"
167 " x[xycount] = data.data_object[c].xy[i,0]\n"
168 " y[xycount] = data.data_object[c].xy[i,1]\n"
169 " z[xycount] = data.data_object[c].c[i]\n"
170 "\n"
171 " if x[xycount]>xmax: xmax = x[xycount]\n"
172 " if x[xycount]<xmin: xmin = x[xycount]\n"
173 " if y[xycount]>ymax: ymax = y[xycount]\n"
174 " if y[xycount]<ymin: ymin = y[xycount]\n"
175 " if z[xycount]>zmax: zmax = z[xycount]\n"
176 " if z[xycount]<zmin: zmin = z[xycount]\n"
177 "\n"
178 "print(\"phi_max=%g phi_min=%g\" %(zmax,zmin))\n"
179 "\n"
180 "fig,ax = plt.subplots(1)\n"
181 "cmapjet = plt.get_cmap('jet')\n"
182 "cNorm = colors.Normalize(vmin=0,vmax=maxavg)\n"
183 "scalarMap = cm.ScalarMappable(norm=cNorm, cmap=cmapjet)\n"
184 "\n"
185 "cb_scale = np.linspace(zmin,zmax*1.00001, 124, endpoint=True)\n"
186 "\n"
187 "cntr1 = plt.tricontourf(x,y,z,cb_scale,cmap=cmapjet)\n"
188 "\n"
189 "for c in range(0,N):\n"
190 " col = scalarMap.to_rgba(data.data_object[c].avg)\n"
191 " poly = Polygon(data.data_object[c].xy,\n"
192 " closed=True,linestyle='-',fill=False)\n"
193 " patch = []\n"
194 " patch.append(poly)\n"
195 " coll = PatchCollection(patch)\n"
196 " coll.set_facecolor([0,0,0,0])\n"
197 " coll.set_edgecolor([0,0,0,1])\n"
198 " coll.set_linewidth(0.3)\n"
199 "\n"
200 " ax.add_collection(coll)\n"
201 "\n"
202 "cb = fig.colorbar(cntr1,ax=ax)\n"
203 "cb.set_ticks(np.linspace(zmin,zmax, 11, endpoint=True))\n"
204 "ax.set_xlim([xmin,xmax])\n"
205 "ax.set_ylim([ymin,ymax])\n"
206 "plt.show()\n";
207 }
208
209 ofile.close();
210
211 Chi::log.Log()
212 << "Exported Python files for field func \""
213 << field_functions_[0]->TextName()
214 << "\" to base name \""
215 << base_name << "\" Successfully";
216
217
218}
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
std::vector< chi_physics::FieldFunctionGridBasedPtr > field_functions_
std::vector< FFICellIntersection > cell_intersections_
void ExportPython(std::string base_name) override