29.03.2015 Views

Modern Programming Concepts in Engineering - Vietnamese ...

Modern Programming Concepts in Engineering - Vietnamese ...

Modern Programming Concepts in Engineering - Vietnamese ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Modern</strong> <strong>Programm<strong>in</strong>g</strong> <strong>Concepts</strong> <strong>in</strong> Eng<strong>in</strong>eer<strong>in</strong>g<br />

universal mach<strong>in</strong>e program specialized mach<strong>in</strong>e<br />

Topic 1: Introduction<br />

Matthias Baitsch<br />

<strong>Vietnamese</strong>-German University


About the course. . .


Goals of the course<br />

After successfully complet<strong>in</strong>g this course, you will<br />

◮ know the key concepts of object-oriented programm<strong>in</strong>g<br />

◮ master a programm<strong>in</strong>g language: Java<br />

◮ be able to tackle many different programm<strong>in</strong>g problems <strong>in</strong><br />

eng<strong>in</strong>eer<strong>in</strong>g sciences<br />

◮ know about types of algorithms and you will be able to<br />

implement them<br />

◮ know a variety of data structures and you will choose the right<br />

one for your problem at hand<br />

◮ build your own software upon exist<strong>in</strong>g libraries


What other students programmed after one semester Java<br />

Simulation of a nonl<strong>in</strong>ear pendulum<br />

Start application: Mac OS X/W<strong>in</strong>dows


We use an exist<strong>in</strong>g software package: view3D<br />

What is the “view3D” package?<br />

◮ Java library for <strong>in</strong>teractive 3D graphics<br />

◮ Based on freely available Visualization Toolkit (VTK)<br />

◮ Developed at the Institute for Computational Eng<strong>in</strong>eer<strong>in</strong>g


Learn<strong>in</strong>g approach: Outside-<strong>in</strong><br />

What does outside-<strong>in</strong> mean?<br />

You start as consumer of software and you will become a producer<br />

of software later on. We will start with the view3D package.<br />

Us<strong>in</strong>g the view3D library, you<br />

◮ build three-dimensional models easily<br />

◮ create <strong>in</strong>terest<strong>in</strong>g programs from the first day on<br />

◮ learn from exist<strong>in</strong>g software which serves as model and<br />

<strong>in</strong>spiration<br />

◮ become familiar to the use of software through abstract<br />

<strong>in</strong>terfaces


Why modern programm<strong>in</strong>g concepts <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g?


Why modern programm<strong>in</strong>g concepts <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g?<br />

1. Comput<strong>in</strong>g is pervasive <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g:<br />

◮ Numerical simulations<br />

◮ Optimization of structures and processes<br />

◮ Computer aided design<br />

◮ Environmental model<strong>in</strong>g<br />

◮ Construction management<br />

◮ Data management<br />

Advances <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g depend on advances <strong>in</strong> comput<strong>in</strong>g


Why modern programm<strong>in</strong>g concepts <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g?<br />

2. Eng<strong>in</strong>eer<strong>in</strong>g problems become:<br />

◮ Bigger (<strong>in</strong> terms of simulation model size)<br />

◮ More complicated (coupled fields, probability, . . . )<br />

◮ Multidiscipl<strong>in</strong>ary (take <strong>in</strong>to account eng<strong>in</strong>eer<strong>in</strong>g aspects,<br />

economics, energy management, . . . )<br />

Industry demands for software solutions


Why modern programm<strong>in</strong>g concepts <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g?<br />

In order to cope with current and future challenges, creators of<br />

eng<strong>in</strong>eer<strong>in</strong>g software have to<br />

◮ employ efficient solution procedures (numerical methods)<br />

◮ exploit modern computer hardware (parallel comput<strong>in</strong>g)<br />

◮ use the Internet (distributed and grid comput<strong>in</strong>g)<br />

◮ establish <strong>in</strong>teractive simulation models (computational<br />

steer<strong>in</strong>g, virtual reality)<br />

◮ provide comprehensible evaluation methods (visualization)<br />

◮ ensure correctness of results (as far as possible)<br />

Problems can not be solved simply by trial and error, rather a rich<br />

set of methods and concepts is required


What are modern programm<strong>in</strong>g concepts <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g?<br />

Most important methods and concepts:<br />

◮ Object-oriented programm<strong>in</strong>g (OOP)<br />

◮ Formal model<strong>in</strong>g techniques (Unified Model<strong>in</strong>g Language)<br />

◮ Use of exist<strong>in</strong>g software packages, frameworks and services<br />

◮ Automated test<strong>in</strong>g and test driven development<br />

