38
Produced using MATLAB® software. MATLAB function m-files for duct_flow_2D problem duct_flow_2D.m This is the main program file that you are to write. duct_flow_2D\duct_flow_2D.m PROJECT TITLE This program calculates the velocity profile of a Newtonian fluid driven by a pressure gradient through a horizontal, rectangular duct. AUTHOR YOUR NAME HERE PROGRAM SUMMARY This program calculates the velocity profile of the flow of a Newtonian fluid through a horizontal, rectangular duct of width L_x and height L_y. A pressure gradient in the z direction drives the flow and no-slip boundary conditions are applied at each wall. PROGRAM IMPLEMENTATION DESIGN NOTES Section 1. Discretization of the PDE using central finite differences The method of finite differences on a regular 2-D grid will be used to convert the partial differential equation to a set of linear algebraic equations. A non-uniform grid will be used with tighter grid spacing near the walls where the velocity gradient is highest. The velocity at the grid points along the wall will be retained as formal unknowns to simplify the notation when setting the values of the matrix elements that discretize the PDE. DESCRIPTION OF VARIABLES STRUCTURE system_param This data structure contains the parameters describing the physical system. .L_x REAL IN The width of the duct in the x-direction .L_y REAL IN The height of the duct in the y-direction .dp_dz REAL IN The pressure gradient in the z-direction driving the flow .viscosity REAL IN The viscosity of the fluid STRUCTURE sim_param .N_x

MATLAB function m-files for duct flow 2D problem

  • Upload
    others

  • View
    15

  • Download
    0

Embed Size (px)

Citation preview

Page 1: MATLAB function m-files for duct flow 2D problem

Produced using MATLAB® software.

MATLAB function m-files for duct_flow_2D problem

duct_flow_2D.m

This is the main program file that you are to write.

duct_flow_2D\duct_flow_2D.m

PROJECT TITLE

This program calculates the velocity profile of a Newtonian fluid driven by a pressure gradient through a horizontal, rectangular duct.

AUTHOR

YOUR NAME HERE

PROGRAM SUMMARY

This program calculates the velocity profile of the flow of a Newtonian fluid through a horizontal, rectangular duct of width L_x and height L_y. A pressure gradient in the z direction drives the flow and no-slip boundary conditions are applied at each wall.

PROGRAM IMPLEMENTATION DESIGN NOTES

Section 1. Discretization of the PDE using central finite differences The method of finite differences on a regular 2-D grid will be used to convert the partial differential equation to a set of linear algebraic equations. A non-uniform grid will be used with tighter grid spacing near the walls where the velocity gradient is highest. The velocity at the grid points along the wall will be retained as formal unknowns to simplify the notation when setting the values of the matrix elements that discretize the PDE.

DESCRIPTION OF VARIABLES

STRUCTURE system_paramThis data structure contains the parameters describing the physical system.

.L_xREAL INThe width of the duct in the x-direction.L_yREAL INThe height of the duct in the y-direction.dp_dzREAL INThe pressure gradient in the z-direction driving the flow.viscosityREAL INThe viscosity of the fluid

STRUCTURE sim_param

.N_x

Page 2: MATLAB function m-files for duct flow 2D problem

INT PIN

The number of grid points in the x-direction.

This should be an odd number.

.N_y

INT PIN

The number of grid points in the y-direction.

This should be an odd number.

.grid_scale_x

REAL PIN

The scaling factor controlling the degree to which the

grid points in the x-direction are placed more closely

near the walls. A value of 1 yields a uniform grid.

.grid_scale_y

REAL PIN

The scaling factor controlling the degree to which the

grid points in the y-direction are placed more

closely near the walls. A value of 1 yields a

uniform grid.

STRUCTURE grid_2D

.x_grid

REAL(N_x) PROG

The vector of grid points values in the x-direction

.y_grid

REAL(N_y) PROG

The vector of grid points in the y-direction

.num_pts

Page 3: MATLAB function m-files for duct flow 2D problem

INT PROG

This is set equal to the product of N_x and N_y, and is the

total number of points in the 2-D grid

.velocity_z

REAL(num_pts) OUT

This vector contains the z-direction velocity at each

grid point in the 2-D grid.

function iflag_main = duct_flow_2D();

This integer flag is set initially to zero to denote that the

program execution is not complete. A return value of 1 indicates

successful program completion, and a negative value indicates

that an error has been encountered.

iflag_main = 0;

