2015年6月21日日曜日

SBCL での defclass slot の :type オプションの挙動


defclass の slot には、:type オプションを指定することができる。

期待としては、:type オプションを指定した場合、指定した型を満たさない値を代入しようとすると、エラーとなって欲しい。ところが、SBCL ではそのようにならない。

(defclass foo ()
  ((x :accessor foo-x
      :initarg :x
      :type integer)))

(let ((x (make-instance 'foo :x 1)))
  (setf (foo-x x) :foo)
  (foo-x x))                           ; => :foo

defclass と slot へのアクセスを optimize safety で評価すると、期待する挙動となる。

(locally (declare (optimize safety))
  (defclass foo ()
    ((x :accessor foo-x
        :initarg :x
        :type integer)))

  (let ((x (make-instance 'foo :x 1)))
    (setf (foo-x x) :foo)              ; error
    (foo-x x)))

CLHSには、以下のようにあるものの、

The consequences of attempting to store in a slot a value that does not satisfy the type of the slot are undefined.

その前の部分には以下のようにある。

The :type slot option specifies that the contents of the slot will always be of the specified data type.

SBCL User Manualには以下のようにあり、その理由は示されていない。悲しい。

Types declared using the :type slot option in defclass are asserted if and only if the class was defined in safe code and the slot access location is in safe code as well.


2015年4月28日火曜日

2015年3月4日水曜日

A list of Physics-Based Animations

Here shows a list of physics-based animations I computed and rendered, including fluid, rigid body and their coupling simulation. Movies are to be added.


Falling Rigid Bunnies
I simulated a lot of falling Stanford bunnies and rendered the result.

watch movie


Bunny-shaped fluid simulation
I simulated bunny-shaped fluid body and rendered the result.

watch movie



Rigid-fluid coupling for SPH
I simulated rigid-fluid coupling for SPH and rendered the result.

watch movie



Rigid-fluid coupling for SPH
I simulated rigid-fluid coupling for SPH and rendered the result.

watch movie



Rigid body simulation on a slope
I simulated a rigid body falling on a slope with a particle-based method.

watch movie



Falling rigid bodies
I simulated falling rigid bodies and rendered the result.

watch movie



Particle-based rigid body simulation
I simulated many colliding rigid bodies and rendered the result.

match movie



Braking wave simulation with SPH
I simulated a breaking wave with SPH and rendered the result.

watch movie


Particle-based fluid simulation in x2 scale
I computed the following fluid simualtion in x2 scale, then rendered the result.

watch movie


Particle-based fluid simulation in 3D
I computed particle-based fluid simulation in 3D and rendered the result.

watch movie



Particle-based fluid simulation
I computed particle-based fluid simulation in 2D and rendered the result.

watch movie



2015年3月1日日曜日

PIC - PIC Is a Compiler for 8-bit PIC micro controllers


PIC is a native compiler for 8-bit PIC micro controllers written in Common Lisp. The host language is a pretty small subset of ML-like language and the target language is 8-bit PIC micro controller assembly. Common Lisp is the compiler language.

Usage
Following is an example of LED blinking with PIC12F683 micro controller. init function is one of the compiler's special functions, where the micro controller's SFR(Special Function Registers) are initialized. Following main function is also the compiler's special function and program's main routine is executed here.

mdelay1 function and mdelay macro are for delaying. Note that since 8-bit PIC handles only 8-bit unsigned integers, nested loops are needed for delaying more than 255 msec (950 msec here). progn and loop are predefined macros for the compiler.
(defpic init ()
  (progn
    (setreg :gpio #x0)                  ; clera GP0-5
    (setreg :cmcon0 #x07)               ; disable comparator
    (setbank1)                          ; switch to bank 1
    (setreg :trisio #x08)               ; only GP3 is outputinputmode
    (setreg :ansel #x00)                ; disable analog IO
    (setreg :ioc #x00)                  ; disable interruption
    (setbank0)                          ; switch to bank 0
    (setreg :intcon #x00)))             ; disable interruption

(defpic main ()
  (progn
    (setreg :gpio #x20)                 ; set GP5 to high
    (mdelay 50)                         ; delay for 50 msec
    (setreg :gpio #x00)                 ; set GP5 to low
    (mdelay 950)                        ; delay for 950 msec
    (main)))                            ; repeat

(defpic mdelay1 ()
  (loop 90                              ; 90 is a magic number to delay
    0))                                 ;  for 1 msec

(defpicmacro mdelay (n)
  (unless (<= 0 n 65535)
    (error "The value ~S is invalid." n))
  (multiple-value-bind (q r) (truncate n 256)
    (if (= q 0)
        `(loop ,r (mdelay1))
        `(loop ,q (loop ,r (mdelay1))))))
Then pic-compile function compiles and outputs the complete assembly for the PIC functions to standard output. The output assembly is expected to be assembled with Microchip's MPASM assembler.
PIC> (pic-compile)
    INCLUDE"p12f683.inc"
    list p=12f683

    __CONFIG _CP_OFF & _CPD_OFF & _WDT_OFF & _BOD_ON & _IESO_OFF& _PWRTE_ON & _INTOSCIO & _MCLRE_OFF

    CBLOCK  020h
    L0,L1,L2,L3,L4,L5,L6,L7 ; local registers
    I0,I1,I2,I3,I4,I5,I6,I7 ; input registers
    SP,STMP,STK             ; stack registers
    ENDC

    ORG 0
    GOTO    MAIN
    ...
    END
; No value
For detail, please see its repository.