◮ <strong>Modern</strong> user <strong>in</strong>terfaces and <strong>in</strong>teractive visualization<br />

→ Choice of methods and concepts depend on software project


Why object-oriented programm<strong>in</strong>g (OOP)?<br />

Short answer<br />

Because object-oriented programm<strong>in</strong>g works!


Why object-oriented programm<strong>in</strong>g (OOP)?<br />

Short answer<br />

Because object-oriented programm<strong>in</strong>g works!<br />

Long answer<br />

Because object-oriented programm<strong>in</strong>g<br />

◮ provides a natural and powerful way to model actual<br />

(eng<strong>in</strong>eer<strong>in</strong>g) problems<br />

◮ simplifies the development of software which is extensible and<br />

ma<strong>in</strong>ta<strong>in</strong>able<br />

◮ lets developers split software up <strong>in</strong>to separate, reusable parts<br />

◮ helps developers to build reliable software systems<br />

◮ simplifies programm<strong>in</strong>g <strong>in</strong> a team of programmers<br />

→ OOP helps to keep complex software systems manageable


Choos<strong>in</strong>g a programm<strong>in</strong>g language for MPCE<br />

We use Java because Java is<br />

◮ strictly object-oriented<br />

◮ comparatively easy to learn<br />

◮ a modern programm<strong>in</strong>g language<br />

◮ supported by various freely available development<br />

environments<br />

◮ platform <strong>in</strong>dependent and programs are portable<br />

We do we not use<br />

◮ Fortran: Not object oriented, limited to numerics<br />

◮ C++: Widely used and powerful but hard to learn<br />

◮ C#: Would be an alternative but proprietary<br />

◮ Visual Basic: Not suitable for numerical computations<br />

Languages <strong>in</strong> green: Widely used <strong>in</strong> eng<strong>in</strong>eer<strong>in</strong>g


The Java Virtual Mach<strong>in</strong>e<br />

Compiled language<br />

Java VM<br />

hello.cpp<br />

Source code<br />

hello.java<br />

Source code<br />

C++ compiler<br />

Java compiler<br />

Byte code<br />

Virtual mach<strong>in</strong>e<br />

hello.exe<br />

Executable<br />

hello.class<br />

java.exe<br />

◮ Compiler generates mach<strong>in</strong>e<br />

code<br />

◮ Mach<strong>in</strong>e code is directly<br />

executed by CPU<br />

◮ Compiler generates byte<br />

code<br />

◮ Byte code is executed by<br />

virtual mach<strong>in</strong>e a which is<br />

executed by CPU<br />

a Often, VMs are implemented <strong>in</strong> C


The Java Virtual Mach<strong>in</strong>e<br />

Advantages of us<strong>in</strong>g a Virtual Mach<strong>in</strong>e<br />

◮ VM manages memory (garbage collection)<br />

◮ VM performs extensive runtime error check<strong>in</strong>g<br />

◮ VM handles access to system resources (memory etc.)<br />

◮ A Java program can run on any platform where a virtual<br />

mach<strong>in</strong>e is available<br />

Simplifies the development of portable, secure and reliable<br />

software!<br />

Disadvantages<br />

◮ Impact on execution speed<br />

◮ Java needs to be <strong>in</strong>stalled on the target computer


Summary<br />

<strong>Modern</strong> <strong>Programm<strong>in</strong>g</strong> <strong>Concepts</strong> <strong>in</strong> Eng<strong>in</strong>eer<strong>in</strong>g<br />

◮ covers theory and practice of object-oriented programm<strong>in</strong>g<br />

◮ uses Java as a programm<strong>in</strong>g language, but concepts can be<br />

transferred easily to other languages<br />

◮ covers fundamental data structures, how they work and how<br />

to make use of them<br />

◮ <strong>in</strong>troduces basic algorithms from the field of eng<strong>in</strong>eer<strong>in</strong>g, both<br />

theoretically (complexity) as well as practically (through<br />

examples)


<strong>Programm<strong>in</strong>g</strong> computers


Computers<br />

Blue Gene at Sandia national labs L<strong>in</strong>ux cluster Bochum Laptop computer Mobile phone<br />

Computers are<br />

◮ com<strong>in</strong>g <strong>in</strong> very different flavours<br />

◮ becom<strong>in</strong>g faster and more powerful<br />

◮ nowadays everywhere<br />

◮ present <strong>in</strong> nearly every aspect of daily life<br />

Computers are universal mach<strong>in</strong>es execut<strong>in</strong>g programs


What do the components of a computers do?<br />

