Modifying velocity at boundary

Dear Users

I am doing a boundary optimal control by setting inlet velocity as the control. my equations are based on solving state and adjoint equations (optimality system). my optimality condition is defined as follow on inlet but I cant define it to be calculated on boundary not the whole domain.

Gradient of cost function=a1*Uin-(dx(u)*N.x+dy(u)*N.y);
how I can set it to be computed just at the inlet?
then I want to save the velocity at the inlet in a text file but I dont know how to define a file with y vs Uin values.

Best regards