W02 D01 LAB Computational Methods

download W02 D01 LAB Computational Methods

of 36

Transcript of W02 D01 LAB Computational Methods

  • 7/27/2019 W02 D01 LAB Computational Methods

    1/36

    (C++)PROGRAMMING

    IN PHYSICS

  • 7/27/2019 W02 D01 LAB Computational Methods

    2/36

    Material(s) we need

    A text editor (gedit, or kate)

    Terminal or konsole

  • 7/27/2019 W02 D01 LAB Computational Methods

    3/36

    Say Hi to simple Hello World Program

    /*

    Program: helloworld.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PMNOTE: program simply prints out hello world

    */

    #include

    int main() {

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    4/36

    Say Hi to simple Hello World Program

    /*

    Program: helloworld.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PMNOTE: program simply prints out hello world

    */

    #include

    int main() {

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    5/36

    Build and Run the Hello World Program

    Build Command:

    g++ helloworld.cc o helloworld

    Run Command:./helloworld

    g++ = C++ compilerhelloworld.cc = source file

    helloworld = executable program

  • 7/27/2019 W02 D01 LAB Computational Methods

    6/36

    Build and Run the Hello World Program

    Build Command:

    g++ helloworld.cc o helloworld

    Run Command:./helloworld

    g++ = C++ compilerhelloworld.cc = source file

    helloworld = executable program

    -o option createsexecutable file

  • 7/27/2019 W02 D01 LAB Computational Methods

    7/36

    What is the difference between:

    int main() {}

    int main(int argc, char** argv) {}

    If a program is going to ignore

    command line arguments, choose the

    first line.Choose the second line if the

    program needs to process line

    arguments.

  • 7/27/2019 W02 D01 LAB Computational Methods

    8/36

    What is the difference between:

    int main(int argc, char** argv) {}

    argc = argument countargv = argument vector

    Similar toint main(int num_args, char** arg_strings) {}

  • 7/27/2019 W02 D01 LAB Computational Methods

    9/36

    What is the difference between:

    #include

    int main(int argc, char** argv) {

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    10/36

    What is the difference between:

    #include

    int main(int argc, char** argv) {

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    11/36

    /*Program: masstoenergy.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PMNOTE: converts mass to energy, E = m*c^2

    */

    #include

    int main(int argc, char** argv) {

    float m, E; // variable m = mass, E = energy

    const float c = 3e8; // c = speed of light in m/s

    m = atof(argv[1]); // atof converts string into float

    E = m*c*c;std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    12/36

    /*Program: masstoenergy.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PMNOTE: converts mass to energy, E = m*c^2

    */

    #include

    int main(int argc, char** argv) {

    float m, E; // variable m = mass, E = energy

    const float c = 3e8; // c = speed of light in m/s

    m = atof(argv[1]); // atof converts string into float

    E = m*c*c;std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    13/36

    Header, Source, & main file

    Suppose we want a program thatcalculates the dot product of twovectorsAand B.

    Needed Files:

    MAIN: VectorDotProduct_main.cc

    HEADER: CalculateDotProduct.hh

    SOURCE: CalculateDotProduct.cc

    Article: Header and Includes: Why and How,

    Link: www.cplusplus.com/forum/articles/10627/

    http://www.cplusplus.com/forum/articles/10627/http://www.cplusplus.com/forum/articles/10627/
  • 7/27/2019 W02 D01 LAB Computational Methods

    14/36

    Header, Source, & main file

    Suppose we want a program thatcalculates the dot product of twovectorsAand B.

    Needed Files:

    MAIN: VectorDotProduct_main.cc

    HEADER: CalculateDotProduct.hh

    SOURCE: CalculateDotProduct.cc

    Article: Header and Includes: Why and How,

    Link: www.cplusplus.com/forum/articles/10627/

    Interface &

    Implementation

    V t D tP d t i

    http://www.cplusplus.com/forum/articles/10627/http://www.cplusplus.com/forum/articles/10627/
  • 7/27/2019 W02 D01 LAB Computational Methods

    15/36

    /*

    Program: VectorDotProduct_main.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PM

    NOTE: calculate the dotproduct of two vectors

    */

    #include

    int main(int argc, char** argv) {

    // define two vectors A and B

    const int ndim = 3; // in 3-dimenasions

    double vectorA[ndim] = {1.0, 1.5, 2.0};

    double vectorB[ndim] = {3.0, 4.0, 5.0};

    double dotproduct = 0.0;

    // calcuate the dotproduct

    for(int i = 0; i < ndim; i++) {dotproduct += vectorA[i] * vectorB[i];

    }

    // print out results

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    16/36

    /*

    Program: VectorDotProduct_main.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PM

    NOTE: calculate the dotproduct of two vectors

    */

    #include

    int main(int argc, char** argv) {

    // define two vectors A and B

    const int ndim = 3; // in 3-dimenasions

    double vectorA[ndim] = {1.0, 1.5, 2.0};

    double vectorB[ndim] = {3.0, 4.0, 5.0};

    double dotproduct = 0.0;

    // calcuate the dotproduct

    for(int i = 0; i < ndim; i++) {dotproduct += vectorA[i] * vectorB[i];

    }

    // print out results

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    17/36

    /*

    Program: VectorDotProduct_main.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PM

    NOTE: calculate the dotproduct of two vectors

    */

    #include

    #include CalculateDotProduct.hh

    int main(int argc, char** argv) {

    // define two vectors A and B

    const int ndim = 3; // in 3-dimenasions

    double vectorA[ndim] = {1.0, 1.5, 2.0};

    double vectorB[ndim] = {3.0, 4.0, 5.0};

    double dotproduct = 0.0;

    // calcuate the dotproduct

    dotproduct = CalculateDotProduct(vectorA, vectorB);

    // print out results

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    18/36

    VectorDotProduct_main.c/*Program: VectorDotProduct_main.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PM

    Date modified: 6/27/2013 12:30PM

    NOTE: calculate the dotproduct of two vectors

    */

    #include

    #include CalculateDotProduct.hh

    int main(int argc, char** argv) {

    // define two vectors A and B

    const int ndim = 3; // in 3-dimenasions

    double vectorA[ndim] = {1.0, 1.5, 2.0};

    double vectorB[ndim] = {3.0, 4.0, 5.0};

    double dotproduct = 0.0;

    // calcuate the dotproduct

    dotproduct = CalculateDotProduct(vectorA, vectorB);

    // print out results

    std::cout

  • 7/27/2019 W02 D01 LAB Computational Methods

    19/36

    CalculateDotProduct.h/*

    Program: CalculateDotProduct.hh

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PMDate modified: 6/27/2013 12:30PM

    NOTE: header file that calculate the dotproduct of two vectors

    */

    #ifndef _CALCULATE_DOT_PRODUCT_HH

    #define CALCULATE_DOT_PRODUCT_HH

    double CalculateDotProduct(double *vector1, double *vector2);

    #endif

  • 7/27/2019 W02 D01 LAB Computational Methods

    20/36

    CalculateDotProduct c

  • 7/27/2019 W02 D01 LAB Computational Methods

    21/36

    CalculateDotProduct.c/*

    Program: CalculateDotProduct.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PMDate modified: 6/27/2013 12:30PM

    NOTE: source file that calculate the dotproduct of two vectors

    */

    #include CalculateDotProduct.hh

    double CalculateDotProduct(double *vector1, double *vector2) {

    // calcuate the dotproduct

    double result = 0.0;

    result = CalculateDotProduct(vectorA, vectorB);

    return result;

    } # end of CalculateDotProduct

    CalculateDotProduct c

  • 7/27/2019 W02 D01 LAB Computational Methods

    22/36

    CalculateDotProduct.c/*

    Program: CalculateDotProduct.cc

    Author: Giovanni J Paylaga

    Date created: 6/27/2013 12:30PMDate modified: 6/27/2013 12:30PM

    NOTE: source file that calculate the dotproduct of two vectors

    */

    #include CalculateDotProduct.hh

    double CalculateDotProduct(double *vector1, double *vector2) {

    // calcuate the dotproduct

    double result = 0.0;

    result = CalculateDotProduct(vectorA, vectorB);

    return result;

    } # end of CalculateDotProduct

    Implementation

  • 7/27/2019 W02 D01 LAB Computational Methods

    23/36

    Running our Program

    Now we have the Needed Files:

    MAIN: VectorDotProduct_main.cc

    HEADER: CalculateDotProduct.hh

    SOURCE: CalculateDotProduct.cc

    Enter these commands:

    g++ -c VectorDotProduct_main.ccg++ -c I ./ CalculateDotProduct.cc

    g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

  • 7/27/2019 W02 D01 LAB Computational Methods

    24/36

    Running our Program

    Now we have the Needed Files:

    MAIN: VectorDotProduct_main.cc

    HEADER: CalculateDotProduct.hh

    SOURCE: CalculateDotProduct.cc

    Enter these commands:

    g++ -c VectorDotProduct_main.ccg++ -c I ./ CalculateDotProduct.cc

    g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

    First line will createan object file

    VectorDotProduct_main.o(a.k.a. library file)

    NOTE: there is noexecutable file yet.

  • 7/27/2019 W02 D01 LAB Computational Methods

    25/36

    Running our Program

    Now we have the Needed Files:

    MAIN: VectorDotProduct_main.cc

    HEADER: CalculateDotProduct.hh

    SOURCE: CalculateDotProduct.cc

    Enter these commands:

    g++ -c VectorDotProduct_main.ccg++ -c I ./ CalculateDotProduct.cc

    g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

    second line will compilesource file into an object

    file CalculateDotProduct.o

    NOTE: compiler will searchfor the header file

    HOW? By adding -I ./

    Where ./ = directory where your

    header is.

  • 7/27/2019 W02 D01 LAB Computational Methods

    26/36

    Running our Program

    Now we have the Needed Files:

    MAIN: VectorDotProduct_main.cc

    HEADER: CalculateDotProduct.hh

    SOURCE: CalculateDotProduct.cc

    Enter these commands:

    g++ -c VectorDotProduct_main.ccg++ -c I ./ CalculateDotProduct.cc

    g++ VectorDotProduct_main.o CalculateDotProduct.o o DotProduct

    Last line will link theexecutable program

    DotProduct to the objectfiles (*.ofiles)

    Note: -o lets you generatethe executable file

    You can then run it:

    ./DotProduct

  • 7/27/2019 W02 D01 LAB Computational Methods

    27/36

    Make you life easier by using Makefiles

    Makefilesspecial format files utilized by

    make to automatically create and

    manage applicationsCommand: make -f yourMakeFile

    Example: place this inside yourMakeFile:all:

    g++ helloworld.cc o helloworld

    Then type/execute the command

  • 7/27/2019 W02 D01 LAB Computational Methods

    28/36

    Make you life easier by using Makefiles

    MakefilesFormat:

    [target] = dependencies

    [tab] system command

    Example:

    all:

    g++ helloworld.cc o helloworld

  • 7/27/2019 W02 D01 LAB Computational Methods

    29/36

    Make you life easier by using Makefiles

    MakefilesFormat:

    [target] = dependencies

    [tab] system command

    Example: UTILIZiNG dependencies

    all: helloworld

    helloworld: main.o helloworld.o

    g++ main.o hello.o o helloworld

    main.o: main.ccg++ -c main.cc

    helloworld.o: helloworld.ccc

    g++ -c helloworld.cc

    clean:

    rm rf *o helloworld

  • 7/27/2019 W02 D01 LAB Computational Methods

    30/36

    Make you life easier by using MakefilesUsing variables $(VAR) and comments # or /**/

    # I am a comment, let variable CC = compiler to useCC = g++

    /* CFLAGS will be the options thatll be passed to the

    compilers */

    CFLAGS = -c Wall Wno-deprecated

    all: helloworld

    helloworld: main.o helloworld.o

    $(CC) main.o hello.o o helloworld

    main.o: main.cc$(CC) $(CFLAGS) main.cc

    helloworld.o: helloworld.ccc

    $(CC) $(CFLAGS) helloworld.cc

    clean:

    rm rf *o helloworld

    Recall our VectorDotProduct program

  • 7/27/2019 W02 D01 LAB Computational Methods

    31/36

    Recall our VectorDotProduct program# Makefile# Author: Giovanni J Paylaga

    CC = g++

    CFLAGS = -cWall

    Wno-deprecated

    SRCS = VectorDotProduct_main.cc CalculateDotProduct.cc

    OBJS = VectorDotProduct_main.o CalculateDotProduct.o

    LIBS = -L ./

    INCS = -I ./

    PROG = DotProduct

    all:$(PROG)

    $(PROG):$(OBJS)$(CC) $(CFLAGS) $^ $(LIBS) o $@

    .cc.o:

    $(CC) $(CFLAGS) $(INCS) -c $< -o $@

    clean:

    rm rf *.o $(PROG)

    Recall our VectorDotProduct program

  • 7/27/2019 W02 D01 LAB Computational Methods

    32/36

    Recall our VectorDotProduct program# Makefile# Author: Giovanni J Paylaga

    CC = g++

    CFLAGS = -cWall

    Wno-deprecated

    SRCS = VectorDotProduct_main.cc CalculateDotProduct.cc

    OBJS = VectorDotProduct_main.o CalculateDotProduct.o

    LIBS = -L ./

    INCS = -I ./

    PROG = DotProduct

    all:$(PROG)

    $(PROG):$(OBJS)$(CC) $(CFLAGS) $^ $(LIBS) o $@

    .cc.o:

    $(CC) $(CFLAGS) $(INCS) -c $< -o $@

    clean:

    rm rf *.o $(PROG)

    $^ = all dependencies

    Equivalent to

    VectorDotProduct_main.o CalculateDotProduct.o

    Recall our VectorDotProduct program

  • 7/27/2019 W02 D01 LAB Computational Methods

    33/36

    Recall our VectorDotProduct program# Makefile# Author: Giovanni J Paylaga

    CC = g++

    CFLAGS = -cWall

    Wno-deprecated

    SRCS = VectorDotProduct_main.cc CalculateDotProduct.cc

    OBJS = VectorDotProduct_main.o CalculateDotProduct.o

    LIBS = -L ./

    INCS = -I ./

    PROG = DotProduct

    all:$(PROG)

    $(PROG):$(OBJS)$(CC) $(CFLAGS) $^ $(LIBS) o $@

    .cc.o:

    $(CC) $(CFLAGS) $(INCS) -c $< -o $@

    clean:

    rm rf *.o $(PROG)

    $@ = target name

    Equivalent to

    $(PROG) = DotProduct

    Recall our VectorDotProduct program

  • 7/27/2019 W02 D01 LAB Computational Methods

    34/36

    Recall our VectorDotProduct program# Makefile# Author: Giovanni J Paylaga

    CC = g++

    CFLAGS = -cWall

    Wno-deprecated

    SRCS = VectorDotProduct_main.cc CalculateDotProduct.cc

    OBJS = VectorDotProduct_main.o CalculateDotProduct.o

    LIBS = -L ./

    INCS = -I ./

    PROG = DotProduct

    all:$(PROG)

    $(PROG):$(OBJS)$(CC) $(CFLAGS) $^ $(LIBS) o $@

    .cc.o:

    $(CC) $(CFLAGS) $(INCS) -c $< -o $@

    clean:

    rm rf *.o $(PROG)

    $< = first dependency.cc.o = This is a suffix

    replacement rule for building.os from .ccs.

    It uses automatic variables $

  • 7/27/2019 W02 D01 LAB Computational Methods

    35/36

    Exercises: Write a program that will calculate the vertical and horizontal

    positions (in meters) and components of the velocity vector(in m/s) of a projectile at any given time (value of time is asked at

    run time). Use the following conditions:

    Write a program to sum the series:

    2 1(1)

    sin()

    = let n = 100, x=0.5

    Write a program that calculates the dot product of n-dimensionalvectors. It asks the user at run time for two vectors anddetermines if they have the same dimensions. It informs the userabout the dimension of each vector and prints out their dotproduct.

    y0 = 5m x0 = 0 m

    v0 = 20m/s theta= 30o.

    Assignment

  • 7/27/2019 W02 D01 LAB Computational Methods

    36/36

    Assignment Write a program that will calculate the mean and standard

    deviation of a set of data. It asks the user at run time the number

    of data points.