Outside world<br />

Computer<br />

- Humans<br />

- Other computers<br />

- Devices<br />

- Environment<br />

Communication<br />

devices<br />

Monitor<br />

Keyboard/Mouse<br />

Processors<br />

Memories<br />

Network device<br />

Sensor<br />

Processors carry out <strong>in</strong>structions, send and receive data from<br />

communication devices, store and retrieve data and<br />

programs from memories<br />

Memories store program and data, persistent or temporary<br />

Communication devices connect the computer to the outside<br />

world, present and gather data<br />

Processors are <strong>in</strong>structed by the program they execute


What is a program?


What is a program?<br />

Examples. . .


Interactive program with graphical user <strong>in</strong>terface<br />

Interactive programs<br />

◮ are started by users by click<strong>in</strong>g an icon<br />

◮ send and receive data from devices such as monitor, pr<strong>in</strong>ter,<br />

network card, speakers, hard disk drive<br />

◮ react on user <strong>in</strong>put from mouse and keyboard


Console program<br />

Console programs<br />

◮ are started by users by typ<strong>in</strong>g a command<br />

◮ typically do not require user <strong>in</strong>put dur<strong>in</strong>g execution<br />

◮ most often store results of computation on the hard disk<br />

◮ are common <strong>in</strong> scientific and high-performance comput<strong>in</strong>g


Program runn<strong>in</strong>g on a server computer<br />

User's computer (yours)<br />

Server computer<br />

http request<br />

Web server<br />

(e.g. apache)<br />

html document<br />

Web browser<br />

Server programs<br />

◮ are started automatically when the computer starts up<br />

◮ run for weeks or months<br />

◮ usually do not require any user <strong>in</strong>teraction<br />

◮ <strong>in</strong>teract with other programs runn<strong>in</strong>g on another computer


Programs turn computers <strong>in</strong>to specialized mach<strong>in</strong>es<br />

universal mach<strong>in</strong>e program specialized mach<strong>in</strong>e<br />

A computer is a universal mach<strong>in</strong>e. When you feed it with a<br />

program, it becomes a specialized mach<strong>in</strong>e which can be used to<br />

carry out a specific task.<br />

Examples:<br />

◮ Start Word – Your computer becomes a mach<strong>in</strong>e for writ<strong>in</strong>g a<br />

letter (for your thesis, you might use someth<strong>in</strong>g else)<br />

◮ Start Ansys – Your computer becomes a mach<strong>in</strong>e for<br />

structural analysis


Program execution<br />

Data<br />

Flow<br />

Monitor<br />

Program<br />

Start of program execution<br />

Keyboard/Mouse<br />

Network device<br />

Sensor<br />

process<strong>in</strong>g<br />

Persistent storage<br />

(HDD)<br />

A program<br />

Program term<strong>in</strong>ation<br />

◮ has a well-def<strong>in</strong>ed entry po<strong>in</strong>t where program execution starts<br />

◮ receives <strong>in</strong>put from communication devices<br />

◮ sends data to communication devices<br />

◮ reads and writes data to persistent storage devices<br />

◮ has one or more conditions which term<strong>in</strong>ate program execution<br />

An execut<strong>in</strong>g program is an immaterial mach<strong>in</strong>e


Build<strong>in</strong>g and runn<strong>in</strong>g programs<br />

A programmer<br />

writes a<br />

which<br />

Program<br />

a user<br />

runs on<br />

a computer


Build<strong>in</strong>g and runn<strong>in</strong>g programs<br />

A programmer<br />

us<strong>in</strong>g<br />

a computer<br />

writes a<br />

which<br />

Program<br />

a user<br />

runs on<br />

a computer


Build<strong>in</strong>g and runn<strong>in</strong>g programs<br />

A programmer<br />

us<strong>in</strong>g<br />

a computer<br />

writes a<br />

which<br />

Program<br />

run on<br />

users<br />

their computers


Build<strong>in</strong>g and runn<strong>in</strong>g programs<br />

programmers<br />

us<strong>in</strong>g<br />

computers<br />

write a<br />

which<br />

Program<br />

run on<br />

users<br />

their computers


What makes a program a good program?<br />

A good program is<br />

correct: It does what it’s supposed to. This sounds trivial but<br />

do you know any program which never crashes?<br />

extensible: It is easy to change and to add new functionality<br />

readable: The program source code is understandable to<br />

humans!<br />

reusable: Parts of the program can be used later to build other<br />

programs (prevent re<strong>in</strong>vention of the wheel)<br />

robust: It reacts appropriately to errors<br />

