<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>t-square</title>
	<atom:link href="http://www.timteatro.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.timteatro.net</link>
	<description>Pursuit of Inspiration, Imagination and Science.</description>
	<lastBuildDate>Fri, 10 Feb 2012 01:34:39 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>A Library for Using the Wii Nunchuk In Arduino Sketches</title>
		<link>http://www.timteatro.net/2012/02/10/a-library-for-using-the-wii-nunchuk-in-arduino-sketches/</link>
		<comments>http://www.timteatro.net/2012/02/10/a-library-for-using-the-wii-nunchuk-in-arduino-sketches/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 01:34:39 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Software & FOSS]]></category>
		<category><![CDATA[Arduino]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Nintendo Wii]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Wii Nunchuk]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1185</guid>
		<description><![CDATA[There are <a href="http://arduino.cc/playground/Main/InterfacingWithHardware" target="_blank">several available libraries</a> for communicating with the Wii nunchuk. I somehow found something lacking in all of them, so I wrote <a href="http://www.timteatro.net/download/wiinunchuk.h" target="_blank">wiinunchuk.h</a> for my projects. It is inspired by <a href="https://raw.github.com/todbot/wiichuck_adapter/master/firmware/WiichuckDemo/nunchuck_funcs.h" target="_blank">Tod E. Kurt.</a> but my <a href="http://www.timteatro.net/download/wiinunchuk.h" target="_blank">wiinunchuk.h</a> boasts more powerful features and a more flexible API.]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.timteatro.net/wp-content/uploads/2012/02/P1271229.jpg"><img src="http://www.timteatro.net/wp-content/uploads/2012/02/P1271229-168x300.jpg" alt="" title="Arduino Wii Nuncuk Project" width="168" height="300" class="alignright size-medium wp-image-1249" /></a>
<p>There are <a href="http://arduino.cc/playground/Main/InterfacingWithHardware" target="_blank">several available libraries</a> for communicating with the Wii nunchuk. I somehow found something lacking in all of them, so I wrote <a href="http://www.timteatro.net/download/wiinunchuk.h" target="_blank">wiinunchuk.h</a> for my projects. It is inspired by <a href="https://raw.github.com/todbot/wiichuck_adapter/master/firmware/WiichuckDemo/nunchuck_funcs.h" target="_blank">Tod E. Kurt.</a> but my <a href="http://www.timteatro.net/download/wiinunchuk.h" target="_blank">wiinunchuk.h</a> boasts more powerful features and a more flexible API.</p>
<p>This library is free software and is licensed under the GPL v3.</p>
<p><b>Click here to <a href="http://www.timteatro.net/download/wiinunchuk.h" target="_blank">Download wiinunchuk.h</a>.</b></p>
<p>The code is relatively well commented on its own. The header provides the functions</p>
<ul>
<li>void nunchuk_setpowerpins()</li>
<li>void nunchuk_init()</li>
<li>int nunchuk_get_data()</li>
<li>void nunchuk_calibrate_joy()</li>
<li>inline unsigned int nunchuk_zbutton()</li>
<li>inline unsigned int nunchuk_cbutton()</li>
<li>inline int nunchuk_joy_x()</li>
<li>inline int nunchuk_cjoy_x()</li>
<li>inline int nunchuk_cjoy_y()</li>
<li>inline uint16_t nunchuk_accelx()</li>
<li>inline uint16_t nunchuk_accely()</li>
<li>inline uint16_t nunchuk_accelz()</li>
<li>inline int nunchuk_caccelx()</li>
<li>inline int nunchuk_caccely()</li>
<li>inline int nunchuk_caccelz()</li>
<li>inline int nunchuk_joyangle()</li>
<li>inline int nunchuk_rollangle()</li>
<li>inline int nunchuk_pitchangle()</li>
<li>void nunchuk_calibrate_accelxy()</li>
<li>void nunchuk_calibrate_accelz().</li>
</ul>
<h2>Calibration</h2>
<p>Although the header provides functions to pull raw data right from the nunchuck, in most typical applications it is useful to have the data from the nunchuck normalised so that certain reference points are defined to be zero. For example, the reading from the joystick potentiometer will range from about 24 to about 230 on both x and y axes. The normal reading when the joystick is centred is about 130 (it will be slightly different in for each axis). If you subtract the centre value from the instantaneous joystick readings, then it is zeroed at the centre and readings will be relative to the centre.</p>
<p>Reasonable default centre values are #defined near the top of the document:</p>
<pre class="brush: cpp; title: ; notranslate">
#define DEFAULT_CENTRE_JOY_X 124
#define DEFAULT_CENTRE_JOY_Y 132
#define ACCEL_ZEROX 490
#define ACCEL_ZEROY 500
#define ACCEL_ZEROZ 525
</pre>
<p>These are loaded into program memory in <code>void nunchuk_init()</code>. If you need accuracy and plan to use the same nunchuk in all applications, it is worth your time to measure these for your nunchuk by spitting the rad nunchuk data to a serial terminal. For other cases, wiinunchuk.h provides functions</p>
<ul>
<li>void nunchuk_calibrate_joy()</li>
<li>void nunchuk_calibrate_accelxy()</li>
<li>void nunchuk_calibrate_accelz().</li>
</ul>
<p>The function <code>nunchuk_calibrate_joy()</code> will take the instantaneous reading of the joystick values and record them as the zero value. The end user should be warned not to manipulate the joystick during the moment it is called. Usually, this is at the beginning of the sketch before the action starts.</p>
<pre class="brush: cpp; title: ; notranslate">
void nunchuk_calibrate_joy()
	{
	joy_zerox = joy_x;
	joy_zeroy = joy_y;
	}
</pre>
<p><code>nunchuk_calibrate_accelxy()</code> and <code>nunchuk_calibrate_accelz()</code> will record zero values for the accelerometer axes. This has to be done in two steps since the force of gravity will always affect at least one of the accelerometer axes. If the nunchuk is held upright, the <i>xy</i> calibration will be clean since gravity is pulling towards negative <i>z</i>. Then, with the nunchuk on its side or face so that gravity affects only <i>x</i> or <i>y</i>, the <i>z</i> accelerometer can be read to take the zero value using <code>nunchuk_calibrate_accelz()</code>.</p>
<pre class="brush: cpp; title: ; notranslate">
void nunchuk_calibrate_accelxy()
	{
	accel_zerox = nunchuk_accelx();
	accel_zeroy = nunchuk_accely();
	}

void nunchuk_calibrate_accelz()
	{
	accel_zeroz = nunchuk_accelz();
	}
</pre>
<h2>Initialisation</h2>
<p><code>nunchuk_setpowerpins()</code> sets up the pins.</p>
<pre class="brush: cpp; title: ; notranslate">
void nunchuk_setpowerpins()
	{
	#define pwrpin PORTC3
	#define gndpin PORTC2
	DDRC |= _BV(pwrpin) | _BV(gndpin);
	PORTC &amp;= ~_BV(gndpin);
	PORTC |=  _BV(pwrpin);
	delay(100); // wait for things to stabilize
	}
</pre>
<p>The function assumes that the nuncuk is connected using a <a href="http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/" target="_blank">WiiChuk</a>, or at least that you are using the same pin configuration. That is to say A2, A3, A4 and A5 are power (-), power (+), data and clock, respectively. Even though these are analog pins, this function treats A2 and A3 as digital pins and sets them high and ground to power the nunchuk with 5V. As I write that, I recall that the nunchuk spec calls for less than 5V, but WiiChuk users don&#8217;t have much choice. If you cut the connector on your nunchuck and are plugging in directly, I would try using the Arduino&#8217;s 3.3V power instead.</p>
<p>The <code>nunchuk_init()</code> function sets up communication with the nunchuk by establishing the i2c bus and handshaking. The way this is done should work for OEM Nintendo nunchuks and for the el-cheapo knock-offs.</p>
<pre class="brush: cpp; title: ; notranslate">
void nunchuk_init()
	{
	Wire.begin();
	delay(1);
	Wire.beginTransmission(0x52);  // device address
	#if (ARDUINO &gt;= 100)
		Wire.write((uint8_t)0xF0);  // 1st initialisation register
		Wire.write((uint8_t)0x55);  // 1st initialisation value
		Wire.endTransmission();
		delay(1);
		Wire.beginTransmission(0x52);
		Wire.write((uint8_t)0xFB);  // 2nd initialisation register
		Wire.write((uint8_t)0x00);  // 2nd initialisation value
	#else
		Wire.send((uint8_t)0xF0);   // 1st initialisation register
		Wire.send((uint8_t)0x55);   // 1st initialisation value
		Wire.endTransmission();
		delay(1);
		Wire.beginTransmission(0x52);
		Wire.send((uint8_t)0xFB);   // 2nd initialisation register
		Wire.send((uint8_t)0x00);   // 2nd initialisation value
	#endif
	Wire.endTransmission();
	delay(1);
	//
	// Set default calibration centres:
	//
	joy_zerox = DEFAULT_CENTRE_JOY_X;
	joy_zeroy = DEFAULT_CENTRE_JOY_Y;
	accel_zerox = ACCEL_ZEROX;
	accel_zeroy = ACCEL_ZEROY;
	accel_zeroz = ACCEL_ZEROZ;
	}
</pre>
<p>Some other libraries do a more canonical sort of initialization that causes the data to be encrypted and is not compatible with 3rd party nunchucks. The function finishes off by loading the default zero values for the sensors into memory. They can hereon be changed directly or using the calibration functions discussed earlier.</p>
<h2>Communication</h2>
<p>We expect to get back six bytes of information:</p>
<table align="center" width=100%>
<tr>
<td><b>Byte</b></td>
<td><b>Contains</b></td>
</tr>
<tr>
<td>1</td>
<td>Joystick x</td>
</tr>
<tr>
<td>2</td>
<td>Joystick y</td>
</tr>
<tr>
<td>3</td>
<td>First 8 of 10-bit accelerometer <i>x</i> value</td>
</tr>
<tr>
<td>4</td>
<td>First 8 of 10-bit accelerometer <i>y</i> value</td>
</tr>
<tr>
<td>5</td>
<td>First 8 of 10-bit accelerometer <i>z</i> value</td>
</tr>
<tr>
<td>6</td>
<td>
<ul>
<li>Last two bits from acc <i>z</i></li>
<li>last two bits from acc <i>y</i></li>
<li>last two bits from acc <i>x</i></li>
<li>1-bit for Cbutton state</li>
<li>1-bit for Z-button state</li>
</ul>
</td>
</tr>
</table>
<p>The function <code>nunchuk_get_data()</code> requests and receives the six bytes data, reads it into a six element array <code>nunchuk_buf</code>. </p>
<pre class="brush: cpp; title: ; notranslate">
int nunchuk_get_data()
	{
	int cnt=0;
	  // Request six bytes from the chuck.
	Wire.requestFrom (0x52, 6);
	while (Wire.available ())
		{
	  // receive byte as an integer
		#if (ARDUINO &gt;= 100)
			nunchuk_buf[cnt] = Wire.read();
		#else
			nunchuk_buf[cnt] = Wire.receive();
		#endif
		cnt++;
		}
	//
	//Send read address, zero-byte.
	#if (ARDUINO &gt;= 100)
		Wire.write((uint8_t)0x00);
	#else
		Wire.send((uint8_t)0x00);
	#endif

	if (cnt &gt;= 5)
		{
		return 1;   // success
		}
	return 0; // failure
	}
</pre>
<h2>Processing and output</h2>
<p>The remaining functions deal with unpacking our six-byte data package, <code>nunchuk_buf[]</code> and presenting it in various useful forms. Since these functions are small and likely to be in a program loop, I cast them all as static inline functions to avoid emission of object code and have the code injected directly into the points of use in the Arduino sketches.</p>
<p>To start, we capture the C and Z buttons with <code>nunchuk_cbutton()</code> and <code>nunchuk_zbutton()</code>. The one-bit on/off values are stored in the last two bits of the sixth byte. We just bit shift an appropriate value and check the state of the last bit of that byte. The checking of the byte value is done using C&#8217;s inline ternary conditional operator <code>(condition)? true : false;</code>.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline unsigned int nunchuk_zbutton()
	{
	return ((nunchuk_buf[5] &gt;&gt; 0) &amp; 1) ? 0 : 1;
	}

static inline unsigned int nunchuk_cbutton()
	{
	return ((nunchuk_buf[5] &gt;&gt; 1) &amp; 1) ? 0 : 1;
	}
</pre>
<p>There isn&#8217;t much to be done about the raw <i>x</i> and <i>y</i> values, so we just return them with <code>nunchuk_joy_x()</code> and <code>nunchuk_joy_x()</code>.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline int nunchuk_joy_x()
	{
	return (int) nunchuk_buf[0];
	}

static inline int nunchuk_joy_y()
	{
	return (int) nunchuk_buf[1];
	}
</pre>
<p>We also want the option of having the calibrated (zeroed and centre) joystick values which are returned with functions <code>nunchuk_cjoy_x()</code> and <code>nunchuk_cjoy_x()</code></p>
<pre class="brush: cpp; title: ; notranslate">
static inline int nunchuk_cjoy_x()
	{
	return (int)nunchuk_buf[0] - joy_zerox;
	}

static inline int nunchuk_cjoy_y()
	{
	return (int)nunchuk_buf[1] - joy_zeroy;
	}
</pre>
<p>The accelerometer readings are a more difficult matter because of the 10-bit values, the least significant two bits are stored in byte six, which is a mish-mash of data. So, take for example the function <code>nunchuk_accelx()</code>.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline uint16_t nunchuk_accelx()
	{
	return (  0x0000 | ( nunchuk_buf[2] &lt;&lt; 2 ) +
		( ( nunchuk_buf[5] &amp; B00001100 ) &gt;&gt; 2 )  );
	}
</pre>
<p>First, <code>0x0000 | ( nunchuk_buf[2] << 2 )</code> takes a blank 16-bits (0x0000) and does a bitwise OR (|) with the 8-bit nunchuk_buf[2] which has been bit-shifted by 2 (<<2). The bitwise OR simply takes nunchuk_buff[2] and imprints it onto 0x0000 creating a 16-bit version of nunchuk_buf[2]. The bit-shift ensures that the last two bits are blank so we can add in the last two bits which are stored in nunchuk_buf[5]. So, to this we add <code>( nunchuk_buf[5] &#038; B00001100 ) >> 2 )</code> which takes the pair of bits starting second from the left of nunchuk_buf[5] and zeros everything else. Then those two bits are shifted to the end to be added to the 16-bit copy of nunchuk_buf[2] which has the right-most two bits empty and waiting.</p>
<p>Rinse and repeat for the <i>y</i> and <i>z</i> accelerometer values.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline uint16_t nunchuk_accely()
	{
	return (  0x0000 ^ ( nunchuk_buf[3] &lt;&lt; 2 ) +
		( ( nunchuk_buf[5] &amp; B00110000 ) &gt;&gt; 4 )  );
	}

static inline uint16_t nunchuk_accelz()
	{
	return (  0x0000 ^ ( nunchuk_buf[4] &lt;&lt; 2 ) +
		( ( nunchuk_buf[5] &amp; B11000000 ) &gt;&gt; 6 )  );
	}
</pre>
<p>The sketch may optionally want the calibrated accelerometer values which are offered by the following three functions.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline int nunchuk_caccelx()
	{
		return (int)(nunchuk_accelx() - accel_zerox);
	}

static inline int nunchuk_caccely()
	{
		return (int)(nunchuk_accely() - accel_zeroy);
	}

static inline int nunchuk_caccelz()
	{
		return (int)(nunchuk_accelz() - accel_zeroz);
	}
</pre>
<p>The final three functions are concerned with angle. I include them because some people have a rather serious trigonometry allergy. The vector magnitudes which compliment these angles, I&#8217;ll leave to the Pythagorean wizardry of whom ever may need them.</p>
<p>You&#8217;ll note the use of the <a href="http://en.wikipedia.org/wiki/Atan2" target="_blank"><code>atan2()</code></a> function. This is exactly like vanilla <code>atan()</code>, except that it keeps track of which quadrant we are in by the signs of the opposite and adjacent lengths, and also handles vectors one the axes gracefully.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline int nunchuk_joyangle()
	{
	double theta;
	theta = atan2( nunchuk_cjoy_y(), nunchuk_cjoy_x() );
	while (theta &lt; 0) theta += 2*M_PI;
	return (int)(theta * 180/M_PI);
	}
</pre>
<p>The little while-loop in here makes sure that we get a positive angle by rotating angles into their positive equivalent.</p>
<p>A little trick to get the orientation of the nunchuk from the accelerometers is to sniff out the gravity vector. If the accelerometer is properly zeroed, and all other external forces are negligible (i.e., gravity is the only significant fore on the numchuk), then the ratios of the accelerometer readings will give the roll and pitch of the nunchuk.</p>
<p>Roll (<code>nunchuk_rollangle()</code>) is the angle created by tipping an upright nunchuk to the left or right as viewed from behind. More precisely, with the z-axis pointing up from the joystick, the y-axis point out of the c-button and the x-axis pointing from the right side, roll is the angle created between the z-axis and the nunchuk when it is rotated about the y-axis.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline int nunchuk_rollangle()
	{
	return (int) (  atan2( (double) nunchuk_caccelx(),
		(double) nunchuk_caccelz() ) * 180 / M_PI  );
	}
</pre>
<p>Then by the same definitions, pitch is the angle created between the nunchuk and the z-axis when the nunchuk is rotated about the x-axis and &#8216;tipped&#8217; forward or backward.</p>
<pre class="brush: cpp; title: ; notranslate">
static inline int nunchuk_pitchangle()
	{
	return (int) (  atan2( (double) nunchuk_caccely(),
		(double)nunchuk_caccelz() ) * 180 / M_PI  );
	}
</pre>
<p>If you haven&#8217;t already, <b>click here to <a href="http://www.timteatro.net/download/wiinunchuk.h" target="_blank">Download wiinunchuk.h</a>.</b></p>
<p>I hope you find this useful. Please feel free to post links to your projects or ask questions. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2012/02/10/a-library-for-using-the-wii-nunchuk-in-arduino-sketches/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Canadians take a stand against online spying</title>
		<link>http://www.timteatro.net/2011/09/15/canadians-take-a-stand-against-online-spying/</link>
		<comments>http://www.timteatro.net/2011/09/15/canadians-take-a-stand-against-online-spying/#comments</comments>
		<pubDate>Thu, 15 Sep 2011 19:23:57 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Headline]]></category>
		<category><![CDATA[Government]]></category>
		<category><![CDATA[Privacy]]></category>
		<category><![CDATA[Security]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1164</guid>
		<description><![CDATA[The government is trying to ram through an anti-Internet set of electronic surveillance laws that will invade your privacy and cost you money. The plan is to...]]></description>
			<content:encoded><![CDATA[<p>I won&#8217;t stand for it. Will you?</p>
<p><iframe width="460" height="264" src="http://www.youtube.com/embed/QwqIYHwRcxY" frameborder="0" allowfullscreen></iframe></p>
<p>Do something about it: <a href="http://www.stopspying.ca" target="_blank">www.stopspying.ca</a> or <a href="http://www.openmedia.ca" target="_blank">www.openmedia.ca<br />
</a></p>
<p><iframe src="http://action.cwa-union.org/c/779/p/dia/action/public/?action_KEY=2276" width="460" height="580" frameborder="0" marginheight="0" marginwidth="0">Loading&#8230;</iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2011/09/15/canadians-take-a-stand-against-online-spying/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Review: Solomon SL-30 Soldering Station</title>
		<link>http://www.timteatro.net/2011/07/26/review-solomon-sl-30-soldering-station/</link>
		<comments>http://www.timteatro.net/2011/07/26/review-solomon-sl-30-soldering-station/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 23:54:02 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Headline]]></category>
		<category><![CDATA[Review]]></category>
		<category><![CDATA[electronics]]></category>
		<category><![CDATA[soldering]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1143</guid>
		<description><![CDATA[<p>About two months ago I bought a <a href="http://www.a1parts.com/solomon/index.html" target="_blank">Solomon SL-30 from a1parts.com</a>, the online store for A1 Electronics here in Toronto. I chose that station after a lot of research, despite not being familiar with the name. This review is not based on a series of cleverly designed test, but just my day-to-day use of the station for soldering connectors, wires and even a little PCB work.</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.timteatro.net/wp-content/uploads/2011/07/solomon_sl-30_box.jpg"><img src="http://www.timteatro.net/wp-content/uploads/2011/07/solomon_sl-30_box-300x275.jpg" alt="" title="solomon_sl-30_box" width="300" height="275" class="alignright size-medium wp-image-1146" /></a>
<p>About two months ago I bought a <a href="http://www.a1parts.com/solomon/index.html" target="_blank">Solomon SL-30 from a1parts.com</a>, the online store for A1 Electronics here in Toronto. I chose that station after a lot of research, despite not being familiar with the name. This review is not based on a series of cleverly designed test, but just my day-to-day use of the station for soldering connectors, wires and even a little PCB work.</p>
<p>I had been shopping around for a while when I decided to try the Solomon SL-30. I knew I wanted temperature control, a comfortable pen, easily to find tips and a good build quality. I had considered the <a href="http://www.amazon.com/WELLER-WESD51-DIGITAL-SOLDERING-STATION/dp/B005C2W4NC">Weller WESD51D</a>, the <a href="http://www.amazon.com/Hakko-Soldering-Station-FX-888/dp/B004M3U0VU/ref=sr_1_14?s=power-hand-tools&#038;ie=UTF8&#038;qid=1311709505&#038;sr=1-14">Hakko FX-888</a> and the <a href="http://www.amazon.com/Edsyn-Soldering-951SX-Temperature-Controlled/dp/B00012YSG4/ref=sr_1_28?s=power-hand-tools&#038;ie=UTF8&#038;qid=1311709544&#038;sr=1-28">Edsyn 951SX</a> <a href="http://www.escience.ca/hobby/RENDER/0001/84/3063/12153.html">SX-500D</a>.</p>
<h2>Summary</h2>
<p>I&#8217;m really very happy with the station overall. I feel like I got good value for my money, and I got just about everything I wanted in a soldering station.</p>
<h3>Pros</h3>
<ul>
<li>Temperature setting and readout, very nice, especially for the price.</li>
<li>Solid quality &mdash; a heavy unit with solid feeling construction. The temperature knob feels smooth and strong. I feel like this may be something that will be in my workshop for a lot of years.</li>
<li>Tip and iron replacement seem readily available, even if the iron is swapped to Elenco brand. However, apparently Solomon has been around for a lot of years and is one of the biggest manufacturers in the world (according to Glen from A1).</li>
<li>Really nice holder for the iron. I&#8217;m not a big fan of the wire coil ones, and because space is an issue for me, I also don&#8217;t want one of the huge ones that hold the iron and sponge (or brass) separate from the unit.</li>
<li>Silicone coated wire on the iron, not that I can imagine ever burning my iron with itself.</li>
</ul>
<h3>Cons</h3>
<p>I can really only think of one con:</p>
<ul>
<li>I really wanted a thin pencil-like iron with a super flexible (spaghetti-like) cord, but this one feels like most any cheap plug-in iron. In fact, it feels just fine in my hand, I just like the look of the smaller ones. The cord is a little stiff at first too. The silicone grip does occasionally slide down on my pen, but I just push it back up. I&#8217;m nitpicking really.</li>
</ul>
<h2>Deciding on the Solomon</h2>
<p>The price of most other offerings, relative to the feature set really made the decision for me. Less than $100.00 got me a temperature controlled soldering station with a digital readout of both current temperature and set temperature.</p>
<p>I read a lot of reviews saying that the Weller had a cheap knob that broke, and basically that people who had owned Wellers from 40+ years back aren&#8217;t happy with the quality of the modern Weller products. Solomon seems to come out with new products, but the quality (from what I hear) is consistent. I was really tempted to get a Hakko, and I probably will some day, but there was just nothing for this price that could compete with the SL-30.</p>
<p>A serious consideration was the <a href="http://www.elenco.com/soldering.htm">Elenco line of soldering stations</a>, some of which seem identical to the Solomon offerings. The price of Elenco stations is a little less online, but shipping costs make it preferable for me to just buy it locally from A1 Electronics.</p>
<h2>Impressions out of the box</h2>
<div id="attachment_1148" class="wp-caption alignright" style="width: 310px"><a href="http://www.timteatro.net/wp-content/uploads/2011/07/solomon_sl-30_instructions.png"><img src="http://www.timteatro.net/wp-content/uploads/2011/07/solomon_sl-30_instructions-300x218.png" alt="Instruction Manual" title="solomon_sl-30_instructions" width="300" height="218" class="size-medium wp-image-1148" /></a><p class="wp-caption-text">A scanned image of the instruction booklet that came in the box with the SL-30. The reverse side seems to contain the same instructions in several languages.</p></div>
<p>
Aside form the fact that the box looks like marketing material for a discount Chinese dish detergent, my first impressions were very good. Out of the box, the weight and build quality felt good, and everything was straight forward to set up.
</p>
<h2>Afterthoughts</h2>
<p>It really just does what you expect. There&#8217;s not much more to say. The tips are very nice for the price, but the handle feels somewhat cheap. I&#8217;m going to be very picky on this point, however, because I really did want a station with a thin pencil-like handle and a very flexible cord. The Solomon is a little disappointing in that light, but there is really nothing to complain out. It is comfortable to use, even for hours at a time.</p>
<p>If anything ever happened and I had trouble getting Solomon parts, I believe that I can buy an Elenco replacement iron and tips (~$20.00 total) and it should be a drop-in replacement for the Solomon iron.<br />
<h2>Overall&#8230;</h2>
<p>I&#8217;m very happy with the unit, and I feel like I&#8217;m going to use it for a long time. I&#8217;m eventually going to replace it with a very expensive one, but that will be years down the road. And based on the build quality, I definitely feel that years down the road when I do replace it, I will be replacing a fully functioning Solomon.</p>
<div class="clearfloat"></div>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2011/07/26/review-solomon-sl-30-soldering-station/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Convert CPMD TRAJECTORY for tfreq Autocorrelation and Vibrational Density of States</title>
		<link>http://www.timteatro.net/2011/05/26/convert-cpmd-trajectory-for-tfreq-autocorrelation-and-vibrational-density-of-states/</link>
		<comments>http://www.timteatro.net/2011/05/26/convert-cpmd-trajectory-for-tfreq-autocorrelation-and-vibrational-density-of-states/#comments</comments>
		<pubDate>Thu, 26 May 2011 20:05:58 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Scientific Computing]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[Physics]]></category>
		<category><![CDATA[Science]]></category>
		<category><![CDATA[solid state]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1130</guid>
		<description><![CDATA[Recently, I&#8217;ve been getting a few of people wanting to use tfreq, autocorrelation and vibrational density of states to analyse data from CPMD. tfreq is written to process output from Quantum-ESPRESSO&#8216;s cp.x output. The two options are to modify the source file tfreq.c to read the new input, which could be difficult for someone inexperienced [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been getting a few of people wanting to use <a href="http://www.timteatro.net/2010/09/29/velocity-autocorrelation-and-vibrational-spectrum-calculation/" target="_blank">tfreq</a>,  autocorrelation and vibrational density of states to analyse data from <a href="http://www.cpmd.org/">CPMD</a>. tfreq is written to process output from <a href="http://www.quantum-espresso.org/">Quantum-ESPRESSO</a>&#8216;s cp.x output.</p>
<p>The two options are to modify the source file tfreq.c to read the new input, which could be difficult for someone inexperienced with C. As a quick solution, I wrote a quick awk script to make a .vel file from a CPMD TRAJECTORY file.</p>
<p>The following script should would with cubic geometry (just a guess). It doesn&#8217;t read anything from the GEOMETRY file and I leave it to the user to make any necessary conversions. I need someone to test it, and I&#8217;ll post the results here.</p>
<pre class="brush: plain; title: ; notranslate">
BEGIN {
	nat=###     # Number of Atoms.
	dt=###       # Duration (in fs) of teach time step.
	}

{
	# For each atom:
	for (i=0; i&lt;=nat; i++) {
		# Read the time step out of the first column:
		ts=$1
		# If this is the first row of a time step, print the time:
		if (i==0) printf &quot; %10d %16.10f\n&quot;, ts, ts*dt
		# Now print the volocity out of the TRAJECTORY file:
		printf &quot;%16.12f  %16.12f  %16.12f\n&quot;, $5, $6, $7
		# Get a new line:
		getline
	}
}
</pre>
<p>Save this code as <code>CPMD2vel.awk</code> and define nat (the number of atoms) and dt (the time step size)</p>
<p>Run this with code: <code>awk -f CPMD2vel.awk TRAJECTORY > output.vel</code></p>
<p>This will create a .vel file (output.vel, or the name you wish), which can be used with tfreq.</p>
<p>ESPRESSO&#8217;s .vel file format is very simple:<br />
<code><br />
timestep  time(in fs)<br />
vx1 vy1 vz1<br />
vx2 vy2 vz2<br />
vx3 vy3 vz3<br />
...<br />
</code><br />
With the format:<br />
<code><br />
integer   float<br />
float  float  float<br />
float  float  float<br />
...<br />
integer   float<br />
float  float  float<br />
float  float  float<br />
</code><br />
For example:<br />
<code><br />
      1       0.123456<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
     2       0.123456<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
     3       0.123456<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
1.2345E01  1.2345E01  1.2345E01<br />
...<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2011/05/26/convert-cpmd-trajectory-for-tfreq-autocorrelation-and-vibrational-density-of-states/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>I&#8217;m Voting Pro-Internet</title>
		<link>http://www.timteatro.net/2011/04/29/im-voting-pro-internet/</link>
		<comments>http://www.timteatro.net/2011/04/29/im-voting-pro-internet/#comments</comments>
		<pubDate>Fri, 29 Apr 2011 21:25:57 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Site News]]></category>
		<category><![CDATA[Politics]]></category>
		<category><![CDATA[The Internet]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1118</guid>
		<description><![CDATA[In case you don't already know, the big Internet Service Providers (ISPs) in Canada want to start billing Internet usage by the byte. Anyone with a smart phone is familiar with this system. For anyone who lives on the Internet, as most twenty-somethings and younger tend to do these days, this could easily become prohibitively expensive.]]></description>
			<content:encoded><![CDATA[<p><div id="attachment_1119" class="wp-caption alignright" style="width: 279px"><img src="http://www.timteatro.net/wp-content/uploads/2011/04/canada_pro_internet_support_2011.jpg" title="canada_pro_internet_support_2011" width="269" height="550" class="size-full wp-image-1119" /><p class="wp-caption-text">Candidates supporting open-Internet by party. Taken from <a href=http://openmedia.ca/candidates target=_blank>openmedia.ca</a></p></div>
<p>
In this day in age, I can&#8217;t believe that there is a politician stupid enough to support metered Internet billing.
</p>
<p>
In case you don&#8217;t already know, the big Internet Service Providers (ISPs) in Canada want to start billing Internet usage by the byte. Anyone with a smart phone is familiar with this system. For anyone who lives on the Internet, as most twenty-somethings and younger tend to do these days, this could easily become prohibitively expensive.
</p>
<p>
The good folks over at <a href=http://openmedia.ca/candidates target=_blank>openmedia.ca</a> have been fighting the good fight. I&#8217;ve been helping them as much as I can by signing petitions, sending emails to politicians, etc. I&#8217;d encourage the reader to do the same. I&#8217;m actually really surprised by how big of an impact openmedia.ca has had.
</p>
<p>
The telecommunications industry in this country is already behind much of the world in terms of the quality of service. I have many friends in Europe who absolutely pity our situation. (Although I&#8217;m quick to remind many of them about three-strikes.) Metered billing would be another major step backward from Canada&#8217;s already embarrassing position.
</p>
<p>
I simply can&#8217;t begin to comprehend the stupidity. It&#8217;s bad for private citizens, especially in the lower and middle class brackets, and it&#8217;s also bad for business.
</p>
<p>
We not only share our lives over the Internet, but also our talents and passions. Show me an independent band that doesn&#8217;t have some mp3s (or FLAC, better yet). Emerging authors are self publishing on the Internet. Visual artists (painters, photographers, etc) are selling their prints on the Internet. Limiting ones access to this content not only hurts the artists, but all of us whose lives are improved by it.
</p>
<p>
As for business, let&#8217;s rime off a few: Google, Netflix, iTunes, Amazon, YouTube, Ebay, Facebook, Steam, Indigo, iStockPhoto and maybe several thousand others I&#8217;m missing. Not only do companies like these make money by selling physical goods online, but also digital products such as mp3s, movies, digital images and software. All things that will be taxed by the cost of downloading the files. And let&#8217;s not forget that we like to look at plenty of high resolution images of products before we buy them.
</p>
<p>
Well, I guess none of the companies mentioned in the last paragraph are doing enough to fund the Conservatives, because they seem dead set on screwing us all over. I&#8217;d love to know how much money they&#8217;re selling us out for.
</p>
<p>
I&#8217;m so disappointed with my choices for this election. I&#8217;m actually thinking of voting Green party, just to shake things up. I can&#8217;t believe how badly our system is broken.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2011/04/29/im-voting-pro-internet/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TeX Live 2010 on Ubuntu 11.04</title>
		<link>http://www.timteatro.net/2011/04/28/tex-live-2010-on-ubuntu-11-04/</link>
		<comments>http://www.timteatro.net/2011/04/28/tex-live-2010-on-ubuntu-11-04/#comments</comments>
		<pubDate>Thu, 28 Apr 2011 06:32:26 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Software & FOSS]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Linux]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1109</guid>
		<description><![CDATA[One day before the release of Ubuntu 11.04, I decided to reformat my netbook a day early. I was really disappointed to find that Natty is still stuck in the past with TeX Live 2009... I've avoided doing a manual install of TeX Live 2010 because I've imagined as being such a pain in the ass. But to my surprise, it was pretty simple.]]></description>
			<content:encoded><![CDATA[<p>
One day before the release of Ubuntu 11.04, I decided to reformat my netbook a day early. I was really disappointed to find that Natty is still stuck in the past with TeX Live 2009. Considering many of the most commonly used packages have broken compatibility with their oder versions, this isn&#8217;t acceptable.
</p>
<p>
I&#8217;ve avoided doing a manual install of TeX Live 2010 because I&#8217;ve imagined as being such a pain in the ass. But to my surprise, it was pretty simple. The hardest part was tricking Ubuntu&#8217;s package manager into knowing that TeX Live is installed without pulling the crappy copy off of the repos.
</p>
<p>
To start, just download the small gzip <a href="http://www.tug.org/texlive/acquire-netinstall.html">from here</a>. Unpack it and change directory into it in a terminal.<br />
<code>sudo apt-get install perl-tk</code><br />
which you&#8217;ll need for the installer GUI. Now, run the installer:<br />
<code>sudo ./install-tl --gui</code>
</p>
<p>
Once your in the installer, go through the list of options. If you don&#8217;t know what they do, you probably don&#8217;t need to bother with it. The main one to look out for are A4 vs. Letter paper. I didn&#8217;t generate the simlinks, but you&#8217;ll have to edit your PATH variable later. Hit the <i>install</i> button and go make yourself a sandwich. You&#8217;ll be a while.
</p>
<p>
At the end of the install, you&#8217;ll get the following message</p>
<blockquote><p>
    Add /usr/local/texlive/2010/texmf/doc/man to MANPATH.<br />
    Add /usr/local/texlive/2010/texmf/doc/info to INFOPATH.<br />
    Most importantly, add /usr/local/texlive/2010/bin/x86-linux<br />
    to your PATH for current and future sessions.
</p></blockquote>
<p>Note, you&#8217;ll get a message which reflects your architecture. (x86_64, for example.)
</p>
<p>
So, to add those directories to the paths, you&#8217;ll want to edit your bashrc:<br />
<code> gedit ~/.bashrc</code><br />
In my case, I added the following lines:</p>
<pre class="brush: plain; title: ; notranslate">
export PATH=$PATH:/usr/local/texlive/2010/bin/i386-linux
export MANPATH=$MANPATH:/usr/local/texlive/2010/texmf/doc/man
export INFOPATH=$INFOPATH:/usr/local/texlive/2010/texmf/doc/info
</pre>
</p>
<p>
Now, install a few more packages:<br />
<code>sudo apt-get install tex-common texinfo lmodern perl-tk</code><br />
Be sure to restart the terminal (or <code>source ~/.bashrc</code>) before you try using anything.
</p>
<p>
Now, if you try to bring anything from the repositories that depends on TeX Live, apt is going to pull over Ubuntu&#8217;s crappy copy. To avoid this, I created a meta package which checks off all of the TeX Live packages but doesn&#8217;t install anything.
</p>
<p>
I got the following from <a href="http://blogs.ethz.ch/ubuntu/2011/03/14/tex-live-2010-installation/">schlasim&#8217;s ubuntu blog</a>. First, paste and execute the following lines one at a time:</p>
<pre class="brush: plain; title: ; notranslate">
sudo apt-get install equivs
mkdir /tmp/tl-equivs &amp;&amp; cd /tmp/tl-equivs
equivs-control texlive-local
</pre>
<p>Now, edit the file texlive-local (<code>gedit texlive-local</code>), which was just created by the equivs-control program. Paste in the following lines:</p>
<pre class="brush: plain; title: ; notranslate">
Section: misc
Priority: optional
Standards-Version: 3.6.2

Package: texlive-local
Version: 2010-1~1
Maintainer: you
Provides: biblatex, biblatex-dw, cm-super, cm-super-minimal, context, dvipng,
 feynmf, fragmaster, lacheck, latex-beamer, latex-cjk-all, latex-cjk-chinese,
 latex-cjk-chinese-arphic-bkai00mp, latex-cjk-chinese-arphic-bsmi00lp,
 latex-cjk-chinese-arphic-gbsn00lp, latex-cjk-chinese-arphic-gkai00mp,
 latex-cjk-common, latex-cjk-japanese, latex-cjk-japanese-wadalab,
 latex-cjk-korean, latex-cjk-thai, latex-cjk-xcjk, latexmk, latex-sanskrit,
 latex-xcolor, lcdf-typetools, lmodern, luatex, musixlyr, musixtex, pgf,
 preview-latex-style, prosper, ps2eps, psutils, purifyeps, t1utils, tex4ht,
 tex4ht-common, tex-gyre, texlive, texlive-base, texlive-base-bin,
 texlive-base-bin-doc, texlive-bibtex-extra, texlive-binaries, texlive-common,
 texlive-doc-base, texlive-doc-bg, texlive-doc-cs+sk, texlive-doc-de,
 texlive-doc-el, texlive-doc-en, texlive-doc-es, texlive-doc-fi,
 texlive-doc-fr, texlive-doc-it, texlive-doc-ja, texlive-doc-ko,
 texlive-doc-mn, texlive-doc-nl, texlive-doc-pl, texlive-doc-pt,
 texlive-doc-ru, texlive-doc-si, texlive-doc-th, texlive-doc-tr,
 texlive-doc-uk, texlive-doc-vi, texlive-doc-zh, texlive-extra-utils,
 texlive-fonts-extra, texlive-fonts-extra-doc, texlive-fonts-recommended,
 texlive-fonts-recommended-doc, texlive-font-utils, texlive-formats-extra,
 texlive-games, texlive-generic-extra, texlive-generic-recommended,
 texlive-humanities, texlive-humanities-doc, texlive-lang-african,
 texlive-lang-all, texlive-lang-arab, texlive-lang-arabic,
 texlive-lang-armenian, texlive-lang-croatian, texlive-lang-cyrillic,
 texlive-lang-czechslovak, texlive-lang-danish, texlive-lang-dutch,
 texlive-lang-finnish, texlive-lang-french, texlive-lang-german,
 texlive-lang-greek, texlive-lang-hebrew, texlive-lang-hungarian,
 texlive-lang-indic, texlive-lang-italian, texlive-lang-latin,
 texlive-lang-latvian, texlive-lang-lithuanian, texlive-lang-manju,
 texlive-lang-mongolian, texlive-lang-norwegian, texlive-lang-other,
 texlive-lang-polish, texlive-lang-portuguese, texlive-lang-spanish,
 texlive-lang-swedish, texlive-lang-tibetan, texlive-lang-ukenglish,
 texlive-lang-vietnamese, texlive-latex3, texlive-latex-base,
 texlive-latex-base-doc, texlive-latex-extra, texlive-latex-extra-doc,
 texlive-latex-recommended, texlive-latex-recommended-doc, texlive-luatex,
 texlive-math-extra, texlive-metapost, texlive-metapost-doc, texlive-music,
 texlive-omega, texlive-pictures, texlive-pictures-doc, texlive-plain-extra,
 texlive-pstricks, texlive-pstricks-doc, texlive-publishers,
 texlive-publishers-doc, texlive-science, texlive-science-doc, texlive-xetex,
 texpower, texpower-manual, thailatex, tipa, ttf-freefont, ttf-gfs-artemisia,
 ttf-gfs-baskerville, ttf-gfs-bodoni-classic, ttf-gfs-didot,
 ttf-gfs-didot-classic, ttf-gfs-gazis, ttf-gfs-neohellenic, ttf-gfs-olga,
 ttf-gfs-porson, ttf-gfs-solomos, ttf-gfs-theokritos, ttf-sil-gentium, xindy,
 xindy-rules
Architecture: all
Description: My local installation of TeX Live 2010.
 A full &quot;vanilla&quot; TeX Live 2010

http://tug.org/texlive/debian#vanilla
</pre>
<p>Now, finish off by executing the last two lines:</p>
<pre class="brush: plain; title: ; notranslate">
equivs-build texlive-local
sudo dpkg -i texlive-local_2010-1~1_all.deb
</pre>
</p>
<p>
You should now be able to maintain your own TeX Live installation. You can update or install packages as you see fit, and you don&#8217;t have to deal with out of date packages causing you frustration.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2011/04/28/tex-live-2010-on-ubuntu-11-04/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sleep timer (suspend) script for Linux</title>
		<link>http://www.timteatro.net/2011/01/16/sleep-timer-suspend-script-for-linux/</link>
		<comments>http://www.timteatro.net/2011/01/16/sleep-timer-suspend-script-for-linux/#comments</comments>
		<pubDate>Sun, 16 Jan 2011 20:23:30 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Software & FOSS]]></category>
		<category><![CDATA[Bash]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1099</guid>
		<description><![CDATA[I usually go to sleep listening to <a href="http://gnaural.sourceforge.net/" target="_blank">gnaural</a> or watching old TV shows on the computer beside my bed (which my wife just loves). Rather than have it running all night, I'd rather have my computer sleep after a certain time.

Here's a simple (Bash) script to do just that.]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.timteatro.net/wp-content/uploads/2011/01/charmaineswart_S6302417_sm.jpg" alt="" title="charmaineswart_S6302417_sm" width="200" height="240" class="alignright size-full wp-image-1104" />
<p>
I usually go to sleep listening to <a href="http://gnaural.sourceforge.net/" target="_blank">gnaural</a> or watching old TV shows on the computer beside my bed (which my wife just loves). Rather than have it running all night, I&#8217;d rather have my computer sleep after a certain time.
</p>
<p>
For some reason, the last time I tried to write a script to suspend my computer (a few years ago), it was a serious pain in the ass. Now, thanks to <a href="http://pm-utils.freedesktop.org/wiki/" target="_blank">pm-utils</a>, it&#8217;s pretty easy. The command <code>pm-suspend</code> must be run as root, but aside from that it pretty much does what it says on the tin.
</p>
<p>
To use the script, copy and paste it into a text file (preferably called <code>sleeptimer</code>). Then, change the permissions on the file to make it executable. From the command line, you can do this with <code>chmod u+x sleeptimer</code>. From the command line, <code>./sleeptimer N</code>  &#8211; will sleep the computer in N minutes, where N is some positive integer. The script will go through a count down for each fifteen minutes and gives a warning at 10, 5 and then 1 minute before it sleeps.
</p>
<p>
Please forgive the crudity. I didn&#8217;t really write it for general public usage, but I&#8217;m posting it in response to a request.
</p>
<pre class="brush: bash; title: ; notranslate">
#!/bin/bash
#
# sleeptimer.sh
#
# Author: Timothy A.V. Teatro &lt;tim@timteatro.net&gt;
# Date  : Jan 11, 2011
#
# Description: A rather unsophisticated little script that I wrote for
#    myself. Since I usually fall asleep listening to gnaural or watching
#    a movie on my computer beside my bed, I wanted something to suspend
#    my computer rather than using shutdown.
#
# Usage : sudo ./sleeptimer &lt;integer&gt;
#    where &lt;integer&gt; is the number of minutes before the computer is suspended
#
#    This script is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#####################################################

#
# Must be run as root, so check id at the door.
#
if [ &quot;$(id -u)&quot; != &quot;0&quot; ]; then
	echo Must be run as root.
	exit 1
fi

min=$1 # Keep the first argument on the command line here
elapsed=0 

#
# Check to be sure argument is a number...
#
if [ $min -eq $min 2&gt; /dev/null ]; then
	#
	# ... and if it is, then decide what to do with it
	#
	# If it's less than 15 minutes, we want to count down to 10, 5 and
	# then 1 minute for feedback to the user.
	#
	if [ $min -lt 15 ]; then
		echo Sleeping in $min minutes [`date`]
		while [ $elapsed -le $min ]; do
			sleep 1m
			elapsed=$((elapsed+1))
			if [ $((min-elapsed)) -eq 10 ]; then
				echo Sleeping in 10 min.
			elif [ $((min-elapsed)) -eq 5 ]; then
				echo Sleeping in 5 min.
			elif [ $((min-elapsed)) -eq 1 ]; then
				echo Sleeping in less than 1 min.
			fi
		done
	else # If it's more, we'll count down in 15 minute intervals.
		#
		# Get the remainder of $min/15 and sleep that down.
		#
		remainder=`expr $min % 15`
		sleep ${remainder}m
		elapsed=$((elapsed+remainder))
		#
		# Now that the time remaining is a factor of 15, we'll sleep
		# in 15 minute incriments until the last 15 minutes of the
		# timer.
		#
		while [ $elapsed -lt $((min-15)) ]; do
			sleep 15m
			elapsed=$((elapsed+15))
			echo Sleeping in $((min-elapsed)) minutes. [`date`]
		done
		sleep 5m
		echo Sleeping in 10 minutes
		sleep 5m
		echo Sleeping in 5 minutes
		sleep 4m
		echo Sleeping in less than a minute.
		sleep 1m
	fi
	#
	# We've done our counting, now suspend.
	#
	echo &quot;!!! NOW SUSPENDING YOUR MACHINE !!!&quot;
	pm-suspend
	exit 0
else
	echo $min is not an integer. Please specify the number of minutes before sleep.
	exit 2
fi
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2011/01/16/sleep-timer-suspend-script-for-linux/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Quadratic approximation in Maple</title>
		<link>http://www.timteatro.net/2010/11/12/quadratic-approximation-in-maple/</link>
		<comments>http://www.timteatro.net/2010/11/12/quadratic-approximation-in-maple/#comments</comments>
		<pubDate>Fri, 12 Nov 2010 01:49:43 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Scientific Computing]]></category>
		<category><![CDATA[Maple]]></category>
		<category><![CDATA[Mathematics]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1094</guid>
		<description><![CDATA[A few of my students are having trouble using Maple to pull off a quadratic approximation. I wanted to post a little code to help them along.]]></description>
			<content:encoded><![CDATA[<p>A few of my students are having trouble using Maple to pull off a quadratic approximation. I wanted to post a little code to help them along.</p>
<p>As usual, I strongly encourage my students to use Maple <i>Classic</i>. The java interface of normal Maple may be flashy an appealing to the ignorant or those unfortunate enough to get used to it, but most real world work in Maple is done with Maple classic.</p>
<p>For a function <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-455852e0b3175cb9044ede0782b37488_l3.png" class="ql-img-inline-formula" alt="&#102;&#40;&#120;&#41;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/>, the quadratic approximation about a point <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-1f2b5ae068b799a6ce682b2c2948861a_l3.png" class="ql-img-inline-formula" alt="&#97;" title="Rendered by QuickLaTeX.com" style="vertical-align: 0px;"/> is given by
<p class="ql-center-displayed-equation" style="line-height: 38px;"><span class="ql-right-eqno"> &nbsp; </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-ad3a7d1860006e105524cb6f15582623_l3.png"class="ql-img-displayed-equation" alt="&#92;&#91; &#32;&#32;&#81;&#40;&#120;&#41;&#32;&#61;&#32;&#102;&#40;&#97;&#41;&#32;&#43;&#32;&#102;&#39;&#40;&#97;&#41;&#32;&#40;&#120;&#45;&#97;&#41;&#32;&#43;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#102;&#39;&#39;&#40;&#97;&#41;&#125;&#123;&#50;&#125;&#92;&#44;&#40;&#120;&#45;&#97;&#41;&#94;&#50;&#46; &#92;&#93; " title="Rendered by QuickLaTeX.com"/></p>
</p>
<p>In Maple, this can be implemented as:<br />
<code><br />
> restart;<br />
> f:=ln(x)/sqrt(x);  # Define a function<br />
> a:=1; # The point about which we'll expand<br />
> Q:=subs(x=a, f) + subs(x=a, diff(f, x))*(x-a) + subs(x=a, diff(f, x, x))*(x-a)^2/2;<br />
> plot([f, Q], x=-5..5, y=-5..5);<br />
</code></p>
<p>Now try playing around with the above code. Change the values of a and f(x).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2010/11/12/quadratic-approximation-in-maple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comic: Busted to the Future!</title>
		<link>http://www.timteatro.net/2010/11/04/comic-busted-to-the-future/</link>
		<comments>http://www.timteatro.net/2010/11/04/comic-busted-to-the-future/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 03:31:20 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Comics]]></category>
		<category><![CDATA[Comic]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=1083</guid>
		<description><![CDATA[Proof positive that you just can't run from the cops... Even if you have a time machine.]]></description>
			<content:encoded><![CDATA[<div id="attachment_1084" class="wp-caption aligncenter" style="width: 490px"><a href="http://www.timteatro.net/wp-content/uploads/2010/11/BTF_comic_1.jpg"><img src="http://www.timteatro.net/wp-content/uploads/2010/11/BTF_comic_1.jpg" alt="Please contact me if you don&#039;t see the comic." title="BTF_comic_1" width="480" height="308" class="size-full wp-image-1084" /></a><p class="wp-caption-text">What'ca gonna do when they come for you?</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2010/11/04/comic-busted-to-the-future/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance Python: Solving The 2D Diffusion Equation With numpy</title>
		<link>http://www.timteatro.net/2010/10/29/performance-python-solving-the-2d-diffusion-equation-with-numpy/</link>
		<comments>http://www.timteatro.net/2010/10/29/performance-python-solving-the-2d-diffusion-equation-with-numpy/#comments</comments>
		<pubDate>Fri, 29 Oct 2010 03:01:10 +0000</pubDate>
		<dc:creator>Tim Teatro</dc:creator>
				<category><![CDATA[Scientific Computing]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://www.timteatro.net/?p=948</guid>
		<description><![CDATA[The aim of this article is to demonstrate how numpy can be used to dramatically increase the efficiency of the simple task of integrating the two dimensional diffusion equation. There will also be an example of advanced visualization with matplotlib, creating an animated image map to visualize the solution. The code in this article is nowhere near as elegant as it could be, but it is intentionally plain for the beginners eye.]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.timteatro.net/wp-content/uploads/2010/10/diffusion_numpy_poster-250x300.jpg" alt="" title="diffusion_numpy_poster" width="250" height="300" class="alignright size-medium wp-image-1073" /></p>
<p>
	The sliding scale between performance and coding simplicity has led to a spectrum of languages over the years. In the world of scientific computing, the two major languages are of course C and FORTRAN. Both of these are compiled, low level and high performance languages. Generally, one rarely considers an executed language for scientific grunt-work since interpreted languages tend to be drastically slower. In recent years, computers have become fast enough for a compromise, but still, most interpreted languages fall short of the task.
</p>
<p>
	Python is different. On its own, it&#8217;s still too slow for general scientific application. However, its ability to easily interface with bits of binary code make it attractively extensible for scientific and numerical applications.
</p>
<p>
	<a href="http://numpy.scipy.org/" target="_blank" title="Numpy's website">Numpy</a>, a package of scientific computing tools for Python, extends Python&#8217;s data structures with a multidimensional array object, various derived objects and an assortment of routines for fast operations on arrays. As long as your problem can be cast into numpy&#8217;s array objects, operations can be carried out with speeds to rival a pure C implementation.
</p>
<p>
	<a href="http://www.scipy.org/" target="_blank" title="Scipy website">SciPy</a> and <a href="http://matplotlib.sourceforge.net/" target="_blank" title="">matplotlib</a> further extend numpy + Python to give functionality similar to Matlab making Python an excellent free and open-source alternative. Python has the added advantage of being a popular and complete programming language.
</p>
<p>
	The aim of this article is to demonstrate how numpy can be used to dramatically increase the efficiency of the simple task of integrating the two dimensional diffusion equation. There will also be an example of advanced visualization with matplotlib, creating an animated image map to visualize the solution. The code in this article is nowhere near as elegant as it could be, but it is intentionally plain for the beginners eye.
</p>
<h2>The two-dimensional diffusion equation</h2>
<p>
	We consider the partial differential equation
<p class="ql-center-displayed-equation" style="line-height: 44px;"><span class="ql-right-eqno"> &nbsp; </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-c09e8b9f10466c7310d4ec3512fcaaa6_l3.png"class="ql-img-displayed-equation" alt=" 	&#92;&#91; 		&#92;&#102;&#114;&#97;&#99;&#123;&#92;&#112;&#97;&#114;&#116;&#105;&#97;&#108;&#32;&#117;&#125;&#123;&#92;&#112;&#97;&#114;&#116;&#105;&#97;&#108;&#32;&#116;&#125;&#32;&#61;&#32;&#97;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#102;&#114;&#97;&#99;&#123;&#92;&#112;&#97;&#114;&#116;&#105;&#97;&#108;&#94;&#50;&#32;&#117;&#125;&#123;&#92;&#112;&#97;&#114;&#116;&#105;&#97;&#108;&#32;&#120;&#94;&#50;&#125;&#32;&#43;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#92;&#112;&#97;&#114;&#116;&#105;&#97;&#108;&#94;&#50;&#32;&#117;&#125;&#123;&#92;&#112;&#97;&#114;&#116;&#105;&#97;&#108;&#32;&#121;&#94;&#50;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#44; 	&#92;&#93; 	" title="Rendered by QuickLaTeX.com"/></p>
<p> or more compactly, <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-1e9a39568bc4a42bc2b9a2d1586122db_l3.png" class="ql-img-inline-formula" alt="&#117;&#95;&#116;&#32;&#61;&#32;&#97;&#92;&#108;&#101;&#102;&#116;&#40;&#117;&#95;&#123;&#120;&#120;&#125;&#43;&#117;&#95;&#123;&#121;&#121;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#41; 	" title="Rendered by QuickLaTeX.com" style="vertical-align: -9px;"/> on the domain of the unit square (x,y between zero and one). This is the diffusion equation which models diffusive processes such as heat and chemical concentration.
</p>
<p>
	We approximate <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-28f164c234557eda0e9c1305e839adad_l3.png" class="ql-img-inline-formula" alt="&#117;&#40;&#120;&#44;&#121;&#44;&#116;&#41;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> by the discrete function <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-7f7b8ce6e492c2371f7f04241a868cfc_l3.png" class="ql-img-inline-formula" alt="&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;" title="Rendered by QuickLaTeX.com" style="vertical-align: -9px;"/>, where <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-222d9fd16f5132b7e6c08c2d2a2b2e08_l3.png" class="ql-img-inline-formula" alt="&#120;&#61;&#105;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;" title="Rendered by QuickLaTeX.com" style="vertical-align: 0px;"/>, <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-5713c020a987b86530d3bd1dd261849a_l3.png" class="ql-img-inline-formula" alt="&#121;&#61;&#106;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#121;" title="Rendered by QuickLaTeX.com" style="vertical-align: -4px;"/> and <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-e75c000c18c69e3d87c88e2b7e557954_l3.png" class="ql-img-inline-formula" alt="&#116;&#61;&#109;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#116;" title="Rendered by QuickLaTeX.com" style="vertical-align: 0px;"/>. Applying finite (forward) difference approximations to the derivatives, then we obtain the partial difference equation
<p class="ql-center-displayed-equation" style="line-height: 123px;"><span class="ql-right-eqno"> &nbsp; </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-baa3c1f25193f40a2cbb9e53c554561b_l3.png"class="ql-img-displayed-equation" alt=" 	&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#108;&#105;&#103;&#110;&#42;&#125; 		&#92;&#102;&#114;&#97;&#99;&#123;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#43;&#49;&#41;&#125;&#45;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#125;&#123;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#116;&#125;&#32;&#61;&#32;&#97;&#32;&#38;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#102;&#114;&#97;&#99;&#123;&#32;&#117;&#95;&#123;&#105;&#43;&#49;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#45;&#32;&#50;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#43;&#32;&#117;&#95;&#123;&#105;&#45;&#49;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#125;&#123;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;&#41;&#94;&#50;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#46;&#92;&#92;&#32;&#38;&#92;&#113;&#113;&#117;&#97;&#100;&#43;&#32;&#92;&#108;&#101;&#102;&#116;&#46;&#92;&#102;&#114;&#97;&#99;&#123;&#32;&#117;&#95;&#123;&#105;&#44;&#106;&#43;&#49;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#45;&#32;&#50;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#43;&#32;&#117;&#95;&#123;&#105;&#44;&#106;&#45;&#49;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#125;&#123;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#121;&#41;&#94;&#50;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#44; 	&#92;&#101;&#110;&#100;&#123;&#97;&#108;&#105;&#103;&#110;&#42;&#125; 	" title="Rendered by QuickLaTeX.com"/></p>
<p> or
<p class="ql-center-displayed-equation" style="line-height: 123px;"><span class="ql-right-eqno"> (1) </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-b8867288be05248f2e8cb10e5b112d48_l3.png"class="ql-img-displayed-equation" alt=" 	&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#108;&#105;&#103;&#110;&#42;&#125; 		&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#43;&#49;&#41;&#125;&#32;&#61;&#32;&#32;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#43;&#32;&#97;&#38;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#116;&#92;&#108;&#101;&#102;&#116;&#40;&#92;&#102;&#114;&#97;&#99;&#123;&#32;&#117;&#95;&#123;&#105;&#43;&#49;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#45;&#32;&#50;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#43;&#32;&#117;&#95;&#123;&#105;&#45;&#49;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#125;&#123;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;&#41;&#94;&#50;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#46;&#92;&#92;&#32;&#38;&#92;&#113;&#113;&#117;&#97;&#100;&#32;&#43;&#32;&#92;&#108;&#101;&#102;&#116;&#46;&#92;&#102;&#114;&#97;&#99;&#123;&#32;&#117;&#95;&#123;&#105;&#44;&#106;&#43;&#49;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#45;&#32;&#50;&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#43;&#32;&#117;&#95;&#123;&#105;&#44;&#106;&#45;&#49;&#125;&#94;&#123;&#40;&#109;&#41;&#125;&#32;&#125;&#123;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#121;&#41;&#94;&#50;&#125;&#92;&#114;&#105;&#103;&#104;&#116;&#41;&#46; 	&#92;&#101;&#110;&#100;&#123;&#97;&#108;&#105;&#103;&#110;&#42;&#125; 	" title="Rendered by QuickLaTeX.com"/></p>
</p>
<h3>A word on stability</h3>
<p>
	The stability of the scheme requires that the following condition is met:
<p class="ql-center-displayed-equation" style="line-height: 44px;"><span class="ql-right-eqno"> &nbsp; </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-fc1d0adc946d349c27884c0d1dbcc4e2_l3.png"class="ql-img-displayed-equation" alt=" 	&#92;&#91; 		&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#116;&#32;&#92;&#108;&#101;&#113;&#32;&#92;&#102;&#114;&#97;&#99;&#123;&#49;&#125;&#123;&#50;&#97;&#125;&#92;&#44;&#92;&#102;&#114;&#97;&#99;&#123;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;&#92;&#44;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#121;&#41;&#94;&#50;&#125;&#123;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;&#41;&#94;&#50;&#32;&#43;&#32;&#40;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#121;&#41;&#94;&#50;&#125;&#46; 	&#92;&#93; 	" title="Rendered by QuickLaTeX.com"/></p>
</p>
<p>
	If we set the time interval at the highest value allowed by the condition, we will still see some noise in the solution. Reducing the time-step size can improve this.
</p>
<h3>Initial conditions, boundary values and characteristics of the solution</h3>
<p>
	Equation (1) is a blueprint for integrating the solution forward in time, calculating the solution at each time-step based on the solution at the previous time-step. Since a previous time-step is required for each subsequent step, the initial conditions (i.e., <img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-455c17e501b824695f701a3975a87f27_l3.png" class="ql-img-inline-formula" alt="&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#48;&#41;&#125;" title="Rendered by QuickLaTeX.com" style="vertical-align: -9px;"/>) must be given. Additionally, the derivatives in the Laplacian require boundary conditions.
</p>
<p>
	For the example in this article, we will choose the initial conditions to be a ring centred at (0.5, 0.5), defined by
<p class="ql-center-displayed-equation" style="line-height: 44px;"><span class="ql-right-eqno"> &nbsp; </span><span class="ql-left-eqno"> &nbsp; </span><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-dad310b0dfba0dcec10f52ba31ade721_l3.png"class="ql-img-displayed-equation" alt=" 	&#92;&#91; 	&#117;&#95;&#123;&#105;&#44;&#106;&#125;&#94;&#123;&#40;&#48;&#41;&#125;&#32;&#61;&#32;&#92;&#108;&#101;&#102;&#116;&#92;&#123; &#32;&#32;&#32;&#32;&#32;&#92;&#98;&#101;&#103;&#105;&#110;&#123;&#97;&#114;&#114;&#97;&#121;&#125;&#123;&#108;&#108;&#125; &#32;&#32;&#32;&#32;&#32;&#32;&#32;&#49;&#32;&#38;&#58;&#32;&#48;&#46;&#48;&#53;&#32;&#92;&#108;&#101;&#113;&#32;&#40;&#105;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;&#32;&#45;&#32;&#48;&#46;&#53;&#41;&#94;&#50;&#32;&#43;&#32;&#40;&#106;&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#121;&#32;&#45;&#32;&#48;&#46;&#53;&#41;&#94;&#50;&#32;&#92;&#108;&#101;&#113;&#32;&#48;&#46;&#49;&#92;&#92; &#32;&#32;&#32;&#32;&#32;&#32;&#32;&#48;&#32;&#38;&#58;&#32;&#92;&#109;&#97;&#116;&#104;&#114;&#109;&#123;&#111;&#116;&#104;&#101;&#114;&#119;&#105;&#115;&#101;&#125; &#32;&#32;&#32;&#32;&#32;&#92;&#101;&#110;&#100;&#123;&#97;&#114;&#114;&#97;&#121;&#125; &#32;&#32;&#32;&#92;&#114;&#105;&#103;&#104;&#116;&#46;&#32;&#46; 	&#92;&#93;&#32; 	" title="Rendered by QuickLaTeX.com"/></p>
<p> We will also choose the boundary conditions to be zero, in agreement with the initial conditions.
</p>
<p>
	The solution given these conditions is illustrated in Fig. 1
</p>
<div id="attachment_1068" class="wp-caption aligncenter" style="width: 490px"><a href="http://www.timteatro.net/wp-content/uploads/2010/10/diffusion_summary.jpg"><img src="http://www.timteatro.net/wp-content/uploads/2010/10/diffusion_summary.jpg" alt="Time evolution of the diffusion equation." title="diffusion_summary" width="480" height="436" class="size-full wp-image-1068" /></a><p class="wp-caption-text">Fig. 1: An illustration of the time evolution of diffusion (A) when t=0 (showing the initial conditions), (B) when m=5, (C) when m=200 and (D) when m=500.</p></div>
<h2>Coding the problem in Python</h2>
<h3>Plain Python</h3>
<p>
	I&#8217;d hope the reader is somewhat familiar with Python; but if not, it should be easy enough to follow along. Feel free to post questions, although I&#8217;d imagine that Google is good enough to answer most.
</p>
<p>
	To integrate Eq. (1) through time, we set up a loop,</p>
<pre class="brush: python; gutter: false; title: ; notranslate">
for m in range(1, timesteps+1):
	print &quot;Computing u for m =&quot;, m
	evolve_ts(u, ui)
</pre>
<p>	where <code>m</code> is our time index and <code>timesteps</code> holds the number of time-steps to calculate (say, 500). We have to define some function <code>evolve_ts(u, ui)</code>, which will calculate <code>u</code> from <code>ui</code>.
</p>
<p>
	Using plain Python syntax, the function <code>evolve_ts</code> should look something like this:</p>
<pre class="brush: python; gutter: false; title: ; notranslate">
def evolve_ts(u, ui):
	global nx, ny
	&quot;&quot;&quot;
	This function uses two plain Python loops to
	evaluate the derivatives in the Laplacian, and
	calculates u[i,j] based on ui[i,j].
	&quot;&quot;&quot;
	for i in range(1,nx-1):
		for j in range(1,ny-1):
			uxx = ( ui[i+1,j] - 2*ui[i,j] + ui[i-1, j] )/dx2
			uyy = ( ui[i,j+1] - 2*ui[i,j] + ui[i, j-1] )/dy2
			u[i,j] = ui[i,j]+dt*a*(uxx+uyy)
</pre>
<p>where <code>nx</code> and <code>ny</code> contain the number of elements of <code>u</code> in the x- and y-direction (i.e., <code>nx=1/dx</code> where <code>dx=</code><img src="http://www.timteatro.net/wp-content/ql-cache/quicklatex.com-e5d2a8b0ef03c74ae4131397f782b779_l3.png" class="ql-img-inline-formula" alt="&#92;&#68;&#101;&#108;&#116;&#97;&#32;&#120;" title="Rendered by QuickLaTeX.com" style="vertical-align: 0px;"/>). Although Python indexes from 0, we want the loops to start at index 1 and end at index nx-1 or ny-1 (as the case may be). This is because we wish to leave the boundary values at zero, as defined by our boundary conditions.
</p>
<p>
	Here is a listing of a fully functional Python script which implements the above code. Copy and paste it into a document and execute it (you may have to <code>chmod u+x</code> to execute it under Linux/UNIX)
</p>
<pre class="brush: python; gutter: true; title: ; notranslate">
#!/usr/bin/env python
&quot;&quot;&quot;
A program which uses an explicit finite difference
scheme to solve the diffusion equation with fixed
boundary values and a given initial value for the
density.

Two steps of the solution are stored: the current
solution, u, and the previous step, ui. At each time-
step, u is calculated from ui. u is moved to ui at the
end of each time-step to move forward in time.

&quot;&quot;&quot;
import scipy as sp
import time

# Declare some variables:

dx=0.01        # Interval size in x-direction.
dy=0.01        # Interval size in y-direction.
a=0.5          # Diffusion constant.
timesteps=500  # Number of time-steps to evolve system.

nx = int(1/dx)
ny = int(1/dy)

dx2=dx**2 # To save CPU cycles, we'll compute Delta x^2
dy2=dy**2 # and Delta y^2 only once and store them.

# For stability, this is the largest interval possible
# for the size of the time-step:
dt = dx2*dy2/( 2*a*(dx2+dy2) )

# Start u and ui off as zero matrices:
ui = sp.zeros([nx,ny])
u = sp.zeros([nx,ny])

# Now, set the initial conditions (ui).
for i in range(nx):
	for j in range(ny):
		if ( ( (i*dx-0.5)**2+(j*dy-0.5)**2 &lt;= 0.1)
			&amp; ((i*dx-0.5)**2+(j*dy-0.5)**2&gt;=.05) ):
				ui[i,j] = 1

def evolve_ts(u, ui):
	global nx, ny
	&quot;&quot;&quot;
	This function uses two plain Python loops to
	evaluate the derivatives in the Laplacian, and
	calculates u[i,j] based on ui[i,j].
	&quot;&quot;&quot;
	for i in range(1,nx-1):
		for j in range(1,ny-1):
			uxx = ( ui[i+1,j] - 2*ui[i,j] + ui[i-1, j] )/dx2
			uyy = ( ui[i,j+1] - 2*ui[i,j] + ui[i, j-1] )/dy2
			u[i,j] = ui[i,j]+dt*a*(uxx+uyy)

# Now, start the time evolution calculation...
tstart = time.time()
for m in range(1, timesteps+1):
	evolve_ts(u, ui)
	print &quot;Computing u for m =&quot;, m
tfinish = time.time()

print &quot;Done.&quot;
print &quot;Total time: &quot;, tfinish-tstart, &quot;s&quot;
print &quot;Average time per time-step using numpy: &quot;, ( tfinish - tstart )/timesteps, &quot;s.&quot;
</pre>
<p>
	Executing the above code on my office workstation for 500 time-steps with <code>dx=dy=0.01</code> takes about 88 seconds, averaging 0.1758s per time-step. Considering a compiled C-code implementation would execute all 500 time-steps in less than two-tenths of a second, the Python performance is pretty dismal.
</p>
<h3>Enhanced by numpy</h3>
<p>
	Luckily, we can make a small change to the above code to boost the perfomance close to that of pure C.
</p>
<p>
	Assuming you&#8217;ve run the above Python code, replace the definition of <code>evolve_ts</code> with the following:</p>
<pre class="brush: python; gutter: false; title: ; notranslate">
def evolve_ts(u, ui):
	&quot;&quot;&quot;
	This function uses a numpy expression to
	evaluate the derivatives in the Laplacian, and
	calculates u[i,j] based on ui[i,j].
	&quot;&quot;&quot;
	u[1:-1, 1:-1] = ui[1:-1, 1:-1] + a*dt*( (ui[2:, 1:-1] - 2*ui[1:-1, 1:-1] + ui[:-2, 1:-1])/dx2 + (ui[1:-1, 2:] - 2*ui[1:-1, 1:-1] + ui[1:-1, :-2])/dy2 )
</pre>
</p>
<p>
	In this version of <code>evolve_ts</code>, the loops for x and y have been replaced by a numpy expression. The benefits here are two-fold: (1) the syntax is leaner, replacing five lines of code with only one (albeit a larger one) and (2) numpy executes the expression using C. Hence, the performance of the code is drastically increased. On my computer, the numpy expression takes less than a quarter-second to execute (compare plain Python at 88s) with an average of 4.70094E-4s per time-step: nearly 400-times faster than the plain Python code.
</p>
<p>
	The trick with the numpy expressions is the way numpy iterates through the slices. To understand exactly what the above statement does, you can use the official (tentative) numpy tutorial, starting with the section on <a href="http://www.scipy.org/Tentative_NumPy_Tutorial#head-864862d3f2bb4c32f04260fac61eb4ef34788c4c" target="_blank" title="Numpy tutorial">slicing</a>.
</p>
<h2 id="appendix_matplotlib">Using matplotlib to visualize</h2>
<div id="attachment_1071" class="wp-caption alignright" style="width: 310px"><a href="http://www.timteatro.net/wp-content/uploads/2010/10/diffusion_matplotlib_density.png"><img src="http://www.timteatro.net/wp-content/uploads/2010/10/diffusion_matplotlib_density-300x256.png" alt="" title="diffusion_matplotlib_density" width="300" height="256" class="size-medium wp-image-1071" /></a><p class="wp-caption-text">Fig 2. An example of the output from the matplotlib visualization, with white edges trimmed off for web presentation.</p></div>
<p>
	Matplotlib can be very simple and easy to use. In this particular case, I wanted to do something quite advanced: create an animated, real-time density map (Fig. 2) of the solution as it is calculated.
</p>
<p>
	The following code uses the numpy enhanced <code>update_ts</code> function, and pops up a windows which renders the solution in real time, as it&#8217;s calculated. The performance is low, due to the rendering. Some details of the code are briefly explained in the comments; but I&#8217;ll generally leave it to the user to figure out what things do. I didn&#8217;t intend this example to be instructive but merely a showpiece for what can be done with relatively little code.
</p>
<pre class="brush: python; gutter: true; title: ; notranslate">
#!/usr/bin/env python
&quot;&quot;&quot;
Author: Timothy A.V. Teatro &lt;http://www.timteatro.net&gt;
Date  : Oct 25, 2010
Lisence: Creative Commons BY-SA
(http://creativecommons.org/licenses/by-sa/2.0/)

Description:
	A program which uses an explicit finite difference
	scheme to solve the diffusion equation with fixed
	boundary values and a given initial value for the
	density u(x,y,t). This version uses a numpy
	expression which is evaluated in C, so the
	computation time is greatly reduced over plain
	Python code.

	This version also uses matplotlib to create an
	animation of the time evolution of the density.
&quot;&quot;&quot;
import scipy as sp
import matplotlib
matplotlib.use('GTKAgg') # Change this as desired.
import gobject
from pylab import *
# Declare some variables:

dx=0.01        # Interval size in x-direction.
dy=0.01        # Interval size in y-direction.
a=0.5          # Diffusion constant.
timesteps=500  # Number of time-steps to evolve system.

nx = int(1/dx)
ny = int(1/dy)

dx2=dx**2 # To save CPU cycles, we'll compute Delta x^2
dy2=dy**2 # and Delta y^2 only once and store them.

# For stability, this is the largest interval possible
# for the size of the time-step:
dt = dx2*dy2/( 2*a*(dx2+dy2) )

# Start u and ui off as zero matrices:
ui = sp.zeros([nx,ny])
u = sp.zeros([nx,ny])

# Now, set the initial conditions (ui).
for i in range(nx):
	for j in range(ny):
		if ( ( (i*dx-0.5)**2+(j*dy-0.5)**2 &lt;= 0.1)
			&amp; ((i*dx-0.5)**2+(j*dy-0.5)**2&gt;=.05) ):
				ui[i,j] = 1

def evolve_ts(u, ui):
	&quot;&quot;&quot;
	This function uses a numpy expression to
	evaluate the derivatives in the Laplacian, and
	calculates u[i,j] based on ui[i,j].
	&quot;&quot;&quot;
	u[1:-1, 1:-1] = ui[1:-1, 1:-1] + a*dt*( (ui[2:, 1:-1] - 2*ui[1:-1, 1:-1] + ui[:-2, 1:-1])/dx2 + (ui[1:-1, 2:] - 2*ui[1:-1, 1:-1] + ui[1:-1, :-2])/dy2 )

def updatefig(*args):
	global u, ui, m
	im.set_array(ui)
	manager.canvas.draw()
	# Uncomment the next two lines to save images as png
	# filename='diffusion_ts'+str(m)+'.png'
	# fig.savefig(filename)
	u[1:-1, 1:-1] = ui[1:-1, 1:-1] + a*dt*(
		(ui[2:, 1:-1] - 2*ui[1:-1, 1:-1] + ui[:-2, 1:-1])/dx2
		+ (ui[1:-1, 2:] - 2*ui[1:-1, 1:-1] + ui[1:-1, :-2])/dy2 )
	ui = sp.copy(u)
	m+=1
	print &quot;Computing and rendering u for m =&quot;, m
	if m &gt;= timesteps:
		return False
	return True

fig = plt.figure(1)
img = subplot(111)
im = img.imshow( ui, cmap=cm.hot, interpolation='nearest', origin='lower')
manager = get_current_fig_manager()

m=1
fig.colorbar( im ) # Show the colorbar along the side

# once idle, call updatefig until it returns false.
gobject.idle_add(updatefig)
show()
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.timteatro.net/2010/10/29/performance-python-solving-the-2d-diffusion-equation-with-numpy/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