We now call in a function read_input() that reads from the keyboard

the input parameters for the program.

PROCEDURE: read_input

PDL> Read in the system parameters : L_x, L_y, dp_dz, viscosity

PDL> Read in the parameters that control the finite

difference solution method :

N_x, N_y, grid_scale_x, grid_scale_y

ENDPROCEDURE

Next, we initialize the 2-D grid used to discretize the PDE.

PROCEDURE: set_grid_2D

PDL> Calculate the components of the x and y vector grid points

according to the scaling parameters

PDL> Calculate the total number of grid points in the

Page 4: MATLAB function m-files for duct flow 2D problem

2-D grid : num_pts

ENDPROCEDURE

PDL> Initialize the velocity_z vector

PROCEDURE: set_A_matrix

PDL> Given the grid point locations, calculate the A matrix that

discretizes the PDE at each interior point and that impose the

no-slip boundary conditions at each grid point on the walls.

ENDPROCEDURE

PROCEDURE: set_b_vector

PDL> Set the elements of the b vector that contains for each

interior point the momentum source term and that contains 0

values for the known velocities at each wall grid point

ENDPROCEDURE

PDL> Calculate the vector velocity_z containing the

velocity at each point in the 2-D grid

PROCEDURE: plot_2D_PDE

PDL> Make a plot of the velocity profile as a function of x and y

ENDPROCEDURE

DON'T WORRY ABOUT CALCULATING EIGENVALUES/EIGENVECTORS

save results to a binary output file.

save duct_flow_2D.mat;

Set iflag_main to signal successful completion.

iflag_main = 1;

return;

read_input.m

duct_flow_2D\read_input.m

Page 5: MATLAB function m-files for duct flow 2D problem

This function reads from the keyboard the user-input

values of the system and simulation parameters for the

2-D duct flow problem.

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

OUTPUT

======

STRUCTURE system_param

This data structure contains the parameters describing the

physical system.

.L_x

REAL

The width of the duct in the x-direction

.L_y

REAL

The height of the duct in the y-direction

.dp_dz

REAL

The pressure gradient in the z-direction driving the flow

.viscosity

REAL

Page 6: MATLAB function m-files for duct flow 2D problem

The viscosity of the fluid

STRUCTURE sim_param

.N_x

INT

The number of grid points in the x-direction.

This should be an odd number.

.N_y

INT

The number of grid points in the y-direction.

This should be an odd number.

.grid_scale_x

REAL

The scaling factor controlling the degree to which the

grid points in the x-direction are placed more closely

near the walls. A value of 1 yields a uniform grid.

.grid_scale_y

REAL

The scaling factor controlling the degree to which the

grid points in the y-direction are placed more

closely near the walls. A value of 1 yields a

uniform grid.

function [system_param,sim_param,iflag] = read_input();

iflag = 0;

PDL> Read from the keyboard the user-input values of the system

parameters: L_x, L_y, dp_dz, and viscosity. These are fields

Page 7: MATLAB function m-files for duct flow 2D problem

in the system_param data structure.

Read sys_param.L_x

prompt = 'Enter the width of the duct in the x-direction : ';

check_real = 1; check_sign = 1; check_int = 0;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

system_param.L_x = value;

Read sys_param.L_y

prompt = 'Enter the height of the duct in the y-direction : ';

check_real = 1; check_sign = 1; check_int = 0;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

system_param.L_y = value;

Read sys_param.dp_dz

prompt = 'Enter the pressure gradient in the +z direction : ';

check_real = 1; check_sign = 0; check_int = 0;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

system_param.dp_dz = value;

Read sys_param.viscosity

prompt = 'Enter the viscosity of the Newtonian fluid : ';

check_real = 1; check_sign = 1; check_int = 0;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

system_param.viscosity = value;

PDL> Read from the keyboard the user-input values of the

simulation parameters controlling the finite difference

discretization of the PDE. The fields of the simulation

parameter data structure are N_x, N_y, grid_scale_x, and

grid_scale_y. In either N_x or N_y are not odd, add 1 to the

Page 8: MATLAB function m-files for duct flow 2D problem

input value to make it so.

Read sim_param.N_x

prompt = 'Enter the number of grid points in the x-direction (odd #) : ';

check_real = 1; check_sign = 1; check_int = 1;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

if(mod(value,2)==0)

disp('Input value is even, adding 1');

value = value + 1;

end

sim_param.N_x = value;

