Date: 2026-06-01T23:13:47+00:00

Actual source code: ex1.c

      1: const char help[] = "Basic TaoTerm usage";

      3: #include <petsctao.h>

      5: int main(int argc, char **argv)
      6: {
      7:   TaoTerm     term;
      8:   PetscViewer viewer;
      9:   PetscBool   view_detail = PETSC_TRUE;
     10:   PetscBool   dup_type    = PETSC_FALSE;
     11:   PetscBool   flg;

     13:   PetscFunctionBeginUser;
     14:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));

     16:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-view_detail", &view_detail, &flg));
     17:   PetscCall(PetscOptionsGetBool(NULL, NULL, "-duplicate_type", &dup_type, &flg));
     18:   PetscCall(PetscViewerCreate(PETSC_COMM_WORLD, &viewer));
     19:   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
     20:   PetscCall(PetscViewerSetUp(viewer));
     21:   PetscCall(PetscViewerSetFromOptions(viewer));
     22:   if (view_detail) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO_DETAIL));
     23:   else PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO));

     25:   PetscCall(TaoTermCreate(PETSC_COMM_WORLD, &term));
     26:   PetscCall(TaoTermSetSolutionSizes(term, PETSC_DECIDE, 10, 1));
     27:   PetscCall(TaoTermSetParametersSizes(term, PETSC_DECIDE, 7, 1));
     28:   PetscCall(PetscObjectSetName((PetscObject)term, "example TaoTerm"));
     29:   PetscCall(TaoTermSetFromOptions(term));
     30:   PetscCall(TaoTermSetUp(term));
     31:   if (dup_type) {
     32:     TaoTermType ttype, ttype2;
     33:     TaoTerm     term2;
     34:     PetscBool   is_same;

     36:     PetscCall(TaoTermDuplicate(term, TAOTERM_DUPLICATE_TYPE, &term2));
     37:     PetscCall(TaoTermGetType(term, &ttype));
     38:     PetscCall(TaoTermGetType(term2, &ttype2));
     39:     PetscCall(PetscStrcmp(ttype, ttype2, &is_same));
     40:     PetscCheck(is_same, PETSC_COMM_WORLD, PETSC_ERR_PLIB, "TaoTermDuplicate not duplicating type correctly");
     41:     PetscCall(TaoTermDestroy(&term2));
     42:   }
     43:   PetscCall(TaoTermView(term, viewer));
     44:   PetscCall(PetscViewerPopFormat(viewer));
     45:   PetscCall(TaoTermDestroy(&term));
     46:   PetscCall(PetscViewerDestroy(&viewer));
     47:   PetscCall(PetscFinalize());
     48:   return 0;
     49: }

     51: /*TEST

     53:   test:
     54:     suffix: 0

     56:   test:
     57:     suffix: 0_from_options
     58:     output_file: output/ex1_0.out
     59:     args: -tao_term_type shell

     61:   test:
     62:     suffix: 1
     63:     args: -tao_term_type callbacks

     65:   test:
     66:     suffix: 1_ascii
     67:     args: -tao_term_type callbacks -view_detail 0

     69:   test:
     70:     suffix: 2
     71:     args: -tao_term_type sum -tao_term_sum_number_terms 2 -term_0_tao_term_type halfl2squared -term_1_tao_term_type l1 -tao_term_sum_term_0_scale 0.5 -tao_term_sum_term_0_mask objective,gradient,hessian

     73:   test:
     74:     suffix: 2_ascii
     75:     args: -tao_term_type sum -tao_term_sum_number_terms 2 -term_0_tao_term_type halfl2squared -term_1_tao_term_type l1 -tao_term_sum_term_0_scale 0.5 -tao_term_sum_term_0_mask objective,gradient,hessian
     76:     args: -view_detail 0

     78:   test:
     79:     suffix: duplicate_type
     80:     output_file: output/ex1_0.out
     81:     args: -tao_term_type shell -duplicate_type 1

     83: TEST*/