Welcome
Username or Email:

Password:


Missing Code




[ ]
[ ]
Online
  • Guests: 19
  • Members: 0
  • Newest Member: omjtest
  • Most ever online: 396
    Guests: 396, Members: 0 on 12 Jan : 12:51
Members Birthdays:
One birthday today, congrats!
GrantX (34)


Next birthdays
03/30 Adam Horden (39)
03/30 Mr.Warwickshire (23)
03/31 Swedish Coiler (41)
Contact
If you need assistance, please send an email to forum at 4hv dot org. To ensure your email is not marked as spam, please include the phrase "4hv help" in the subject line. You can also find assistance via IRC, at irc.shadowworld.net, room #hvcomm.
Support 4hv.org!
Donate:
4hv.org is hosted on a dedicated server. Unfortunately, this server costs and we rely on the help of site members to keep 4hv.org running. Please consider donating. We will place your name on the thanks list and you'll be helping to keep 4hv.org alive and free for everyone. Members whose names appear in red bold have donated recently. Green bold denotes those who have recently donated to keep the server carbon neutral.


Special Thanks To:
  • Aaron Holmes
  • Aaron Wheeler
  • Adam Horden
  • Alan Scrimgeour
  • Andre
  • Andrew Haynes
  • Anonymous000
  • asabase
  • Austin Weil
  • barney
  • Barry
  • Bert Hickman
  • Bill Kukowski
  • Blitzorn
  • Brandon Paradelas
  • Bruce Bowling
  • BubeeMike
  • Byong Park
  • Cesiumsponge
  • Chris F.
  • Chris Hooper
  • Corey Worthington
  • Derek Woodroffe
  • Dalus
  • Dan Strother
  • Daniel Davis
  • Daniel Uhrenholt
  • datasheetarchive
  • Dave Billington
  • Dave Marshall
  • David F.
  • Dennis Rogers
  • drelectrix
  • Dr. John Gudenas
  • Dr. Spark
  • E.TexasTesla
  • eastvoltresearch
  • Eirik Taylor
  • Erik Dyakov
  • Erlend^SE
  • Finn Hammer
  • Firebug24k
  • GalliumMan
  • Gary Peterson
  • George Slade
  • GhostNull
  • Gordon Mcknight
  • Graham Armitage
  • Grant
  • GreySoul
  • Henry H
  • IamSmooth
  • In memory of Leo Powning
  • Jacob Cash
  • James Howells
  • James Pawson
  • Jeff Greenfield
  • Jeff Thomas
  • Jesse Frost
  • Jim Mitchell
  • jlr134
  • Joe Mastroianni
  • John Forcina
  • John Oberg
  • John Willcutt
  • Jon Newcomb
  • klugesmith
  • Leslie Wright
  • Lutz Hoffman
  • Mads Barnkob
  • Martin King
  • Mats Karlsson
  • Matt Gibson
  • Matthew Guidry
  • mbd
  • Michael D'Angelo
  • Mikkel
  • mileswaldron
  • mister_rf
  • Neil Foster
  • Nick de Smith
  • Nick Soroka
  • nicklenorp
  • Nik
  • Norman Stanley
  • Patrick Coleman
  • Paul Brodie
  • Paul Jordan
  • Paul Montgomery
  • Ped
  • Peter Krogen
  • Peter Terren
  • PhilGood
  • Richard Feldman
  • Robert Bush
  • Royce Bailey
  • Scott Fusare
  • Scott Newman
  • smiffy
  • Stella
  • Steven Busic
  • Steve Conner
  • Steve Jones
  • Steve Ward
  • Sulaiman
  • Thomas Coyle
  • Thomas A. Wallace
  • Thomas W
  • Timo
  • Torch
  • Ulf Jonsson
  • vasil
  • Vaxian
  • vladi mazzilli
  • wastehl
  • Weston
  • William Kim
  • William N.
  • William Stehl
  • Wesley Venis
The aforementioned have contributed financially to the continuing triumph of 4hv.org. They are deserving of my most heartfelt thanks.
Forums
4hv.org :: Forums :: Computer Science
« Previous topic | Next topic »   

Pressure-Volume Eenergy Calculator

1 2 
Move Thread LAN_403
Slava
Thu Aug 25 2016, 10:35PM Print
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
I just wrote a program that calculates the energy in a pressure tank pressurized by a piston.

Please check it out and let me know if I made any mistakes....

For some reason my P*V output stays constant...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace PressureEnergy001
{
    class Program
    {
        static void Main(string[] args)
        {
            double w = 1;
            double h = 1;
            double start_depth = 1;
            double depth = start_depth;
            double p1 = 103421;
            double p2 = 103421;
            double v1 = 0;
            double v2 = 0;

            double energy = 0;

            double dx = 0.0001;

            while(depth > 0.05)
            {
                v1 = depth * (w * h);

                v2 = (depth - dx) * (w * h);

                p1 = p2;

                p2 = (p1 * v1) / v2;

                energy += (dx * ((p2 * w * h) + (103421 * w * h)));

                depth -= dx;


                Console.WriteLine("--------------------------------------------");

                Console.WriteLine("DEPTH: " + depth);
                Console.WriteLine("PRESSURE: " + Math.Round(p2 / 6894.76,2) + " PSI");
                Console.WriteLine("VOLUME: " + Math.Round(w * h * depth,2));

                Console.WriteLine("PISTON ENERGY: " + Math.Round(energy,2));
                Console.WriteLine("P * V: " + Math.Round(p2 * (w * h * depth), 2));


                Thread.Sleep(10);




            }


            Console.ReadLine();


        }
    }
}
Back to top
Slava
Thu Aug 25 2016, 10:54PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
Corrected for atmospheric pressure...

Still getting 103421 Joules from any volume * pressure...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace PressureEnergy001
{
    class Program
    {
        static void Main(string[] args)
        {
            double w = 1;
            double h = 1;
            double start_depth = 1;
            double depth = start_depth;
            double p1 = 103421;
            double p2 = 103421;
            double v1 = 0;
            double v2 = 0;

            double energy = 0;

            double dx = 0.0001;

            while(depth > 0.01)
            {
                v1 = depth * (w * h);

                v2 = (depth - dx) * (w * h);

                p1 = p2;

                p2 = (p1 * v1) / v2;

                energy += (dx * ((p2 * w * h) - (103421 * w * h)));

                depth -= dx;


                Console.WriteLine("--------------------------------------------");

                Console.WriteLine("DEPTH: " + depth);
                Console.WriteLine("PRESSURE: " + (Math.Round(p2 / 6894.76,2) - 14.659) + " PSI");
                Console.WriteLine("VOLUME: " + Math.Round(w * h * depth,2));

                Console.WriteLine("PISTON ENERGY: " + Math.Round(energy,2));
                Console.WriteLine("P * V: " + Math.Round(p2 * (w * h * depth), 2));


                Thread.Sleep(10);




            }


            Console.ReadLine();


        }
    }
}

Back to top
klugesmith
Fri Aug 26 2016, 06:03AM
klugesmith Registered Member #2099 Joined: Wed Apr 29 2009, 12:22AM
Location: Los Altos, California
Posts: 1714
I see a lot of simplifying assumptions, for example it looks like the gas temperature is constant. Any energy calculations will miss the heat going in or out to maintain the temperature, as you move the piston.

In that case, the product P*V had better be constant. It's called Boyle's Law.
You can't figure the work by taking the difference between initial and final P*V values, even though pressure is dimensionally equivalent to energy per unit volume.

Now if you want to figure the energy or work needed to compress the gas by moving the piston,
it would be proper to integrate P * dV in small steps. Same as integrating force * dX.
You already know that P would have to be the difference between absolute pressures inside and outside the cylinder.

I can think of one test case where the numbers are particularly simple.
Start with a vacuum between the piston and the closed cylinder end.
For any linear position X, P_inside = 0 and P_outside = 1 atm. Work is directly proportional to stroke length, piston area, and atmospheric pressure.
At standard pressure (101.325 kPa, unless you are a follower of IUPAC) it's, uh, 101.325 kJ/m^3 = 101.325 joules per liter of displacement.
That's the sucking energy of a vacuum reservoir.