Read sim_param.N_y

prompt = 'Enter the number of grid points in the y-direction (odd #) : ';

check_real = 1; check_sign = 1; check_int = 1;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

if(mod(value,2)==0)

disp('Input value is even, adding 1');

value = value + 1;

end

sim_param.N_y = value;

Read sim_param.grid_scale_x

prompt = 'Enter x-dir. grid scaling parameter (=1 for uniform grid) : ';

check_real = 1; check_sign = 1; check_int = 0;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

sim_param.grid_scale_x = value;

Read sim_param.grid_scale_y

prompt = 'Enter y-dir. grid scaling parameter (=1 for uniform grid) : ';

Page 9: MATLAB function m-files for duct flow 2D problem

check_real = 1; check_sign = 1; check_int = 0;

value = get_input_scalar(prompt,check_real,check_sign,check_int);

sim_param.grid_scale_y = value;

iflag = 1;

return;

set_grid_2D.m

duct_flow_2D\set_grid_2D.m

This function takes as input the data structure sim_param

containing the number of grid points in the x and y directions

and the scaling parameters that control the grid spacing.

The vectors of the x and y grid point values and the total

number of points are then calculated.

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

INPUT

=====

STRUCTURE system_param

This data structure contains the parameters describing the

Page 10: MATLAB function m-files for duct flow 2D problem

physical system.

.L_x

REAL IN

The width of the duct in the x-direction

.L_y

REAL IN

The height of the duct in the y-direction

STRUCTURE sim_param

.N_x

INT

The number of grid points in the x-direction.

This should be an odd number.

.N_y

INT

The number of grid points in the y-direction.

This should be an odd number.

.grid_scale_x

REAL

The scaling factor controlling the degree to which the

grid points in the x-direction are placed more closely

near the walls. A value of 1 yields a uniform grid.

.grid_scale_y

REAL

The scaling factor controlling the degree to which the

grid points in the y-direction are placed more

Page 11: MATLAB function m-files for duct flow 2D problem

closely near the walls. A value of 1 yields a

uniform grid.

OUTPUT

======

STRUCTURE grid_2D

.x_grid

REAL(N_x)

The vector of grid points values in the x-direction

.y_grid

REAL(N_y)

The vector of grid points in the y-direction

.num_pts

INT

This is set equal to the product of N_x and N_y, and is

the total number of points in the 2-D grid

function [grid_2D,iflag] = set_grid_2D(system_param,sim_param);

iflag = 0;

PDL> Calculate the total number of points in the 2-D grid as the

product of the number of grid points in the x direction and the

number of points along the y-axis

grid_2D.num_pts = sim_param.N_x*sim_param.N_y;

PROCEDURE: place_1D_grid

PDL> Place the x-axis grid points on the left hand side according

to a scaling factor that makes the spacing between successive

Page 12: MATLAB function m-files for duct flow 2D problem

grid points tighter near the wall at x=0.

ENDPROCEDURE

grid_2D.x_grid = linspace(0,0,sim_param.N_x)';

x_start = 0;

x_end = system_param.L_x/2;

num_end = (sim_param.N_x+1)/2;

alpha_x = 1 + (sim_param.grid_scale_x-1)/sim_param.N_x;

[grid_new,iflag_sub] = ...

place_1D_grid(x_start,x_end,num_end,alpha_x,grid_2D.x_grid);

grid_2D.x_grid = grid_new;

if(iflag_sub <= 0)

iflag = -1;

return;

end

PROCEDURE: fill_1D_grid

PDL> Complete the placement of the x-direction grid points

on the right hand side of the duct according to mirror symmetry.

[grid_new,iflag_sub] = ...

fill_1D_grid(num_end,grid_2D.x_grid);

grid_2D.x_grid = grid_new;

if(iflag_sub <= 0)

iflag = -2;

return;

end

PDL> Use the function place_1D_grid to place the grid points on the

y-axis on the lower half of the duct, placing the points more

Page 13: MATLAB function m-files for duct flow 2D problem

closely near the wall at y=0.

grid_2D.y_grid = linspace(0,0,sim_param.N_y)';

y_start = 0;

y_end = system_param.L_y/2;

num_end = (sim_param.N_y+1)/2;

alpha_y = 1 + (sim_param.grid_scale_y-1)/sim_param.N_y;

[grid_new,iflag_sub] = ...

place_1D_grid(y_start,y_end,num_end,alpha_y,grid_2D.y_grid);

