Monday, February 18, 2013

PRACE Summer of HPC

Here's my source code for the PRACE admissions code test for the summer internship.

http://summerofhpc.prace-ri.eu/code-test/

Wednesday, January 30, 2013

ODE for Nutrition

It is so liberating to be doing something different while using the tools that you know and come to appreciate. After a conversation with one of my old high school friends, this matlab code was written in about half a day, for the fun of it of course. This code should be capable of producing a "diet" plan based on a Proportional-Differential controller, and also outputs the predicted mass evolution. I would personally be testing it out and hopefully mathematics/physics will prevail. :-)

Edit: Added some references to the numbers in the source code after a suggestion from a friend.


function Diet_PD
%Using the Mifflin-St.Jeor model for BMR, modified with a linear interpolation of metabolic adaptation rate for calory intake changes. Controller scheme used is the Proportional-Differential controller (widely used in engineering)

clear all; clc;
%User inputs
t = 0:1:1*365;
h = 165; %Height (cm)
a = 27; %Age (years)
s = 1; %Gender (1 - Male, 2 - Female)

m0 = 75; %Initial mass (kg)
mtar = 60; %Target mass (kg)

phys_frac = 1.3; %1.2 <= Physical activity fraction <= 1.9 (Default: 1.2 = Sedentary, 1.375 = Light activity, 1.55 = Moderate, 1.725 = Very active, 1.9 = Professional/Hard labour)

Tmin = 1000; %Minimum calorie intake per day
Tmax = 5000; %Maximum calorie intake per day

%Controller gain constants (Trial & Error/Tuning methods)
K1 = 2000; %Proportional Controller gain
K2 = 10; %Derivative Controller gain

%Do not mess with the values after this line if not familiar
%Initial metabolic adaptation fraction calculation (Numbers from Martin et al, Human Physiology, doi:
10.1038/oby.2007.354., 2007)
x0 = [m0 met_ad_frac];
a1 = 1-1437/1523;
a2 = 1-1484/1514;
dadt = (1437/1523-1484/1514)/90;
met_ad_frac = ((a1+a2)-(270*dadt))/2;

if(s==1)
    gen = 5;
else
    gen = -161;
end

x0 = [m0 met_ad_frac];
[mplot] = odeft(@deriv_RHS,t,x0,5,h,a,dadt,gen,phys_frac,Tmin,Tmax,K1,K2,mtar);
figure(1)
plot(t,mplot(:,1),'ok');
xlabel('Days'); ylabel('Mass (kg)');
source = max(Tmin,K1*(-mplot(:,1)+mtar) + K2*phys_frac*(10*mplot(:,1)+6.25*h-5*a+gen)/7700);
source = min(Tmax,source);
figure(2)
plot(t,source,'ok');
xlabel('Days'); ylabel('Energy Intake (kCal)');
return


function [wdot] = deriv_RHS(t,x0,h,a,dadt,gen,phys_frac,Tmin,Tmax,K1,K2,mtar)
    T = K1*(-x0(1)+mtar) + K2*phys_frac*(10*x0(1)+6.25*h-5*a+gen)/7700;
    if(T < Tmin)
        T = Tmin;
    elseif(T > Tmax)
        T = Tmax;
    end
    if(x0(2)<=0)
    x0(2)=0;
    end
    wdot(1,1) = T/7700 - (1-x0(2))*phys_frac*(10*x0(1)+6.25*h-5*(a+t/365)+gen)/7700;
    wdot(2,1) = dadt;
end

end

Tuesday, November 27, 2012

Today I just had a very annoying encounter with UPS. Long story short, I didn't receive my item on time and am very inconvenienced the next day, just to get the item again. Spoke to the customer service and they all claimed that their drivers have no phones with them when they do their routes, which means they can't actually call the dropoff customer prior to sending to make sure that the package can be delivered or scheduled at a different time. This made me think of a simple tech solution (systems overview of course).

Problem: No phones provided to the delivery guys.
Possible causes: Expensive (prone to stealing, company distrusts the delivery guys)

Solution: Provide basic smartphone/communication device to the delivery guys. (Raspberry PI can be modified to include a radio receiver/transmitter and a GPS unit). OS and firmware locked (remove super user account). Prior to dispatch, delivery guy syncs route data along with customer gps locations and contact details. When the unit is approximately 30 mins away from the location, the device sends an automated sms to the customer, alerting them. If the customer is not in, s/he can reschedule it via a number generated by the device.