Here is another interesting test case.
Start with some initial values of P and V, and let the outside pressure be zero (as if we are doing the experiment in space).
How much work can be done by isothermal, Boyle-ian expansion of the gas pushing on the piston?

No matter how far the piston moves (if cylinder is long enough), the gas pressure will never reach zero and there will always be force on the piston.
One might guess that the cumulative work approaches a limit, which might happen to be the product of initial pressure and initial volume.
Nope. The cumulative work increases without bound. Just like integral(1/x dx) = ln(x).
Back to top
Slava
Sat Aug 27 2016, 09:56PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
I fixed some bugs....

Link2

I start with 15 psi on the inside of the chamber, and 15 psi on the outside.... (at first i made the imstake to set inside pressure to 0, and then there was nothing to compress.... you can't compress a vacuum)

I move the piston in by a quantity of delta_x ....then i use the formula, force time distance to get a finite energy amount and add it to the total....

then I compute the new chamber pressure using P1V1 = P2V2 ...P2 = P1V1 / V2

That's pretty much it.
Back to top
Slava
Sat Aug 27 2016, 10:01PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
Here is my latest code...


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace PressureEnergy001
{
    class Program
    {

        static void DrawPiston(double depth)
        {
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("\n");
            Console.WriteLine("   ###########################################");
            Console.Write("   #");

            Console.ForegroundColor = ConsoleColor.Red;
            for (int i = 0; i < (depth * 40); i++)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.Write("X");
            }

            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine("|------------------");
            Console.ForegroundColor = ConsoleColor.Blue;
            Console.WriteLine("   ###########################################");

        }


        static void Main(string[] args)
        {

            //Thread.Sleep(15000);

            double radius = 0.01143;


            double w = Math.Sqrt(.5 * 3.14159 * Math.Pow(radius, 2));
            double h = Math.Sqrt(.5 * 3.14159 * Math.Pow(radius, 2));

            //double w = 1;
            //double h = 1;

            double depth = 1;
            double p1 = 103421;
            double p2 = 103421;
            double v1 = 0;
            double v2 = 0;

            double depth_stop = 0.01;

            double energy = 0;

            double dx = 0.001;

            while (depth > depth_stop)
            {
                v1 = depth * (w * h);

                v2 = (depth - dx) * (w * h);

                p1 = p2;

                p2 = (p1 * v1) / v2;

                energy += (dx * (((p2 - 103421) * w * h)));

                depth -= dx;


                Console.Clear();

                Console.ForegroundColor = ConsoleColor.Green;


                Console.WriteLine("DELTA X (mm): " + (dx*1000));
                Console.WriteLine("RADIUS OF PISTON (m): " + radius);
                Console.WriteLine("AREA OF PISTON (m^2): " + (w * h));
                Console.WriteLine("DEPTH (mm): " + (depth*1000));
                Console.WriteLine("VOLUME (meters^3): " + Math.Round(w * h * depth, 8));
                Console.WriteLine("PRESSURE (PSI): " + (Math.Round((p2 - 103421) / 6894.76, 2)));

                Console.WriteLine("PISTON FORCE (LB): " + Math.Round((p2 - 103421) * w * h * .224809,2));

                Console.WriteLine("PISTON ENERGY (Joules): " + Math.Round(energy, 2));

                Console.WriteLine("TANK ENERGY (Joules): " + ((p2 - 103421) * (w * h * depth) * Math.Log((103421 / (p2 - 103421)), Math.E)));
                Console.WriteLine("TANK ENERGY (Joules): " + ((p2) * (w * h * depth) * Math.Log((103421 / (p2)), Math.E)));


                DrawPiston(depth);


                Thread.Sleep(100);




            }


            Console.ReadLine();


        }
    }
}



I found a formula on... Link2

and put it in my code to compare the formula to my finite element calculator... but for some reason the numbers don't match... please help.
Back to top
Slava
Sat Aug 27 2016, 10:02PM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
I just found another bug.... area = pi * r^2 .... i accidentally put, 1/2 pi r^2


