0% found this document useful (0 votes)
73 views21 pages

CIVE50003 Computational Methods II - Lecture VII - 200223 V2

This document provides an overview of Lecture 7 from the Computational Methods II module. It introduces object-oriented programming concepts in MATLAB, including: - Using classes and structs to organize data in a structured way - Defining your own MATLAB classes through classdef files, with properties to define variables and methods to define functions - Creating instances of a class and accessing object properties and methods - Encouraging students to use object-oriented programming for their coursework, defining a main class with appropriate properties, methods and encapsulation of reusable code.

Uploaded by

Twiny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views21 pages

CIVE50003 Computational Methods II - Lecture VII - 200223 V2

This document provides an overview of Lecture 7 from the Computational Methods II module. It introduces object-oriented programming concepts in MATLAB, including: - Using classes and structs to organize data in a structured way - Defining your own MATLAB classes through classdef files, with properties to define variables and methods to define functions - Creating instances of a class and accessing object properties and methods - Encouraging students to use object-oriented programming for their coursework, defining a main class with appropriate properties, methods and encapsulation of reusable code.

Uploaded by

Twiny
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Computational Methods II

MEng Module CIVE50003

Lecture 7

Dr Adam Jan Sadowski

Spring Term 2023

CIVE50003 Computational Methods II – Lecture 7 1


The last lecture
 Today will be a little different. No more FE theory!

 I want to present a powerful feature of Matlab programming


that you must use in your coursework

 After a short learning curve you will find that the code becomes
more manageable, shorter, less repetitive, and thus easier to
debug and far more elegant

 Elegant code is maintainable code: easy to read and debug

 I am talking about object-oriented programming, implemented


in Matlab through the use of user-defined Classes
CIVE50003 Computational Methods II – Lecture 7 2
The Matlab Class
 Every single piece of data that you create in Matlab is stored in
memory as a particular Class
 For example:
real no. 1.245 2×2 matrix of ones complex no. 1+2i the characters ‘blah’

π in double precision π in single precision The Matlab workspace tells you what you are
(16 significant figures) (8 significant figures) storing, its class, value, name and size (bytes)

 Matlab knows Class automatically


CIVE50003 Computational Methods II – Lecture 7 3
Matlab structs
 You have seen these before – these are abstract data structures

 The ‘field’ of the struct is its attribute or property, and beside it


you have its value (can be anything, a string, scalar, matrix etc.)
 To illustrate this, we create a struct full of general junk:

A ‘struct’ data structure has its own Matlab Class


CIVE50003 Computational Methods II – Lecture 7 4
Matlab structs
 MYSTRUCT itself is of a Class struct, but what is
contained within its fields can be of any data type

 The variable a is a string ‘blah’ and thus of Class char


 Variables b and c are a real scalar and matrix, and thus of Class double
 So the Matlab struct Class is already quite general and powerful

CIVE50003 Computational Methods II – Lecture 7 5


Matlab structs
 The struct may be used to store data in an organised and
convenient way for the user or programmer

 Let us say we are working on a program that analyses very


many cardboard boxes, their geometry and contents
 Our first box should have a width w = 50 cm, depth d = 30 cm
and height h = 40 cm, and it should be called ‘Timmy’

 What is the surface area A?


 What is the volume V?
 How can we ‘put’ things in the box?

 Well, let’s model Timmy by a struct


CIVE50003 Computational Methods II – Lecture 7 6
Matlab structs
 We can start by setting up the arrangement of the struct

 Except the name, the fields are currently empty. So we define:

 For now we have:

CIVE50003 Computational Methods II – Lecture 7 7


Matlab structs
 If we want the surface area and volume we have to calculate
these ourselves:

 Similarly, if we want to ‘put’ a ‘1’ or ‘4’ or ‘-8’ into box, we


similarly need to code this:

 Our final BOX1 struct is:

CIVE50003 Computational Methods II – Lecture 7 8


Creating your own Matlab Classes
 Structs are nice, but if you want to create a new instance of the
same struct (with the same fields), you have to code it all over
again (can become very cumbersome)
 If you are using a custom-written data structure repeatedly, it is
best to program it as its own Class
 We can create a general Class BOX, and invoke instances of it
whenever necessary
 Type help classdef in Matlab to read up on this powerful capability

CIVE50003 Computational Methods II – Lecture 7 9


Creating your own Matlab Classes
 A Class definition goes in its very own .m file that must have the
same name as the Class. So a Class BOX is stored in BOX.m

 The general structure is thus:

 Properties are the fields of the