Yeap, seems that's about it... Man, I must be on a bit of weed...

Monday, August 13, 2012

The Run

I run and run and run, trying to find a filled spot, a spot filled with joy, understanding, and love, yet with each stride I take, I seem to be farther away from the spot. From where I stand now, all I see is emptiness and yet logic dictates that I cannot be further from where I am to the spot which you are currently occupying in my heart.

Thursday, June 28, 2012

Derek's in Kulim!

I kinda vowed not to delve into food blogging when I started this blog, but since I wanted to try out the new blogger app in my phone and this place I was in, deserved a mention, I thought, why not?

So, here's the lowdown on this quaint place in Kulim for a 80s kind of coffee house grub. For the past few days, I've been at a loss on good food options in my hometown. Due to a combination of desperation and Google, I found this link, which introduced me virtually to Derek's.
Since my curiosity was piqued, I tried it out today for lunch and am really happy that I did so. The place is indeed very hard to find so I will put in a geotag for my next visit there or you can visit the link for a rough location.

As I entered Derek's, I felt that the setting has a very simple, 80s like coffee house ambience to it. Pretty much no frills. There's only one server, who kind of does everything except cooking, so service can be slightly slow but still faster than a one-man restaurant in Barcelona. I helped myself to a seat near the window and after waiting a while, the server handed a menu card.

The menu consists of three soup choices, the ubiquitous mushroom, cauliflower and potato, and madras pumpkin. Tempted as I was to try the pumpkin soup, it was unfortunate as the soup of the day in the set was... you've guessed it... mushroom... Never mind! The main course options are also not that many but as a rough idea of the choices available, there is a dory fish and chips, spaghetti vongole, and barbecued lamb shanks. In total the number of main courses is about 15ish. If one goes for the set meal, one can get a soup of the day, two scoops of ice cream or a slice of fruit, and a hot beverage (coffee or tea).

I went with a set meal of pan fried chicken breasts. The mushroom soup was decent, not too thick, with a distinct mushroom aftertaste and a slight hint of tarragon. Not too creamy but at times there are some small blobs of salty starch which is kind of what one would expect in the soups of an 80s diner. The bread roll is not really worth mentioning except that it comes hot and is accompanied by a small bowl of butter. Yes, a BOWL of butter, free for you to spread on anything... You hardly see this anywhere else nowadays.

The chicken main course was very good. The dish came with two pieces of pan fried breasts, a boiled cauliflower and broccoli, and a big blob of mashed potatoes. Taste wise it was excellent with enough herbs, mainly marjoram, and thyme, and also the lemon butter was quite, shall I say, authentic? The breasts were not dry but as always with a single cooking preparation, the crusts were crispy but the meat themselves were not melt in your mouth tender like those prepped via sous-vide style. The boiled veggies were not over cooked and that bowl of butter helped in augmenting the flavors. The mashed potato was decent with enough fat and herbs. The chopped garlic bits add bite and flavor to the mash. I do have to add that the mash could've used an old style hand mashing/ricer instead of the blades of the food processor as the texture was not smooth and velvety but more like wallpaper paste. Overall, it was a very good plate of main course.

The dessert part was the ice cream topped with crushed peanuts. Standard coffee house fare but I like the crushed peanuts as it tasted fresh, unlike the ones you get in Malaysian/Singaporean mall "restaurants" where the nuts are usually rancid. Coffee was horrible though, but no complaints as it's the same anywhere else in Penang or Singapore outside of specialized cafes. For the record, I love kopi-o sold in those old 70s kind of coffee shops (kopitiams). The way the beans are roasted with sugar, gives it a unique and complex flavor profile due to the Maillard reaction. Much better than Americanos from Shittybucks. Marketing ideas people!

On the whole, I like this place and the food served here. What was most surprising to me was that Derek's is found in my small hometown, and believe it or not, the food served here puts many mall restaurants to shame. The whole meal only set me back by 25 MYR which is roughly 12.50 SGD.

8.5/10


Sunday, December 4, 2011

PID controller for FLUENT