grid_2D.y_grid = grid_new;

if(iflag_sub <= 0)

iflag = -3;

return;

end

PDL> Use the function fill_1D_grid to fill the remainder of the

y-axis points in the upper half of the duct according to mirror symmetry.

[grid_new,iflag_sub] = ...

fill_1D_grid(num_end,grid_2D.y_grid);

grid_2D.y_grid = grid_new;

if(iflag_sub <= 0)

iflag = -4;

return;

end

Finally, make a plot of the grid points.

figure;

hold on;

for i=1:sim_param.N_x

Page 14: MATLAB function m-files for duct flow 2D problem

for j=1:sim_param.N_y

plot(grid_2D.x_grid(i),grid_2D.y_grid(j),'.');

end

end

xlabel('x'); ylabel('y'); axis equal

iflag = 1;

return;

set_A_matrix.m

duct_flow_2D\set_A_matrix.m

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

This function sets the A matrix that discretizes the

Laplacian on a rectangular 2-D grid with non-uniform

spacing, employing non-uniform grid spacing.

INPUT

=====

STRUCTURE grid_2D

.x_grid

REAL(N_x)

Page 15: MATLAB function m-files for duct flow 2D problem

The vector of grid points values in the x-direction

.y_grid

REAL(N_y)

The vector of grid points in the y-direction

.num_pts

INT

This is set equal to the product of N_x and N_y, and is

the total number of points in the 2-D grid

OUTPUT

======

A

REAL(grid_2D.num_pts,grid_2D.num_pts)

The matrix that discretizes the second derivative

operator on the regular 2-D grid.

function [A,iflag] = set_A_matrix(grid_2D);

iflag = 0;

PDL> Allocate space in memory for A using the sparse matrix

format with an upper bound of 5*num_pts for the number of

non-zero elements.

nzA = 5*grid_2D.num_pts;

A = spalloc(grid_2D.num_pts,grid_2D.num_pts,nzA);

Extract the number of grid points along each axis.

N_x = length(grid_2D.x_grid);

N_y = length(grid_2D.y_grid);

Page 16: MATLAB function m-files for duct flow 2D problem

PDL> For every point along the x-axis, calculate for each

interior grid point with that x value and the neighbors in

the on either side in the x-direction, the contribution to

the elements of A from discretizing the second derivative

in the x-direction.

for i = 2:(N_x-1)

Fi = 0.5*(grid_2D.x_grid(i+1)-grid_2D.x_grid(i-1)) * ...

(grid_2D.x_grid(i+1)-grid_2D.x_grid(i)) * ...

(grid_2D.x_grid(i) - grid_2D.x_grid(i-1));

for j = 2:(N_y-1)

k = (i-1)*N_y + j;

A(k,k-N_y) = -(grid_2D.x_grid(i+1)-grid_2D.x_grid(i))/Fi;

A(k,k) = (grid_2D.x_grid(i+1)-grid_2D.x_grid(i-1))/Fi;

A(k,k+N_y) = -(grid_2D.x_grid(i)-grid_2D.x_grid(i-1))/Fi;

end

end

PDL> Repeat the calculation above for discretizing the second

derivative in the y-direction for each interior point.

for j = 2:(N_y-1)

Gj = 0.5*(grid_2D.y_grid(j+1)-grid_2D.y_grid(j-1)) * ...

(grid_2D.y_grid(j+1)-grid_2D.y_grid(j)) * ...

(grid_2D.y_grid(j) - grid_2D.y_grid(j-1));

for i = 2:(N_x-1)

k = (i-1)*N_y + j;

A(k,k-1) = -(grid_2D.y_grid(j+1)-grid_2D.y_grid(j))/Gj;

A(k,k) = A(k,k) + ...

Page 17: MATLAB function m-files for duct flow 2D problem

(grid_2D.y_grid(j+1)-grid_2D.y_grid(j-1))/Gj;

A(k,k+1) = -(grid_2D.y_grid(j)-grid_2D.y_grid(j-1))/Gj;

end

end

PDL> For each wall in turn, set the elements of A for each grid

point on the wall to be 1 to enforce the Diriclet boundary conditions.

B.C. # 1, north boundary

for i=1:N_x

j = N_y;

k = (i-1)*N_y + j;

A(k,k) = 1;

end

B.C. # 2, south boundary

for i=1:N_x

j = 1;

k = (i-1)*N_y + j;

