Physics 2660: Fundamentals of Scientific Computing
Lecture 14 Instructor: Prof. Chris Neu ([email protected])
Notes• Last lecture!• Upcoming homeworks:
– HW13 due Wednesday 2 May at 11:59pm– HW14 due Wednesday 2 May at 11:59pm – a course evaluation
• alt3zq Tuma, Andrew• ces2fa Stephens, Catherine• ecj4wb Jones, Ethan• jls6gw Selby, Jonathan• raj9qv Jennings, Robert• rm3xw Mora, Raymundo• sam3bf Murri, Samuel• vh2[ Huang, Vicki
• Solutions to all labs and hw’s are in the process of being posted …sorry for recent delays
2
Why have you not completed
this?
Reminder• Grading of HWs almost caught up!
• Register for piazza! – Only 50 out of 60 so far– All questions about final exam will only be answered via piazza
• Office hours: all held in our computer lab, room 022-C of this bldg
– Me: After lecture 3:30-4:30 every Tuesday – TAs:
• Mondays: 3-5pm and 6-8pm• Wednesdays: 5-9pm
3
Notes
• Final exam is coming:
– Take-home projects• 3 problems
– Like a more involved, longer multipart homework assignment– Assigned last week of semester on Tuesday 2 May
• will be posted by 3:30pm today
– Due Thursday May 10:• electronic copies by 9:00am • hard-copies must be submiied Thursday 10 May between 08:00-10:00
in room 022-C, our computer lab….or earlier by personal arrangement with me.
4
Notes• Final exam is coming:
– Take-home projects• 3 problems
– Like a more involved, longer multipart homework assignment– Assigned last week of semester on Tuesday 1 May– Due Thursday May 10:
• electronic copies by 9:00am • hard-copies must be submiied Thursday 10 May between 08:00-10:00
in room 022-C, our computer lab….or earlier by personal arrangement with me.
5
Due Thursday May 10, electronically by 9:00am and hard copies between 08:00 and 10:00 in 022-C
Review and Outline• Last time:
– Comparing data to a prediction
• Today:– Tuning a model to best represent the data– More tests of compatibility
6
Summary so far…• Compare some data to a model, account
for uncertainties• Calculate reduced χ2
• If good agreement– should see different points sometimes
high/low– if k large, reduced χ2 ~ 1.0
7
Tuning a Model to Best Match Some Data
8
Tuning a Model
9
Tuning a Model
10
Can we figure out which model – which values of a and b – the data most
favors?
Probability of Some Observation
11
Probability of Multiple Observations
12
The probability of the collection of data – 3 observations – is just the product of the three individual probabilities
Probability of Multiple Observations
13
The probability of the collection of data – k observations – is just the product of the k individual probabilities
P: The χ2 Likelihood Function
14
Minimizing the χ2
15
Minimizing the χ2
16
Minimizing the χ2
17
Minimizing the χ2
18
More Powerful Application: An Arbitrary Theory
19
FiPing with Gnuplot
20
P(x;a,b,c) = 12πc2
e−(ax−b)2
2c2
Assessing the Quality of a Fit
21
Assessing the Quality of a Fit
22
Consequence: If the number of fit parameters is greater than or equal to the number of data points the χ2 is undefined.
Trivial case
Assessing the Quality of a Fit
23
P(x;a,b,c) = 12πc2
e−(ax−b)2
2c2
Assessing the Quality of a Fit
24
So there is a 90% probability that, if the data were consistent with the model (here a Gaussian-like thing with 3 params), the data would have a higher chi2 value. Too good to be true? Why are the points so close to the model? Did the fit procedure cheat in some way? Are the uncertainties over-estimated?
FiPing is Done EVERYWHERE
25
Aside: Gravitational Waves
hips://www.nytimes.com/video/science/100000004200661/what-are-gravitational-waves-ligo-black-holes.html
26
Curve FiPing
27
Deviations from the Model
28
The Pull Distribution
29
Bias – Is the Prediction In Accord with the Data?
30
Clusters of Data Above/Below
31
Clusters of Data Above/Below
32
What Have We Learned
33
What Have We Learned
• We learned a whole lot this semester!
• Computers• Programming Language• C
– For many of you this was your first entrypoint into the use of a computer as a tool for data analysis
– Many of you started from very liile experience– Generated a set of tools you can rely on in the future for a whole
host of problems in your chosen field, science or otherwise
• Data Presentation• Data Analysis and Assessment• Some Statistics – some very important statistical concepts
34
C vs. C++
35
C vs. C++
0 C is much simpler for beginners than C++ but far less powerful1 C++ can run most C code but not the other way round!2 Functions are the building blocks of C whereas objects are the building
blocks of C++3 In C, the program is formulated step-by-step but in C++ the base
elements are formulated first and then linked together in larger systems4 In C++, functions can be overloaded, but not in C5 C++ has a much larger library than C6 C++ allows user to create classes (similar to structures) to which methods
and functions can be assigned and within which they can operate7 In C++ you can add your own types and objects inheriting properties and
routines from other classes. This allows you to add more features to your own classes and the standard library as well
8 C++ uses “Namespace”, which is very useful in avoiding multiple declarations
9 C++ has built in exception support
36
What is next? What more could you learn?
37
What Could You Try Next?
• You now have a firm foundation in C. What could be next?– Another powerful language used in scientific seiings, such as python
– Some scripting language: Bash scripts, perl, etc., to automate many types of text and data manipulation, etc.
– Object Oriented features of C++, Java, C#, etc: Powerful (and sometimes dangerous) design tools!
– A physics/math library, like ROOT, R, CERNLIB, CLHEP, LINPACK, GSL, etc…
– Overview of analytical techniques in classes like PHYS 5630/40(Computational Physics 1/2)
– Get a job w/ a research group, apply your skills, and learn more!38
What Could You Try Next?• Worldwide distributed computing grids
– hip://www.opensciencegrid.org
• UVa’s High-Performance Computing cluster:– hip://arcs.virginia.edu/rivanna
• Massively Parallel Processing
• Real time controls– automation– robotics– eg. autonomous vehicles, ...
• Quantum Computing
– hip://www.sciencedaily.com/news/computers_math/quantum_computers/
39
Grid Computing
• We have operated this semester in the following fashion:– sit down at some personal or lab computer– use this as a portal to a cluster of computers, galileo, featuring
• large amounts of storage• many nodes allowing multiple users to exploit the same resource
simultaneously
• Some drawbacks:– galileo can get slow – bogged down by other users. What do we do in
these cases? Other clusters available to us – and where?– /home is a shared resource and has goien full a few times,
preventing work – not an infinite space resource– interactive jobs are tempermental: what if you needed to run 1000s of
jobs – need 1000s of terminal sessions?– what if I needed lots of data to be stored but only needed it very
occasionally?
40
TIER-0 CERN
• AT LHC, need to store, analyze and distribute 15 Petabytes of data per year (1% of digital information produced in the world)• All data is processed at CERN and backed up on tape• But CERN provides only ~20% total compute capacity
Tier-1 sites
• Sufficient storage capacity for large fraction of the data
• Large scale reprocessing
• Distribution to Tier-2 sites
• 11 sites for all LHC experiments
Distributes data to ~30 Tier-3 sites
Only store data specific to given task
Distributed Computing
Worldwide LHC Computing Grid
• LHC Grid gives access to more than 7000 scientists in more than 30 countries• World’s largest computer grid connecting more than 200,000 CPUs at universities and research institutes • Runs over 1,000,000 tasks every day
TIER-0/1s
TIER-3sTIER-2s
• CMS observes more than 400 unique users submitting jobs per week
• Capacity run up to 200,000 jobs per day on the Grid
CMS Data Processing on the Grid
CERN Computing
45
hips://www.youtube.com/watch?v=jDC3-QSiLB4
The Final Exam
46
Notes
• Final exam:– Take-home projects– Like a more involved, longer multipart homework assignment– Due Thursday May 10:
• electronic copies by 9:00am • hard-copies must be submiied Thursday 10 May between 08:00-10:00
in room 022-C, our computer lab…or earlier by personal arrangement with me
47
Due Thursday May 10, electronically by 9:00am and hard copies between 08:00 and 10:00 in 022-C
Some Guidelines for Final• All students must work individually, no collaboration.
• You can ask questions via email or chat or on Piazza but the insight will be limited to conceptual assistance or clarifications.
• TA hours will be held on Thursday 3 May xxxxx and Friday 4 May yyyyyyy. • These will not be standard lab-TA hours: students can come individually to ask questions to
the TA but then you will go and work on the questions on your own. Conceptual and clarifications on content only, no assistance in determining the solutions. No examination of code will be allowed.
• Pose all questions before Monday 8 May 8pm.
• Allowed resources: class texts, class notes, class web page (including solutions), and web links provided directly from the class web pages. You can use functions available in the standard C libraries we have used in class/lab/homeworks.
• Email/chat/Piazza any requests for problem clarifications by Monday, May 7, 8pm. This may be followed by a single FAQ email covering appropriate questions. Watch for this email. After this, there will be no more discussion during the exam.
48
Some Guidelines for Final• All students must work individually, no collaboration.
• You can ask questions via email or chat or on Piazza but the insight will be limited to conceptual assistance or clarifications.
• TA hours will be held on Thursday 3 May xxxxx and Friday 4 May yyyyyyy. • These will not be standard lab-TA hours: students can come individually to ask questions to
the TA but then you will go and work on the questions on your own. Conceptual and clarifications on content only, no assistance in determining the solutions. No examination of code will be allowed.
• Pose all questions before Monday 8 May 8pm.
• Allowed resources: class texts, class notes, class web page (including solutions), and web links provided directly from the class web pages. You can use functions available in the standard C libraries we have used in class/lab/homeworks.
• Email/chat/Piazza any requests for problem clarifications by Monday, May 7, 8pm. This may be followed by a single FAQ email covering appropriate questions. Watch for this email. After this, there will be no more discussion during the exam.
49
Start your work early!
There is an early deadline on questions to encourage you to start working on it – at least
thinking about it – right away.
The End!
50
More Testing of Compatibility
51
Cumulative Distribution Function
52
Cumulative Distribution Function
53
Example: PDF and CDF
54
Empirical Distribution Function
55
Empirical Distribution Function• The ECDF is made from
“unbinned” data– not from a binned
histogram– use raw measured values
• Do this by:1. say you have N values, xi2. sort the N values in order
of increasing value3. plot each of the N values
with xi on the x-axis and i/N on the y-axis
• Now, compare model’s CDF and the data’s ECDF…
56
Testing Compatibility
57
Testing Compatibility
58
Manipulating Binary Data
59
Bits and Bytes
60
Flipping Bits
61
Bitwise Operators
62
Bitwise AND
63
Bitwise OR
64
Bitwise Left Shift
65
Bitwise Right Shift
66
Bitwise Inverse
67
Bitwise Shift on Constants
68
Testing and SePing Bits
69
( )
Testing and SePing Bits: Masks
70
( )“Mask”
Testing and SePing Bits: Masks
71
( )
Clearing a Bit
72
Manipulating Binary Data: Example
73
Hexadecimal Representation
74
Hexadecimal Representation
75
Hexadecimal Representation
76
Data Storage
77
Example
78
Example
79
Example
80
The Exclusive Or (XOR) Operation
81
The Exclusive Or (XOR) Operation
82
XOR Application
83
XOR Application
84
1 01 1 0 0 0 0
1 010 1 010
010 1 0100
Say you want to send this plain-text “message”:
You and the recipient have a copy of some pre-determined “key”
key ^ message = encrypted message:
And you send this encrypted message to your intended recipient….
^ ^ ^ ^ ^ ^ ^ ^
XOR Application
85
1 01 1 0 0 0 0
1 010 1 010
010 1 0100Your recipient receives your encrypted message:
and applies the key:
^ ^ ^ ^ ^ ^ ^ ^
and gets a copy of the plain-text message. Successful secret communication!
XOR Application
86
1 01 1 0 0 0 0
1 010 1 010
010 1 0100
One flaw: if someone intercepts both the coded and plain message:
^ ^ ^ ^ ^ ^ ^ ^
They can derive the key!This a problem if the sender/recipient ever re-use a key.
Review: Bitwise Operators
87
Review: Testing and SePing Bits
88
Storage of Binary Data
89
The fputc Function
90
The fputc Function
91
1 byte
1 byte4 bytes
4 bytes = 4 chars
Comparison of ASCII and Binary Storage
92
4 bytes = 4 chars
10 bytes = 10 chars
More on Characters
93
Signed v. Unsigned Vars
94
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 10000001b
95
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 10000001b
– Thena+b = 1d + (-1d) = ?
96
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 10000001b
– Thena+b = 1d + (-1d) = 00000001b + 10000001b
97
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 10000001b
– Thena+b = 1d + (-1d) = 00000001b + 10000001b
= 10000010b
98
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 10000001b
– Thena+b = 1d + (-1d) = 00000001b + 10000001b
= 10000010b = -2d
99
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 10000001b
– Thena+b = 1d + (-1d) = 00000001b + 10000001b
= 10000010b = -2d
– That looks wrong to me….
100
Trick: Two’s Complement Notation
101
Trick: Two’s Complement Notation
• To form the two’s complement of any negative number:1. Convert |number| to N-bit representation2. Subtract |number| from 2N
3. Add one to the number
• Examples, 8-bit negative numbers:– If a = -1 then |-1| in 8-bit representation is 00000001
a2c = (11111111 – 00000001) + 00000001 = 11111111
– If a = -5 thena2c = 11111111 – 00000101 + 00000001 = 11111011
102
Signed vs. Unsigned Vars
• Example:– Say
a = 1d = 00000001b and b = -1d = 11111111b (using 2’s complement)
– Thena+b = 1d + (-1d) = 00000001b + 11111111b
= 00000000b = 0d
– That’s beier!
103
More On Binary Data Output
104
More On Binary Data Output
105
More On Binary Data Output
106
More On Binary Data Input
107
More On Binary Data Output
108
More On Binary Data Output
109
More On Binary Data Input
110
More On Binary Data Input
111
fopen: File I/O Modes
112
fopen, fwrite and fread
113
rewind
114
fseek
115
fseek
116
ftell
117
feof
118
Writing Binary for Arbitrary Structs
119
Filesize Comparison of Binary v. ASCII Formats
120
Solving Differential Equations
121
A Trajectory
122
A Trajectory
123
We don’t know v(t) but we know v(t0) and the time derivative.We seek v(t).
A Trajectory
124one step:
result:
not very accurate!!!All we know is the derivative of v! Here v=y and t=x.
A BePer Method
125
A BePer Method
126
All we know is the derivative!
A BePer Method
127
All we know is the derivative!
A BePer Method
128
A BePer Method
129
Passing Pointers to Structures
130
Unintended Consequences
131
Unintended Consequences
132
Warning tells you to rewrite this!
Optional Function Arguments
133
Default Argument Values in Functions: C++
134
Default Argument Values in Functions: C++
135
Default Argument Values in Functions: C++
136
Example:
Searching in Arrays
137
Searching for a Particular Item
138
Method: Linear Search
139
Method: Binary Search
140
Method: Binary Search
141
Method: Binary Search
142
Method: Binary Search
143
Method: Binary Search
144
Search Speed: Linear Method
145
Search Speed: Binary Method
146
Sorting
147
Sorting Methods
148
Method: Selection Sort
149
Method: Selection Sort
150
Method: Selection Sort
151
Method: Quicksort
152
Method: Quicksort
153
Method: Quicksort
154
Method: Quicksort
155
Method: Quicksort
156
Method: Quicksort
157
Linked Lists
158
Reminder About Arrays
159
Linked Lists: A Special Data Structure
160
Linked Lists: A Special Data Structure
161
mallccallocrealloc
Linked Lists: A Special Data Structure
162
Linked Lists: A Special Data Structure
163
Linked Lists: A Special Data Structure
164
If these were stored in an array, this could be a huge job.
Linked Lists: A Special Data Structure
165
If these were stored in an array, this could be a huge job.
Linked Lists: A Special Data Structure
166
Linked Lists: A Special Data Structure
167
Linked Lists: A Special Data Structure
168
Linked Lists: A Special Data Structure
169
Linked Lists: A Special Data Structure
170
171
So What?
These things can actually be very useful!
Linked Lists Example: FIFOs
172
Linked Lists: Summary
173
Dynamic Memory Allocation:
Memory is typically allocated by the compiler…but what if we need to change the requirements on the fly while executing a
program?
(We did not go through this unit in lecture.)
174
The Stack
175
The Stack
• Each running program has its own stack
• As functions are called, the variables local to that function are loaded to the top of the stack. When the function is complete that memory is freed for other uses
• One can use up all the available memory in a stack:“stack overflow” error
176
The Heap
177
The malloc function
178
The calloc function
179
The free function
180
The realloc function
181
Memory Allocation Failure
182
Dynamic Memory Allocation: Example I
183
Dynamic Memory Allocation: Example II
184
Dynamic Memory Allocation: Example II
185
Optional Function Arguments
186
Default Argument Values in Functions: C++
187
Default Argument Values in Functions: C++
188
Default Argument Values in Functions: C++
189
Example: