How to retrieve category of a post in have_post loop?

wp_get_post_categories() and get_category() are specifically for the category taxonomy. You’re using a custom taxonomy, portfolio_cat, so you need to use get_the_terms() to get categories.

$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str="";

foreach ( $post_categories as $category ) {
    $cat_str .= $category->name;
}

If you just want a comma separated list of category links, you can use get_the_term_list() to simplify the code:

$cat_str = get_the_term_list( $post_id, 'portfolio_cat', '', ', ', '' );

Or, if you don’t want links, you can do this instead:

$categories = get_the_terms( $post_id, 'portfolio_cat' );
$cat_str    = implode( ', ', wp_list_pluck( $categories, 'name' ) );

Also, instead of $query->post->post_title you should really just be using get_the_title();.