Mastodon Politics, Power, and Science

Thursday, March 20, 2014

4th Order Runge-Kutta numerical method.

Wrote a C object to manage the code and data around generating Runge-Kutta 4th order interpolation of data.  Fairly efficient algorithm.  I estimate that it will process about 3 million points a second on a single core of a slow netbook computer.  I tried to use the same variable names in the interface that seemed to be used in most books and papers on this topic.

I  do auto ranging based on a step size per number unit.  So if your function runs from 0 to 5 and you ask for 5 steps per number, then you would end up with 5*5+1 = 26 max steps.  The +1 comes from the initial value at the start of the range, then you would do 5 steps in the 1 interval, 5 steps in the 2 interval, and so on.

Source code is here:
https://github.com/BuckRogers1965/Examples/blob/master/Math/NumericAnalysis/RungeKuttaTest.c
https://github.com/BuckRogers1965/Examples/blob/master/Math/NumericAnalysis/RungeKutta.h
https://github.com/BuckRogers1965/Examples/blob/master/Math/NumericAnalysis/RungeKutta.c



The example program, you pass in the starting value, the start and end points, the step per unit, and the function you are interpolating.
 
The object does the rest with the interface below:


The Object Interface to the Runge-Kutta module.
Once you create an object you can retrieve all the properties and data values for the object.
When you are done, you dispose the object to free the memory. 


The output for the example from the book, exact same values.

Saturday, March 15, 2014

The recent Apple SSL issue.

The double goto fail line caused the next line to be unreachable.  A fact that should have been see in compiler warnings, in code reviews, and in the IDE itself. 



Tuesday, March 11, 2014

GNU MP arbitrary precision library.

Tonight I am learning the arbitrary precision math library from gnu called gmp.   It is tough to work only with functions instead of just using math expressions.   It takes multiple lines of obscure code to do what you can do with a single C expression.  Instead of saying:
bb = floor (n *sqrt(.5) +1);
You have to say:
  mpf_t n;  mpf_init2(n, 256);  mpf_set_ui(n, 10);  mpf_pow_ui(n,n,12);
  mpf_t bb;  mpf_init2(bb, 256);  mpf_set_ui(bb, 5);  mpf_div_ui(bb, bb, 10);
  mpf_t srpf;  mpf_init2(srpf, 256);  mpf_sqrt(srpf, bb);

  mpf_mul(bb, srpf, n);  mpf_add_ui(bb,bb, 1);  mpf_floor(bb, bb);

Why deal with this extra complexity?  Because it is amazingly easy to run into problems of precision with just the 64 bits that the standard data structures give you.  Simple things like working with numbers in the 10^16 range, or 100!, or the 5000th Fibonacci number and you get the wrong values coming out of your functions.   The 5000th Fibonacci number runs you out of bits in the exponent of long double.

I am playing around with the problems on the Project Euler site and am running into a lot of situations where I cannot solve the problems with C because the problem sets have large enough numbers that they are even overflowing 64 bit integers and floating point numbers are fuzzy enough that two things that should match don't because the lowest precision bits don't match, when they should.

Monday, March 10, 2014

Going solar.

If you spend a fortune on 50KW of solar panels and the batteries to store 3 days worth of power, you can live just like you always have, with no changes needed.  But this is foolish, expensive, and wasteful. A better way to go solar is to adjust your life to require a much lower level of power, and size the solar panels to meet that reduced footprint.  This means that you need less batteries, less panels, less wiring, and less things to go wrong.

Of course, this would require people to take positive action, so it would be bad and difficult to get anyone to agree to do.  A better way would be to let the appliances work together intelligently in order to reduce the maximum power draw, and to run the most power hungry appliances during times when the batteries are charged and you can run entirely off the solar panels.

Power efficient appliances.

There are freezers and refrigerators that are much more efficient and lower power than anything you can buy at a mall store.  http://www.sundanzer.com/  These freezers and refridgerators are smaller than the huge ones in most people's houses today, but you have to ask yourself, do we really need such huge fridges?  Can't we make do with less rather than eventually having to make do with none.  There are washing machines that use much less energy and water to run as well.

