Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit



CHEM0062 Report

Perfect Gas Property Calculator

[Italic blue – notes to read. Black non-italic – the actual report]

This is in Word….. you could do it as a Jupyter notebook using markdown. You could do it in LaTeX. As long as we can read it.

Submit one of these (no matter then what you wrote it in): docx, pdf, Jupyter notebook. All figures must be included in the document and the full code listing should be included at the end of the document (as well as being submitted separately so we can run it).

 

Use Styles! I’ve added a code style here – feel free to use.

Summary

A simple application is presented where the perfect gas equation is used to calculate requested properties.

The functions of the code are described, and sample data provided with output to demonstrate the capabilities of the code.

Problem – routine and repetitive calculations

The perfect gas law states that

PV = nRT

Therefore, often, we are required to compute any of the variables p, V, n, T from data provided for the remainder. Whilst straightforward they can be prone to calculator error, require consideration of units and repetitive entry of constants. Thus often, we use a spreadsheet to perform such calculations. However, there is often the need to perform these calculations on more simple devices or where a graphical interface is neither needs or practical (e.g. on a bench instrument). Similarly, it would be advantageous to be able to compute data over ranges or a given variable in one entry e.g. to plot the pressure changes as a function of T between T1 and T2.

Program specification

To provide a single command line driven code that outputs tabulated data.

The code will allow:

o Entry of which variable is to be computed together with the required data for the other variables

o Output either single values or tabulated for ranges in formats that can be easily read on screen or converted to data for input into other codes (e.g. Excel) or to be plotted (e.g. using MatPlotLib).

At a simple level

o Calculate the pressure when  the volume is X, temperature is T and the number of moles is n.

o Calculate the T when p is etc etc

More sophisticated input would be that the units of the variable can be input if they are non-standard (within reason!) e.g. degree C instead of K, atm instead of Pa

A development would then be to allow the entry of ranges.

· Calculate the pressure when the volume is X, the number of moles is n over the pressure range P1 to P2.

Output now is a table (on screen or a file) and/or a plot to the screen (or file?) of the data output.

Approach

1. To write functions to perform the required calculation for a single point. i.e. solve p=nRT/V, V= etc

2. To parse input for single data points. e.g. need to know what to calculate (T) and then parse the values of p, V and n.

3. Write output in a variety of formats: screen, file (text as csv, xls?) and graphical plot

4. To parse entry of ranges for a variable (and step size)

5. To parse output control

 

Code development

See perfectgas.py for full code listing

Here is a diagram of how it works…..

I now explain how the key functions were constructed and operate

One initial design decision was to decide whether there should be separate functions to calculate each variable or one where the input makes clear which to perform and to return a single value. The decision to use a single function was based on the need later for range to be used – ….. justify

Therefore, my initial design had a function which was called depending on what was to be calculated

#Here is the function

def perfectgas_p(Volume, Moles, Temperature):
  p = Moles * R * Temperature / Volume

  return (p)

def perfectgas_V(pressure, Moles,Temperature):

Volume = Moles * R * Temperature  / pressure

return (Volume)

etc

But to allow more flexibility I changed to a single function

def perfectgas(what, A,B,C) # pass what we want to calc

if (what = p):

gas_prop = A * R * B /C

elif (what = V):

      gas_prop = A * R * B / C

….


NOW WRITE MORE DEPENDING ON WHAT YOU HAVE DONE THAT IS WORTH TALKING ABOUT.

Used a Library? Say what, where you got it from, how you use it – explain that you know how it works!

 

Example:

[Not applicable to Python fortunately – If you need to know the code is in a language called REXX, not really designed for maths…]

Here is an example of how to show that you have taken a problem (the lack of a ln function) and solved it – so you explain why and how (the text) and then show in the code snippet how you coded it. You would only do this for “clever” bits.

The same in your report would be shorter as you only need to discuss the Beer-Lambert law and how you did the conversion (do you change the yvalue or put in a new list, why? Etc)

 

 

This is coded for absorbance to transmittance in the function CONVTOTRANS (extracts below). Whilst a function for factorial exists, for speed here the values of 1! to 5! are stored as a series of values in the list factorial.n alleviating the need to calculate for each y-value (stored in the list yvalues)

 