I suppose I was too bored on Friday evening and hence, I implemented a very simple PID controller for my FEEP thruster heater. In the interest of freely publishing code, and self-gloating (well, I don't think this is something to gloat about anyways), here's the subroutine I wrote in C.

DEFINE_SOURCE(heater_pow_PID,c,t,dS,eqn) /* PID controller */
{
Domain *domain1;
domain1 = Get_Domain(1);
int ID_heat=6; /* Get the ID for the heater */
int ID_emit=7; /* Get the ID for the emitter */
int axi1 = 1; /* 1 if axisymmetric, 0 if cartesian */
Thread *tc_heat=Lookup_Thread(domain1,ID_heat); /* Get the thread for cells in the heater */
Thread *tc_emit=Lookup_Thread(domain1,ID_emit); /* Get the thread for cells in the emitter */
real vol_tot_heat = 0;
real vol_tot_emit = 0;
real cell_vol = 0;
real temp_emit = 0;
real temp_emit_M1 = 0;
real dtemp_dt = 0;
real dt = CURRENT_TIMESTEP;
real temp_target = 473.15; /* 200 C */
real heater_power_max = 0.5; /* in Watt */
real heater_power_ss = 0;
real dheat_dT = 0;
real heater_power = 0;
/* Controller constants */
real kp = 20*2.857e-3;
real kd = 2*kp;
real ki = 0*kp;
real axi_k1 = 0;
real source = 0;
cell_t c1;
face_t f1;
axi_k1 = (axi1==1)? 2*M_PI : 1;

begin_c_loop(c1,tc_heat) /* Obtain total heater volume */
{
vol_tot_heat += C_VOLUME(c1,tc_heat)*axi_k1;
}
end_c_loop(c1,tc_heat)
begin_c_loop(c1,tc_emit) /* Obtain emitter temperature at current and previous timesteps */
{
cell_vol = C_VOLUME(c1,tc_emit)*axi_k1;
vol_tot_emit += cell_vol;
temp_emit += C_T(c1,tc_emit)*cell_vol;
temp_emit_M1 += C_T_M1(c1,tc_emit)*cell_vol;
}
end_c_loop(c1,tc_emit)
temp_emit = temp_emit/vol_tot_emit;
temp_emit_M1 = temp_emit_M1/vol_tot_emit;
dtemp_dt = (temp_emit - temp_emit_M1)/dt;
dheat_dT = 2*4.382e06*temp_emit-0.001093;
heater_power = -kp*(temp_emit - temp_target) - kd*dtemp_dt + ki*C_UDMI(c,t,1);
heater_power = (heater_power >= 0)? heater_power : 0;
heater_power = (heater_power<=heater_power_max)? heater_power: heater_power_max;
source = heater_power/vol_tot_heat;
dS[eqn] = 0;
/* Enter UDMI 1 for error integral of PID controller C_UDMI(1) = int(e dt,0,t-1) */
C_UDMI(c,t,1) = C_UDMI(c,t,1) + (temp_target-temp_emit)*dt;
/* Enter into UDMI for post_processing */
C_UDMI(c,t,0) = heater_power;
return source;
}

The subroutine calculates the average of the emitter temperature which in this case is the temperature to be controlled. Also, it requires the allocation of two user-defined memory. Other than that, it's all self-explanatory.

Friday, November 4, 2011

If (by Rudyard Kipling)

If you can keep your head when all about you
Are losing theirs and blaming it on you;
If you can trust yourself when all men doubt you,
But make allowance for their doubting too;
If you can wait and not be tired by waiting,
Or, being lied about, don't deal in lies,
Or, being hated, don't give way to hating,
And yet don't look too good, nor talk too wise;

If you can dream - and not make dreams your master;
If you can think - and not make thoughts your aim;
If you can meet with triumph and disaster
And treat those two imposters just the same;
If you can bear to hear the truth you've spoken
Twisted by knaves to make a trap for fools,
Or watch the things you gave your life to broken,
And stoop and build 'em up with wornout tools;

If you can make one heap of all your winnings
And risk it on one turn of pitch-and-toss,
And lose, and start again at your beginnings
And never breath a word about your loss;
If you can force your heart and nerve and sinew
To serve your turn long after they are gone,
And so hold on when there is nothing in you
Except the Will which says to them: "Hold on";

If you can talk with crowds and keep your virtue,
Or walk with kings - nor lose the common touch;
If neither foes nor loving friends can hurt you;
If all men count with you, but none too much;
If you can fill the unforgiving minute
With sixty seconds' worth of distance run -
Yours is the Earth and everything that's in it,
And - which is more - you'll be a Man my son!