One of the things you have to watch out for are hidden energy costs.  A 1200 Watt microwave oven actually draws 2400 watts of power.  Many gas ovens use up to 400 Watts of power when the oven is on to keep the ignitor hot enough to light the gas as it kicks on and off.  A watt meter like the Kill-A-Watt meter is very useful to see how many watts and KWh a machine uses.  http://www.p3international.com/products/p4400.html

Solar Water Heating.

One of the highest energy expenses in any home is the water heater and heating water on the stove. One thing that helps with this is to use a small, well insulated water heater that can provide several cups of boiling water on demand. This small water heater could keep running even when the house was in a degraded mode, allowing people to make hot beverages and small hot meals.

By directly heating water with the heat of the sun you can reach amazingly high efficiencies.  Any system I build will have a large insulated water tank to store heat to both heat the house and my own drinking water, and a solar panel designed to heat antifreeze up to 180 degrees.  Or there may be a water tank to store heat for the house and a different tank for drinking water. This heated fluid would circulate in heat transfer loops around the base of the water tank to transfer the hot water to the cold water in the bottom of each water tank.  Another loop could come from a wood stove to heat the water in the tank as well.  The best design is to limit the length of your water lines and heavily insulate both lines.  More loops could be placed in the floor to heat the floors in your house, the most efficient kind of heating.

Solar cooking.

It is possible to cook entire tasty, nutritious and healthy meals in a couple of hours times in solar ovens. You can also sterilize water this way for drinking.  There are entire websites devoted to these techniques. It would be interesting to explore how to build several solar ovens into the design of a house to allow free cooking of foods.

Home entertainment.

My media player is already based on the Raspi running XBMC.  http://www.raspbmc.com/   http://www.raspberrypi.org/ This media player only draws 3.5 watts, plays HD video, and it is easy to load in another SD memory card to turn your TV into a game system that can play most of the games written before about 8 years ago.   As similar more powerful boards are released every few years it should be easy to upgrade the player.   I eliminated the older file server I was using, saving 10 watts of power, moving all the functions it was performing over to the Raspi already acting as my media player.  So far it has been working flawlessly.

One of the most power hungry appliances in the house has always been the TV, and while they are more efficient for the screen size than older TVs, a large plasma or LCD monitor can still draw up to hundreds of watts.  I would like to use a low powered projection device drawing no more than a dozen watts with a highly reflective screen  in a dark room to get a great picture while not drawing hundreds of watts of power.

The intelligent house.

Ideally an advanced house would schedule when the various appliances ran.  I would load the laundry and dishwasher, and the house would clean the dishes, then wash the clothes so they were done just as I got home for the day, sending a notice to my phone to put the clothes in the dryer.  While the washing machine motor was running at full speed, the freezer and refrigerator compressors would be turned off for a few minutes.  The water heater would only run when nothing else needed power, turning off when the furnace ran the blower.

In this way, instead of needing to provide 100 amp service, you could run the house on 20 or even 10 amps, if the compressors and motors are made smaller, and more efficient, with a corresponding reduction in the size of the solar array and batteries you need to provide power for a house.  It may be possible to run a generator on demand to top off batteries periodically when there is no grid power available and demand is greater than can be provided by solar alone.

An intelligent house would monitor the environment, in and out of the house, adjust light, humidity, and temperature levels to make things comfortable for the people living inside.  There should be a reporting area that graphs how much power is available projecting how many days the house can provide full power before shifting to a degraded mode of operation.  The generator fuel supply could be intelligently applied to this prediction and usage graphed over time.

It may be possible to run a few LED lights, and run the solar water heater to heat the house indefinitely, while not being able to run various appliances except when the batteries are charged and the solar panels voltage would otherwise be wasted.

Sunday, March 9, 2014

Dotnet on Linux using Mono

I had a function with no counterpoint in Linux that I wanted to run, something that returns just the last few digits of a very large number.  There was a function in a Windows Numerics library that did the trick.

When I first tried to compile the program it complained that it couldn't find the Numerics.dll, so I had to use the Ubuntu Software Center to install "libmono-system-numerics4.0-cil".

After that I had to figure out where it put the file I needed:
locate System.Numerics.dll
And finally I was able to compile and run the program:
 gmcs -r:/usr/lib/mono/4.0/System.Numerics.dll Test.cs  