A(k,k) = 1;

end

B.C. # 3, west boundary

for j=2:(N_y-1)

i = 1;

k = (i-1)*N_y + j;

A(k,k) = 1;

end

B.C. # 4, east boundary

for j=2:(N_y-1)

Page 18: MATLAB function m-files for duct flow 2D problem

i = N_x;

k = (i-1)*N_y + j;

A(k,k) = 1;

end

iflag = 1;

return;

set_b_vector.m

duct_flow_2D\set_b_vector.m

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

This function sets the right hand side vector for the 2-D

duct flow problem, where the components of the vector for

the grid points at the walls are set to 0 to impose the

no-slip Diriclet boundary conditions and the values at

the interior points are set to the momentum source term in

the equation of motion arising from the pressure gradient.

INPUT

=====

STRUCTURE system_param

Page 19: MATLAB function m-files for duct flow 2D problem

This data structure contains the parameters describing the

physical system.

.dp_dz

REAL

The pressure gradient in the z-direction driving the flow

.viscosity

REAL

The viscosity of the fluid

STRUCTURE grid_2D

.x_grid

REAL(N_x)

The vector of grid points values in the x-direction

.y_grid

REAL(N_y)

The vector of grid points in the y-direction

.num_pts

INT

This is set equal to the product of N_x and N_y, and is

the total number of points in the 2-D grid

OUTPUT

======

b_RHS

function [b_RHS,iflag] = set_b_vector(system_param,grid_2D);

iflag = 0;

Page 20: MATLAB function m-files for duct flow 2D problem

PDL> Initialize the b_RHS vector to contain all zeros

b_RHS = linspace(0,0,grid_2D.num_pts)';

PDL> For each interior grid point, set the value of b_RHS to be

the negative of the pressure gradient divided by the viscosity.

N_x = length(grid_2D.x_grid);

N_y = length(grid_2D.y_grid);

b_val = -system_param.dp_dz/system_param.viscosity;

for i=2:(N_x-1)

for j=2:(N_y-2)

k = (i-1)*N_y + j;

b_RHS(k) = b_val;

end

end

iflag = 1;

return;

plot_2D_PDE.m

duct_flow_2D\plot_2D_PDE.m

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

This function takes as input a column vector containing the

values of a field at each of the points in a 2-D rectangular

Page 21: MATLAB function m-files for duct flow 2D problem

grid and makes a contour plot.

INPUT

=====

field

REAL(num_pts)

The vector containing the field that is to be plotted on the 2D grid.

STRUCTURE grid_2D

.x_grid

REAL(N_x)

The vector of grid points values in the x-direction

.y_grid

REAL(N_y)

The vector of grid points in the y-direction

.num_pts

INT

This is set equal to the product of N_x and N_y, and is

the total number of points in the 2-D grid

title_name

CHARACTER STRING

The title of the plot.

function iflag = plot_2D_PDE(field,grid_2D,title_name);

iflag = 0;

Page 22: MATLAB function m-files for duct flow 2D problem

Create a new figure window.

figure;

PDL> From the grid point vectors in the x and y directions,

and the column vector containing the field values, convert

the data to the format expected by the plotting routine that

produces 2-D filled contour plots.

First, for 2D plotting, we use the meshgrid command

that returns. X_grid(j,i) = x(i), Y_grid(j,i) = y(j)

[X_grid,Y_grid]=meshgrid(grid_2D.x_grid,grid_2D.y_grid);

Extract from the grid vectors the number of points in

each axis.

N_x = length(grid_2D.x_grid);

N_y = length(grid_2D.y_grid);

We now store the field values in the 2D-grid

format expected by the 2D plotting routine.

Z_grid=zeros(N_y,N_x);

for i=1:N_x

for j=1:N_y

k = (i-1)*N_y + j;

Z_grid(j,i) = field(k);

end

end

PDL> Make a filled contour plot of the field with the

title name given as input.

contourf(X_grid,Y_grid,Z_grid,20);

axis equal;

Page 23: MATLAB function m-files for duct flow 2D problem

colorbar;

title(title_name); xlabel('x'); ylabel('y');

iflag = 1;

return;

place_1D_grid.m

duct_flow_2D\place_1D_grid.m

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

This function places grid points into an input/output

vector so that the distance between each successive

grid point is a factor alpha larger or smaller than

the previous grid interval. The input grid vector is

partially filled beginning from the first element, where