Class, it is where you define
the variables (any data type)
associated with each instance of this Class

 Methods are where you define functions that are associated with
each instance of the Class, operating on the object itself
CIVE50003 Computational Methods II – Lecture 7 10
Class BOX
 We start our Class BOX as follows

 You should list all fields that the Class BOX will ever have
here, but you don’t necessarily have to give any a value

 Now any instance of this Class BOX will have these fields

CIVE50003 Computational Methods II – Lecture 7 11


Class BOX
 Once we have specified the properties, we go on to the
methods

 The very first method must be the one invokes to actually


create an instance of Class BOX (the ‘Constructor’ method)
 It doesn’t need to do any calculations, it just passes inputs
and sets them as values of the respective properties (fields)
CIVE50003 Computational Methods II – Lecture 7 12
Class BOX
 Once an instance of Class BOX is created (i.e. we have given the
box a name, width, depth and height), we can do calculations on it

 Two new methods:


 obj = Area(obj)
 obj = Volume(obj)

 The necessary data


is already stored
within the object
properties, so no
new arguments have to passed to these functions
 Note ‘obj’ refers to any future instance (object) of the Class BOX
CIVE50003 Computational Methods II – Lecture 7 13
Class BOX
 Finally, if we want to pass external data to the object, we can
do so. There is a special syntax that we must respect, though!

 We have now defined my entire Class BOX


 Nothing more should be written after the final end command

CIVE50003 Computational Methods II – Lecture 7 14


Class BOX
 Let us use our new Class BOX to create new BOXes easily

 So now:

CIVE50003 Computational Methods II – Lecture 7 15


Class BOX
 If we want to create new BOXes? Easy.

 And another one:

CIVE50003 Computational Methods II – Lecture 7 16


Class BOX
 We can create arrays full of Class BOX objects

 So BOX1 can be accessed as BOXES(1) etc.


 You can create for loops to access each BOX in turn etc.
 This array can be accessed as usual, but you cannot do
something like this:
 BOXES is Class BOX but a = 2 is Class double – these are
different data structures and the operation is not supported
CIVE50003 Computational Methods II – Lecture 7 17
OOP for your coursework
 You need a single Bridge Class in a single .m file
 But you can have multiple calling .m files which make use the Class
e.g. one .m file per Q if you want
 But these calling .m files should be quite small (not much code), and
the bulk of your code (certainly whatever is reused often) should be
‘encapsulated’ within the Class

 Aside from relevant properties, the Class should contain:


 A Constructor method
 This is what brings an instance of your Class into existence

 Creates the Data Object and allows you to use it further

 An Assembler method
 This is what constructs your system matrices (more than one)

 Think carefully how often the expensive assembly operation must


be performed for each analysis....
CIVE50003 Computational Methods II – Lecture 7 18
OOP for your coursework
 Aside from relevant properties, this Class should contain:
 Solve methods
 Think carefully how to solve an equation of the type [A]{x} = {b}
when you have multiple {b}
 You are asked to investigate Cholesky decomposition and to use
this as efficiently as possible. This permits, for some matrices, the
decomposition [A] = [G]’[G] where [G] is an upper triangular matrix.
 Explain why Cholesky may be an appropriate method, and how it
can be used as efficiently as possible for the computation of
influence lines.
 Post-processor method
 Each analysis asks for something different from each solver

 Post-processing includes plotting figures (deformed shapes, results


curves etc.)
 Any other method you feel is appropriate!

CIVE50003 Computational Methods II – Lecture 7 19


OOP for your coursework
 A premium is placed on:
 Correct code that is not buggy and runs to completion

 Well structured code that shows you have thought through the logic

 Well commented code that explains the policy but not the mechanism

 Good use of encapsulation (hiding reusable code inside the Class)

 Minimising duplication of computational effort across the Qs

 A professionally presented report with a title page, page numbers, no


spelling errors, no hand-drawn figures or equations

 Good explanations of results with structural & computational arguments


CIVE50003 Computational Methods II – Lecture 7 20
Python
 A ‘true’ and very powerful object-oriented programming language
 It is completely free and comes with an advanced programming
environment – Anaconda or simply Visual Studio Code (I use this)

 Python can be used for very advanced data manipulation and


visualisation thanks to its ‘native’ implantation of objects

 Not quite as convenient for matrices as MATLAB

 Used as:
 Script interface with the powerful ABAQUS FE software
 Data analysis and data science (e.g. Panda)
 Machine learning (e.g. PyTorch)
CIVE50003 Computational Methods II – Lecture 7 21

You might also like