Computational Science I - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Computational Science I

Description:

Exercise, download Makefile and Make.opts ... The idea is that one can keep a set of Make. ... www.eng.hawaii.edu/Tutor/Make/ courtesy of Engineering at the ... – PowerPoint PPT presentation

Number of Views:73
Avg rating:3.0/5.0
Slides: 23
Provided by: timwar1
Category:

less

Transcript and Presenter's Notes

Title: Computational Science I


1
Computational Science I
  • Lecture 9
  • CAAM 420, Fall 2004
  • Instructor Tim Warburton

2
Last Class
  • In the last class I set Class Exercise K.
  • This was an exercise to give you an idea of how
    some software development is done.
  • I designed an ivec struct and a set of functions
    which form an interface to the ivec (i.e.
    controlling access and functionality of the ivec)
  • Your task was to implement the design.
  • Next I will demonstrate a reasonable
    implementation.

3
Class Exercise K solved
  • Download ivec.h from the SourceCode directory.
  • Create your own ivec.c file which contains the
    implementations of each of the prototyped
    functions.0

4
Class Exercise K solved cont
  • In a separate file mymain.c implement the
    following (where only functions in ivec.c are
    allowed to access the contents of an ivec)
  • create an ivec named ivecA of length 10
  • set the entries of the array to be the 3 times
    table
  • change the length of ivecA to 20
  • print out the entries

5
ivecconstructor
  • A constructor is a function which allows the user
    to create an instance of a struct with contents
    initialized.

6
ivecnewlength
  • Next, I specified the prototype for a function
    which changes the length of the int vector
    represented by an ivec.
  • Notice how this function does not return anything
    it only modifies the contents of the ivecA
    struct (by pointer).

7
ivecgetentry
  • The next prototype was a function which returns
    the specified entry in an ivec.
  • This function does not change anything in the
    ivec.
  • If every other function uses this function to
    read the data inside the actual array, then we
    could easily change the way data is stored and
    only have to change this function (and the next
    function).
  • For instance we could introduce a permutation
    array which determines where the ith entry
    actually is read from in the array.
  • This is one of the most important reasons to
    program in an object oriented way (more advanced
    features later in the course).

8
ivecsetentry
  • We need a similar function which actually changes
    the data at a specified entry in the vector
  • Again this allows us to control access to the
    data.
  • This function and ivecgetentry are intimately
    related..

9
Comparing ivecsetentry and ivecgetentry
10
Finally ivecprint
  • Useful for debugging and general IO I designed
    the interface for a print statement for the ivec
  • For consistency, I should have used ivecgetentry
    to access the contents of the array. (i.e I
    should not have used the iptr reference). Why?

11
More Interface Issues
  • Technically I should also have required a
    function which returns the length of the vector
    in the ivec and used this in any other function.

12
The Calling Program
  • Spot the details

13
The Calling Program
Using ivec.h for user .h files
Should have used a functionto find length
ofvector
14
Re Interface Team Projects
  • Notice, once the struct and interface is designed
    each of the functions can be created
    independently (i.e. all parties involved can work
    on implementing the functionality).
  • From now on the design/coding sequence should
    be
  • design ( then code ) struct
  • design interface ( then write C prototypes)
  • design ( then code ) interface functions
  • (4) test each function.
  • However, during the coding phase it may become
    apparent that
  • original struct and/or interface is insufficient
    for the functionality to
  • be completed. In this case the group working on
    the project should
  • cease development until this is resolved. This is
    important!, and
  • why it is paramount to think about the entity you
    are trying to
  • encapsulate in the struct and interface before
    starting to code.

15
Some math Functions
  • There is a large range of math functions in C
    which are prototyped in math.h and compiled into
    a library of objects called libm.a
  • Some common one-argument functions include
  • fabs
  • sin,cos,tan
  • asin,acos,atan
  • sinh, cosh, tanh
  • asinh, acosh, atanh
  • sqrt, exp, log, log10
  • they take one double as an argument and return a
    double value.
  • I looked in /usr/include/math.h (common place to
    find math.h) and some more obscure functions
    available are
  • ceil, floor
  • erf, erfc
  • j0, j1, jn
  • y0, y1, yn
  • Some two-argument functions include
  • atan2, pow, fmod
  • they take two doubles as arguments and return a
    double value

16
  • sin(x) sine of x
  • cos(x) cosine of x
  • tan(x) tan of x
  • asin(x) arcsine of x, result between -pi/2
    and pi/2
  • acos(x) arccosine of x, result between 0 and
    pi
  • atan(x) arctan of x, result between -pi/2
    and pi/2
  • atan2(y,x) arctan of (y/x), result between -pi
    and pi
  • sinh(x) hyperbolic sine of x
  • cosh(x) hyperbolic cosine of x
  • tanh(x) hyperbolic tan of x
  • exp(x) exponential function
  • log(x) natural logarithm
  • log10(x) logarithm to base 10
  • pow(x,y) x to the power of y
  • sqrt(x) the square root of x
  • ceil(x) ceiling the smallest integer not
    less than x
  • floor(x) floor the largest integer not
    greater than x
  • fabs(x) absolute value of x
  • fmod(x,y) returns the floating-point remainder of
    x/y, with the sign of x

17
Indenting your C code
  • You can use emacs to nicely indent your C code.
  • Move your text cursor to the beginning of your
    code with ctrl-x then
  • Press control-space
  • Move to the end of the text with ctrl-x then
  • Type escape then x
  • Next type indent-region
  • You should see your code nicely indented
    automatically.

If you are a vi or vim user you should find the
equivalent functionality. All submitted homework
code must be indented in the emacs format.
18
Makefile
  • In Exercise K we started to use more than one .c
    file and a .h file. The compilation now took
    three steps.
  • In a general code project there may be hundreds
    of such files, each requiring a compiling.
  • However, it may not be necessary to recompile
    every .c file each time you change a piece of
    code.
  • Ignoring these issues of inter-file dependency
    for the moment I have posted a reasonable
    Makefile and options file (Make.opts) on the web
    page.
  • This is a barebones Makefile which can be used
    for your projects.
  • To compile with this Makefile, it is as simple as
    invoking the command make in the directory
    containing the Makefile.

19
Typical Makefile
  • For your immediate purposes you can edit the list
    of OBJS to be the .o object files you wish to be
    included in your compile.
  • Exercise, download Makefile and Make.opts
  • i.e. edit Makefile to use your list of objects
    (change OBJS list)
  • Finally invoke make.

20
Why Make.opts
  • I put machine/OS dependent definitions in
    Make.opts
  • The idea is that one can keep a set of Make.
    files each one containing OS specific
    definitions.
  • e.g. different systems have different compiles.
  • In this case I chose the gnu compilers

21
make in action
  • I invoke make to process the Makefile

Invoke make
Check what happened
Invoke make clean
And the objectsare removed
22
If time permits..
  • There is an excellent tutorial athttp//www.eng.
    hawaii.edu/Tutor/Make/
  • courtesy of Engineering at the University of
    Hawaii.
  • I will point towards interesting parts allowing
    you to interpret the Makefile and Make.opts I set
    up.
  • If we are out of time, go to this web page and
    read the basics.
Write a Comment
User Comments (0)
About PowerShow.com