the grid starts and ends at given x values. The elements

num_end+1, num_end+2, ..., length(grid) are not changed

by the function.

INPUT

=====

x_start

Page 24: MATLAB function m-files for duct flow 2D problem

REAL

This is the x-value at which the grid will start at the

first element.

x_end

REAL

This is the x-value for the last point placed

into the grid vector

num_end

REAL

This is the component of grid that will contain the

last value x_end. If we only partially wish to fill

the grid vector (starting from the first element), this

number is smaller than the total length of the input

grid vector.

alpha

REAL

This is the scaling factor that controls how much larger

or smaller each successive grid interval is than the one

proceeding it. A value of 1 results in a uniform grid.

INPUT/OUTPUT

============

grid

Page 25: MATLAB function m-files for duct flow 2D problem

REAL(N_x)

the vector of the grid points in the x (or y) direction

function [grid_new,iflag] = ...

place_1D_grid(x_start,x_end,num_end,alpha,grid);

iflag = 0;

PDL> From the values of x_start, x_end, num_end, and the

scaling parameter alpha, calculate the value of grid(2)-grid(1).

if(alpha ~= 1)

dx = (x_end-x_start)*(alpha-1)/(alpha^(num_end-1)-1);

else

dx = (x_end-x_start)/(num_end-1);

end

PDL> Set grid(1) = x_start

grid(1) = x_start;

PDL> Set grid(2) = grid(1) + (grid(2)-grid(1))

grid(2) = grid(1) + dx;

PDL> Place the remainder of the grid points

according to the scaling law.

for i=3:num_end

dx = alpha*dx;

grid(i) = grid(i-1) + dx;

end

grid_new = grid;

iflag = 1;

return;

Page 26: MATLAB function m-files for duct flow 2D problem

fill_1D_grid.m

duct_flow_2D\fill_1D_grid.m

AUTHOR

======

Kenneth Beers

MIT ChE

9/25/2001

This function continues placing the points in a 1-D grid

according to mirror symmetry.

INPUT

=====

num_end

REAL

This is the location of the last value placed in the grid

vector by the function place_1D_grid.

INPUT/OUTPUT

============

grid

REAL(num_pts)

This grid contains the positions of each point in the x axis.

Page 27: MATLAB function m-files for duct flow 2D problem

function [grid_new,iflag] = fill_1D_grid(num_end,grid);

iflag = 0;

PDL> Set the grid counter variable count = 0

count = 0;

PDL> For each grid point to the right of num_end, place it according to mirror symmetry.

FOR i=num_end + 1, length(grid)

N_x = length(grid);

for i = (num_end+1):N_x

***PDL> Increment the integer count by 1

count = count + 1;

***PDL> Set grid(i) = grid(num_end) + (grid(num_end)-grid(num_end-count))

grid(i) = grid(num_end) + (grid(num_end)-grid(num_end-count));

PDL> ENDFOR

end

grid_new = grid;

iflag = 1;

return;

get_input_scalar.m

duct_flow_2D\get_input_scalar.m

function value = get_input_scalar(prompt, ...

check_real,check_sign,check_int);

This MATLAB m-file gets from the user an

input scalar value of the appropriate type.

It asks for input over and over again

Page 28: MATLAB function m-files for duct flow 2D problem

until a correctly typed input value

is entered.

Kenneth Beers

Massachusetts Institute of Technology

Department of Chemical Engineering

[email protected]

7/2/2001

Version as of 7/25/2001

function value = get_input_scalar(prompt, ...

check_real,check_sign,check_int);

func_name = 'get_input_scalar';

name = 'trial_value';

input_OK = 0;

while (input_OK ~= 1)

trial_value = input(prompt);

[iflag_assert,message] = ...

assert_scalar(0,trial_value, ...

name,func_name, ...

check_real,check_sign,check_int);

if(iflag_assert == 1)

input_OK = 1;

value = trial_value;

else

disp(message);

Page 29: MATLAB function m-files for duct flow 2D problem

end

end

return;

assert_scalar.m

duct_flow_2D\assert_scalar.m

function [iflag_assert,message] = assert_scalar( ...

i_error,value,name,func_name, ...

check_real,check_sign,check_int,i_error);

This m-file contains logical checks to assert than an

input value is a type of scalar number. This function is

passed the value and name of the variable, the name of

the function making the assertion, and four integer

flags that have the following usage :

i_error : controls what to do if test fails