….

 


Tests and Sample outputs

To test that the code works, the tests were constructed.

Each set tests a specific feature to verify code is working correctly and that user input is trapped.

Test and input

Why?

Expected output

Calculated p with V  = n =  1, T = 10

perfectgas -p -V 1 -n 1 -T 10

Check Simple parse and R is okay

Pressure = 83.145 Pa

Etc etc etc

 

What should you test?

· Does it give the answer you expect for sensible input?

· Does it cope with weird values? [-ve T in K? but is -ve in degree C coped with?]

· Extremes – what if (here) I put in a bonkers range that will take ether forever to do or fill up any storage device you own! Should your code limit what the user can do? E.g. Temp range from 0 – 1000000 K in step of 0.0000001 K to a file makes a file that is 10,000,000,000,000 lines long. Or 200 Gb! Should you check if there is enough disk space? Or just say “don’t be daft”. You should build that into your design.

Think about your tests as / before you write. Test functions for correctness as you go!

How many examples? Enough to convince the reader that you know what you are doing. And if you don’t think of it anyone testing your code will. If I can break your code easily is that good code? A good report? [and sorry, a good mark?]

 

 

Or any way that is good for your project

Eg. If read in a spectrum and peak pick, test that it works with different peak intensities etc. Show what the code does and that it does it right! You can put in “and it doesn’t work because…. You read in data an convert it to a %scale – show you have found the maximum and minimum in the original and that thee processed data has these data set to 100 and 0. How? A short example or probably easier a graph right?


User documentation

Brief set of instructions so that anyone can take your test data and run it themselves.

Your marker will read these and use them to run the test. Does it work?

Suggest two pages should be enough to tell the user how to run your code and what options they can choose.

· What the code does

· Installation instructions if needed

· How to run

· What options

· A few examples of the more common ways you could use, with example input and output. (these can be the same as the test you describe)

Ideally running the code will present the user with some instructions (not the prompts it may have rather explanations of what is needed, expected and any options)

Using my example above

Perfectgas

The perfectgas.py code calculates the properties of a perfect gas….. etc using the perfect gas law [give the equation – you want to show the user you are doing science not “oh it’s a magic box that does… but I won’t tell you how (which is what people selling you AI will do – there is science in there but they can’t explain it so go “woooooo look…..”)]

To do this you provide the code with what variable you wish the calculate and ….. you can do ranges….

The code can output single values to the screen or if ranges are given, to a variety of outputs including Excel files and figures, including saving as jpg

Installation

Copy perfectgas.py to a working directory (I’d be more explicit)

Open a command prompt (again you may need to say more)

Usage

Simple and more complex examples. You need to explain how each option can be called. If there are restrictions (e.g. maybe we only allow graphs to drawn if you are on a “proper computer”)

 

Ideally running the code will present the user with some instructions (not the prompts it may have, but rather explanations of what is needed, expected and any options)

python perfectgas.py -h  [the h for help]

 

would print to the screen

Usage:

perfectgas -gasprop var1 var2 var3

perfectgas -gasprop range1 [step] var2 var3 [-g [name]] [-t] [-x [name]]

 

Where gasprop is n, p, V, or T and then var1 var2 var3 being the other variables. Ranges can be put etc  etc

Output options

-g plot a graph of gasprop over range1. Save to name.jpg if given as well.

-t tabulate to screen

-x write to an Excel file (called name.xlsx if given – otherwise perfectgas.xlsx

 

[for a simple code this is almost all the documentation as well!]

 

Conclusions

The above code is lovely. The code utilises the following concepts in Python…. Mathematics, functions, plotting and input parsing.

{list what you have learned to do – show off, link to the checklist in the course description}.

I have given examples of how to use the code which….

I’m very pleased with portion of the code that parses user input as I found it very challenging to understand regular expressions…..

[But don’t say just that – it’s a little bland right?]

Given time I would have liked to have been able to better specify the plotting, allowing more control by the user and making the plots of publishable quality particularly in the formatting of units.

Further developments could include…. a GUI, a way to do multiple ranges to give 3-D plots, and the ability to consider the van der Waals equation rather than the perfect gas equation.

Code Listing

Pasted copy of you code, reasonably formatted so it can be referred to when reading the report.