Link2
Back to top
Dr. H.
Sun Aug 28 2016, 10:51AM
Dr. H. Registered Member #931 Joined: Mon Jul 30 2007, 05:25PM
Location: Bulgaria
Posts: 486
triple posting man ... in 6 mins .... not cool....
Back to top
klugesmith
Sun Aug 28 2016, 09:16PM
klugesmith Registered Member #2099 Joined: Wed Apr 29 2009, 12:22AM
Location: Los Altos, California
Posts: 1714
Yeah, what Dr H said. Are you familiar with the edit button?

For help with the program, could you please post the exact details of a test case? It's easier to work it from scratch, and give step by step results, than to actually review your code.
- Cylinder diameter or area
- Cylinder length (piston position) X: initial value, final value, and step size.
- Initial gas pressure, inside and out.
- Confirm that we are talking about isothermal compression or expansion, and ideal gas behavior.
- What value does your program give for the work? What is the correct value, and how did you get it?

By the way, is the program in question an assignment for a class you are taking or teaching?
Back to top
Slava
Mon Aug 29 2016, 12:01AM
Slava Registered Member #518 Joined: Tue Feb 13 2007, 05:20AM
Location: New York
Posts: 168
klugesmith wrote ...

Yeah, what Dr H said. Are you familiar with the edit button?

For help with the program, could you please post the exact details of a test case? It's easier to work it from scratch, and give step by step results, than to actually review your code.
- Cylinder diameter or area
- Cylinder length (piston position) X: initial value, final value, and step size.
- Initial gas pressure, inside and out.
- Confirm that we are talking about isothermal compression or expansion, and ideal gas behavior.
- What value does your program give for the work? What is the correct value, and how did you get it?

By the way, is the program in question an assignment for a class you are taking or teaching?



Nah, I'm just doing it for fun. I'm practicing finite element algorithms so that I can get by without calculus smile ...

Today I wrote a program that simulates terminal velocity of rain drops given their diameter....

Link2


Last week I wrote a model rocket simulator....

Link2
Back to top
klugesmith
Mon Aug 29 2016, 04:01AM
klugesmith Registered Member #2099 Joined: Wed Apr 29 2009, 12:22AM
Location: Los Altos, California
Posts: 1714
The wikipedia article you cited is pretty sloppy in places.
That formula accounts for force from cylinder pressure between pA and pB, but not for force from the atmosphere on the opposite side of the piston.
1472441475 2099 FT177722 Pve Example

I computed the wiki example case in Excel, with compression in 78 discrete steps. Cylinder area 1 m^2. Piston moves from x = 70 m to x = 1 m, and internal pressure increases from 1 bar to 70 bar. Got a work value of 29.76 MJ when p_atm = 0.
1472442357 2099 FT177722 Pve Sim 0


External pressure of 1 bar changes the answer to 22.86 MJ, a loss of exactly 6.9 MJ (same as 1 bar x 1 m^2 x 69 m). We can make the correction on the bottom line or on each incremental step:
1472443119 2099 FT177722 Pve Sim 1

By the way, the wikipedia formula agrees* with the curious case I mentioned in previous email. If there's no atmosphere, there is no limit to the work extractable from a finite amount of gas expanded isothermally (as we let pA approach zero).

* Saying that wikipedia agrees with a statement is not the same as saying the statement is confirmed because wikipedia agrees with it. smile
Back to top
1 2 

Moderator(s): Chris Russell, Noelle, Alex, Tesladownunder, Dave Marshall, Dave Billington, Bjørn, Steve Conner, Wolfram, Kizmo, Mads Barnkob

Go to:

Powered by e107 Forum System
 
Legal Information
This site is powered by e107, which is released under the GNU GPL License. All work on this site, except where otherwise noted, is licensed under a Creative Commons Attribution-ShareAlike 2.5 License. By submitting any information to this site, you agree that anything submitted will be so licensed. Please read our Disclaimer and Policies page for information on your rights and responsibilities regarding this site.