[Gmsh] Extracting the electric field data

Max Orok morok at mevex.com
Sun Sep 30 17:29:28 CEST 2018


Hi Mansour,

Typically I use a script to extract all of the data and put in a vector,
etc.
Attached is an example in C++.

Through the GUI, you can use the command
Export ->  Post-processing - Gmsh POS  [or] Post-processing - Generic TXT
[or the other post processing formats]

For this method, the output files will still have the element data, but
only to tie the values to the right elements.

[image: image.png]
Sincerely,
Max Orok

On Sun, Sep 30, 2018 at 10:11 AM mansour alawi <mansour.alawi at gmail.com>
wrote:

> Dear there,
>
> I have been using GMSH software for quite some time to analyse the outcome
> of brain electric stimulation (TMS) generated by Simnibs software. However,
> recently I'm facing difficulties in extracting the electric field data, in
> other words, I wish to have the results into numbers instead of the colour
> bar as shown in the attachment.
>
> I have tried the export functions. However, the data I get wasn't labelled
> to differentiate the brain structure from the electric field data.
>
> I would like to seek your help in this regard, what function I can use to
> have the electric field data and brain structure data separated and in a
> numerical form not colour bar form.
>
> --
>
> *Best regards *
> *Mansour Ayman Alawi *
>
> _______________________________________________
> gmsh mailing list
> gmsh at onelab.info
> http://onelab.info/mailman/listinfo/gmsh
>


-- 
Max Orok
Contractor
www.mevex.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://onelab.info/pipermail/gmsh/attachments/20180930/9a74c1b0/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 97165 bytes
Desc: not available
URL: <http://onelab.info/pipermail/gmsh/attachments/20180930/9a74c1b0/attachment-0001.png>
-------------- next part --------------
#include "gmsh.h"
#include <iostream>
#include <sstream>

// WARNING: this script only works for extracting scalar data,
// however, it could be modified for higher orders

// function to extract scalar data from the result vector
inline std::vector<double> unformat_data(std::vector<std::vector<double>> data) {
  std::vector<double> unformatted;
  for (auto dataIt = data.cbegin(); dataIt != data.cend(); ++dataIt){
    unformatted.emplace_back((*dataIt)[0]);
  }
  return unformatted;
}

int main(void) {
  gmsh::initialize();
  // open results mesh file
  puts("opening file...");
  gmsh::open("results.msh");
  // get the numbers of all of the views
  std::vector<int> viewTags;
  gmsh::view::getTags(viewTags);
  // gmsh data format variables
  std::string dataLabel;
  std::vector<std::string> allLabels;
  std::vector<int> eltNumbers;
  int numComponents;
  double viewTime;
  std::vector<std::vector<double>> data;
  std::vector<std::vector<double>> viewData;

  puts("extracting data");
  // loop over views and extract element data
  // assume that view tags are 0-N
  for (auto view : viewTags){
    printf("reading view %d\n", view);
    gmsh::view::getModelData(view, 0, dataLabel, eltNumbers, data, viewTime, numComponents);
    // get the view name in a slightly convoluted way
    std::stringstream viewOpt;
    viewOpt << "View[" << view << "].Name";
    std::string viewName;
    gmsh::option::getString(viewOpt.str(), viewName);
    allLabels.emplace_back(viewName);
    // save the (scalar) data we care about
    viewData.emplace_back(unformat_data(data));
  }

  // print the first few data for each view
  for (auto i = 0; i < allLabels.size(); ++i) {
    printf("%s\n", allLabels[i].c_str());
    for (auto j = 0; j < 10 && j < viewData[i].size(); ++j) {
      printf("%f\n", viewData[i][j]);
    }
  }

  gmsh::finalize();
}


More information about the gmsh mailing list