What did Avicenna write after being released from a castle near Hamahhan