Computational Physics Course Introduction

Transcription

Course IntroductionComputational PhysicsCourse Introduction

OutlineCourse DescriptionCourse InformationInstructorsWeb PageScheduleGetting Started with ComputersGetting Started with Python

Course DescriptionIntroduction to computational methodsfor simulating physical systems andsolving problems.Sophomore LevelNo previous experience withcomputational methods"Lab Course"

Course DescriptionThe Learning CurveWe need to learn many new things.Dealing with Lab ComputersUnix operating systemX Windows SystemText Editors for X WindowsReport Writing (e.g. with Open Office)Using Computer Language: PythonPython's user interfaceArraysObjectsInteractive, Scripts, FunctionsPlotting

Course DescriptionThe Learning CurveOnce we learn these things we willlearn to:Simulate Physical SystemsSolve Ordinary Differential EquationsSolve Partial Differential EquationsExamples of Physics .Motion of projectiles with air resistanceMotion of pendulum and ChaosOrbits in the Solar SystemDiffusion

Course InformationInstructorsPete Schloerb (Instructor)847 LGRT-B - 545 4303Email: schloerb @ astro.umass.eduTeaching Assistant: Yuping Tang619F LGRT-BEmail: yupingt @ astro.umass.eduComputer Lab Supervisor: Tariq AhmadReport Problems to:rt @ cs.astro.umass.edu

TextbookNo official textbook – we teachwhat you need to know.Google is a vast resource forpython and for numericaltechniquesIf you want a book, try:Computational Physics: ProblemSolving with Python, 3rd EditionRubin H. Landau, Manuel J. Páez,Cristian C. Bordeianu

Course InformationWeb Pageswww.astro.umass.edu/ schloerb/ph281This is subject to change.

What do we do?Exercises (every class)Short problems; practiceProjects (every few weeks)Calculation of a real Physics ProblemExplore behaviorSubmit Lab ReportFinal Problem

A Word to the WiseAttend ClassThere is not always a lecture, but we oftenreview matters that have been troublingInstructors are here to helpDon't fall behindExercises and Projects take longer thanyou think.Learn Python!Spend some time with tutorials andmanual.

Course InformationTentative SchedulePython!!Interpolation; Numerical IntegrationRandom Numbers and SimulationModel FittingDifferential EquationsOrdinaryPartial

Getting Started withComputers

General Lab GuidelinesSee document on desktop for rules.Lab open 24/7; use combination lock.All computers are the same.Ubuntu Lucid (12.04); Gnu/LinuxReport problems to Professor or tort@cs.astro.umass.edu in off hours

Lab Do'sUse the lab!Respect equipment.You are responsible for your ownbackups.Move computers in their slide holdersgently.Log off when you are done.Last one out, please turn off lights andlock door.

Lab Don'tsDon't share combination of door lock.Don't unplug any cable or power cord.No public internet connections in thelab. Don't connect your computer tolab network.No food or drink in Lab.

Login/LogoutLogin ProcedureLogin with your assigned usernameFirst Time:Enter temporary passwordChange to new passwordThereafter . remember your password!!Logout ProcedureClick mouse on menubar at top of screenSelect ”Log Out”Select “Log Out” when dialog box appears.

MenuVirtualDesktops

Starting Firefox Web Browser

Starting Terminal Shell

Getting Started withPython

Why Python?Widely UsedPython is Number 1 S/W tool in AstronomyLots of programs and libraries (called “packages”written for Python)InteractiveUse it like a graphing calculatorSpeeds development of programsGraphics integrated with languageObject OrientedPortable (if you are careful)Free

How do I Learn Python?See python.org for information.NB: There are two major versions ofPython: version 2 and version 3. We useversion 2 in class.Tutorial at:docs.python.org/2/tutorial/index.htmlMany other tutorials on line. Google“python tutorial” for example.Get Python and fool around:www.enthought.com/products/canopy/Free for students!!

Python ObjectsObjects can:Have characteristics - AttributesDo stuff - MethodsThink of a cat.Cats have characteristics: breed, fur color,name, etc.Cats do stuff: hunt, eat, sleep, play, purr .The goings on in a real cat are verycomplicated . but we don't have to know allthat to use a cat.S/W objects can be used without deepknowledge of how they work!

Object InheritanceObjects can be organized in a hierarchy wheremore and more specifc cases inherit attributesand methods from higher levels:Example:MammalCarnivoreFelineTabby CatInheritance in S/W allows us to reuse useful partsof objects that might already exist.

Python Variables and DataStructuresInteger (called int)Real (called float – for floating point)Complex (called complex)Logical (called bool – for boolean)String – an array of charactersList – an array of variables (of any kind)Tuples – a kind of list that is immutableDictionaries – a list of “key” and “value” pairs.The “type” function identifies the variable.

Dealing with Python ListsMake a list with 5 items:s [0, 1, 2, 3, 4]g 'Hello World!' (a string is a kind of list)Get the ith item of the list (index starts at 0):s[i]Get items i through j of the list:s[i:j 1]Concatenate 2 lists s and t:s tGet length of listlen(s)

Try this out!Start terminal windowStart “interactive python” in terminalwindowType: ipythonEnter some python commands.Exit ipythonType: exit or quitNote: we could also use the “python” interpreter directly ratherthan “ipython”, but the latter provides more useful capabilities as weproceed in this class.

Some ipython featuresOS Commands:mkdir new dir : make a new directory “new dir”ls : show contents of current directorypwd : show current directorycd new dir: change to directory “new dir”Help:help(object) : provides help on “object”magic : lists “magic” commands%quickref is a magic command whichprovides a short list of magic commandsobject? : tells about “object”

Calculate SomethingBasic python doesn't know much .We must “import” useful functions fromthe “math” module: from math import sin imports the “sin” functionCall the sin function: sin(theta) from math import *imports all math functionsCall the sin function: sin(theta) import mathimports the math moduleCall the sin function: math.sin(theta) import math as mimports math, calls it “m”Call the sin function: m.sin(theta)

Write a functionIn [10]: import mathIn [11]: def sind(theta):.: t theta/180.*math.pi.: return(math.sin(t)).:In [12]: sind(90)Out[12]: 1.0Return whendone withdefi nition

Write a scriptUse Text Editor to create afi le with python commands.##if file:le:myscript.pymyscript.pyIn this example: myscript.pyprintprintAAAA 'Hello'HelloWorld!'World!'To run the script enter:In [13]: run myscript.pyOut[13]: Hello World!

Try some stuff out!Defne some numbers and operate onthem ( ,-,*,/)Build a long string by concatenatingshort stringsSelect substrings in your long stringusing the : operator.Write a “degree input” function tocompute cosine.Write a fle with all trig functions readyto accept degree arguments (e.g. sind,cosd, tand, atand, atan2d)

Introduction to computational methods for simulating physical systems and solving problems. Sophomore Level No previous experience with computational methods . Computational Physics: Problem Solving with Python, 3rd Edition Rubin H. Landau, Manuel J. P