Pressure-Volume Eenergy Calculator

Slava, Thu Aug 25 2016, 10:35PM

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();


        }
    }
}
Re: Pressure-Volume Eenergy Calculator
Slava, Thu Aug 25 2016, 10:54PM

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();


        }
    }
}

Re: Pressure-Volume Eenergy Calculator
klugesmith, Fri Aug 26 2016, 06:03AM

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).
Re: Pressure-Volume Eenergy Calculator
Slava, Sat Aug 27 2016, 09:56PM

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.
Re: Pressure-Volume Eenergy Calculator
Slava, Sat Aug 27 2016, 10:01PM

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.
Re: Pressure-Volume Eenergy Calculator
Slava, Sat Aug 27 2016, 10:02PM

I just found another bug.... area = pi * r^2 .... i accidentally put, 1/2 pi r^2


Link2
Re: Pressure-Volume Eenergy Calculator
Dr. H., Sun Aug 28 2016, 10:51AM

triple posting man ... in 6 mins .... not cool....
Re: Pressure-Volume Eenergy Calculator
klugesmith, Sun Aug 28 2016, 09:16PM

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?
Re: Pressure-Volume Eenergy Calculator
Slava, Mon Aug 29 2016, 12:01AM

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
Re: Pressure-Volume Eenergy Calculator
klugesmith, Mon Aug 29 2016, 04:01AM

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