./Test.exe 
It did the trick.  Now I am ready to try to do larger projects using C# and dotnet on Linux.

Here is a link to an example of a hello world program: http://www.mono-project.com/Mono_Basics

Raspberry Pi taking over my home network.

I had a separate small mini-itx board acting as a file server.  I didn't have a good enough power supply attached to it, so it would lock up about once a week. Despite this, it seemed to do a good job until last week.  Suddenly last week the server locked up, and afterwards it appeared that upnp services were gone from my network.  I have no clue why even now.

I had been watching how much running Raspbmc was loading the Raspberry PI I have been using as a media player for over a year now, and it seemed like I could put many more services on this low power device.  Losing upnp services gave me an excuse to try to move everything over to the Raspi.

First thing I did was install COPS, from here: http://blog.slucas.fr/en/oss/calibre-opds-php-server  with some explanation here: http://www.linux-magazine.com/Online/Blogs/Productivity-Sauce/Turn-Raspberry-Pi-into-an-Ebook-Server-with-Calibre  Essentially you load your books into Calibre, storing everything on the file server, then you point the COPs server configuration file to where the Calibre database file is located and this will share all those books to many different book readers on your local network.  I use fbreader on a galaxy tab 2 pad computer to read my books.

To get cops to work I had to load in the php and data base modules with the following commands:
sudo apt-get updatesudo apt-get install php5-gd php5-sqlite
I used a long USB extension cord to connect the hard drives from where they were sitting to a free port on the Raspi.  The hub I used does not feed power back into the Raspi.  Feeding power backwards through the usb port is something you evidently must avoid doing, from what I have read.  This can cause the Poly fuses to trip.  Evidently in newer raspi designs these usb poly fuses have been removed.
Once attached all the drives immediately were mounted and instantly showed up in the XBMC video file menu.  I was able to browse and play all the media I had on these drives right away.  I pointed the COPS server at the calibre database on the drives and it instantly began to work.

The last thing I needed was the transmission file download server.  I had been running it for a while on the mini-itx box and loved how easy it was to manage the downloads from my laptop machine.  It installed with the following command:
sudo apt-get install transmission-daemon
Then I followed this guide to configure it: http://www.computeristas.com/wp/?p=98

I am concerned about running the external HD's all the time so they don't use a lot of power and wear out faster than they should, and I didn't want to write to the SD Card in the Raspi that boots the OS, so I added an 8 GB SD card to the hub, formatted it was an ext4 file system and am having transmission save incomplete files to this drive, then copying the files to a hard drive once it is done.  So now, running just the Raspi and saving to this extra SD card is only drawing about 3 watts, not the 20 watts it was taking before.

You can easily log into the Raspi and run top to watch what is going on and most of the time I see over 40% idle even while watching an HD movie and copying a few GB of files across the network.


Friday, February 28, 2014

Spline Interpolation using matrix math.

Started at 2 pm yesterday and finished up at at 1:50 am today.  Finished it after 3 hours today.  Total of 15 hours, with about 8 just implementing the matrix library.

https://github.com/BuckRogers1965/Examples/blob/master/Math/NumericAnalysis/04_Spline.c
https://github.com/BuckRogers1965/Examples/blob/master/Math/NumericAnalysis/matrix.c
https://github.com/BuckRogers1965/Examples/blob/master/Math/NumericAnalysis/matrix.h

Compile with the following command:
gcc matrix.c 04_Spline.c
I did the matrix work earlier in support of this project.  That article is here: http://mystry-geek.blogspot.com/2014/02/a-small-c-matrix-library.html

The output and inline comments in the code step through the processing that it is doing.  I tend to keep the verbose output until I can get a good test harness in place so that I know I am not breaking things.

Output is:

time ./a.out 
h:     2.0000  2.0000  2.0000   1.0000   2.0000  
x: 1.0000   3.0000  5.0000  7.0000  8.0000  10.0000 
a: 6.0000   2.0000  5.0000 10.0000  4.0000   1.0000 
A = 
    1.000000000     0.000000000     0.000000000     0.000000000     0.000000000     0.000000000 
    2.000000000     8.000000000     2.000000000     0.000000000     0.000000000     0.000000000 
    0.000000000     2.000000000     8.000000000     2.000000000     0.000000000     0.000000000 
    0.000000000     0.000000000     2.000000000     6.000000000     1.000000000     0.000000000 
    0.000000000     0.000000000     0.000000000     1.000000000     6.000000000     2.000000000 
    0.000000000     0.000000000     0.000000000     0.000000000     0.000000000     1.000000000 

