Download presentation
Presentation is loading. Please wait.
Published byMartina Cox Modified over 9 years ago
1
1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal 14300 Pulau Pinang Week 6
2
2 Functions and Subroutines Writing a code with only a main function is efficient or less efficient? Efficiency can be measured more than one way. Efficiency in terms of program management and hence debugging Can also be measured in terms of how much data is being passed from one part of the program to another.
3
3 Functions versus subroutines It will be messy if you include all your algorithm in the main Use subroutines or functions This add additional cost (and perhaps errors) due to information passing of variables and identifiers Need to declare functions and subroutines in header files
4
4 Example Solving a fluid flow over an airfoil In one main function In a main function with many subfunctions or subroutines Pseudo-code presentation
5
5 1 Main function int main() { initializations { initializations ….. ….. geometry and grid modelings geometry and grid modelings …. …. mathematical and physical models mathematical and physical models …… …… loop to solve the problem for pressure and velocities loop to solve the problem for pressure and velocities …….. …….. plot the velocity and pressure fields plot the velocity and pressure fields …….. …….. return 0; return 0; }
6
6 Problems with 1 Main functions Main program becomes messy Difficult to debug The program is very ‘rigid’ since any changes to part of the program requires going through the whole main program Difficult to extend the code to the purposes
7
7 How to overcome? Use ‘divide and conquer’ approach in managing the main programs into smaller subprograms Use either functions or subroutines
8
8 Main function + subroutines Global declarations int main() { subroutines for initializations { subroutines for initializations subroutines for geometry and grid modelings subroutines for geometry and grid modelings subroutines for mathematical and physical models subroutines for mathematical and physical models subroutines to solve the problem for pressure &velocities subroutines to solve the problem for pressure &velocities subroutines for plot the velocity and pressure fields subroutines for plot the velocity and pressure fields return 0; return 0; }
9
9 Discussions on how the program and subprograms work The algorithm for each subroutine lies outside main Requires the declaration of the subroutines either in the same main file or another file which can be read and compiled together with the main file (similar to using a C++ library) Example subroutine for geometric model: geometric_model (input parameters, grid size, coordinates, boundary conditions….. ) geometric_model (input parameters, grid size, coordinates, boundary conditions….. ) Each subroutine requires information as input to perform
10
10 The advantages with using subroutines The whole program is more manageable in terms of program developments and debugging The program is now flexible to changes, i.e. for example if another geometry is used, no need to change main function, just change the subroutine function The subroutines can be used with other main functions (compatibility with other code) However, there is a disadvantage of using subroutines…
11
11 Kinetic Energy Program Using Function #include #include using namespace std; double Compute_KE(double u, double v) { double KE=0.0; KE= u*u + v*v; return KE; } int main() { int n; int n; cout << "Enter number of particles: " << endl; cout << "Enter number of particles: " << endl; cin >>n; cin >>n; double KE[n]; double u[n]; double v[n]; double KE[n]; double u[n]; double v[n]; for (int i=0; i<n; i++) for (int i=0; i<n; i++) { cout << "Enter the u velocity for particles: " << endl; cout << "Enter the u velocity for particles: " << endl; cin >> u[i]; cin >> u[i]; cout << "Enter the v velocity for particles: " << endl; cout << "Enter the v velocity for particles: " << endl; cin >> v[i]; cin >> v[i]; KE[i]= Compute_KE(u[i],v[i]); KE[i]= Compute_KE(u[i],v[i]); cout << "KE of particle " << i+1 << " is " << KE[i] << endl; cout << "KE of particle " << i+1 << " is " << KE[i] << endl; } getch(); getch(); return 0; } // end of program body
12
12 Kinetic Energy Program Using Subroutine #include #include using namespace std; //Declaration of a subroutine to be used in Main void Determine_KE(int n, double u[], double v[], double KE[], double TKE) { for (int i=0; i<n; i++) for (int i=0; i<n; i++) { KE[i]= u[i]*u[i] + v[i]*v[i]; KE[i]= u[i]*u[i] + v[i]*v[i]; TKE += KE[i]; TKE += KE[i]; } cout << "TKE is " << TKE << endl; cout << "TKE is " << TKE << endl; } int main() { int n; double TKE; double TKE; cout << "Enter number of particles: " << endl; cout << "Enter number of particles: " << endl; cin >>n; cin >>n; double KE[n]; double u[n]; double v[n]; double KE[n]; double u[n]; double v[n]; for (int i=0; i<n; i++) for (int i=0; i<n; i++) { cout << "Enter the u velocity for particles: " << endl; cout << "Enter the u velocity for particles: " << endl; cin >>u[i]; cin >>u[i]; cout << "Enter the v velocity for particles: " << endl; cout << "Enter the v velocity for particles: " << endl; cin >> v[i]; cin >> v[i]; } Determine_KE(n,u,v,KE, TKE); Determine_KE(n,u,v,KE, TKE); return 0; }
13
13 Tutorial 1 Using subroutines write a program to perform a numerical integration of f(x)=x^2*sin(x)*exp(x^2) over x=[0,Pi] having a choice of f(x)=x^2*sin(x)*exp(x^2) over x=[0,Pi] having a choice of (i) rectangular rule (i) rectangular rule (ii) the Simpson’s rule (take home) (ii) the Simpson’s rule (take home) Divide the domain into N subsections, where N=5,10,20,40. Compare your results. Divide the domain into N subsections, where N=5,10,20,40. Compare your results.
14
14 Plot of point data? The computer produces point data which are difficult to analyze Need to plot these point data to clearly view the results You can create your own plotting functions but it is easier to just ‘dump’ the data into a professional plotting software like Excel, Tecplot and even Matlab. The key to ‘dump’ out data is using the fstream library
15
15 #include #include int main() int main() { fstream data_name fstream data_name data_name.open(“C:/Data/test.dat",ios::out); data_name.open(“C:/Data/test.dat",ios::out); data_name << "VARIABLES = X, Y” << endl; data_name << "VARIABLES = X, Y” << endl; data_name << "ZONE T=T" << No <<", I=" << (M) << ", J=" << N << ", F=POINT" << endl; data_name << "ZONE T=T" << No <<", I=" << (M) << ", J=" << N << ", F=POINT" << endl; data_name.precision(12); data_name.precision(12); for (int j=1; j<N+1; j++) for (int j=1; j<N+1; j++) { for (int i=1; i<M+1; i++) { for (int i=1; i<M+1; i++) { data_name.width(20); data_name << X[i][j];} { data_name.width(20); data_name << X[i][j];} } return 0; return 0; }
16
16 Tutorial 2 Please refer to Homework 3 in website
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.