if i_error is non-zero, then use error()

MATLAB command to stop execution, otherwise

just return the appropriate negative number.

if i_error > 1, then dump current state to

dump_error.mat before calling error().

check_real : check to examine whether input number is

real or not. See table after function header for set

values of these case flags

Page 30: MATLAB function m-files for duct flow 2D problem

check_real = i_real (make sure that input is real)

check_real = i_imag (make sure that input is

purely imaginary)

any other value of check_real (esp. 0) results

in no check

check_real

i_real = 1;

i_imag = -1;

check_sign : check to examine sign of input value

see table after function header for set values

of these case flags

check_sign = i_pos (make sure input is positive)

check_sign = i_nonneg (make sure input is non-negative)

check_sign = i_neg (make sure input is negative)

check_sign = i_nonpos (make sure input is non-positive)

check_sign = i_nonzero (make sure input is non-zero)

check_sign = i_zero (make sure input is zero)

any other value of check_sign (esp. 0)

results in no check

check_sign

i_pos = 1;

i_nonneg = 2;

i_neg = -1;

Page 31: MATLAB function m-files for duct flow 2D problem

i_nonpos = -2;

i_nonzero = 3;

i_zero = -3;

check_int : check to see if input is an integer

if = 1, then check to make sure input is an integer

any other value, perform no check

Kenneth Beers

Massachusetts Institute of Technology

Department of Chemical Engineering

[email protected]

7/2/2001

Version as of 7/21/2001

function [iflag_assert,message] = assert_scalar( ...

i_error,value,name,func_name, ...

check_real,check_sign,check_int);

iflag_assert = 0;

message = 'false';

First, set case values of check integer flags.

check_real

i_real = 1;

i_imag = -1;

check_sign

Page 32: MATLAB function m-files for duct flow 2D problem

i_pos = 1;

i_nonneg = 2;

i_neg = -1;

i_nonpos = -2;

i_nonzero = 3;

i_zero = -3;

iflag_assert = 0;

Check to make sure input is numerical and not a string.

if(~isnumeric(value))

message = [ func_name, ': ', ...

name, ' is not numeric'];

iflag_assert = -1;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

Check to see if it is a scalar.

if(max(size(value)) ~= 1)

message = [ func_name, ': ', ...

name, ' is not scalar'];

iflag_assert = -2;

Page 33: MATLAB function m-files for duct flow 2D problem

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

Then, check to see if it is real.

switch check_real;

case {i_real}

if(~isreal(value))

message = [ func_name, ': ', ...

name, ' is not real'];

iflag_assert = -3;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

case {i_imag}

Page 34: MATLAB function m-files for duct flow 2D problem

if(real(value))

message = [ func_name, ': ', ...

name, ' is not imaginary'];

iflag_assert = -3;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

end

Next, check sign.

switch check_sign;

case {i_pos}

if(value <= 0)

message = [ func_name, ': ', ...

name, ' is not positive'];

iflag_assert = -4;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

Page 35: MATLAB function m-files for duct flow 2D problem

else

return;

end

end

case {i_nonneg}

if(value < 0)

message = [ func_name, ': ', ...

name, ' is not non-negative'];

iflag_assert = -4;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

case {i_neg}

if(value >= 0)

message = [ func_name, ': ', ...

name, ' is not negative'];

iflag_assert = -4;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

Page 36: MATLAB function m-files for duct flow 2D problem

end

error(message);

else

return;

end

end

case {i_nonpos}

if(value > 0)

message = [ func_name, ': ', ...

name, ' is not non-positive'];

iflag_assert = -4;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

case {i_nonzero}

if(value == 0)

message = [ func_name, ': ', ...

name, ' is not non-zero'];

iflag_assert = -4;

if(i_error ~= 0)

Page 37: MATLAB function m-files for duct flow 2D problem

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

case {i_zero}

if(value ~= 0)

message = [ func_name, ': ', ...

name, ' is not zero'];

iflag_assert = -4;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

end

Finally, check to make sure it is an integer.

if(check_int == 1)

if(round(value) ~= value)

Page 38: MATLAB function m-files for duct flow 2D problem

message = [ func_name, ': ', ...

name, ' is not an integer'];

iflag_assert = -5;

if(i_error ~= 0)

if(i_error > 1)

save dump_error.mat;

end

error(message);

else

return;

end

end

end

set flag for succesful passing of all checks

iflag_assert = 1;

message = 'true';

return;