Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

paraview functions #490

Merged
merged 5 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
move class paraview methods to functions
  • Loading branch information
akeeste committed Jan 6, 2021
commit 20922682f46fec958fa755d87d6f1c613f499068
10 changes: 5 additions & 5 deletions source/functions/paraViewVisualization.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
NewTimeParaview(:,1) = simu.StartTimeParaview:simu.dtParaview:simu.EndTimeParaview;
PositionBodyParav = interp1(TimeBodyParav,PositionBodyParav,NewTimeParaview);
TimeBodyParav = NewTimeParaview-simu.StartTimeParaview;
body(ii).write_paraview_vtp(TimeBodyParav, PositionBodyParav, bodyname, simu.simMechanicsFile, datestr(simu.simulationDate), output.bodies(ii).cellPressures_hydrostatic, output.bodies(ii).cellPressures_waveNonLinear, output.bodies(ii).cellPressures_waveLinear, simu.pathParaviewVideo,vtkbodiesii);
write_paraview_body(body(ii), TimeBodyParav, PositionBodyParav, bodyname, simu.simMechanicsFile, datestr(simu.simulationDate), output.bodies(ii).cellPressures_hydrostatic, output.bodies(ii).cellPressures_waveNonLinear, output.bodies(ii).cellPressures_waveLinear, simu.pathParaviewVideo,vtkbodiesii);
bodies{vtkbodiesii} = bodyname;
fprintf(fid,[bodyname '\n']);
fprintf(fid,[num2str(body(vtkbodiesii).viz.color) '\n']);
Expand All @@ -61,15 +61,15 @@
end; clear ii
fclose(fid);
% waves
mkdir([simu.pathParaviewVideo filesep 'waves'])
waves.write_paraview_vtp(NewTimeParaview, waves.viz.numPointsX, waves.viz.numPointsY, simu.domainSize, simu.simMechanicsFile, datestr(simu.simulationDate),moordynFlag,simu.pathParaviewVideo,TimeBodyParav, simu.g); % mooring
mkdir([simu.pathParaviewVideo filesep 'waves'])
write_paraview_wave(waves, NewTimeParaview, waves.viz.numPointsX, waves.viz.numPointsY, simu.domainSize, simu.simMechanicsFile, datestr(simu.simulationDate),moordynFlag,simu.pathParaviewVideo,TimeBodyParav, simu.g); % mooring
% mooring
if moordynFlag == 1
mkdir([simu.pathParaviewVideo filesep 'mooring'])
mooring.write_paraview_vtp(output.moorDyn, simu.simMechanicsFile, output.moorDyn.Lines.Time, datestr(simu.simulationDate), mooring.moorDynLines, mooring.moorDynNodes,simu.pathParaviewVideo,TimeBodyParav,NewTimeParaview)
write_paraview_mooring(output.moorDyn, simu.simMechanicsFile, output.moorDyn.Lines.Time, datestr(simu.simulationDate), mooring.moorDynLines, mooring.moorDynNodes,simu.pathParaviewVideo,TimeBodyParav,NewTimeParaview)
end
% all
output.write_paraview(bodies, TimeBodyParav, simu.simMechanicsFile, datestr(simu.simulationDate), waves.type, moordynFlag, simu.pathParaviewVideo);
write_paraview_response(bodies, TimeBodyParav, simu.simMechanicsFile, datestr(simu.simulationDate), waves.type, moordynFlag, simu.pathParaviewVideo);
clear bodies fid filename
end
clear body*_hspressure_out body*_wavenonlinearpressure_out body*_wavelinearpressure_out hspressure wpressurenl wpressurel cellareas bodyname
125 changes: 125 additions & 0 deletions source/functions/write_paraview_body.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
function write_paraview_body(body, t, pos_all, bodyname, model, simdate, hspressure,wavenonlinearpressure,wavelinearpressure,pathParaviewVideo,vtkbodiesii)
% Method to write ``vtp`` Paraview visualization files for the bodyClass.
% Executed by paraViewVisualization.m when simu.paraview=1 in the
% wecSimInputFile.m
%
% Parameters
% ------------
% body : bodyClass
% Instance of the bodyClass that is being written to Paraview files.
% t : float
% asdf
% pos_all :
% asdf
% bodyname :
% asdf
% model :
% asdf
% simdate :
% asdf
% hspressure :
% asdf
% wavenonlinearpressure :
% asdf
% wavelinearpressure :
% asdf
% pathParaviewVideo :
% asdf
% vtkbodiesii :
% asdf
%
numVertex = body.bodyGeometry.numVertex;
numFace = body.bodyGeometry.numFace;
vertex = body.bodyGeometry.vertex;
face = body.bodyGeometry.face;
cellareas = body.bodyGeometry.area;
for it = 1:length(t)
% calculate new position
pos = pos_all(it,:);
vertex_mod = body.rotateXYZ(vertex,[1 0 0],pos(4));
vertex_mod = body.rotateXYZ(vertex_mod,[0 1 0],pos(5));
vertex_mod = body.rotateXYZ(vertex_mod,[0 0 1],pos(6));
vertex_mod = body.offsetXYZ(vertex_mod,pos(1:3));
% open file
filename = [pathParaviewVideo, filesep 'body' num2str(vtkbodiesii) '_' bodyname filesep bodyname '_' num2str(it) '.vtp'];
fid = fopen(filename, 'w');
% write header
fprintf(fid, '<?xml version="1.0"?>\n');
fprintf(fid, ['<!-- WEC-Sim Visualization using ParaView -->\n']);
fprintf(fid, ['<!-- model: ' model ' - ran on ' simdate ' -->\n']);
fprintf(fid, ['<!-- body: ' bodyname ' -->\n']);
fprintf(fid, ['<!-- time: ' num2str(t(it)) ' -->\n']);
fprintf(fid, '<VTKFile type="PolyData" version="0.1">\n');
fprintf(fid, ' <PolyData>\n');
% write body info
fprintf(fid,[' <Piece NumberOfPoints="' num2str(numVertex) '" NumberOfPolys="' num2str(numFace) '">\n']);
% write points
fprintf(fid,' <Points>\n');
fprintf(fid,' <DataArray type="Float32" NumberOfComponents="3" format="ascii">\n');
for ii = 1:numVertex
fprintf(fid, ' %5.5f %5.5f %5.5f\n', vertex_mod(ii,:));
end
clear vertex_mod
fprintf(fid,' </DataArray>\n');
fprintf(fid,' </Points>\n');
% write tirangles connectivity
fprintf(fid,' <Polys>\n');
fprintf(fid,' <DataArray type="Int32" Name="connectivity" format="ascii">\n');
for ii = 1:numFace
fprintf(fid, ' %i %i %i\n', face(ii,:)-1);
end
fprintf(fid,' </DataArray>\n');
fprintf(fid,' <DataArray type="Int32" Name="offsets" format="ascii">\n');
fprintf(fid, ' ');
for ii = 1:numFace
n = ii * 3;
fprintf(fid, ' %i', n);
end
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
fprintf(fid, ' </Polys>\n');
% write cell data
fprintf(fid,' <CellData>\n');
% Cell Areas
fprintf(fid,' <DataArray type="Float32" Name="Cell Area" NumberOfComponents="1" format="ascii">\n');
for ii = 1:numFace
fprintf(fid, ' %i', cellareas(ii));
end
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
% Hydrostatic Pressure
if ~isempty(hspressure)
fprintf(fid,' <DataArray type="Float32" Name="Hydrostatic Pressure" NumberOfComponents="1" format="ascii">\n');
for ii = 1:numFace
fprintf(fid, ' %i', hspressure(it,ii));
end
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
end
% Non-Linear Froude-Krylov Wave Pressure
if ~isempty(wavenonlinearpressure)
fprintf(fid,' <DataArray type="Float32" Name="Wave Pressure NonLinear" NumberOfComponents="1" format="ascii">\n');
for ii = 1:numFace
fprintf(fid, ' %i', wavenonlinearpressure(it,ii));
end
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
end
% Linear Froude-Krylov Wave Pressure
if ~isempty(wavelinearpressure)
fprintf(fid,' <DataArray type="Float32" Name="Wave Pressure Linear" NumberOfComponents="1" format="ascii">\n');
for ii = 1:numFace
fprintf(fid, ' %i', wavelinearpressure(it,ii));
end
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
end
fprintf(fid,' </CellData>\n');
% end file
fprintf(fid, ' </Piece>\n');
fprintf(fid, ' </PolyData>\n');
fprintf(fid, '</VTKFile>');
% close file
fclose(fid);
end
end
102 changes: 102 additions & 0 deletions source/functions/write_paraview_mooring.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
function write_paraview_mooring(moorDyn, model,t,simdate,nline,nnode,pathParaviewVideo,TimeBodyParav,NewTimeParaview)
% Method to write ``vtp`` Paraview visualization files for the
% mooringClass. Executed by paraViewVisualization.m when simu.paraview=1
% in the wecSimInputFile.m
%
% Parameters
% ------------
% moorDyn : obj
% The moorDyn object
% model : string
% The simMechanics ``.slx`` file
% t : float vector
% Moordyn time vector
% simdate : string
% Date string
% nline : integer
% Number of mooring lines
% nnode : integer
% Number of nodes on mooring lines
% pathParaviewVideo : directory
% Directory the Paraview files were saved
% TimeBodyParav : float vector
% Paraview time vector
% simu.g : float
% Gravitational acceleration from simulationClass
%

