examples/postprocess_serial.cpp

Go to the documentation of this file.
00001 #include "unistd.h"
00002 #include <cmath>
00003 #include <iostream> 
00004 #include "Teuchos_ParameterList.hpp"
00005 #include "rlog/rlog.h"
00006 #include "rlog/StdioNode.h"
00007 #include "rlog/RLogChannel.h"
00008 #include "myrlog.h"
00009 #include "pbe.h"
00010 #include "h5_tools.h"
00011 #include "tetmesh/tetmesh.h"
00012 #include "tetmesh/vector3.h"
00013 #include "tetmeshbuilder.h"
00014 #include "netgenreader.h"
00015 #include "femaxmesh.h"
00016 #include "nedelecmesh.h"
00017 #include "lagrangemesh.h"
00018 
00019 using namespace rlog;
00020 using namespace std;
00021 
00022 //
00023 // Global variables
00024 //
00025 mesh::TetMesh* tmesh(0);
00026 FemaxMesh* femax_mesh(0);
00027 int num_eigenpairs(0);
00028 colarray::ColumnVector<double> lambda;
00029 colarray::Matrix<double> Q;
00030 
00031 void log_mem_footprint(string msg) {
00032     size_t local_size = get_mem_footprint();
00033     if (msg != "")
00034         msg = " (" + msg + ")";
00035     rDebug("Memory footprint%s: %s",
00036            msg.c_str(),
00037            rlog::bytes2str(local_size).c_str());
00038 }
00039 
00040 void load_mesh(const Teuchos::ParameterList& params_) {
00041     // log_mem_footprint(comm_, "initial");
00042     rInfo("Read mesh file...");
00043     if (access(params_.get<string>("mesh_file_name").c_str(), R_OK)) {
00044         rError("Unable to access mesh file \"%s\".", params_.get<string>("mesh_file_name").c_str());
00045         exit(1);
00046     }
00047 
00048     ifstream istr(params_.get<string>("mesh_file_name").c_str());
00049 
00050     log_mem_footprint("before loading mesh");
00051     TetMeshBuilder* builder = new TetMeshBuilder();
00052     NetgenReader* reader = new NetgenReader(&istr, builder);
00053     reader->read();
00054     delete reader;
00055     tmesh = builder->get_mesh();
00056     delete builder;
00057     tmesh->log_mesh_info();
00058     log_mem_footprint("after loading tetmesh");
00059 
00060     // create FE mesh data structures
00061     int sym_plane_config = params_.get<int>("sym_plane_config");
00062     rInfo("Calculate DOF mappings (sym_plane_config=%d)...", sym_plane_config);
00063     femax_mesh = new FemaxMesh(*tmesh, params_.get<int>("element_order"), sym_plane_config);
00064     log_mem_footprint("after calc dof mappings");
00065 }
00066 
00067 double q_factor(int k) {
00068     colarray::ColumnVector<double> q(Q, k);
00069     return femax_mesh->get_nedelec_mesh().q_factor(lambda[k], q._v);
00070 }
00071 
00072 void eigenmode_summary() {
00073     // pi
00074     const double pi = ::acos(-1.0);
00075     // speed of light [m / s]
00076     const double cLight = 2.99792458e8;
00077     
00078     ostringstream buf;
00079     buf << "Eigenvalue summary:" << endl;
00080     buf << "nr.   norm. frequency       freq. [MHz]            Q";
00081     for (int k = 0; k < num_eigenpairs; ++ k) {
00082         double omega = ::sqrt(lambda[k]) * cLight;
00083         double f = omega / 2.0 / pi;
00084         buf << endl << right
00085             << setw(3) << k+1 << " "
00086             << setw(17) << setprecision(8) << fixed << lambda[k] << " "
00087             << setw(17) << setprecision(8) << fixed << f / 1e6 << " "
00088             << setw(12) << setprecision(3) << fixed << q_factor(k);
00089     }
00090     rInfo(buf.str().c_str());
00091 }
00092 
00093 void vtk_export_eigenfields(string file) {
00094     const int precision = 4;
00095     NedelecMesh* nedelec_mesh(&femax_mesh->get_nedelec_mesh());
00096     int nofPoints = tmesh->get_nof_points();
00097     int nofTets = tmesh->get_nof_tets();
00098 
00099     mesh::Vector3 coord;
00100     mesh::Vector3 value;
00101     mesh::Tet* curTet;
00102     std::ofstream of;
00103 
00104     of.open(file.c_str());
00105     assert(of.is_open());
00106 
00107     of.precision(precision);
00108     of << "# vtk DataFile Version 2.0" << endl;                      // first line of a vtk file
00109     of << "generated using VtkExport::export_eigenfields" << endl;   // header
00110     of << "ASCII" << endl << endl;                                   // file format
00111     of << "DATASET UNSTRUCTURED_GRID" << endl;
00112     of << "POINTS " << nofPoints << " float" << endl;
00113     of << scientific;
00114     for (int i = 0; i < nofPoints; ++ i) {
00115         coord   = tmesh->get_point(i)->get_coord();
00116         of << coord.x << " " << coord.y << " " << coord.z << endl;
00117     }
00118     of << endl;
00119     of << "CELLS " <<  nofTets << " " << 5*nofTets << endl;
00120 
00121     // for each cell and write IDs of its points (preceded by number of points
00122     // in that cell)
00123     for (int i = 0; i < nofTets; i ++) {
00124         curTet = tmesh->get_tet(i);
00125         of << 4;    //assume that it is always a tetrahedron, i.e. with 4 corners
00126         for (int j = 0; j < 4; j ++) {
00127             of << " " << curTet->get_corner_id(j);
00128         }
00129         of << endl;
00130     }
00131     of << endl;
00132 
00133     // cell-type always = 10 as it is a tetrahedron
00134     of << "CELL_TYPES " << nofTets << endl;
00135     for (int i = 0; i < nofTets; i++) {
00136         of << 10 << endl;
00137     }
00138     of << endl;
00139 
00140     of << "POINT_DATA " << nofPoints << endl << endl;
00141 
00142     // E-field data is written into the vtk-file
00143     for (int k = 0; k < num_eigenpairs; k ++) {
00144         colarray::ColumnVector<double> q(Q, k);
00145 
00146         of << "VECTORS el_field_mode_" << k << " float" << endl;
00147         //get the value for each point
00148         for (int j =0; j < nofPoints; j++) {
00149             value = nedelec_mesh->eval(tmesh->get_point(j)->get_coord(), q);
00150             of << value.x << " " << value.y << " " << value.z << endl;
00151         }
00152         of << endl;
00153     } // for k
00154 
00155     // B-field data is written into the vtk-file
00156     for (int k = 0; k < num_eigenpairs; k ++) {
00157         colarray::ColumnVector<double> q(Q, k);
00158 
00159         of << "VECTORS mag_field_mode_" << k << " float" << endl;
00160         //get the value for each point
00161         for (int j = 0; j < nofPoints; j++) {
00162             value = nedelec_mesh->eval_curl(tmesh->get_point(j)->get_coord(), q);
00163             of << value.x << " " << value.y << " " << value.z << endl;
00164         }
00165         of << endl;
00166     } // for k
00167 
00168 }
00169 
00173 int main(int argc, char *argv[])
00174 {
00175     Teuchos::ParameterList params;
00176     RLogInit(argc, argv);
00177     MyStdioNode stdLog;
00178     stdLog.subscribeTo(RLOG_CHANNEL("error"));
00179     stdLog.subscribeTo(RLOG_CHANNEL("warning"));
00180     stdLog.subscribeTo(RLOG_CHANNEL("info"));
00181     stdLog.subscribeTo(RLOG_CHANNEL("debug"));
00182 
00183     const int nof_pbe_timers = 150;
00184     pbe_init("femaxx", nof_pbe_timers, 0, NULL);
00185     pbe_start(50, "Whole program");
00186 
00187     // Read parameter list
00188     h5_read_param_list("eigenmodes.h5", params);
00189     params.print(cout);
00190     
00191     // Read tetmesh
00192     pbe_start(52, "Mesh generation");
00193     load_mesh(params);
00194     pbe_stop(52);
00195     
00196     pbe_start(56, "Read eigenmodes");
00197     h5_read_eigenmodes("eigenmodes.h5", Q, lambda);
00198     pbe_stop(56);
00199     num_eigenpairs = lambda._n;
00200     eigenmode_summary();
00201 
00202     rInfo("Export modes mesh and eigenmodes to VTK file...");
00203     pbe_start(131, "Construction of octree");
00204     tmesh->construct_octree();
00205     pbe_stop(131);
00206     pbe_start(134, "VTK export");
00207     vtk_export_eigenfields("out.vtk");
00208     pbe_stop(134);
00209     
00210     delete femax_mesh;
00211     delete tmesh;
00212 
00213     pbe_stop(50);
00214     pbe_dump();
00215     pbe_finalize(0);
00216 
00217     return 0;
00218 }

Generated on Fri Oct 26 13:35:11 2007 for FEMAXX (Finite Element Maxwell Eigensolver) by  doxygen 1.4.7