secure: It does not let others misuse your computer


Writ<strong>in</strong>g computer programs<br />

Writ<strong>in</strong>g programs it tough:<br />

◮ Programs crash<br />

◮ Programs give wrong results<br />

◮ Gett<strong>in</strong>g a program right can be really hard<br />

→ As a programmer, you are <strong>in</strong> charge


Writ<strong>in</strong>g computer programs<br />

Writ<strong>in</strong>g programs it tough:<br />

◮ Programs crash<br />

◮ Programs give wrong results<br />

◮ Gett<strong>in</strong>g a program right can be really hard<br />

→ As a programmer, you are <strong>in</strong> charge<br />

Writ<strong>in</strong>g programs is fun:<br />

◮ Design and build your own mach<strong>in</strong>es<br />

◮ Exert your creativity and imag<strong>in</strong>ation<br />

◮ Hav<strong>in</strong>g created a runn<strong>in</strong>g program is a good feel<strong>in</strong>g<br />

→ There are (nearly) no limits, what you can do


A first example. . .


A first example: Elastic pendulum<br />

Elements:<br />

- circular section d = 0.1<br />

Material:<br />

- E = 500000 N/m², ρ = 10 kg/m³<br />

Node at (0.0, 2.5)<br />

Node at (2.5, 2.5)<br />

Node at (0, 0)<br />

y<br />

x<br />

Constra<strong>in</strong>t<br />

g = 9.81 m/s²<br />

Next slide: Use exist<strong>in</strong>g Java package (m<strong>in</strong>iFE – developed <strong>in</strong><br />

Bochum) <strong>in</strong> order to create a f<strong>in</strong>ite-element model of the system<br />

and to perform a nonl<strong>in</strong>ear transient analysis simulat<strong>in</strong>g 20 seconds<br />

us<strong>in</strong>g 2000 timesteps.


import i n f . m i n i f e . f e . Node ;<br />

import i n f . m i n i f e . f e . e l e m e n t s . e1d . Truss2D ;<br />

import i n f . m i n i f e . f e . s c t . C i r c l e S ;<br />

import i n f . m i n i f e . f e . u t i l . A n a l y s i s T y p e s ;<br />

import i n f . m i n i f e . view2 . Viewer2 ;<br />

p u b l i c c l a s s PendulumProgram {<br />

p u b l i c s t a t i c void ma<strong>in</strong> ( S t r i n g [ ] a r g s ) {<br />

Model m = new Model ( ) ;<br />

Node n1 = m. createNode ( 0 . 0 , 0 . 0 ) ;<br />

Node n2 = m. createNode ( 2 . 5 , 2 . 5 ) ;<br />

Node n3 = m. createNode ( 0 . 0 , 2 . 5 ) ;<br />

C o n s t r a i n t c = new C o n s t r a i n t ( f a l s e , f a l s e , true ) ;<br />

M a t e r i a l mat = m. c r e a t e M a t e r i a l ( 5 0 0 0 0 0 . 0 , 1 0 . 0 ) ;<br />

C i r c l e S s c t = m. c r e a t e S e c t i o n ( C i r c l e S . TYPE ) ;<br />

n1 . s e t C o n s t r a i n t ( c ) ;<br />

s c t . s e t D i a m e t e r ( 0 . 1 ) ;<br />

m. c r e a t e E l e m e n t ( Truss2D . TYPE, mat , s ct , n1 , n2 ) ;<br />

m. c r e a t e E l e m e n t ( Truss2D . TYPE, mat , s ct , n2 , n3 ) ;<br />

m. g e t S e t t i n g s ( ) . s e t A c c e l e r a t i o n (DOF. T Y , 9 . 8 1 ) ;<br />

A n a l y s i s T y p e s . n o n l i n e a r T r a n s i e n t (m, 2 0 , 2 0 0 0 , 1 e − 6 , 0 . 7 ) ;


The runn<strong>in</strong>g program


Summary<br />

◮ Today, eng<strong>in</strong>eer<strong>in</strong>g and comput<strong>in</strong>g are closely connected<br />

◮ Computers are universal mach<strong>in</strong>es<br />

◮ Feed<strong>in</strong>g a computer with a program turns the computer <strong>in</strong>to a<br />

specialized mach<strong>in</strong>e<br />

◮ An execut<strong>in</strong>g program is an immaterial mach<strong>in</strong>e<br />

◮ Object-oriented software models can be close to the problem<br />

doma<strong>in</strong><br />

◮ Exist<strong>in</strong>g libraries can provide powerful functionality

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!