nsegment = nnode -1;
for iline = 1:nline
for inode = 0:nnode(iline)-1
moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'px']) = interp1(t,moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'px'])(:),NewTimeParaview);
moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'py']) = interp1(t,moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'py'])(:),NewTimeParaview);
moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'pz']) = interp1(t,moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'pz'])(:),NewTimeParaview);
end
end
for iline = 1:nline
for isegment = 0:nsegment(iline)-1
moorDyn.(['Line' num2str(iline)]).(['Seg' num2str(isegment) 'Te']) = interp1(t,moorDyn.(['Line' num2str(iline)]).(['Seg' num2str(isegment) 'Te'])(:),NewTimeParaview);
end
end
for it = 1:length(TimeBodyParav)
% open file
filename = [pathParaviewVideo, filesep 'mooring' filesep 'mooring_' num2str(it) '.vtp'];
fid = fopen(filename, 'w');
% write header
fprintf(fid, '<?xml version="1.0"?>\n');
fprintf(fid, ['<!-- WEC-Sim Visualization using ParaView -->\n']);
fprintf(fid, ['<!-- model: ' model ' - ran on ' simdate ' -->\n']);
fprintf(fid, ['<!-- mooring: MoorDyn -->\n']);
fprintf(fid, ['<!-- time: ' num2str(TimeBodyParav(it)) ' -->\n']);
fprintf(fid, '<VTKFile type="PolyData" version="0.1">\n');
fprintf(fid, ' <PolyData>\n');
% write line info
for iline = 1:nline
fprintf(fid,[' <Piece NumberOfPoints="' num2str(nnode(iline)) '" NumberOfLines="' num2str(nsegment(iline)) '">\n']);
% write points
fprintf(fid,' <Points>\n');
fprintf(fid,' <DataArray type="Float32" NumberOfComponents="3" format="ascii">\n');
for inode = 0:nnode(iline)-1
pt = [moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'px'])(it), moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'py'])(it), moorDyn.(['Line' num2str(iline)]).(['Node' num2str(inode) 'pz'])(it)];
fprintf(fid, ' %5.5f %5.5f %5.5f\n', pt);
end; clear pt inode
fprintf(fid,' </DataArray>\n');
fprintf(fid,' </Points>\n');
% write lines connectivity
fprintf(fid,' <Lines>\n');
fprintf(fid,' <DataArray type="Int32" Name="connectivity" format="ascii">\n');
count = 0;
for isegment = 1:nsegment(iline)
fprintf(fid, [' ' num2str(count) ' ' num2str(count+1) '\n']);
count = count +1;
end; clear count isegment
fprintf(fid,' </DataArray>\n');
fprintf(fid,' <DataArray type="Int32" Name="offsets" format="ascii">\n');
fprintf(fid, ' ');
for isegment = 1:nsegment(iline)
n = 2*isegment;
fprintf(fid, ' %i', n);
end; clear n isegment
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
fprintf(fid, ' </Lines>\n');
% write cell data
fprintf(fid,' <CellData>\n');
% Segment Tension
fprintf(fid,' <DataArray type="Float32" Name="Segment Tension" NumberOfComponents="1" format="ascii">\n');
for isegment = 0:nsegment(iline)-1
fprintf(fid, ' %i', moorDyn.(['Line' num2str(iline)]).(['Seg' num2str(isegment) 'Te'])(it));
end;
fprintf(fid, '\n');
fprintf(fid,' </DataArray>\n');
fprintf(fid,' </CellData>\n');
% end file
fprintf(fid, ' </Piece>\n');
end;
% close file
fprintf(fid, ' </PolyData>\n');
fprintf(fid, '</VTKFile>');
fclose(fid);
end;
clear it iline nline nnode nsegment model t
end
63 changes: 63 additions & 0 deletions source/functions/write_paraview_response.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
function write_paraview_response(bodies, t, model, simdate, wavetype, mooring, pathParaviewVideo)
% Method to write ``vtp`` Paraview visualization files for the
% responseClass. Executed by paraViewVisualization.m when
% simu.paraview=1 in the wecSimInputFile.m
%
% Parameters
% ------------
% var : type
% description
%
%
%
%
%
This method is executed by specifying
% ``simu.paraview=1`` in the ``wecSimInputFile.m``.

