CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   OpenFOAM Post-Processing (https://www.cfd-online.com/Forums/openfoam-post-processing/)
-   -   Some scripts for GNU/Octave or Matlab Post-processing (https://www.cfd-online.com/Forums/openfoam-post-processing/127684-some-scripts-gnu-octave-matlab-post-processing.html)

avigrod December 17, 2013 12:14

Some scripts for GNU/Octave or Matlab Post-processing
 
In case you want to read xyz coordinates of cell centers, and simulated scalars or vectors, here I paste some easy function which could help someone, they are used as follows:

phase = readEscalarOF('alpha1',510)
[xCell,yCell,zCell] = readCellCentersOF(510)
V=readVectorOF('U',510)


Code:

function seriededatos = readEscalarOF (variable,tiempo)
% seriededatos = leeescalarOF (variable,tiempo)
% lee tiempo/variable o tiempo/variable.gz
 
  if !ischar(tiempo), tiempo = num2str(tiempo); end
  fichero = [tiempo '/' variable]

descomprimido = length( dir(fichero));

if !descomprimido,
  ficherogz = [fichero '.gz']
  check = length( dir(ficherogz));
  system(['gunzip ' ficherogz]);
else
  check = 1;
end

if check,
  FID = fopen(fichero,'r');

  for i=1:20,
    fgetl(FID);
  end

  num_celdas = fscanf(FID,'%d',1);
  fgetl(FID);fgetl(FID);

  seriededatos = fscanf(FID,'%f\n',num_celdas);

  check = fgetl(FID);

  if !strcmp(check,')'),
    display('\nposible error tamanho variable!!\n')
  end

  fclose(FID);

  if !descomprimido,
    system(['gzip ' fichero]);
  end
else
  fprintf(1,'\nfichero no encontrado\n\n')
end

Code:

function seriededatos = readVectorOF (variable,tiempo)
% seriededatos = leeescalarOF (variable,tiempo)
% lee tiempo/variable o tiempo/variable.gz
 
  if !ischar(tiempo), tiempo = num2str(tiempo); end
  fichero = [tiempo '/' variable]

descomprimido = length( dir(fichero));

if !descomprimido,
  ficherogz = [fichero '.gz']
  check = length( dir(ficherogz));
  system(['gunzip ' ficherogz]);
else
  check = 1;
end

if check,
  FID = fopen(fichero,'r');

  for i=1:20,
    fgetl(FID);
  end

  num_celdas = fscanf(FID,'%d',1);
  fgetl(FID);fgetl(FID);

  datos = fscanf(FID,'(%f %f %f)\n',3*num_celdas);
  seriededatos = [datos(1:3:end) datos(2:3:end) datos(3:3:end)];
  check = fgetl(FID);

  if !strcmp(check,')'),
    display('\nposible error tamanho variable!!\n')
  end

  fclose(FID);

  if !descomprimido,
    system(['gzip ' fichero]);
  end
else
  fprintf(1,'\nfichero no encontrado\n\n')
end

Code:

function [xCell, yCell, zCell] = readCellCentersOF (tiempo)
% seriededatos = leeescalarOF (tiempo)
% lee tiempo/ccx o tiempo/ccx.gz ccy y ccz
 
   
if !ischar(tiempo), tiempo = num2str(tiempo); end

 
if  !length( dir([tiempo '/ccx*'])),
  system(['writeCellCentres -time ' tiempo]);
end

xCell= readEscalarOF('ccx',tiempo);
yCell= readEscalarOF('ccy',tiempo);
zCell= readEscalarOF('ccz',tiempo);

%seriededatos =[x y z];



All times are GMT -4. The time now is 07:50.