b_vector = 
    0.000000000 
   10.500000000 
    3.000000000 
  -25.500000000 
   13.500000000 
    0.000000000 

A^-1 = 
    1.000000000     0.000000000     0.000000000     0.000000000     0.000000000     0.000000000 
   -0.268343816     0.134171908    -0.036687631     0.012578616    -0.002096436     0.004192872 
    0.073375262    -0.036687631     0.146750524    -0.050314465     0.008385744    -0.016771488 
   -0.025157233     0.012578616    -0.050314465     0.188679245    -0.031446541     0.062893082 
    0.004192872    -0.002096436     0.008385744    -0.031446541     0.171907757    -0.343815514 
    0.000000000     0.000000000     0.000000000     0.000000000     0.000000000     1.000000000 

A^-1 * b_vector = c = 
    0.000000000 
    0.949685535 
    1.451257862 
   -5.254716981 
    3.125786164 
    0.000000000 

b = -2.6331  -0.7338   4.0681  -3.5388  -5.6677  
d =  0.1583   0.0836  -1.1177   2.7935  -0.5210  


Solution for x: -1.500000 with 6 points: nan 
First derivative at x: nan 

Solution for x: 1.500000 with 6 points: 4.703223 
First derivative at x: -2.514413 

Solution for x: 3.500000 with 6 points: 1.880994 
First derivative at x: 0.278629 

Solution for x: 4.500000 with 6 points: 3.318298 
First derivative at x: 2.679573 

Solution for x: 5.500000 with 6 points: 7.257174 
First derivative at x: 4.681145 

Solution for x: 6.500000 with 6 points: 10.595421 
First derivative at x: 0.877686 

Solution for x: 7.500000 with 6 points: 7.266116 
First derivative at x: -6.698375 

Solution for x: 8.500000 with 6 points: 1.882469 
First derivative at x: -2.932652 

Solution for x: 9.500000 with 6 points: 0.773192 
First derivative at x: 0.193134 

Solution for x: 15.000000 with 6 points: nan 
First derivative at x: nan 

real 0m0.009s
user 0m0.000s
sys 0m0.004s

As you can see from above the code does the following:
  • Builds the (x, a) point list, with a= f(x).
  • Builds the h list in points, this is the difference between 2 consecutive x values.
  • Builds the A matrix from the h values.
  • Builds the b vector from a formula using a and h
  • Inverts the A matrix and multiplies it against the b vector to get the c_vector
  • Copies the c_vector into points.
  • Loads b into points using a formula involving h, j, and c.
  • Loads d into points using a formula involving h and c.
Finished the following a few hours later, after sleeping:
  • In the finished version we can release the matrices we generated and return the point value.
  • We can reportedly generate interpolated values using the points table as is.
  • Actually generate specific values.
To generate an f(x) for a given x, you have to:  
  • figure out which interval between points you are in (j).
  • if you are outside the bounds of the points, reject the request, return NaN.
  • solve a third degree polynomial using the aj, bj, cj, dj, and xj and the provided x value.
Two ways to get the rate of change at any point
    Calculate a point on each side of the value you are looking at and find the slope of the line that connects the two points.
   You could easily program the computer to take the derivative of the simple formula for one of these lines, basically just pre-program a function that is the derivative.
  I went with calculating the derivative to get an absolutely exact estimated value. Yeah, exact estimate is a problem. :D 

---

We got a take home test with 21 points and the code I wrote calculated the result in .016 seconds right out of the box.  I was able to solve it before the class period was over.   It looks like a duck!  I also calculated the first derivative of the function so we can perform more analysis on the information. :D  Of course it took me about 20 hours to write the matrix code and this code to get to that point. :D




The Chart and the Shape: Physics as the Resolution of Derivative Rungs

  J. Rogers, SE Ohio Abstract The standard framework treats the history of physics as a sequence of discoveries: Newton discovered gravity, ...