% set fileseperator to fs
if strcmp(filesep, '\')
fs = '\\';
else
fs = filesep;
end
% open file
fid = fopen([pathParaviewVideo, fs model(1:end-4) '.pvd'], 'w');
% write header
fprintf(fid, '<?xml version="1.0"?>\n');
fprintf(fid, ['<!-- WEC-Sim Visualization using ParaView -->\n']);
fprintf(fid, ['<!-- model: ' model ' - ran on ' simdate ' -->\n']);
fprintf(fid, ['<!-- wave: ' wavetype ' -->\n']);
fprintf(fid, ['<!-- bodies: ' num2str(length(bodies)) ' -->\n']);
for ii = 1:length(bodies)
fprintf(fid, ['<!-- body ' num2str(ii) ': ' bodies{ii} ' -->\n']);
end
fprintf(fid, '<VTKFile type="Collection" version="0.1">\n');
fprintf(fid, ' <Collection>\n');
% write wave
fprintf(fid,[' <!-- Wave: ' wavetype ' -->\n']);
for jj = 1:length(t)
fprintf(fid, [' <DataSet timestep="' num2str(t(jj)) '" group="" part="" \n']);
fprintf(fid, [' file="waves' fs 'waves_' num2str(jj) '.vtp"/>\n']);
end
% write bodies
for ii = 1:length(bodies)
fprintf(fid,[' <!-- Body' num2str(ii) ': ' bodies{ii} ' -->\n']);
for jj = 1:length(t)
fprintf(fid, [' <DataSet timestep="' num2str(t(jj)) '" group="" part="" \n']);
fprintf(fid, [' file="body' num2str(ii) '_' bodies{ii} fs bodies{ii} '_' num2str(jj) '.vtp"/>\n']);
end
end
% write mooring
if mooring==1
fprintf(fid,[' <!-- Mooring: MoorDyn -->\n']);
for jj = 1:length(t)
fprintf(fid, [' <DataSet timestep="' num2str(t(jj)) '" group="" part="" \n']);
fprintf(fid, [' file="mooring' fs 'mooring_' num2str(jj) '.vtp"/>\n']);
end
end
% close file
fprintf(fid, ' </Collection>\n');
fprintf(fid, '</VTKFile>');
fclose(fid);
end
Loading