Bonjour,

Mon code plante a la ligne 78 a cause de la fonction Multimatrice() qui multiplie une matrice carre et une matrice colonne.
Cette fonction prend en parametre un tableau 2D, 1D et la taille. Puis je retourne un tableau 1D dans u[].
J'ai ce probleme de segmentation et je ne comprend pas l'erreur ?

La grande fonction main :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
 
#include "Inverseur.h"
#include "Multimatrice.h"
#include "alloc2D.h"
#include "free2D.h"
 
 
struct Parametre
{
  double Longueur;
  int Nbrepoints;
  int tend;
  double Re;
};
 
int main(void)
{
  struct Parametre p = {4.,81,2,1.};
 
  double dx = p.Longueur/(p.Nbrepoints-1);
  double dt = dx/2.;
  double a = -dt/(p.Re*pow(dx,2));
  double b = 1+2*dt/(p.Re*pow(dx,2));
  double c = -dt/(p.Re*pow(dx,2));
  /**
     dynamic allocation table
  */
  double*x=NULL,*B=NULL, *u=NULL;	
  x= calloc(p.Nbrepoints,sizeof(double));
  u= calloc(p.Nbrepoints,sizeof(double));
  B= calloc(p.Nbrepoints, sizeof(double));
 
  /**
     initial exponential elevation
  */
 
  for(int i=0; i<p.Nbrepoints-1;i++)
      x[i+1] = x[i] + dx;
 
  for(int i =0; i<p.Nbrepoints-1;i++)
    u[i]=1.*exp(-(pow(x[i]-1.,2)/0.1));
  /** 
      Velocity Boundaries Conditions 
  */
  u[0]=0.;
  u[p.Nbrepoints-1]=0.;
 
  /** 
      Matrice B (table 1D)
  */
  for (int i=1;i<p.Nbrepoints-1;i++)
    B[i] = (u[i]*dt/dx)*(u[i-1]-u[i])+u[i];
  B[0] = u[0];
  B[p.Nbrepoints-1] = u[p.Nbrepoints-1];
/**
   Matrice inverse
*/
  double ** Ainv;
  Ainv = arr_alloc(p.Nbrepoints, p.Nbrepoints);
  Ainv = Matrice(&p.Nbrepoints, &a, &b, &c);
 
/**
   begin the time loop
*/
  FILE *fic = fopen("data.txt", "w");
  fprintf(fic, "%s %s %s\n", "#x", "#u", "#t");
 
  double t=0.;
  while(t<=p.tend){
    for(int i=0;i<p.Nbrepoints;i++)
      fprintf(fic, "%g %g %g\n", x[i], u[i], t);
    fflush(fic);
    u=Multimatrice(Ainv, B, u, &p.Nbrepoints);
    for (int i=1;i<p.Nbrepoints-1;i++)
      B[i] = (u[i]*dt/dx)*(u[i-1]-u[i])+u[i];
    B[0] = u[0] = 0.;
    B[p.Nbrepoints-1] = u[p.Nbrepoints-1] = 0.;
    fprintf(fic,"\n\n");
    t+=dt;
  }
 
  free(x);
  free(u);
  free(B);
  fclose(fic);
  arr_free(Ainv, p.Nbrepoints, p.Nbrepoints);
  return 0;
}
La fonction arr_alloc qui alloue le bloc memoire pour un tableau 2D:

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdlib.h>
#include <assert.h>
#include "alloc2D.h"
double** arr_alloc (size_t x, size_t y)
{
  double** pp = calloc(x,sizeof(*pp));
  assert(pp != NULL);
  for(size_t i=0; i<x; i++)
  {
    pp[i] = calloc(y,sizeof(**pp));
    assert(pp[i] != NULL);
  }
 
  return pp;
}
La fonction Multimatrice source de l'erreur :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
#include <stdlib.h>
#include "Multimatrice.h"
 
double *Multimatrice(double **mat1, double *mat2, double *U, int *taille){
  int n = *taille;
  for(int i = 0; i < n; i++)
    {
	for(int k = 0; k < n; k++)
	{
	   	U[i] += mat1[i][k] * mat2[k];
	}	
    }
  return U;
}


Lorsque je compile :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 
(base) alexandre@alexandre-Latitude-E7270:~/Gitlab/finite-difference/1D/Burger$ gcc -g *.c -o prog -lm
(base) alexandre@alexandre-Latitude-E7270:~/Gitlab/finite-difference/1D/Burger$ ./prog gbd
Erreur de segmentation (core dumped)
(base) alexandre@alexandre-Latitude-E7270:~/Gitlab/finite-difference/1D/Burger$ gdb
GNU gdb (Ubuntu 8.1-0ubuntu3.2) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) run
Starting program:  
No executable file specified.
Use the "file" or "exec-file" command.
(gdb) file prog
Reading symbols from prog...done.
(gdb) run
Starting program: /home/alexandre/Gitlab/finite-difference/1D/Burger/prog 
 
Program received signal SIGSEGV, Segmentation fault.
0x0000555555555842 in Multimatrice (mat1=0x555555764c30, mat2=0x555555757780, 
    U=0x5555557574f0, taille=0x7fffffffdb88) at Multimatrice.c:11
11		   	temp[i] += mat1[i][k] * mat2[k];
Je suis ouvert a vos conseils car je debut depuis peu en c. Je sais que mon main est horrible mais je l'ameliorais dans un deuxieme temps lorsque que mon code tournera.
Je vous en remercie !

Si besoin voici un lien vers mon gitlab avec tous les fichiers : https://gitlab.com/v_alexandre/finit...ster/1D/Burger