CDT - Next Generation Nuclear, University of Manchester
27nd October 2014
Introduction to OpenCossan and to Object OrientedProgramming in MATLAB
Matteo Broggi
E: [email protected]: www.liv.ac.uk/risk-and-uncertaintyT: +44 (0)1925 864849
Institute for Risk and Uncertainty
A MEMBER OF THE RUSSELL GROUP
M.Broggi University of Liverpool 27nd October 2014 1 / 31
OpenCOSSAN History and developments
COSSANHistory and developments
1985 ISPUD (Importance sampling using design points)2006 - 2011 development of next generation of COSSAN2011 COSSAN (hosted at University of Liverpool)2012 First Release of OpenCossan
Transfer of stochastic simulation methods into engineering practice
M.Broggi University of Liverpool 27nd October 2014 2 / 31
OpenCOSSAN History and developments
OpenCossanIntuitive and Flexible toolbox
Programmed object oriented fashion in MATLAB R©Modular Framework: easy to use/reuse components
M.Broggi University of Liverpool 27nd October 2014 3 / 31
OpenCOSSAN History and developments
OpenCossanIntuitive and Flexible toolbox
Programmed object oriented fashion in MATLAB R©Modular Framework: easy to use/reuse components
M.Broggi University of Liverpool 27nd October 2014 3 / 31
OpenCOSSAN High Performance capability
OpenCossanParallelization solutions
1 Loop parallelismshared memory multi-coreuse Matlab parfor command
2 Job parallelismDistributed memory clusterindependent jobs sent throughGridEngine
3 Hybrid Job+Loop parallelismEach job start a pool of 12 Matlabworkers
M.Broggi University of Liverpool 27nd October 2014 4 / 31
OpenCOSSAN Algorithm and tools
Algorithms and toolsAdvanced Monte Carlo Simulation
Importance SamplingLine SamplingSubset Simulation
Pf =∫IF (x) gX
fX(x)gX(x)
dx
Pf = 1N∑N
k=1 IF (X(k))w(X(k))
Recent advances
Patelli, E.; Pradlwarter, H. J. & Schueller, G. I.
On Multinormal Integrals by Importance
Sampling for Parallel System Reliability
Structural Safety, 2011, 33, 1-7−1 0 1 2 3 4 5
−1
0
1
2
3
4
5
U1
U2
Moderately non−linear limit state functions − Efficient IS
g1g2
g1⋂
g2
samples ∈ g1 ∩ g2
samples /∈ g1 ∩g2
Intersection point
M.Broggi University of Liverpool 27nd October 2014 5 / 31
OpenCOSSAN Algorithm and tools
Algorithms and toolsAdvanced Monte Carlo Simulation
Importance SamplingLine SamplingSubset Simulation
Pf = 1NL
∑NLi=1 p(i)
f
Pf = 1NL
∑NLi=1 Φ(−c(i))
Recent advances
de Angelis, Patelli, Beer, Advanced linesampling for efficient robust reliabilityanalysis Structural Safety, submitted
M.Broggi University of Liverpool 27nd October 2014 5 / 31
OpenCOSSAN Algorithm and tools
Algorithms and toolsAdvanced Monte Carlo Simulation
Importance SamplingLine SamplingSubset Simulation
Pf = P
(m⋂
i=1
Fi
)P(F1)
m−1∏
i=1
P(Fi+1|Fi)
M.Broggi University of Liverpool 27nd October 2014 5 / 31
OpenCOSSAN Algorithm and tools
Surrogate (meta) model
Approximation of input/outputrelations with simplemathematical expression
Response surfaceKrigingArtificial Neural NetworksPolyharmonic Splines
Great reduction in computationaltime
M.Broggi University of Liverpool 27nd October 2014 6 / 31
Object Oriented Programming Introduction to OOP
Procedural Programming
The programming paradigm that we are most familiar withA program is a list of instructions for the computer to perform toaccomplish a given taskComponents of a program are code (subroutines or functions)and dataNo association between functions and the data on which theyoperateLanguages examples: FORTRAN, C, BASIC
M.Broggi University of Liverpool 27nd October 2014 7 / 31
Object Oriented Programming Introduction to OOP
Object Oriented Programming
Programming paradigm that uses the concept of objects thatunite data (saved as in structure fields) with associatedfunctionsEnsures that the correct functions are called by the correct dataExtreme modularity of the codeObject are used to interact with each otherAllows for a more natural, human readable codeEasier code maintenance and avoid code duplication
If you copy-paste blocks of code, you are doing it wrong!!!
Languages: C++, Objective-C, Java, C], Perl, Jscript, Python,PHP
M.Broggi University of Liverpool 27nd October 2014 8 / 31
Object Oriented Programming Introduction to OOP
Generic concepts
Class: blueprint for creating objects, defining properties andmethods, as well as default values/behaviourObject : instance of a class that has a specific state (properties)and behaviour (methods)Properties: data associated with an objectMethods: functions defined in a class and associated with anobjectInheritance: object or class (subclass) derived from anotherobject or class (superclass)
M.Broggi University of Liverpool 27nd October 2014 9 / 31
Object Oriented Programming OOP in MATLAB
Class component
A class is defined in a .m file named as the class.
classdef blockContains class definition, class attributes, and definessuperclasses
properties blockDefines all properties to be associated with a class instanceDefines attributes of all properties and default valuesMultiple property blocks for different property attributes
methods blockDefines methods associated with the class and their attributesFirst method is “special”, must have the same name as the class,and is called constructorMultiple method blocks for different method attributes
M.Broggi University of Liverpool 27nd October 2014 10 / 31
Object Oriented Programming OOP in MATLAB
Classdef block
Block delineated by classdef <class name> till endContains all the other sub-blocks ( properties, methods, etc.)No code before classdef
No code after end
M.Broggi University of Liverpool 27nd October 2014 11 / 31
Object Oriented Programming OOP in MATLAB
Properties
Properties are variablesassociated a particular classDefined in specialproperties blockCan be multiple propertiesblocks, each with ownattributesExamples of attributes1:
Access (Set-/Get-)DependentHidden
1http:
//www.mathworks.co.uk/help/matlab/matlab_oop/property-attributes.html
M.Broggi University of Liverpool 27nd October 2014 12 / 31
Object Oriented Programming OOP in MATLAB
Properties
Properties are variablesassociated a particular classDefined in specialproperties blockCan be multiple propertiesblocks, each with ownattributesExamples of attributes1:
Access (Set-/Get-)DependentHidden
1http:
//www.mathworks.co.uk/help/matlab/matlab_oop/property-attributes.html
M.Broggi University of Liverpool 27nd October 2014 12 / 31
Object Oriented Programming OOP in MATLAB
Properties
Properties are variablesassociated a particular classDefined in specialproperties blockCan be multiple propertiesblocks, each with ownattributesExamples of attributes1:
Access (Set-/Get-)DependentHidden
1http:
//www.mathworks.co.uk/help/matlab/matlab_oop/property-attributes.htmlM.Broggi University of Liverpool 27nd October 2014 12 / 31
Object Oriented Programming OOP in MATLAB
Properties
Properties are variablesassociated a particular classDefined in specialproperties blockCan be multiple propertiesblocks, each with ownattributesExamples of attributes1:
Access (Set-/Get-)DependentHidden
1http:
//www.mathworks.co.uk/help/matlab/matlab_oop/property-attributes.html
M.Broggi University of Liverpool 27nd October 2014 12 / 31
Object Oriented Programming OOP in MATLAB
MethodsMethods are MATLAB functions associated with a particularclassDefined in special methods blockCan be multiple methods blocksCan be stored in separate files in a folder named as the class(together with main .m class file)Type of methods2:
Ordinary methods - functions that act on one or more objects(plus additional data) and return a new object or some computedvalueConstructor methods - special function that creates the objectsof a classDestructor methods - function called when instance of class isdeleted (used mainly with handle objects)Statics methods - functions associated with a class that do notact on class objects
2http:
//www.mathworks.co.uk/help/matlab/matlab_oop/how-to-use-methods.htmlM.Broggi University of Liverpool 27nd October 2014 13 / 31
Object Oriented Programming OOP in MATLAB
MethodsMethods are MATLAB functions associated with a particularclassDefined in special methods blockCan be multiple methods blocksCan be stored in separate files in a folder named as the class(together with main .m class file)Type of methods2:
Ordinary methods - functions that act on one or more objects(plus additional data) and return a new object or some computedvalueConstructor methods - special function that creates the objectsof a classDestructor methods - function called when instance of class isdeleted (used mainly with handle objects)Statics methods - functions associated with a class that do notact on class objects
2http:
//www.mathworks.co.uk/help/matlab/matlab_oop/how-to-use-methods.htmlM.Broggi University of Liverpool 27nd October 2014 13 / 31
Object Oriented Programming OOP in MATLAB
MethodsMethods are MATLAB functions associated with a particularclassDefined in special methods blockCan be multiple methods blocksCan be stored in separate files in a folder named as the class(together with main .m class file)Type of methods2:
Ordinary methods - functions that act on one or more objects(plus additional data) and return a new object or some computedvalueConstructor methods - special function that creates the objectsof a classDestructor methods - function called when instance of class isdeleted (used mainly with handle objects)Statics methods - functions associated with a class that do notact on class objects
2http:
//www.mathworks.co.uk/help/matlab/matlab_oop/how-to-use-methods.htmlM.Broggi University of Liverpool 27nd October 2014 13 / 31
Object Oriented Programming OOP in MATLAB
MethodsMethods are MATLAB functions associated with a particularclassDefined in special methods blockCan be multiple methods blocksCan be stored in separate files in a folder named as the class(together with main .m class file)Type of methods2:
Ordinary methods - functions that act on one or more objects(plus additional data) and return a new object or some computedvalueConstructor methods - special function that creates the objectsof a classDestructor methods - function called when instance of class isdeleted (used mainly with handle objects)Statics methods - functions associated with a class that do notact on class objects
2http:
//www.mathworks.co.uk/help/matlab/matlab_oop/how-to-use-methods.htmlM.Broggi University of Liverpool 27nd October 2014 13 / 31
Object Oriented Programming OOP in MATLAB
MethodsMethods are MATLAB functions associated with a particularclassDefined in special methods blockCan be multiple methods blocksCan be stored in separate files in a folder named as the class(together with main .m class file)Type of methods2:
Ordinary methods - functions that act on one or more objects(plus additional data) and return a new object or some computedvalueConstructor methods - special function that creates the objectsof a classDestructor methods - function called when instance of class isdeleted (used mainly with handle objects)Statics methods - functions associated with a class that do notact on class objects
2http:
//www.mathworks.co.uk/help/matlab/matlab_oop/how-to-use-methods.htmlM.Broggi University of Liverpool 27nd October 2014 13 / 31
Object Oriented Programming OOP in MATLAB
MethodsAccessing methods
All non-static methods must accept the class instance (i.e.,object) as their first argumentStatic methods are called by referencing to the class name onlyMethods can be accessed in two ways
Directly passing the class instance as the first argumentUsing the . operator with the class instance
the class instance is implicitly passed as the first argument of themethod
More on this through practical examples...
M.Broggi University of Liverpool 27nd October 2014 14 / 31
Object Oriented Programming OOP in MATLAB
Type of Objects
Two fundamental type of Objects available in MATLABValue objectsHandle objects
Value objectRepresent entities that are not unique (e.g.: numeric values)If the variable is reassigned, a copy of the original object iscreatedIf the variable is passed to a function, the function must returnthe modified object
Handle objectRepresent a reference to a set of dataIf the a copy is created, the same data is accessedIf the variable is passed to a function that modifies it, it is notnecessary to return the object
M.Broggi University of Liverpool 27nd October 2014 15 / 31
Object Oriented Programming OOP in MATLAB
Type of Objects
Two fundamental type of Objects available in MATLABValue objectsHandle objects
Value objectRepresent entities that are not unique (e.g.: numeric values)If the variable is reassigned, a copy of the original object iscreatedIf the variable is passed to a function, the function must returnthe modified object
Handle objectRepresent a reference to a set of dataIf the a copy is created, the same data is accessedIf the variable is passed to a function that modifies it, it is notnecessary to return the object
M.Broggi University of Liverpool 27nd October 2014 15 / 31
Object Oriented Programming OOP in MATLAB
Type of Objects
Two fundamental type of Objects available in MATLABValue objectsHandle objects
Value objectRepresent entities that are not unique (e.g.: numeric values)If the variable is reassigned, a copy of the original object iscreatedIf the variable is passed to a function, the function must returnthe modified object
Handle objectRepresent a reference to a set of dataIf the a copy is created, the same data is accessedIf the variable is passed to a function that modifies it, it is notnecessary to return the object
M.Broggi University of Liverpool 27nd October 2014 15 / 31
Object Oriented Programming OOP in MATLAB
Type of ObjectsValue vs. Handle object behaviour
M.Broggi University of Liverpool 27nd October 2014 16 / 31
Object Oriented Programming OOP in MATLAB
Type of ObjectsValue vs. Handle object behaviour
M.Broggi University of Liverpool 27nd October 2014 16 / 31
Object Oriented Programming OOP in MATLAB
Type of ObjectsValue vs. Handle object behaviour
M.Broggi University of Liverpool 27nd October 2014 16 / 31
Object Oriented Programming OOP in MATLAB
Example
Hands-on exampleSome concept of MATLAB object oriented programming will beexplained through some (partially completed) examples. Theexamples will be completed either through the lecture or as anassignement.
Simple class: rectangleProperties and methodsConstructor and error checking
Child class: squareMore “involved” class: Triangle
Constructor as property/value pairsDependent properties
Flexibility through OOP: the prism class
M.Broggi University of Liverpool 27nd October 2014 17 / 31
Object Oriented Programming OOP in MATLAB
Simple class: RectangleProperties and Constructor
M.Broggi University of Liverpool 27nd October 2014 18 / 31
Object Oriented Programming OOP in MATLAB
Simple class: RectanglePublic and Private Methods
M.Broggi University of Liverpool 27nd October 2014 19 / 31
Object Oriented Programming OOP in MATLAB
Child class: squareInheritance
M.Broggi University of Liverpool 27nd October 2014 20 / 31
Object Oriented Programming OOP in MATLAB
Let’s complicate things a bitTriangles
As we know from Geometry, a triangle can be defined in manyways by giving
three sidestwo side and the angle between themone side and two anglesand many more...
All the geometric properties are dependent between one andanother!A class must be able to cope with all these possibilities ofdefining a triangle
A simple constructor is not sufficient anymoreWe need to specify which property we are setting and assign thevalueThe other dependent properties must be determined from thegiven data
M.Broggi University of Liverpool 27nd October 2014 21 / 31
Object Oriented Programming OOP in MATLAB
Class TriangleProperties
M.Broggi University of Liverpool 27nd October 2014 22 / 31
Object Oriented Programming OOP in MATLAB
Class TriangleConstructor with pairs property/value
M.Broggi University of Liverpool 27nd October 2014 23 / 31
Object Oriented Programming OOP in MATLAB
Class TriangleEfficient reutilization of methods
M.Broggi University of Liverpool 27nd October 2014 24 / 31
Object Oriented Programming OOP in MATLAB
Class TriangleDependent properties get methods and Static methods
M.Broggi University of Liverpool 27nd October 2014 25 / 31
Object Oriented Programming OOP in MATLAB
Triangle
Assignment 1Complete the Triangle class to support the definition of a triangleusing other combination of data. Check if the user passes too manyinputs and assign the right values to the public properties!
M.Broggi University of Liverpool 27nd October 2014 26 / 31
Object Oriented Programming OOP in MATLAB
OOP FlexibilityPrism class
An object can be assigned as a property to another object.
M.Broggi University of Liverpool 27nd October 2014 27 / 31
Object Oriented Programming OOP in MATLAB
OOP FlexibilityPrism class
An object can be assigned as a property to another object.All the public properties and methods will be accessible (noneed to recode them!)Using methods from other classes allows for easy to read andstreamlinde code
M.Broggi University of Liverpool 27nd October 2014 27 / 31
Object Oriented Programming OOP in MATLAB
Prism
Assignment 2Expand the Prism class to support also non-right Prisms. Changethe Prism class so that the right prism is a child class of prism.
M.Broggi University of Liverpool 27nd October 2014 28 / 31
Object Oriented Programming OOP in MATLAB
Abstract classes
Abstract classes are classes identified by the Abstract attributeAn abstract class serves as a basis (superclass) for a group ofrelated subclassesAn abstract class can define abstract properties and methodsthat every subclasses must implement.Each subclass can implement the concrete properties andmethods in a way that supports their specific requirements.Abstract classes can define properties and methods that arenot abstract.Abstract classes pass on their concrete members throughinheritance.
M.Broggi University of Liverpool 27nd October 2014 29 / 31
Object Oriented Programming OOP in MATLAB
Abstract classes
Assignment 3Create an abstract class Shape and change Rectangle and Triangleso that they inherit from Shape. Is it possible to simplify the Prismconstructor by introducing Shape?
Assignment 4Create a new class inheriting from Shape from scratch and check ifit works with Prism out-of-the-box
M.Broggi University of Liverpool 27nd October 2014 30 / 31
Object Oriented Programming OOP in MATLAB
Abstract classes
Assignment 3Create an abstract class Shape and change Rectangle and Triangleso that they inherit from Shape. Is it possible to simplify the Prismconstructor by introducing Shape?
Assignment 4Create a new class inheriting from Shape from scratch and check ifit works with Prism out-of-the-box
M.Broggi University of Liverpool 27nd October 2014 30 / 31
Object Oriented Programming OOP in MATLAB
For more help:http://www.mathworks.co.uk/help/matlab/
object-oriented-programming.html
In-line MATLAB help and documentationGoogle!
M.Broggi University of Liverpool 27nd October 2014 31 / 31