Upload
clarissa-merritt
View
223
Download
0
Tags:
Embed Size (px)
Citation preview
Post-Processing Output with MATLAB
Claudia Fricke
Institute of Petroleum Engineering, Heriot Watt University
Overview
• How to create MATLAB Output with CSMP?
• How to use the CSMP Output with MATLAB?
Why MATLAB – not VTK?
Advantages Disadvantages• easy to manipulate data• analyse and compare data from
various calculations by subsequent editing
• automated batch processing• export data in high quality• lots and lots of plot options• possibility to call MATLAB from
C code – lots of other interfaces
• LOTS of possibilities
• sometimes extensive preparations
• so far 3D not implemented in CSMP!
General CSMP++ Workflow
1. Load libraries
2. Generate SuperGroup (from mesh) and set properties, boundary and start values
3. Set up FE and FV algorithms
4. Output initial values
5. Main calculation loop over different time steps
6. Final outputCSMP or MATLAB Code: essential commands in red or green
Include Header-File
1. Load libraries#include "CSP_MatlabInterface.h" *)
2. Generate supergroup (from mesh) and set properties, boundary and start values
3. …
*) in ...\CSMP\source_code\interfaces
CSP_MatlabInterface.h
void write2DMatlabFile(SuperGroup<csp_float,2>& sg, const char* name,const char* variable,long step );
void write2DMatlabFile(SuperGroup<csp_float,2>& sg,const char* groupname,const char* name,const char* variable,long step );
void extractAndWrite1DVariableProfileAlongX( ... );void extractAndWrite1DVariableProfileAlongY( ... );
void writeSavedTimeStepsFile( long step, const char* name="saved_time_steps.txt" );
name of supergroupname of output filesCSMP output variablevalue of CSMP variable to file
save current time step in file
extract 1D data from 2D
current time step
restriction to sub-groupcurrent time stepdefault output filename
Export Initial Values…
3. Set up FE and FV algorithms
4. Output initial valueswrite2DMatlabFile (
supergroup, name of output file,CSMP variable,time step := start time )
writeSavedTimeStepsFile (time step := start time ,"filename" );
5. Main calculation loop over different time steps
…
Export Values for each Time Step
…
5. Main calculation loop over different time stepswhile ( global_time < max_time ) {
...//calculate time step FE / time step FV...write2DMatlabFile(
supergroup, name of output file, CSMP variable,time step := current time )
writeSavedTimeStepsFile( ... );
//increment global_time ... }
…
Export Final Values
...
5. Main calculation loop over different time steps
6. Final outputwrite2DMatlabFile(
supergroup, name of output file,CSMP variable,time step := final time )
writeSavedTimeStepsFile( ... );
Example
CSMP
Finite Element Mesh
m Finite Elements(here m = 8)1
2
6
5
3
4
7
8
n Nodes(here n = 9)
12
6
5
3
4
78
9
Generated Output Files
1
12
3
1
x-y-coordinatesof nodes
one value for each nodeone file for each time step
time steps in extra file
m rows n rows n rows
t files
t rows
Part 2
And now?
How to use the CSMP Output with MATLAB?
How to read data into MATLAB?
variable = load (file name)• returns matrix variable • each matrix column represents a column from the former data table• each matrix row represents one row from the former data table
Command load
n x 2 matrix
variable (:,1)returns 1st column of matrix
n rowsm rows m x 3 matrix
How to plot in 1D?
1D Command plot( nodes on x axis,calculated y-values,plot options)
Plot Options:• multiple plots• plot title• legend• axes markings• change colours• add coordinates grid• size of text• ...
How to plot in 2D?
2D Commands
surface plot (for triangular data)
trisurf(mx3 matrix of triangular
elements,n x-values of nodes,n y-values of nodes,calculated z-values,options)
Mesh Plot trimesh ( matrix of triangular elements,x coordinates of nodes,y coordinates of nodes,options)
one plot command per time step for animation
How to save plot as image file?
print( resolution, file type, file name );
Stack of Images Animation (e.g with Movie Maker)
compose file name (loop counter j)
['concentration', num2str(j),'.jpg']
Command print
Exampleprint( '-r200',
'-djpeg', 'concentration.jpg' );
2D Animation Example% start valuestime_steps = load ('time_steps.txt'); % 1 columnj = 1; %loop counterlast_time_step = length(time_steps);
% load meshno = load ( 'nodes.txt' );el = load ( 'elements.txt' );
% loopwhile j <= last_time_step
u = load ( ['concentration', num2str(j),'.txt'] );trisurf ( el, no(:,1), no(:,2), u, possible options)view ( 2 ) % top view... % more plot options
% save single jpg'sprint( '-r200', '-djpeg', ['concentration', num2str(j),'.jpg'] );
j = j + 1 ; % time step incrementend
% see a plot of just the triangles withtrimesh ( t, no(:,1), no(:,2) )
% start valuestime_steps = load ('time_steps.txt'); % 1 columnj = 1; %loop counterlast_time_step = length(time_steps);% load meshno = load ( 'nodes.txt' );el = load ( 'elements.txt' );
% loopwhile j <= last_time_step
u = load ( ['concentration', num2str(j),'.txt'] );trisurf ( el, no(:,1), no(:,2), u, possible options)view ( 2 ) % top view... % more plot options
% save single jpg'sprint( '-r200', '-djpeg', ['concentration', num2str(j),'.jpg'] );
j = j + 1 ; % time step incrementend% see a plot of just the triangles withtrimesh ( t, no(:,1), no(:,2) )
Output
MATLAB
Change Plot Options withBatch Function
1. Create basic plot
2. Change various plot options with Property Editor
3. Generate m-Code Function
4. Call this function and pass in new datafrom other m-File in main loop
• in detail see Youtube video“MATLAB Plot: Interactively Creating Plots in MATLAB”
http://www.youtube.com/watch?v=GJVvBqR8Mws
Useful Links to MATLAB
Always try Help Manual of MATLAB = easy
MATLAB “homepage”http://www.mathworks.com
…/products/featured/videos/…/academia/student_center/tutorials/launchpad.htmlIntroduction, Tutorials, Documentation, Examples, etc.
Some useful tutorialshttp://www.youtube.com/user/MATLAB
Octave (free alternative, mostly compatible with MATLAB) http://www.octave.org/
1D Output
Extract data from 2D
void extractAndWrite1DVariableProfileAlongY(... );
void extractAndWrite1DVariableProfileAlongX( SuperGroup<csp_float,2>& sg, csp_float distance, const char* name, const char* variable,long step );
1D Post-Processing Plot
• 1-dimensional advection & diffusion equation
• compare different time stepping schemes
• plot 4 results in one figure with MATLAB
extractAndWrite1DVariableProfileAlongX(..)
1D Animation Example% start valuesobs_function = 'fluid_pressure';time_steps = load ('time_steps.txt'); % 1 columnstep = time_steps(2) - time_steps(1);max_time = time_steps(length(time_steps));t = time_steps(1);
while t <= max_time % read CSMP data u = load ( [obs_function, num2str(t),'.txt'] ); %2 columns x = u(:,1); % 1st column p_x_t_CSMP = u(:,2); % 2nd column % calculate analytical solution p_x_t_ana = ...
%plot figure(1) plot( x, p_x_t_ana, x, p_x_t_CSMP)
... % more plot options
t = t + step;end
MATLAB Terms
• Environment:– Command Window– Workspace– Current Directory– Command History
• Programming– m-code– m-file– script vs. function– plot, figure, surface plot, ...